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 @ 352
History | View | Annotate | Download (5.38 KB)
1 |
package org.gvsig.crs; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
|
9 |
import org.apache.commons.io.FileUtils; |
10 |
import org.apache.commons.io.filefilter.FileFilterUtils; |
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.getDefaultEPSGDatabaseFile(plugin, properties));
|
49 |
|
50 |
EPSGDatabaseInstallerFactory.registerEPSGDatabaseInstaller(); |
51 |
} |
52 |
|
53 |
private File getDefaultEPSGDatabaseFile(PluginServices plugin, DynObject pluginProperties) { |
54 |
String fname = null; |
55 |
File dbfolder = null; |
56 |
try {
|
57 |
dbfolder = new File (plugin.getPluginHomeFolder(),"db"); |
58 |
File f2;
|
59 |
fname = (String) pluginProperties.getDynValue("epsgDatabase"); |
60 |
if( fname!=null ) { |
61 |
f2 = new File(dbfolder, fname); |
62 |
if( f2.exists() ) {
|
63 |
return new File(fname); |
64 |
} |
65 |
} |
66 |
File folder = new File(dbfolder,"EPSG"); |
67 |
List<File> subfolders = new ArrayList(FileUtils.listFiles( |
68 |
folder, |
69 |
new String[] { "sql" }, |
70 |
true
|
71 |
)); |
72 |
if( subfolders.size()>0 ) { |
73 |
// Asumimos que el primer script de SQL que encuentre es una
|
74 |
// de las BBDD a utilizar.
|
75 |
f2 = subfolders.get(0);
|
76 |
return f2;
|
77 |
} |
78 |
logger.warn("Can't locate a EPSG database in folder '"+dbfolder+"' use '"+fname+"'."); |
79 |
} catch(Exception ex) { |
80 |
logger.warn("Can't locate a EPSG database in folder '"+dbfolder+"' use '"+fname+"'.", ex); |
81 |
} |
82 |
fname = "EPSG" + File.separator + "EPSG_v8_7" + File.separator + "EPSG.sql"; |
83 |
return new File(fname); |
84 |
} |
85 |
|
86 |
private void initalizeHomeDB(PluginServices plugin) { |
87 |
|
88 |
File sourceDBFolder = new File(plugin.getPluginDirectory(), "db"); |
89 |
File sourceProjLibFolder = new File(plugin.getPluginDirectory(), "projlib"); |
90 |
File sourceEPSGFolder = new File(sourceDBFolder, "EPSG"); |
91 |
|
92 |
File targetDBFolder = new File(plugin.getPluginHomeFolder(), "db"); |
93 |
File targetProjLibFolder = new File(plugin.getPluginHomeFolder(), "projlib"); |
94 |
File targetEPSGFolder = new File(targetDBFolder, "EPSG"); |
95 |
|
96 |
if( !targetDBFolder.exists() ) {
|
97 |
try {
|
98 |
logger.info("Initializing CRS db in "+targetDBFolder.getAbsolutePath());
|
99 |
FileUtils.forceMkdir(targetDBFolder); |
100 |
FileUtils.copyDirectory(sourceDBFolder, targetDBFolder); |
101 |
} catch (IOException e) { |
102 |
logger.error("Can't initilize the CRS database in the gvSIG home (src="+sourceDBFolder.getAbsolutePath()+",target="+targetDBFolder.getAbsolutePath()+").",e); |
103 |
throw new RuntimeException(e); |
104 |
} |
105 |
} |
106 |
if( !targetProjLibFolder.exists() ) {
|
107 |
try {
|
108 |
logger.info("Initializing PROJ_LIB folder in "+targetProjLibFolder.getAbsolutePath());
|
109 |
FileUtils.forceMkdir(targetProjLibFolder); |
110 |
FileUtils.copyDirectory(sourceProjLibFolder, targetProjLibFolder); |
111 |
} catch (IOException e) { |
112 |
logger.error("Can't initilize the PROJ_LIB folder in the gvSIG home (src="+sourceProjLibFolder.getAbsolutePath()+",target="+targetProjLibFolder.getAbsolutePath()+").",e); |
113 |
throw new RuntimeException(e); |
114 |
} |
115 |
} |
116 |
if( !targetEPSGFolder.exists() ) {
|
117 |
try {
|
118 |
logger.info("Initializing EPSG database folder in "+targetEPSGFolder.getAbsolutePath());
|
119 |
FileUtils.forceMkdir(targetEPSGFolder); |
120 |
FileUtils.copyDirectory(sourceEPSGFolder, targetEPSGFolder); |
121 |
} catch (IOException e) { |
122 |
logger.error("Can't initilize EPSG database folder in the gvSIG home (src="+sourceEPSGFolder.getAbsolutePath()+",target="+targetEPSGFolder.getAbsolutePath()+").",e); |
123 |
throw new RuntimeException(e); |
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
protected void doPostInitialize() throws LibraryException { |
129 |
|
130 |
} |
131 |
|
132 |
} |