root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.app.jcrs / org.gvsig.projection.app.jcrs.common / src / main / java / org / gvsig / crs / JCRSPluginLibrary.java @ 306
History | View | Annotate | Download (5.1 KB)
1 |
package org.gvsig.crs; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.Collection; |
6 |
import java.util.Iterator; |
7 |
|
8 |
import org.apache.commons.io.FileUtils; |
9 |
import org.apache.commons.io.filefilter.FileFilterUtils; |
10 |
import org.apache.commons.io.filefilter.TrueFileFilter; |
11 |
import org.apache.commons.lang3.BooleanUtils; |
12 |
import org.cresques.ProjectionLibrary; |
13 |
import org.gvsig.andami.PluginServices; |
14 |
import org.gvsig.andami.PluginsLocator; |
15 |
import org.gvsig.crs.installer.EPSGDatabaseInstallerFactory; |
16 |
import org.gvsig.crs.persistence.CompTrData; |
17 |
import org.gvsig.crs.persistence.CrsData; |
18 |
import org.gvsig.crs.persistence.TrData; |
19 |
import org.gvsig.tools.dynobject.DynObject; |
20 |
import org.gvsig.tools.library.AbstractLibrary; |
21 |
import org.gvsig.tools.library.LibraryException; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
public class JCRSPluginLibrary extends AbstractLibrary { |
26 |
|
27 |
private final static Logger logger = LoggerFactory.getLogger(JCrsExtension.class); |
28 |
|
29 |
public void doRegistration() { |
30 |
// Hack to assign database folder to the jCRS library.
|
31 |
registerAsServiceOf(ProjectionLibrary.class); |
32 |
} |
33 |
|
34 |
protected void doInitialize() throws LibraryException { |
35 |
CrsData.registerPersistence(); |
36 |
TrData.registerPersistence(); |
37 |
CompTrData.registerPersistence(); |
38 |
|
39 |
PluginServices plugin = PluginsLocator.getManager().getPlugin(JCrsExtension.class); |
40 |
DynObject properties = plugin.getPluginProperties(); |
41 |
|
42 |
initalizeHomeDB(plugin); |
43 |
CrsFactory.setDataBaseFolder(new File (plugin.getPluginHomeFolder(),"db")); |
44 |
CrsFactory.setProjLibFolder(new File (plugin.getPluginHomeFolder(),"projlib")); |
45 |
CrsFactory.setEnableMemoryCacheOfCRS( |
46 |
BooleanUtils.isTrue((Boolean) properties.getDynValue("useMemoryCacheForCRSs")) |
47 |
); |
48 |
CrsFactory.setEpsgDatabaseFile(this.getDefaultDatabaseFolder(plugin, properties));
|
49 |
|
50 |
EPSGDatabaseInstallerFactory.registerEPSGDatabaseInstaller(); |
51 |
} |
52 |
|
53 |
private File getDefaultDatabaseFolder(PluginServices plugin, DynObject pluginProperties) { |
54 |
File dbfolder = new File (plugin.getPluginHomeFolder(),"db"); |
55 |
File f2;
|
56 |
String fname = (String) pluginProperties.getDynValue("epsgDatabase"); |
57 |
if( fname!=null ) { |
58 |
f2 = new File(dbfolder, fname); |
59 |
if( f2.exists() ) {
|
60 |
return new File(fname); |
61 |
} |
62 |
} |
63 |
File folder = new File(dbfolder,"EPSG"); |
64 |
Collection<File> subfolders = FileUtils.listFilesAndDirs( |
65 |
folder, |
66 |
FileFilterUtils.falseFileFilter(), |
67 |
FileFilterUtils.trueFileFilter() |
68 |
); |
69 |
Iterator<File> it = subfolders.iterator(); |
70 |
if( it.hasNext() ) {
|
71 |
f2 = it.next(); |
72 |
return FileUtils.getFile(dbfolder, "EPSG",f2.getName()); |
73 |
} |
74 |
fname = "EPSG" + File.separator + "EPSG_v8_5"; |
75 |
logger.warn("Can't locate a EPSG database in '"+dbfolder+"' use '"+fname+"'."); |
76 |
return new File(fname); |
77 |
} |
78 |
|
79 |
private void initalizeHomeDB(PluginServices plugin) { |
80 |
|
81 |
File sourceDBFolder = new File(plugin.getPluginDirectory(), "db"); |
82 |
File sourceProjLibFolder = new File(plugin.getPluginDirectory(), "projlib"); |
83 |
File sourceEPSGFolder = new File(sourceDBFolder, "EPSG"); |
84 |
|
85 |
File targetDBFolder = new File(plugin.getPluginHomeFolder(), "db"); |
86 |
File targetProjLibFolder = new File(plugin.getPluginHomeFolder(), "projlib"); |
87 |
File targetEPSGFolder = new File(targetDBFolder, "EPSG"); |
88 |
|
89 |
if( !targetDBFolder.exists() ) {
|
90 |
try {
|
91 |
logger.info("Initializing CRS db in "+targetDBFolder.getAbsolutePath());
|
92 |
FileUtils.forceMkdir(targetDBFolder); |
93 |
FileUtils.copyDirectory(sourceDBFolder, targetDBFolder); |
94 |
} catch (IOException e) { |
95 |
logger.error("Can't initilize the CRS database in the gvSIG home (src="+sourceDBFolder.getAbsolutePath()+",target="+targetDBFolder.getAbsolutePath()+").",e); |
96 |
throw new RuntimeException(e); |
97 |
} |
98 |
} |
99 |
if( !targetProjLibFolder.exists() ) {
|
100 |
try {
|
101 |
logger.info("Initializing PROJ_LIB folder in "+targetProjLibFolder.getAbsolutePath());
|
102 |
FileUtils.forceMkdir(targetProjLibFolder); |
103 |
FileUtils.copyDirectory(sourceProjLibFolder, targetProjLibFolder); |
104 |
} catch (IOException e) { |
105 |
logger.error("Can't initilize the PROJ_LIB folder in the gvSIG home (src="+sourceProjLibFolder.getAbsolutePath()+",target="+targetProjLibFolder.getAbsolutePath()+").",e); |
106 |
throw new RuntimeException(e); |
107 |
} |
108 |
} |
109 |
if( !targetEPSGFolder.exists() ) {
|
110 |
try {
|
111 |
logger.info("Initializing EPSG database folder in "+targetEPSGFolder.getAbsolutePath());
|
112 |
FileUtils.forceMkdir(targetEPSGFolder); |
113 |
FileUtils.copyDirectory(sourceEPSGFolder, targetEPSGFolder); |
114 |
} catch (IOException e) { |
115 |
logger.error("Can't initilize EPSG database folder in the gvSIG home (src="+sourceEPSGFolder.getAbsolutePath()+",target="+targetEPSGFolder.getAbsolutePath()+").",e); |
116 |
throw new RuntimeException(e); |
117 |
} |
118 |
} |
119 |
} |
120 |
|
121 |
protected void doPostInitialize() throws LibraryException { |
122 |
|
123 |
} |
124 |
|
125 |
} |