svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / installer / translations / TranslationsInstaller.java @ 41284
History | View | Annotate | Download (6.16 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 |
|
25 |
package org.gvsig.andami.installer.translations; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.FileOutputStream; |
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
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.PluginsLocator; |
37 |
import org.gvsig.andami.PluginsManager; |
38 |
import org.gvsig.installer.lib.api.PackageInfo; |
39 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
40 |
import org.gvsig.installer.lib.spi.InstallerInfoFileException; |
41 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.service.spi.AbstractProvider; |
44 |
import org.gvsig.tools.service.spi.ProviderServices; |
45 |
import org.gvsig.tools.task.SimpleTaskStatus; |
46 |
import org.gvsig.tools.task.TaskStatusManager; |
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
|
51 |
public class TranslationsInstaller extends AbstractProvider implements InstallPackageProvider { |
52 |
|
53 |
private static Logger logger = LoggerFactory.getLogger(TranslationsInstaller.class); |
54 |
|
55 |
private int BUFFER = 2048; |
56 |
|
57 |
public TranslationsInstaller(ProviderServices providerServices) {
|
58 |
super(providerServices);
|
59 |
} |
60 |
|
61 |
public void install(File applicationFolder, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException { |
62 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
63 |
File i18nFolder = pluginsManager.getApplicationI18nFolder();
|
64 |
|
65 |
logger.info("Installing package '"+packageInfo.getCode()+"' in '"+i18nFolder.getAbsolutePath()+"'."); |
66 |
try {
|
67 |
if (!i18nFolder.exists()) {
|
68 |
logger.warn("Can install package '"+packageInfo.getCode()+"', install folder '"+i18nFolder+"' does not exists."); |
69 |
throw new TranslationsIntallerFolderNotFoundException(); |
70 |
} |
71 |
decompress(inputStream, i18nFolder); |
72 |
} catch (Exception e) { |
73 |
try {
|
74 |
logger.warn("Can install package '"+packageInfo.getCode()+"'.", e); |
75 |
// if there is an exception, installLater is called
|
76 |
installLater(applicationFolder, inputStream, packageInfo); |
77 |
} catch (IOException e1) { |
78 |
logger.warn("Can install package '"+packageInfo.getCode()+"'.", e1); |
79 |
throw new InstallPackageServiceException(e1); |
80 |
} |
81 |
} |
82 |
|
83 |
} |
84 |
|
85 |
private void decompress(InputStream inputStream, File folder) |
86 |
throws ZipException, IOException, InstallerInfoFileException { |
87 |
|
88 |
ZipInputStream zis = null; |
89 |
ZipEntry entry = null; |
90 |
byte data[] = new byte[BUFFER]; |
91 |
int count = 0; |
92 |
|
93 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
94 |
SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Uncompressing...");
|
95 |
|
96 |
manager.add(taskStatus); |
97 |
String entryName = "(header)"; |
98 |
try {
|
99 |
long readed = 0; |
100 |
zis = new ZipInputStream(inputStream); |
101 |
while ((entry = zis.getNextEntry()) != null) { |
102 |
entryName = FilenameUtils.separatorsToSystem(entry.getName()); |
103 |
taskStatus.message(entryName); |
104 |
|
105 |
File file = new File(folder, entryName); |
106 |
if (entry.isDirectory()) {
|
107 |
file.mkdirs(); |
108 |
} else {
|
109 |
if (file.exists()) {
|
110 |
FileUtils.forceDelete(file); |
111 |
} |
112 |
if( !file.getParentFile().exists() ) {
|
113 |
FileUtils.forceMkdir(file.getParentFile()); |
114 |
} |
115 |
logger.debug("extracting " + file.getAbsolutePath());
|
116 |
FileOutputStream fos = new FileOutputStream(file); |
117 |
while ((count = zis.read(data, 0, BUFFER)) != -1) { |
118 |
fos.write(data, 0, count);
|
119 |
readed += count; |
120 |
taskStatus.setCurValue(readed); |
121 |
} |
122 |
fos.flush(); |
123 |
fos.close(); |
124 |
} |
125 |
} |
126 |
zis.close(); |
127 |
} catch(IOException ex) { |
128 |
logger.warn("Problems uncompresing 'translations' (last entry '"+entryName+"'.",ex); |
129 |
throw ex;
|
130 |
|
131 |
} catch(RuntimeException ex) { |
132 |
logger.warn("Problems uncompresing 'translations' (last entry '"+entryName+"'.",ex); |
133 |
throw ex;
|
134 |
|
135 |
} finally {
|
136 |
taskStatus.remove(); |
137 |
|
138 |
} |
139 |
} |
140 |
|
141 |
|
142 |
public void installLater(File applicationDirectory, InputStream inputStream, PackageInfo packageInfo) throws InstallPackageServiceException, IOException { |
143 |
logger.warn("installLater is not implementes.");
|
144 |
} |
145 |
|
146 |
|
147 |
public class TranslationsIntallerFolderNotFoundException extends |
148 |
InstallPackageServiceException { |
149 |
|
150 |
private static final long serialVersionUID = 4416143986837955881L; |
151 |
|
152 |
private static final String message = "Translations folder not found"; |
153 |
|
154 |
private static final String KEY = "translations_folder_not_found"; |
155 |
|
156 |
public TranslationsIntallerFolderNotFoundException() {
|
157 |
super(message, KEY, serialVersionUID);
|
158 |
} |
159 |
|
160 |
} |
161 |
} |