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 / installer / EPSGDatabaseInstaller.java @ 258
History | View | Annotate | Download (6.54 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.crs.installer; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileOutputStream; |
28 |
import java.io.IOException; |
29 |
import java.io.InputStream; |
30 |
import java.net.MalformedURLException; |
31 |
import java.util.zip.ZipEntry; |
32 |
import java.util.zip.ZipException; |
33 |
import java.util.zip.ZipInputStream; |
34 |
import org.apache.commons.io.FileUtils; |
35 |
import org.apache.commons.io.FilenameUtils; |
36 |
import org.gvsig.andami.LocaleManager; |
37 |
import org.gvsig.andami.PluginServices; |
38 |
import org.gvsig.andami.PluginsLocator; |
39 |
import org.gvsig.andami.PluginsManager; |
40 |
import org.gvsig.crs.CrsFactory; |
41 |
import org.gvsig.crs.JCrsExtension; |
42 |
import org.gvsig.installer.lib.api.PackageInfo; |
43 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
44 |
import org.gvsig.installer.lib.spi.InstallerInfoFileException; |
45 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
46 |
import org.gvsig.tools.ToolsLocator; |
47 |
import org.gvsig.tools.dynobject.DynObject; |
48 |
import org.gvsig.tools.service.spi.AbstractProvider; |
49 |
import org.gvsig.tools.service.spi.ProviderServices; |
50 |
import org.gvsig.tools.task.SimpleTaskStatus; |
51 |
import org.gvsig.tools.task.TaskStatusManager; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
public class EPSGDatabaseInstaller extends AbstractProvider implements InstallPackageProvider { |
56 |
|
57 |
private static Logger logger = LoggerFactory.getLogger(EPSGDatabaseInstaller.class); |
58 |
|
59 |
private int BUFFER = 2048; |
60 |
|
61 |
public EPSGDatabaseInstaller(ProviderServices providerServices) {
|
62 |
super(providerServices);
|
63 |
} |
64 |
|
65 |
public void install(File applicationFolder, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException { |
66 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
67 |
File i18nFolder = pluginsManager.getApplicationI18nFolder();
|
68 |
|
69 |
File epsgDatabaseFolder = new File(CrsFactory.getDataBaseFolder(),"EPSG"); |
70 |
|
71 |
logger.info("Installing package '" + packageInfo.getCode() + "' in '" + epsgDatabaseFolder.getAbsolutePath() + "'."); |
72 |
try {
|
73 |
FileUtils.forceMkdir(epsgDatabaseFolder); |
74 |
decompress(inputStream, epsgDatabaseFolder); |
75 |
activateThisDataBase(packageInfo); |
76 |
} catch (Exception e) { |
77 |
try {
|
78 |
logger.warn("Can install package '" + packageInfo.getCode() + "'.", e); |
79 |
// if there is an exception, installLater is called
|
80 |
installLater(applicationFolder, inputStream, packageInfo); |
81 |
} catch (IOException e1) { |
82 |
logger.warn("Can install package '" + packageInfo.getCode() + "'.", e1); |
83 |
throw new InstallPackageServiceException(e1); |
84 |
} |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
private void activateThisDataBase(PackageInfo packageInfo) { |
90 |
String dbentry = "EPSG/" + packageInfo.getCode() + "/EPSG.sql"; |
91 |
File f = new File(CrsFactory.getDataBaseFolder(),dbentry); |
92 |
if( !f.exists() ) {
|
93 |
logger.warn("Can't activate EPSG database, problems locating in '"+f.getAbsolutePath()+"'."); |
94 |
return;
|
95 |
} |
96 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
97 |
PluginServices plugin = pluginManager.getPlugin(this);
|
98 |
DynObject pluginProperties = plugin.getPluginProperties(); |
99 |
|
100 |
pluginProperties.setDynValue("epsgDatabase", dbentry);
|
101 |
plugin.savePluginProperties(); |
102 |
} |
103 |
|
104 |
private void decompress(InputStream inputStream, File folder) |
105 |
throws ZipException, IOException, InstallerInfoFileException { |
106 |
|
107 |
ZipInputStream zis = null; |
108 |
ZipEntry entry = null; |
109 |
byte data[] = new byte[BUFFER]; |
110 |
int count = 0; |
111 |
|
112 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
113 |
SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Uncompressing...");
|
114 |
|
115 |
manager.add(taskStatus); |
116 |
String entryName = "(header)"; |
117 |
try {
|
118 |
long readed = 0; |
119 |
zis = new ZipInputStream(inputStream); |
120 |
while ( (entry = zis.getNextEntry()) != null ) { |
121 |
entryName = FilenameUtils.separatorsToSystem(entry.getName()); |
122 |
taskStatus.message(entryName); |
123 |
|
124 |
File file = new File(folder, entryName); |
125 |
if ( entry.isDirectory() ) {
|
126 |
file.mkdirs(); |
127 |
} else {
|
128 |
if ( file.exists() ) {
|
129 |
FileUtils.forceDelete(file); |
130 |
} |
131 |
if ( !file.getParentFile().exists() ) {
|
132 |
FileUtils.forceMkdir(file.getParentFile()); |
133 |
} |
134 |
logger.debug("extracting " + file.getAbsolutePath());
|
135 |
FileOutputStream fos = new FileOutputStream(file); |
136 |
while ( (count = zis.read(data, 0, BUFFER)) != -1 ) { |
137 |
fos.write(data, 0, count);
|
138 |
readed += count; |
139 |
taskStatus.setCurValue(readed); |
140 |
} |
141 |
fos.flush(); |
142 |
fos.close(); |
143 |
} |
144 |
} |
145 |
zis.close(); |
146 |
} catch (IOException ex) { |
147 |
logger.warn("Problems uncompresing 'EPSG database' (last entry '" + entryName + "'.", ex); |
148 |
throw ex;
|
149 |
|
150 |
} catch (RuntimeException ex) { |
151 |
logger.warn("Problems uncompresing 'EPSG database' (last entry '" + entryName + "'.", ex); |
152 |
throw ex;
|
153 |
|
154 |
} finally {
|
155 |
taskStatus.remove(); |
156 |
|
157 |
} |
158 |
} |
159 |
|
160 |
public void installLater(File applicationDirectory, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException, IOException { |
161 |
logger.warn("installLater is not implementes.");
|
162 |
} |
163 |
} |