31 |
31 |
import org.apache.commons.io.FilenameUtils;
|
32 |
32 |
import org.apache.commons.lang3.ObjectUtils;
|
33 |
33 |
import org.apache.commons.lang3.StringUtils;
|
34 |
|
import org.slf4j.Logger;
|
35 |
|
import org.slf4j.LoggerFactory;
|
36 |
|
|
37 |
34 |
import org.gvsig.educa.thematicmap.ThematicMapException;
|
38 |
35 |
import org.gvsig.educa.thematicmap.ThematicMapManager;
|
39 |
36 |
import org.gvsig.educa.thematicmap.compilation.ThematicMapCompilation;
|
... | ... | |
48 |
45 |
import org.gvsig.educa.thematicmap.map.InvalidThematicMapFormatException;
|
49 |
46 |
import org.gvsig.educa.thematicmap.map.ThematicMap;
|
50 |
47 |
import org.gvsig.educa.thematicmap.map.ThematicMapInformation;
|
|
48 |
import org.gvsig.educa.thematicmap.spi.ThematicMapProviderLocator;
|
51 |
49 |
import org.gvsig.i18n.Messages;
|
52 |
50 |
import org.gvsig.installer.lib.api.InstallerLocator;
|
53 |
51 |
import org.gvsig.installer.lib.api.InstallerManager;
|
54 |
52 |
import org.gvsig.installer.lib.api.PackageInfo;
|
55 |
53 |
import org.gvsig.tools.ToolsLocator;
|
|
54 |
import org.gvsig.tools.dynobject.DynObject;
|
|
55 |
import org.gvsig.tools.service.AbstractManager;
|
|
56 |
import org.gvsig.tools.service.Service;
|
|
57 |
import org.gvsig.tools.service.ServiceException;
|
|
58 |
import org.gvsig.tools.service.spi.ProviderManager;
|
|
59 |
import org.slf4j.Logger;
|
|
60 |
import org.slf4j.LoggerFactory;
|
56 |
61 |
|
57 |
62 |
/**
|
58 |
63 |
* Default {@link ThematicMapManager} implementation.
|
... | ... | |
60 |
65 |
* @author gvSIG Team
|
61 |
66 |
* @version $Id$
|
62 |
67 |
*/
|
63 |
|
public class DefaultThematicMapManager implements ThematicMapManager {
|
|
68 |
public class DefaultThematicMapManager extends AbstractManager implements
|
|
69 |
ThematicMapManager {
|
64 |
70 |
|
65 |
|
/**
|
66 |
|
* Extension for Thematic Map files
|
67 |
|
*/
|
68 |
|
public static String THEMATIC_MAP_FILE_EXTENSION = "gvsthm";
|
|
71 |
/**
|
|
72 |
* Extension for Thematic Map files
|
|
73 |
*/
|
|
74 |
public static String THEMATIC_MAP_FILE_EXTENSION = "gvsthm";
|
69 |
75 |
|
70 |
|
private static final Logger LOG = LoggerFactory
|
71 |
|
.getLogger(DefaultThematicMapManager.class);
|
|
76 |
private static final Logger LOG = LoggerFactory
|
|
77 |
.getLogger(DefaultThematicMapManager.class);
|
72 |
78 |
|
73 |
|
public static final String BASENAME_FORMAT_STRING = "{0}-v{1}-BN{2}";
|
|
79 |
public static final String BASENAME_FORMAT_STRING = "{0}-v{1}-BN{2}";
|
74 |
80 |
|
75 |
|
public static final String PACKAGE_BASENAME_FORMAT_STRING =
|
76 |
|
"ThematicMap-{0}-{1}-BN{2}";
|
|
81 |
public static final String PACKAGE_BASENAME_FORMAT_STRING = "ThematicMap-{0}-{1}-BN{2}";
|
77 |
82 |
|
78 |
|
/**
|
79 |
|
* Manager which manages when deploy/clean maps in
|
80 |
|
* temporal folder
|
81 |
|
*/
|
82 |
|
private MapResoucesManager resourceManager;
|
|
83 |
/**
|
|
84 |
* Manager which manages when deploy/clean maps in temporal folder
|
|
85 |
*/
|
|
86 |
private MapResoucesManager resourceManager;
|
83 |
87 |
|
84 |
|
private final ThematicMapLoader mapLoader;
|
|
88 |
private final ThematicMapLoader mapLoader;
|
85 |
89 |
|
86 |
|
private final InstallerManager installerManager;
|
|
90 |
private final InstallerManager installerManager;
|
87 |
91 |
|
88 |
|
/**
|
89 |
|
* Map Instalation's folder
|
90 |
|
*/
|
91 |
|
private String installationFolderInput;
|
92 |
|
private File installationFolder;
|
|
92 |
/**
|
|
93 |
* Map Instalation's folder
|
|
94 |
*/
|
|
95 |
private String installationFolderInput;
|
|
96 |
private File installationFolder;
|
93 |
97 |
|
94 |
|
private String temporalDelployFolderInput;
|
95 |
|
private File temporalDeployMapFolder;
|
|
98 |
private String temporalDelployFolderInput;
|
|
99 |
private File temporalDeployMapFolder;
|
96 |
100 |
|
97 |
|
private final ThematicMapFileFilter thematicMapFileFilter;
|
|
101 |
private final ThematicMapFileFilter thematicMapFileFilter;
|
98 |
102 |
|
99 |
|
public DefaultThematicMapManager() {
|
|
103 |
public DefaultThematicMapManager(ProviderManager providerManager) {
|
|
104 |
super(providerManager);
|
100 |
105 |
|
101 |
|
// initially, installation folder is null
|
102 |
|
installationFolder = null;
|
103 |
|
installationFolderInput = null;
|
|
106 |
// TODO Auto-generated constructor stub
|
104 |
107 |
|
105 |
|
// default temporal folder tmp.dir + '/ThematicMap.tmp'
|
106 |
|
temporalDeployMapFolder =
|
107 |
|
FileUtils.getWritableFolder(
|
108 |
|
new File(System.getProperty("java.io.tmpdir")),
|
109 |
|
"ThematicMap.tmp");
|
110 |
|
temporalDelployFolderInput = temporalDeployMapFolder.getAbsolutePath();
|
|
108 |
// initially, installation folder is null
|
|
109 |
installationFolder = null;
|
|
110 |
installationFolderInput = null;
|
111 |
111 |
|
112 |
|
// Create file filter to identify map candidate in a folder
|
113 |
|
thematicMapFileFilter =
|
114 |
|
new ThematicMapFileFilter(THEMATIC_MAP_FILE_EXTENSION);
|
|
112 |
// default temporal folder tmp.dir + '/ThematicMap.tmp'
|
|
113 |
temporalDeployMapFolder = FileUtils.getWritableFolder(
|
|
114 |
new File(System.getProperty("java.io.tmpdir")),
|
|
115 |
"ThematicMap.tmp");
|
|
116 |
temporalDelployFolderInput = temporalDeployMapFolder.getAbsolutePath();
|
115 |
117 |
|
116 |
|
// Create loader instance
|
117 |
|
mapLoader = new ThematicMapLoader();
|
|
118 |
// Create file filter to identify map candidate in a folder
|
|
119 |
thematicMapFileFilter = new ThematicMapFileFilter(
|
|
120 |
THEMATIC_MAP_FILE_EXTENSION);
|
118 |
121 |
|
119 |
|
// Create resource manager instance
|
120 |
|
resourceManager = new MapResoucesManager(mapLoader);
|
|
122 |
// Create loader instance
|
|
123 |
mapLoader = new ThematicMapLoader();
|
121 |
124 |
|
122 |
|
installerManager = InstallerLocator.getInstallerManager();
|
123 |
|
}
|
|
125 |
// Create resource manager instance
|
|
126 |
resourceManager = new MapResoucesManager(mapLoader);
|
124 |
127 |
|
125 |
|
/** {@inheridDoc} */
|
126 |
|
public String getInstallationMapFolder() {
|
127 |
|
return installationFolderInput;
|
128 |
|
}
|
|
128 |
installerManager = InstallerLocator.getInstallerManager();
|
|
129 |
}
|
|
130 |
|
|
131 |
public DefaultThematicMapManager() {
|
|
132 |
// super(new DefaultInstallerProviderManager());
|
|
133 |
// this(ThematicMapProviderLocator.getManager());
|
|
134 |
this(null);
|
|
135 |
}
|
129 |
136 |
|
130 |
|
/**
|
131 |
|
* Checks if <code>folder</code> is a folder an is readable
|
132 |
|
*
|
133 |
|
* @param folder
|
134 |
|
* @return file representation of folder
|
135 |
|
* @throws IllegalArgumentException
|
136 |
|
*/
|
137 |
|
private File checkValidReadableFolder(String folder) {
|
138 |
|
File fileObject = new File(folder);
|
139 |
|
if (!FileUtils.isReadableFolder(fileObject)) {
|
140 |
|
throw new IllegalArgumentException("Invalid folder");
|
141 |
|
}
|
142 |
|
return fileObject;
|
143 |
|
}
|
|
137 |
// public DefaultThematicMapManager() {
|
|
138 |
//
|
|
139 |
// // initially, installation folder is null
|
|
140 |
// installationFolder = null;
|
|
141 |
// installationFolderInput = null;
|
|
142 |
//
|
|
143 |
// // default temporal folder tmp.dir + '/ThematicMap.tmp'
|
|
144 |
// temporalDeployMapFolder = FileUtils.getWritableFolder(
|
|
145 |
// new File(System.getProperty("java.io.tmpdir")),
|
|
146 |
// "ThematicMap.tmp");
|
|
147 |
// temporalDelployFolderInput = temporalDeployMapFolder.getAbsolutePath();
|
|
148 |
//
|
|
149 |
// // Create file filter to identify map candidate in a folder
|
|
150 |
// thematicMapFileFilter = new ThematicMapFileFilter(
|
|
151 |
// THEMATIC_MAP_FILE_EXTENSION);
|
|
152 |
//
|
|
153 |
// // Create loader instance
|
|
154 |
// mapLoader = new ThematicMapLoader();
|
|
155 |
//
|
|
156 |
// // Create resource manager instance
|
|
157 |
// resourceManager = new MapResoucesManager(mapLoader);
|
|
158 |
//
|
|
159 |
// installerManager = InstallerLocator.getInstallerManager();
|
|
160 |
// }
|
144 |
161 |
|
145 |
|
/**
|
146 |
|
* Checks if <code>folder</code> is a folder and is writable
|
147 |
|
*
|
148 |
|
* @param folder
|
149 |
|
* @return file representation of folder
|
150 |
|
* @throws IllegalArgumentException
|
151 |
|
*/
|
152 |
|
private File checkValidWritableFolder(String folder) {
|
153 |
|
File fileObject = new File(folder);
|
154 |
|
if (!FileUtils.isWritableFolder(fileObject)) {
|
155 |
|
throw new IllegalArgumentException("Invalid folder");
|
156 |
|
}
|
157 |
|
return fileObject;
|
158 |
|
}
|
|
162 |
/** {@inheridDoc} */
|
|
163 |
public String getInstallationMapFolder() {
|
|
164 |
return installationFolderInput;
|
|
165 |
}
|
159 |
166 |
|
160 |
|
/** {@inheridDoc} */
|
161 |
|
public void setInstallationMapFolder(String folder) {
|
162 |
|
installationFolder = checkValidReadableFolder(folder);
|
163 |
|
installationFolderInput = folder;
|
164 |
|
}
|
|
167 |
/**
|
|
168 |
* Checks if <code>folder</code> is a folder an is readable
|
|
169 |
*
|
|
170 |
* @param folder
|
|
171 |
* @return file representation of folder
|
|
172 |
* @throws IllegalArgumentException
|
|
173 |
*/
|
|
174 |
private File checkValidReadableFolder(String folder) {
|
|
175 |
File fileObject = new File(folder);
|
|
176 |
if (!FileUtils.isReadableFolder(fileObject)) {
|
|
177 |
throw new IllegalArgumentException("Invalid folder");
|
|
178 |
}
|
|
179 |
return fileObject;
|
|
180 |
}
|
165 |
181 |
|
166 |
|
/** {@inheridDoc} */
|
167 |
|
public String getTemporalFolder() {
|
168 |
|
return temporalDelployFolderInput;
|
169 |
|
}
|
|
182 |
/**
|
|
183 |
* Checks if <code>folder</code> is a folder and is writable
|
|
184 |
*
|
|
185 |
* @param folder
|
|
186 |
* @return file representation of folder
|
|
187 |
* @throws IllegalArgumentException
|
|
188 |
*/
|
|
189 |
private File checkValidWritableFolder(String folder) {
|
|
190 |
File fileObject = new File(folder);
|
|
191 |
if (!FileUtils.isWritableFolder(fileObject)) {
|
|
192 |
throw new IllegalArgumentException("Invalid folder");
|
|
193 |
}
|
|
194 |
return fileObject;
|
|
195 |
}
|
170 |
196 |
|
171 |
|
/** {@inheridDoc} */
|
172 |
|
public void setTemporalFolder(String folder) {
|
173 |
|
if (FilenameUtils.equals(temporalDelployFolderInput, folder)) {
|
174 |
|
return;
|
175 |
|
}
|
176 |
|
temporalDeployMapFolder = checkValidWritableFolder(folder);
|
177 |
|
temporalDelployFolderInput = folder;
|
178 |
|
resourceManager.cleanAllResources();
|
179 |
|
}
|
|
197 |
/** {@inheridDoc} */
|
|
198 |
public void setInstallationMapFolder(String folder) {
|
|
199 |
installationFolder = checkValidReadableFolder(folder);
|
|
200 |
installationFolderInput = folder;
|
|
201 |
}
|
180 |
202 |
|
181 |
|
/** {@inheridDoc} */
|
182 |
|
public List<ThematicMap> getInstalledMaps() {
|
183 |
|
return getMapsFromFolder(installationFolder);
|
184 |
|
}
|
|
203 |
/** {@inheridDoc} */
|
|
204 |
public String getTemporalFolder() {
|
|
205 |
return temporalDelployFolderInput;
|
|
206 |
}
|
185 |
207 |
|
186 |
|
/** {@inheridDoc} */
|
187 |
|
public List<ThematicMap> getInstalledMaps(String folder) {
|
188 |
|
File fileObject = checkValidReadableFolder(folder);
|
189 |
|
return getMapsFromFolder(fileObject);
|
190 |
|
}
|
|
208 |
/** {@inheridDoc} */
|
|
209 |
public void setTemporalFolder(String folder) {
|
|
210 |
if (FilenameUtils.equals(temporalDelployFolderInput, folder)) {
|
|
211 |
return;
|
|
212 |
}
|
|
213 |
temporalDeployMapFolder = checkValidWritableFolder(folder);
|
|
214 |
temporalDelployFolderInput = folder;
|
|
215 |
resourceManager.cleanAllResources();
|
|
216 |
}
|
191 |
217 |
|
192 |
|
/**
|
193 |
|
* <p>
|
194 |
|
* Gets all maps from a folder
|
195 |
|
* </p>
|
196 |
|
* <p>
|
197 |
|
* List all files which matches with filter and try to load it information.
|
198 |
|
* If a file load throws an Invalid format found ignores this file.
|
199 |
|
* </p>
|
200 |
|
*
|
201 |
|
* @param folder
|
202 |
|
* @return
|
203 |
|
*/
|
204 |
|
private List<ThematicMap> getMapsFromFolder(File folder) {
|
205 |
|
File[] files = folder.listFiles();
|
206 |
|
List<ThematicMap> result = new ArrayList<ThematicMap>(files.length);
|
207 |
|
ThematicMap map;
|
208 |
|
for (File file : files) {
|
|
218 |
/** {@inheridDoc} */
|
|
219 |
public List<ThematicMap> getInstalledMaps() {
|
|
220 |
return getMapsFromFolder(installationFolder);
|
|
221 |
}
|
209 |
222 |
|
210 |
|
try {
|
211 |
|
map = getThematicMapIntalledFolder(file);
|
212 |
|
} catch (ThematicMapException ex) {
|
213 |
|
// Ignore this folder
|
214 |
|
if (LOG.isDebugEnabled()) {
|
215 |
|
LOG.debug(
|
216 |
|
"Folder exception reading map from it (not a map): {}"
|
217 |
|
.concat(file.getAbsolutePath()), ex);
|
218 |
|
}
|
219 |
|
continue;
|
220 |
|
}
|
221 |
|
result.add(map);
|
222 |
|
}
|
223 |
|
resourceManager.adjustResourceSize();
|
224 |
|
return Collections.unmodifiableList(result);
|
225 |
|
}
|
|
223 |
/** {@inheridDoc} */
|
|
224 |
public List<ThematicMap> getInstalledMaps(String folder) {
|
|
225 |
File fileObject = checkValidReadableFolder(folder);
|
|
226 |
return getMapsFromFolder(fileObject);
|
|
227 |
}
|
226 |
228 |
|
227 |
|
/** {@inheridDoc} */
|
228 |
|
public ThematicMap getMapFromFile(File file)
|
229 |
|
throws InvalidThematicMapFormatException {
|
230 |
|
if (!thematicMapFileFilter.accept(file)) {
|
231 |
|
throw new InvalidThematicMapFormatException(file,
|
232 |
|
"Bad file extension (".concat(
|
233 |
|
thematicMapFileFilter.getFileNameEnding()).concat(")"));
|
234 |
|
}
|
235 |
|
ThematicMapInformation info = resourceManager.getInfoOfFile(file);
|
|
229 |
/**
|
|
230 |
* <p>
|
|
231 |
* Gets all maps from a folder
|
|
232 |
* </p>
|
|
233 |
* <p>
|
|
234 |
* List all files which matches with filter and try to load it information.
|
|
235 |
* If a file load throws an Invalid format found ignores this file.
|
|
236 |
* </p>
|
|
237 |
*
|
|
238 |
* @param folder
|
|
239 |
* @return
|
|
240 |
*/
|
|
241 |
private List<ThematicMap> getMapsFromFolder(File folder) {
|
|
242 |
File[] files = folder.listFiles();
|
|
243 |
List<ThematicMap> result = new ArrayList<ThematicMap>(files.length);
|
|
244 |
ThematicMap map;
|
|
245 |
for (File file : files) {
|
236 |
246 |
|
237 |
|
if (info == null) {
|
238 |
|
try {
|
239 |
|
info = mapLoader.getInformationFromMapFile(file);
|
240 |
|
} catch (Exception e) {
|
241 |
|
throw new InvalidThematicMapFormatException(file,
|
242 |
|
"Can't load information file", e);
|
243 |
|
}
|
244 |
|
}
|
245 |
|
return createThematicMap(file, info);
|
246 |
|
}
|
|
247 |
try {
|
|
248 |
map = getThematicMapIntalledFolder(file);
|
|
249 |
} catch (ThematicMapException ex) {
|
|
250 |
// Ignore this folder
|
|
251 |
if (LOG.isDebugEnabled()) {
|
|
252 |
LOG.debug(
|
|
253 |
"Folder exception reading map from it (not a map): {}"
|
|
254 |
.concat(file.getAbsolutePath()), ex);
|
|
255 |
}
|
|
256 |
continue;
|
|
257 |
}
|
|
258 |
result.add(map);
|
|
259 |
}
|
|
260 |
resourceManager.adjustResourceSize();
|
|
261 |
return Collections.unmodifiableList(result);
|
|
262 |
}
|
247 |
263 |
|
248 |
|
public ThematicMap getMapById(String mapId) {
|
249 |
|
List<ThematicMap> installedMaps = getInstalledMaps();
|
250 |
|
for (ThematicMap map : installedMaps) {
|
251 |
|
if (ObjectUtils.equals(map.getInformation().getId(), mapId)) {
|
252 |
|
return map;
|
253 |
|
}
|
254 |
|
}
|
255 |
|
return null;
|
256 |
|
}
|
|
264 |
/** {@inheridDoc} */
|
|
265 |
public ThematicMap getMapFromFile(File file)
|
|
266 |
throws InvalidThematicMapFormatException {
|
|
267 |
if (!thematicMapFileFilter.accept(file)) {
|
|
268 |
throw new InvalidThematicMapFormatException(file,
|
|
269 |
"Bad file extension (".concat(
|
|
270 |
thematicMapFileFilter.getFileNameEnding()).concat(
|
|
271 |
")"));
|
|
272 |
}
|
|
273 |
ThematicMapInformation info = resourceManager.getInfoOfFile(file);
|
257 |
274 |
|
258 |
|
private ThematicMap createThematicMap(File file, ThematicMapInformation info) {
|
259 |
|
return new DefaultThematicMap(file, info, resourceManager);
|
260 |
|
}
|
|
275 |
if (info == null) {
|
|
276 |
try {
|
|
277 |
info = mapLoader.getInformationFromMapFile(file);
|
|
278 |
} catch (Exception e) {
|
|
279 |
throw new InvalidThematicMapFormatException(file,
|
|
280 |
"Can't load information file", e);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
return createThematicMap(file, info);
|
|
284 |
}
|
261 |
285 |
|
262 |
|
/** {@inheridDoc} */
|
263 |
|
public boolean isMap(File file) {
|
264 |
|
if (LOG.isDebugEnabled()) {
|
265 |
|
LOG.debug("Checking is map : {}", file.getAbsolutePath());
|
266 |
|
}
|
|
286 |
public ThematicMap getMapById(String mapId) {
|
|
287 |
List<ThematicMap> installedMaps = getInstalledMaps();
|
|
288 |
for (ThematicMap map : installedMaps) {
|
|
289 |
if (ObjectUtils.equals(map.getInformation().getId(), mapId)) {
|
|
290 |
return map;
|
|
291 |
}
|
|
292 |
}
|
|
293 |
return null;
|
|
294 |
}
|
267 |
295 |
|
268 |
|
if (!thematicMapFileFilter.accept(file)) {
|
269 |
|
return false;
|
270 |
|
}
|
271 |
|
try {
|
272 |
|
return mapLoader.getInformationFromMapFile(file) != null;
|
273 |
|
} catch (Exception e) {
|
274 |
|
LOG.debug("file is not a mapFile:", e);
|
275 |
|
return false;
|
276 |
|
}
|
277 |
|
}
|
|
296 |
private ThematicMap createThematicMap(File file, ThematicMapInformation info) {
|
|
297 |
return new DefaultThematicMap(file, info, resourceManager);
|
|
298 |
}
|
278 |
299 |
|
279 |
|
/** {@inheridDoc} */
|
280 |
|
public ThematicMapCompilation createCompilationInstance() {
|
281 |
|
return new DefaultThematicMapCompilation();
|
282 |
|
}
|
|
300 |
/** {@inheridDoc} */
|
|
301 |
public boolean isMap(File file) {
|
|
302 |
if (LOG.isDebugEnabled()) {
|
|
303 |
LOG.debug("Checking is map : {}", file.getAbsolutePath());
|
|
304 |
}
|
283 |
305 |
|
284 |
|
/** {@inheridDoc} */
|
285 |
|
public ThematicMapCompiler createCompilerInstance() {
|
286 |
|
return new DefaultThematicMapCompiler(THEMATIC_MAP_FILE_EXTENSION);
|
287 |
|
}
|
|
306 |
if (!thematicMapFileFilter.accept(file)) {
|
|
307 |
return false;
|
|
308 |
}
|
|
309 |
try {
|
|
310 |
return mapLoader.getInformationFromMapFile(file) != null;
|
|
311 |
} catch (Exception e) {
|
|
312 |
LOG.debug("file is not a mapFile:", e);
|
|
313 |
return false;
|
|
314 |
}
|
|
315 |
}
|
288 |
316 |
|
289 |
|
/** {@inheridDoc} */
|
290 |
|
public void dispose() {
|
291 |
|
resourceManager.dispose();
|
292 |
|
resourceManager = null;
|
293 |
|
ToolsLocator.getDisposableManager().release(this);
|
294 |
|
}
|
|
317 |
/** {@inheridDoc} */
|
|
318 |
public ThematicMapCompilation createCompilationInstance() {
|
|
319 |
return new DefaultThematicMapCompilation(this);
|
|
320 |
}
|
295 |
321 |
|
296 |
|
/** {@inheridDoc} */
|
297 |
|
public void cleanAllTemporalData() {
|
298 |
|
resourceManager.cleanAllResources();
|
299 |
|
}
|
|
322 |
/** {@inheridDoc} */
|
|
323 |
public ThematicMapCompiler createCompilerInstance() {
|
|
324 |
return new DefaultThematicMapCompiler(THEMATIC_MAP_FILE_EXTENSION);
|
|
325 |
}
|
300 |
326 |
|
301 |
|
/** {@inheridDoc} */
|
302 |
|
public ThematicMapCompilation createCompilationInstanceFromMap(
|
303 |
|
ThematicMap map) throws InvalidThematicMapFormatException,
|
304 |
|
CantLoadContextException, IOException {
|
305 |
|
if (!map.isOpen()) {
|
306 |
|
map.open();
|
307 |
|
}
|
308 |
|
return new DefaultThematicMapCompilation(map);
|
309 |
|
}
|
|
327 |
/** {@inheridDoc} */
|
|
328 |
public void dispose() {
|
|
329 |
resourceManager.dispose();
|
|
330 |
resourceManager = null;
|
|
331 |
ToolsLocator.getDisposableManager().release(this);
|
|
332 |
}
|
310 |
333 |
|
311 |
|
/**
|
312 |
|
* Gets a ThematicMap from a installed maps
|
313 |
|
*
|
314 |
|
* @param folder
|
315 |
|
* @return
|
316 |
|
* @throws InvalidInstalledThematicMapException
|
317 |
|
*/
|
318 |
|
public ThematicMap getThematicMapIntalledFolder(File folder)
|
319 |
|
throws ThematicMapException {
|
320 |
|
// Only readable folder
|
321 |
|
if (!FileUtils.isReadableFolder(folder)) {
|
322 |
|
throw new InvalidInstalledThematicMapException(folder,
|
323 |
|
"not a readable folder");
|
324 |
|
}
|
325 |
|
String code = folder.getName();
|
|
334 |
/** {@inheridDoc} */
|
|
335 |
public void cleanAllTemporalData() {
|
|
336 |
resourceManager.cleanAllResources();
|
|
337 |
}
|
326 |
338 |
|
327 |
|
// Load package info
|
328 |
|
PackageInfo packageInfo =
|
329 |
|
mapLoader.getPackageInfoFromInstalledThematicMap(folder);
|
|
339 |
/** {@inheridDoc} */
|
|
340 |
public ThematicMapCompilation createCompilationInstanceFromMap(
|
|
341 |
ThematicMap map) throws InvalidThematicMapFormatException,
|
|
342 |
CantLoadContextException, IOException {
|
|
343 |
if (!map.isOpen()) {
|
|
344 |
map.open();
|
|
345 |
}
|
|
346 |
return new DefaultThematicMapCompilation(map, this);
|
|
347 |
}
|
330 |
348 |
|
331 |
|
if (packageInfo == null) {
|
332 |
|
throw new InvalidInstalledThematicMapException(folder,
|
333 |
|
"missing package.info file");
|
334 |
|
}
|
|
349 |
/**
|
|
350 |
* Gets a ThematicMap from a installed maps
|
|
351 |
*
|
|
352 |
* @param folder
|
|
353 |
* @return
|
|
354 |
* @throws InvalidInstalledThematicMapException
|
|
355 |
*/
|
|
356 |
public ThematicMap getThematicMapIntalledFolder(File folder)
|
|
357 |
throws ThematicMapException {
|
|
358 |
// Only readable folder
|
|
359 |
if (!FileUtils.isReadableFolder(folder)) {
|
|
360 |
throw new InvalidInstalledThematicMapException(folder,
|
|
361 |
"not a readable folder");
|
|
362 |
}
|
|
363 |
String code = folder.getName();
|
335 |
364 |
|
336 |
|
if (!StringUtils.equals(code, packageInfo.getCode())) {
|
337 |
|
throw new InvalidInstalledThematicMapException(folder,
|
338 |
|
"folder name doesn't match with package.info code");
|
339 |
|
}
|
|
365 |
// Load package info
|
|
366 |
PackageInfo packageInfo = mapLoader
|
|
367 |
.getPackageInfoFromInstalledThematicMap(folder);
|
340 |
368 |
|
341 |
|
if (!StringUtils.equals("ThematicMap", packageInfo.getType())) {
|
342 |
|
throw new InvalidInstalledThematicMapException(folder,
|
343 |
|
"package.info type is not 'ThematicMap': ".concat(packageInfo
|
344 |
|
.getType()));
|
345 |
|
}
|
|
369 |
if (packageInfo == null) {
|
|
370 |
throw new InvalidInstalledThematicMapException(folder,
|
|
371 |
"missing package.info file");
|
|
372 |
}
|
346 |
373 |
|
347 |
|
// Gets the thematic map file
|
348 |
|
ThematicMap map;
|
349 |
|
String mapFileName =
|
350 |
|
getBaseFileNameFromInfo(packageInfo.getCode(),
|
351 |
|
packageInfo.getName(), packageInfo.getVersion().getMayor(),
|
352 |
|
packageInfo.getVersion().getBuild()).concat(
|
353 |
|
thematicMapFileFilter.getFileNameEnding());
|
354 |
|
File mapFile = new File(folder, mapFileName);
|
355 |
|
if (!mapFile.exists()) {
|
356 |
|
throw new InvalidInstalledThematicMapException(folder, "missing '"
|
357 |
|
.concat(mapFileName).concat("' file"));
|
358 |
|
}
|
359 |
|
map = getMapFromFile(mapFile);
|
|
374 |
if (!StringUtils.equals(code, packageInfo.getCode())) {
|
|
375 |
throw new InvalidInstalledThematicMapException(folder,
|
|
376 |
"folder name doesn't match with package.info code");
|
|
377 |
}
|
360 |
378 |
|
361 |
|
if (map == null) {
|
362 |
|
// This must be unreachable code
|
363 |
|
throw new IllegalStateException();
|
364 |
|
}
|
365 |
|
ThematicMapInformation info = map.getInformation();
|
|
379 |
if (!StringUtils.equals("ThematicMap", packageInfo.getType())) {
|
|
380 |
throw new InvalidInstalledThematicMapException(folder,
|
|
381 |
"package.info type is not 'ThematicMap': "
|
|
382 |
.concat(packageInfo.getType()));
|
|
383 |
}
|
366 |
384 |
|
367 |
|
// Compares pkgInfo.code and thmInfo.id
|
368 |
|
if (!StringUtils.equals(packageInfo.getCode(), info.getId())) {
|
369 |
|
throw new InvalidInstalledThematicMapException(folder,
|
370 |
|
"package.info code doesn't match with ThematicMap.id");
|
371 |
|
}
|
|
385 |
// Gets the thematic map file
|
|
386 |
ThematicMap map;
|
|
387 |
String mapFileName = getBaseFileNameFromInfo(packageInfo.getCode(),
|
|
388 |
packageInfo.getName(), packageInfo.getVersion().getMayor(),
|
|
389 |
packageInfo.getVersion().getBuild()).concat(
|
|
390 |
thematicMapFileFilter.getFileNameEnding());
|
|
391 |
File mapFile = new File(folder, mapFileName);
|
|
392 |
if (!mapFile.exists()) {
|
|
393 |
throw new InvalidInstalledThematicMapException(folder, "missing '"
|
|
394 |
.concat(mapFileName).concat("' file"));
|
|
395 |
}
|
|
396 |
map = getMapFromFile(mapFile);
|
372 |
397 |
|
373 |
|
// Compares pkgInfo.version and thmInfo.version
|
374 |
|
if (packageInfo.getVersion().getMayor() != info.getVersion()) {
|
375 |
|
throw new InvalidInstalledThematicMapException(folder,
|
376 |
|
"package.info version doesn't match with ThematicMap.version");
|
377 |
|
}
|
|
398 |
if (map == null) {
|
|
399 |
// This must be unreachable code
|
|
400 |
throw new IllegalStateException();
|
|
401 |
}
|
|
402 |
ThematicMapInformation info = map.getInformation();
|
378 |
403 |
|
379 |
|
// Compares pkgInfo.buildNumber and thmInfo.buildNumber
|
380 |
|
if (packageInfo.getVersion().getBuild() != info.getBuildNumber()) {
|
381 |
|
throw new InvalidInstalledThematicMapException(folder,
|
382 |
|
"package.info build doesn't match with ThematicMap.buildNumber");
|
383 |
|
}
|
|
404 |
// Compares pkgInfo.code and thmInfo.id
|
|
405 |
if (!StringUtils.equals(packageInfo.getCode(), info.getId())) {
|
|
406 |
throw new InvalidInstalledThematicMapException(folder,
|
|
407 |
"package.info code doesn't match with ThematicMap.id");
|
|
408 |
}
|
384 |
409 |
|
385 |
|
return map;
|
386 |
|
}
|
|
410 |
// Compares pkgInfo.version and thmInfo.version
|
|
411 |
if (packageInfo.getVersion().getMayor() != info.getVersion()) {
|
|
412 |
throw new InvalidInstalledThematicMapException(folder,
|
|
413 |
"package.info version doesn't match with ThematicMap.version");
|
|
414 |
}
|
387 |
415 |
|
388 |
|
/** {@inheridDoc} */
|
389 |
|
public boolean isAThematicMapIntalledFolder(File folder) {
|
390 |
|
if (!FileUtils.isReadableFolder(folder)) {
|
391 |
|
return false;
|
392 |
|
}
|
|
416 |
// Compares pkgInfo.buildNumber and thmInfo.buildNumber
|
|
417 |
if (packageInfo.getVersion().getBuild() != info.getBuildNumber()) {
|
|
418 |
throw new InvalidInstalledThematicMapException(folder,
|
|
419 |
"package.info build doesn't match with ThematicMap.buildNumber");
|
|
420 |
}
|
393 |
421 |
|
394 |
|
ThematicMap map;
|
395 |
|
try {
|
396 |
|
map = getThematicMapIntalledFolder(folder);
|
397 |
|
} catch (Exception ex) {
|
398 |
|
return false;
|
399 |
|
}
|
|
422 |
return map;
|
|
423 |
}
|
400 |
424 |
|
401 |
|
return map != null;
|
|
425 |
/** {@inheridDoc} */
|
|
426 |
public boolean isAThematicMapIntalledFolder(File folder) {
|
|
427 |
if (!FileUtils.isReadableFolder(folder)) {
|
|
428 |
return false;
|
|
429 |
}
|
402 |
430 |
|
403 |
|
}
|
|
431 |
ThematicMap map;
|
|
432 |
try {
|
|
433 |
map = getThematicMapIntalledFolder(folder);
|
|
434 |
} catch (Exception ex) {
|
|
435 |
return false;
|
|
436 |
}
|
404 |
437 |
|
405 |
|
/** {@inheridDoc} */
|
406 |
|
public String getBaseFileNameFromInfo(ThematicMapInformation info) {
|
407 |
|
return getBaseFileNameFromInfo(info.getId(), info.getName(),
|
408 |
|
info.getVersion(), info.getBuildNumber());
|
409 |
|
}
|
|
438 |
return map != null;
|
410 |
439 |
|
411 |
|
/** {@inheridDoc} */
|
412 |
|
public String getBaseFileNameFromInfo(String id, String name, int version,
|
413 |
|
int buildNumber) {
|
414 |
|
return MessageFormat.format(BASENAME_FORMAT_STRING, id, version,
|
415 |
|
buildNumber);
|
416 |
|
}
|
|
440 |
}
|
417 |
441 |
|
418 |
|
/** {@inheridDoc} */
|
419 |
|
public File generatePackageFile(String mapId, File targetFolder)
|
420 |
|
throws ThematicMapException, IOException {
|
|
442 |
/** {@inheridDoc} */
|
|
443 |
public String getBaseFileNameFromInfo(ThematicMapInformation info) {
|
|
444 |
return getBaseFileNameFromInfo(info.getId(), info.getName(),
|
|
445 |
info.getVersion(), info.getBuildNumber());
|
|
446 |
}
|
421 |
447 |
|
422 |
|
ThematicMap map = getMapById(mapId);
|
423 |
|
if (map == null) {
|
424 |
|
throw new IllegalArgumentException("Thematic Map not foudn: '"
|
425 |
|
.concat(mapId).concat("'"));
|
426 |
|
}
|
|
448 |
/** {@inheridDoc} */
|
|
449 |
public String getBaseFileNameFromInfo(String id, String name, int version,
|
|
450 |
int buildNumber) {
|
|
451 |
return MessageFormat.format(BASENAME_FORMAT_STRING, id, version,
|
|
452 |
buildNumber);
|
|
453 |
}
|
427 |
454 |
|
428 |
|
File mapFolder = new File(installationFolder, mapId);
|
|
455 |
/** {@inheridDoc} */
|
|
456 |
public File generatePackageFile(String mapId, File targetFolder)
|
|
457 |
throws ThematicMapException, IOException {
|
429 |
458 |
|
430 |
|
return generatePackageFile(mapFolder, targetFolder);
|
431 |
|
}
|
|
459 |
ThematicMap map = getMapById(mapId);
|
|
460 |
if (map == null) {
|
|
461 |
throw new IllegalArgumentException("Thematic Map not foudn: '"
|
|
462 |
.concat(mapId).concat("'"));
|
|
463 |
}
|
432 |
464 |
|
433 |
|
/** {@inheridDoc} */
|
434 |
|
public File generatePackageFile(File thematicMapIntallFolder,
|
435 |
|
File targetFolder) throws ThematicMapException, IOException {
|
|
465 |
File mapFolder = new File(installationFolder, mapId);
|
436 |
466 |
|
437 |
|
// Checks if it's a map folder
|
438 |
|
ThematicMap map = null;
|
439 |
|
try {
|
440 |
|
map = getThematicMapIntalledFolder(thematicMapIntallFolder);
|
441 |
|
} catch (Exception ex) {
|
442 |
|
throw new IllegalArgumentException("Source is not a map folder: '"
|
443 |
|
.concat(thematicMapIntallFolder.getAbsolutePath()).concat("'"));
|
444 |
|
}
|
445 |
|
if (map == null) {
|
446 |
|
throw new IllegalArgumentException("Source is not a map folder: '"
|
447 |
|
.concat(thematicMapIntallFolder.getAbsolutePath()).concat("'"));
|
448 |
|
}
|
|
467 |
return generatePackageFile(mapFolder, targetFolder);
|
|
468 |
}
|
449 |
469 |
|
450 |
|
// Check target folder
|
451 |
|
if (!FileUtils.isWritableFolder(targetFolder)) {
|
452 |
|
throw new IllegalArgumentException(
|
453 |
|
"Target is not a writable folder: '".concat(
|
454 |
|
targetFolder.getAbsolutePath()).concat("'"));
|
455 |
|
}
|
|
470 |
/** {@inheridDoc} */
|
|
471 |
public File generatePackageFile(File thematicMapIntallFolder,
|
|
472 |
File targetFolder) throws ThematicMapException, IOException {
|
456 |
473 |
|
457 |
|
// Prepare target file name
|
458 |
|
ThematicMapInformation info = map.getInformation();
|
459 |
|
String pkgFileName =
|
460 |
|
MessageFormat
|
461 |
|
.format(PACKAGE_BASENAME_FORMAT_STRING, info.getId(),
|
462 |
|
info.getVersion(), info.getBuildNumber()).concat(".")
|
463 |
|
.concat(installerManager.getDefaultPackageFileExtension());
|
464 |
|
File targetFile = new File(targetFolder, pkgFileName);
|
|
474 |
// Checks if it's a map folder
|
|
475 |
ThematicMap map = null;
|
|
476 |
try {
|
|
477 |
map = getThematicMapIntalledFolder(thematicMapIntallFolder);
|
|
478 |
} catch (Exception ex) {
|
|
479 |
throw new IllegalArgumentException("Source is not a map folder: '"
|
|
480 |
.concat(thematicMapIntallFolder.getAbsolutePath()).concat(
|
|
481 |
"'"));
|
|
482 |
}
|
|
483 |
if (map == null) {
|
|
484 |
throw new IllegalArgumentException("Source is not a map folder: '"
|
|
485 |
.concat(thematicMapIntallFolder.getAbsolutePath()).concat(
|
|
486 |
"'"));
|
|
487 |
}
|
465 |
488 |
|
466 |
|
// Prepare temporal folder
|
467 |
|
File workFolder =
|
468 |
|
FileUtils.getNewWritableFolder(temporalDeployMapFolder,
|
469 |
|
"__tmp__".concat(pkgFileName));
|
470 |
|
File tempFolder = new File(workFolder, info.getId());
|
|
489 |
// Check target folder
|
|
490 |
if (!FileUtils.isWritableFolder(targetFolder)) {
|
|
491 |
throw new IllegalArgumentException(
|
|
492 |
"Target is not a writable folder: '".concat(
|
|
493 |
targetFolder.getAbsolutePath()).concat("'"));
|
|
494 |
}
|
471 |
495 |
|
472 |
|
// Copy all content of map folder
|
473 |
|
FileUtils.copyDirectory(thematicMapIntallFolder, tempFolder, true);
|
|
496 |
// Prepare target file name
|
|
497 |
ThematicMapInformation info = map.getInformation();
|
|
498 |
String pkgFileName = MessageFormat
|
|
499 |
.format(PACKAGE_BASENAME_FORMAT_STRING, info.getId(),
|
|
500 |
info.getVersion(), info.getBuildNumber()).concat(".")
|
|
501 |
.concat(installerManager.getDefaultPackageFileExtension());
|
|
502 |
File targetFile = new File(targetFolder, pkgFileName);
|
474 |
503 |
|
475 |
|
// Remove unused thematicMap files
|
476 |
|
String mapFileName = FilenameUtils.getName(map.getSourceFilePath());
|
477 |
|
File[] tmFiles = tempFolder.listFiles(thematicMapFileFilter);
|
478 |
|
for (File thFile : tmFiles) {
|
479 |
|
if (!StringUtils.equals(thFile.getName(), mapFileName)) {
|
480 |
|
thFile.delete();
|
481 |
|
}
|
482 |
|
}
|
|
504 |
// Prepare temporal folder
|
|
505 |
File workFolder = FileUtils.getNewWritableFolder(
|
|
506 |
temporalDeployMapFolder, "__tmp__".concat(pkgFileName));
|
|
507 |
File tempFolder = new File(workFolder, info.getId());
|
483 |
508 |
|
484 |
|
// Zip file
|
485 |
|
FileUtils.zipFolder(workFolder, targetFile);
|
|
509 |
// Copy all content of map folder
|
|
510 |
FileUtils.copyDirectory(thematicMapIntallFolder, tempFolder, true);
|
486 |
511 |
|
487 |
|
return targetFile;
|
488 |
|
}
|
|
512 |
// Remove unused thematicMap files
|
|
513 |
String mapFileName = FilenameUtils.getName(map.getSourceFilePath());
|
|
514 |
File[] tmFiles = tempFolder.listFiles(thematicMapFileFilter);
|
|
515 |
for (File thFile : tmFiles) {
|
|
516 |
if (!StringUtils.equals(thFile.getName(), mapFileName)) {
|
|
517 |
thFile.delete();
|
|
518 |
}
|
|
519 |
}
|
489 |
520 |
|
490 |
|
public String getTranslation(String key) {
|
491 |
|
// TODO: add I18nManager usage when org.gvsig.tools.lib 2.1.0 becomes
|
492 |
|
// available
|
493 |
|
// return this.i18nmanager.getTranslation(key);
|
494 |
|
return Messages.getText(key);
|
495 |
|
}
|
|
521 |
// Zip file
|
|
522 |
FileUtils.zipFolder(workFolder, targetFile);
|
|
523 |
|
|
524 |
return targetFile;
|
|
525 |
}
|
|
526 |
|
|
527 |
public String getTranslation(String key) {
|
|
528 |
// TODO: add I18nManager usage when org.gvsig.tools.lib 2.1.0 becomes
|
|
529 |
// available
|
|
530 |
// return this.i18nmanager.getTranslation(key);
|
|
531 |
return Messages.getText(key);
|
|
532 |
}
|
|
533 |
|
|
534 |
public Service getService(DynObject parameters) throws ServiceException {
|
|
535 |
// return thematic map compilation (ThematicMapService)
|
|
536 |
|
|
537 |
// supose que he d'agafar un identificador de parameters del mapa que busque
|
|
538 |
// despres, buscar-lo i passar-lo a DefaultThematicMapCompilation i
|
|
539 |
// tornar la compilacio.
|
|
540 |
|
|
541 |
ThematicMap map = null;
|
|
542 |
|
|
543 |
List<ThematicMap> mapsList = getInstalledMaps();
|
|
544 |
|
|
545 |
// TODO: Get the thematicmap that has the given parameters
|
|
546 |
|
|
547 |
DefaultThematicMapCompilation serviceCompilation = new DefaultThematicMapCompilation(
|
|
548 |
map, this);
|
|
549 |
|
|
550 |
return serviceCompilation;
|
|
551 |
|
|
552 |
}
|
|
553 |
|
|
554 |
public ThematicMapCompilation createCompilationInstance(String gameName)
|
|
555 |
throws ServiceException {
|
|
556 |
DynObject params = createServiceParameters(gameName);
|
|
557 |
|
|
558 |
|
|
559 |
// DefaultThematicMapCompilation compilation = new DefaultThematicMapCompilation(this);
|
|
560 |
// compilation.
|
|
561 |
// Instanciar thematicmapcompilation i assignar-li params
|
|
562 |
return null;
|
|
563 |
}
|
496 |
564 |
}
|