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 @ 353
History | View | Annotate | Download (6.16 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 |
|
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.andami.PluginsLocator; |
16 |
import org.gvsig.crs.installer.EPSGDatabaseInstallerFactory; |
17 |
import org.gvsig.crs.persistence.CompTrData; |
18 |
import org.gvsig.crs.persistence.CrsData; |
19 |
import org.gvsig.crs.persistence.TrData; |
20 |
import org.gvsig.tools.dynobject.DynObject; |
21 |
import org.gvsig.tools.library.AbstractLibrary; |
22 |
import org.gvsig.tools.library.LibraryException; |
23 |
|
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
public class JCRSPluginLibrary extends AbstractLibrary { |
28 |
|
29 |
private static final String EPSG_VERSION = "EPSG_v8_7"; |
30 |
private final static Logger logger = LoggerFactory.getLogger(JCrsExtension.class); |
31 |
|
32 |
public void doRegistration() { |
33 |
// Hack to assign database folder to the jCRS library.
|
34 |
registerAsServiceOf(ProjectionLibrary.class); |
35 |
} |
36 |
|
37 |
protected void doInitialize() throws LibraryException { |
38 |
CrsData.registerPersistence(); |
39 |
TrData.registerPersistence(); |
40 |
CompTrData.registerPersistence(); |
41 |
|
42 |
PluginServices plugin = PluginsLocator.getManager().getPlugin(JCrsExtension.class); |
43 |
DynObject properties = plugin.getPluginProperties(); |
44 |
|
45 |
initalizeHomeDB(plugin); |
46 |
CrsFactory.setDataBaseFolder(new File (plugin.getPluginHomeFolder(),"db")); |
47 |
CrsFactory.setProjLibFolder(new File (plugin.getPluginHomeFolder(),"projlib")); |
48 |
CrsFactory.setEnableMemoryCacheOfCRS( |
49 |
BooleanUtils.isTrue((Boolean) properties.getDynValue("useMemoryCacheForCRSs")) |
50 |
); |
51 |
CrsFactory.setEpsgDatabaseFile(this.getDefaultEPSGDatabaseFile(plugin, properties));
|
52 |
|
53 |
EPSGDatabaseInstallerFactory.registerEPSGDatabaseInstaller(); |
54 |
} |
55 |
|
56 |
private File getDefaultEPSGDatabaseFile(PluginServices plugin, DynObject pluginProperties) { |
57 |
String fname = null; |
58 |
File dbfolder = null; |
59 |
try {
|
60 |
dbfolder = new File (plugin.getPluginHomeFolder(),"db"); |
61 |
File f2;
|
62 |
fname = (String) pluginProperties.getDynValue("epsgDatabase"); |
63 |
if( fname!=null ) { |
64 |
f2 = new File(dbfolder, fname); |
65 |
if( f2.exists() ) {
|
66 |
return new File(fname); |
67 |
} |
68 |
} |
69 |
File folder = new File(dbfolder,"EPSG"); |
70 |
List<File> subfolders = new ArrayList(FileUtils.listFiles( |
71 |
folder, |
72 |
new String[] { "sql" }, |
73 |
true
|
74 |
)); |
75 |
if( subfolders.size()>0 ) { |
76 |
// Asumimos que el primer script de SQL que encuentre es una
|
77 |
// de las BBDD a utilizar.
|
78 |
f2 = subfolders.get(0);
|
79 |
return f2;
|
80 |
} |
81 |
logger.warn("Can't locate a EPSG database in folder '"+dbfolder+"' use '"+fname+"'."); |
82 |
} catch(Exception ex) { |
83 |
logger.warn("Can't locate a EPSG database in folder '"+dbfolder+"' use '"+fname+"'.", ex); |
84 |
} |
85 |
fname = "EPSG" + File.separator + EPSG_VERSION + File.separator + "EPSG.sql"; |
86 |
return new File(fname); |
87 |
} |
88 |
|
89 |
private void initalizeHomeDB(PluginServices plugin) { |
90 |
|
91 |
File sourceDBFolder = new File(plugin.getPluginDirectory(), "db"); |
92 |
File sourceProjLibFolder = new File(plugin.getPluginDirectory(), "projlib"); |
93 |
File sourceEPSGFolder = new File(sourceDBFolder, "EPSG"); |
94 |
|
95 |
File targetDBFolder = new File(plugin.getPluginHomeFolder(), "db"); |
96 |
File targetProjLibFolder = new File(plugin.getPluginHomeFolder(), "projlib"); |
97 |
File targetEPSGFolder = new File(targetDBFolder, "EPSG"); |
98 |
|
99 |
if( !targetDBFolder.exists() ) {
|
100 |
try {
|
101 |
logger.info("Initializing CRS db in "+targetDBFolder.getAbsolutePath());
|
102 |
FileUtils.forceMkdir(targetDBFolder); |
103 |
FileUtils.copyDirectory(sourceDBFolder, targetDBFolder); |
104 |
} catch (IOException e) { |
105 |
logger.error("Can't initilize the CRS database in the gvSIG home (src="+sourceDBFolder.getAbsolutePath()+",target="+targetDBFolder.getAbsolutePath()+").",e); |
106 |
throw new RuntimeException(e); |
107 |
} |
108 |
} |
109 |
if( !targetProjLibFolder.exists() ) {
|
110 |
try {
|
111 |
logger.info("Initializing PROJ_LIB folder in "+targetProjLibFolder.getAbsolutePath());
|
112 |
FileUtils.forceMkdir(targetProjLibFolder); |
113 |
FileUtils.copyDirectory(sourceProjLibFolder, targetProjLibFolder); |
114 |
} catch (IOException e) { |
115 |
logger.error("Can't initilize the PROJ_LIB folder in the gvSIG home (src="+sourceProjLibFolder.getAbsolutePath()+",target="+targetProjLibFolder.getAbsolutePath()+").",e); |
116 |
throw new RuntimeException(e); |
117 |
} |
118 |
} |
119 |
if( !targetEPSGFolder.exists() ) {
|
120 |
try {
|
121 |
logger.info("Initializing EPSG database folder in "+targetEPSGFolder.getAbsolutePath());
|
122 |
FileUtils.forceMkdir(targetEPSGFolder); |
123 |
FileUtils.copyDirectory(sourceEPSGFolder, targetEPSGFolder); |
124 |
} catch (IOException e) { |
125 |
logger.error("Can't initilize EPSG database folder in the gvSIG home (src="+sourceEPSGFolder.getAbsolutePath()+",target="+targetEPSGFolder.getAbsolutePath()+").",e); |
126 |
throw new RuntimeException(e); |
127 |
} |
128 |
} |
129 |
targetEPSGFolder = new File(targetEPSGFolder,EPSG_VERSION); |
130 |
if( !targetEPSGFolder.exists() ) {
|
131 |
sourceEPSGFolder = new File(sourceEPSGFolder, EPSG_VERSION); |
132 |
try {
|
133 |
logger.info("Initializing EPSG database folder in "+targetEPSGFolder.getAbsolutePath());
|
134 |
FileUtils.forceMkdir(targetEPSGFolder); |
135 |
FileUtils.copyDirectory(sourceEPSGFolder, targetEPSGFolder); |
136 |
} catch (IOException e) { |
137 |
logger.error("Can't initilize EPSG database folder in the gvSIG home (src="+sourceEPSGFolder.getAbsolutePath()+",target="+targetEPSGFolder.getAbsolutePath()+").",e); |
138 |
throw new RuntimeException(e); |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
protected void doPostInitialize() throws LibraryException { |
144 |
|
145 |
} |
146 |
|
147 |
} |