gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / tile / impl / TileCacheImpl.java @ 2424
History | View | Annotate | Download (4.18 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.cache.tile.impl; |
23 |
|
24 |
import java.io.File; |
25 |
import java.io.IOException; |
26 |
import java.util.ArrayList; |
27 |
|
28 |
import org.gvsig.raster.cache.tile.TileCache; |
29 |
import org.gvsig.raster.cache.tile.TileCacheLibrary; |
30 |
import org.gvsig.raster.cache.tile.TileCacheLocator; |
31 |
import org.gvsig.raster.cache.tile.disk.ITileFileSystemStrategy; |
32 |
import org.gvsig.raster.cache.tile.impl.layer.TiledLayerImpl; |
33 |
import org.gvsig.raster.cache.tile.layer.TiledLayer; |
34 |
import org.gvsig.raster.cache.tile.provider.TileServer; |
35 |
|
36 |
/**
|
37 |
* Main implementation for the tile cache
|
38 |
*
|
39 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
40 |
*/
|
41 |
public class TileCacheImpl implements TileCache { |
42 |
private ArrayList<TiledLayerImpl> layerList = null; |
43 |
//private MemoryManager memoryManager = null;
|
44 |
private String baseDir = null; |
45 |
private double currentSize = 0; |
46 |
protected String preffixSizeFileName = "c_len"; |
47 |
protected String configDir = ".metadata"; |
48 |
|
49 |
public TileCacheImpl(String baseDir) { |
50 |
this.layerList = new ArrayList<TiledLayerImpl>(); |
51 |
//memoryManager = new MemoryManager();
|
52 |
this.baseDir = baseDir;
|
53 |
loadSize(); |
54 |
} |
55 |
|
56 |
public void updateBaseDirectory(String dir) { |
57 |
this.baseDir = dir;
|
58 |
for (int i = 0; i < layerList.size(); i++) { |
59 |
layerList.get(i).updateBaseDirectory(dir); |
60 |
} |
61 |
} |
62 |
|
63 |
/**
|
64 |
* Updates the size of the tiles downloaded
|
65 |
* @param sizeBytes
|
66 |
*/
|
67 |
public synchronized void updateSize(long sizeBytes) { |
68 |
currentSize += ((double)sizeBytes);
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Loads the size of the directory
|
73 |
*/
|
74 |
private void loadSize() { |
75 |
File f = new File(baseDir + File.separator + configDir); |
76 |
if(!f.exists()) {
|
77 |
f.mkdirs(); |
78 |
saveSize(); |
79 |
} |
80 |
String[] list = f.list(); |
81 |
for (int i = 0; i < list.length; i++) { |
82 |
if(list[i].startsWith(preffixSizeFileName)) {
|
83 |
String s = list[i].substring(5, list[i].length()); |
84 |
try {
|
85 |
currentSize = Double.valueOf(s);
|
86 |
} catch (NumberFormatException e) { |
87 |
} |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Saves a size file
|
94 |
* @throws IOException
|
95 |
*/
|
96 |
public synchronized void saveSize() { |
97 |
String dir = baseDir + File.separator + configDir; |
98 |
File f = new File(dir); |
99 |
if(!f.exists()) {
|
100 |
f.mkdirs(); |
101 |
} |
102 |
String[] list = f.list(); |
103 |
if(list != null) { |
104 |
for (int i = 0; i < list.length; i++) { |
105 |
if(list[i].startsWith(preffixSizeFileName)) {
|
106 |
new File(dir + File.separator + list[i]).delete(); |
107 |
} |
108 |
} |
109 |
} |
110 |
try {
|
111 |
new File(dir + File.separator + preffixSizeFileName + (int)currentSize).createNewFile(); |
112 |
} catch (IOException e) { |
113 |
} |
114 |
} |
115 |
|
116 |
public boolean isFullDiskCache() { |
117 |
return currentSize >= (TileCacheLibrary.MAX_CACHE_SIZE * 1048576); |
118 |
} |
119 |
|
120 |
public TiledLayer createLayer(TileServer provider, String strategyType) { |
121 |
ITileFileSystemStrategy strat = ((DefaultTileCacheManager)TileCacheLocator.getManager()).createStrategy(strategyType); |
122 |
TiledLayerImpl l = new TiledLayerImpl(this, provider, strat/*, memoryManager.createLRUTileCache()*/); |
123 |
layerList.add(l); |
124 |
return l;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Gets the base directory
|
129 |
* @return
|
130 |
*/
|
131 |
public String getBaseDirectory() { |
132 |
return baseDir;
|
133 |
} |
134 |
|
135 |
public String getConfigurationDirectory() { |
136 |
return configDir;
|
137 |
} |
138 |
|
139 |
public void removeLayer(TiledLayer layer) { |
140 |
layerList.remove(layer); |
141 |
updateSize(-layer.delete()); |
142 |
saveSize(); |
143 |
} |
144 |
} |