gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.impl / src / main / java / org / gvsig / raster / cache / tile / impl / TileCacheImpl.java @ 319
History | View | Annotate | Download (4.58 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.impl.memory.MemoryManager; |
34 |
import org.gvsig.raster.cache.tile.layer.TiledLayer; |
35 |
import org.gvsig.raster.cache.tile.provider.TileServer; |
36 |
|
37 |
/**
|
38 |
* Main implementation for the tile cache
|
39 |
*
|
40 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
41 |
*/
|
42 |
public class TileCacheImpl implements TileCache { |
43 |
private ArrayList<TiledLayerImpl> layerList = null; |
44 |
private MemoryManager memoryManager = null; |
45 |
private String baseDir = null; |
46 |
private double currentSize = 0; |
47 |
protected String preffixSizeFileName = "c_len"; |
48 |
protected String configDir = ".metadata"; |
49 |
|
50 |
public TileCacheImpl(String baseDir) { |
51 |
this.layerList = new ArrayList<TiledLayerImpl>(); |
52 |
memoryManager = new MemoryManager();
|
53 |
this.baseDir = baseDir;
|
54 |
loadSize(); |
55 |
} |
56 |
|
57 |
/*
|
58 |
* (non-Javadoc)
|
59 |
* @see org.gvsig.raster.cache.tile.TileCache#updateBaseDirectory(java.lang.String)
|
60 |
*/
|
61 |
public void updateBaseDirectory(String dir) { |
62 |
this.baseDir = dir;
|
63 |
for (int i = 0; i < layerList.size(); i++) { |
64 |
layerList.get(i).updateBaseDirectory(dir); |
65 |
} |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Updates the size of the tiles downloaded
|
70 |
* @param sizeBytes
|
71 |
*/
|
72 |
public synchronized void updateSize(long sizeBytes) { |
73 |
currentSize += ((double)sizeBytes / 1048576); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Loads the size of the directory
|
78 |
*/
|
79 |
private void loadSize() { |
80 |
File f = new File(baseDir + File.separator + configDir); |
81 |
if(!f.exists()) {
|
82 |
f.mkdirs(); |
83 |
saveSize(); |
84 |
} |
85 |
String[] list = f.list(); |
86 |
for (int i = 0; i < list.length; i++) { |
87 |
if(list[i].startsWith(preffixSizeFileName)) {
|
88 |
String s = list[i].substring(5, list[i].length()); |
89 |
try {
|
90 |
currentSize = Double.valueOf(s);
|
91 |
} catch (NumberFormatException e) { |
92 |
} |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Saves a size file
|
99 |
* @throws IOException
|
100 |
*/
|
101 |
public synchronized void saveSize() { |
102 |
String dir = baseDir + File.separator + configDir; |
103 |
File f = new File(dir); |
104 |
if(!f.exists()) {
|
105 |
f.mkdirs(); |
106 |
} |
107 |
String[] list = f.list(); |
108 |
if(list != null) { |
109 |
for (int i = 0; i < list.length; i++) { |
110 |
if(list[i].startsWith(preffixSizeFileName)) {
|
111 |
new File(dir + File.separator + list[i]).delete(); |
112 |
} |
113 |
} |
114 |
} |
115 |
try {
|
116 |
new File(dir + File.separator + preffixSizeFileName + (int)currentSize).createNewFile(); |
117 |
} catch (IOException e) { |
118 |
} |
119 |
} |
120 |
|
121 |
/*
|
122 |
* (non-Javadoc)
|
123 |
* @see org.gvsig.raster.cache.tile.TileCache#isFullDiskCache()
|
124 |
*/
|
125 |
public boolean isFullDiskCache() { |
126 |
return currentSize >= TileCacheLibrary.MAX_CACHE_SIZE;
|
127 |
} |
128 |
|
129 |
/*
|
130 |
* (non-Javadoc)
|
131 |
* @see org.gvsig.raster.tilecache.TileCache#createLayer(org.gvsig.raster.tilecache.provider.TileProvider)
|
132 |
*/
|
133 |
public TiledLayer createLayer(TileServer provider, String strategyType) { |
134 |
ITileFileSystemStrategy strat = ((DefaultTileCacheManager)TileCacheLocator.getManager()).createStrategy(strategyType); |
135 |
TiledLayerImpl l = new TiledLayerImpl(this, provider, strat, memoryManager.createLRUTileCache()); |
136 |
layerList.add(l); |
137 |
return l;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Gets the base directory
|
142 |
* @return
|
143 |
*/
|
144 |
public String getBaseDirectory() { |
145 |
return baseDir;
|
146 |
} |
147 |
|
148 |
/*
|
149 |
* (non-Javadoc)
|
150 |
* @see org.gvsig.raster.tilecache.TileCache#removeLayer(org.gvsig.raster.tilecache.layer.TiledLayer)
|
151 |
*/
|
152 |
public void removeLayer(TiledLayer layer) { |
153 |
layerList.remove(layer); |
154 |
} |
155 |
} |