svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / creation / DefaultMakePluginPackageService.java @ 34107
History | View | Annotate | Download (15 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2010 {Prodevelop} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.installer.lib.impl.creation; |
29 |
|
30 |
import java.io.BufferedReader; |
31 |
import java.io.ByteArrayInputStream; |
32 |
import java.io.File; |
33 |
import java.io.FileInputStream; |
34 |
import java.io.FileOutputStream; |
35 |
import java.io.FileReader; |
36 |
import java.io.IOException; |
37 |
import java.io.InputStream; |
38 |
import java.io.OutputStream; |
39 |
import java.util.ArrayList; |
40 |
import java.util.HashMap; |
41 |
import java.util.List; |
42 |
import java.util.Map; |
43 |
|
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
import org.gvsig.installer.lib.api.InstallerManager; |
48 |
import org.gvsig.installer.lib.api.PackageInfo; |
49 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageService; |
50 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
51 |
import org.gvsig.installer.lib.impl.DefaultPackageInfo; |
52 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
53 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
54 |
import org.gvsig.tools.service.Manager; |
55 |
|
56 |
/**
|
57 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
58 |
*/
|
59 |
public class DefaultMakePluginPackageService implements |
60 |
MakePluginPackageService { |
61 |
|
62 |
private static final Logger LOG = LoggerFactory |
63 |
.getLogger(DefaultMakePluginPackageService.class); |
64 |
|
65 |
public static final String ANT_FILE_NAME = "install.xml"; |
66 |
public static final String COPIED_FILES_DIRECTORY_NAME = "files"; |
67 |
private File pluginsDirectory; |
68 |
private static final Logger logger = org.slf4j.LoggerFactory |
69 |
.getLogger(DefaultMakePluginPackageService.class); |
70 |
private final InstallerManager manager; |
71 |
|
72 |
private List<PackageInfo> installerInfos = null; |
73 |
private Map<PackageInfo, String> directories = null; |
74 |
|
75 |
protected List<File> selectedFiles = null; |
76 |
protected String antScript = null; |
77 |
private InstallPackageProviderServices installerProviderServices = null; |
78 |
|
79 |
public DefaultMakePluginPackageService(InstallerManager manager,
|
80 |
File pluginsDirectory) throws MakePluginPackageServiceException { |
81 |
super();
|
82 |
this.manager = manager;
|
83 |
installerInfos = new ArrayList<PackageInfo>(); |
84 |
directories = new HashMap<PackageInfo, String>(); |
85 |
selectedFiles = new ArrayList<File>(); |
86 |
installerProviderServices = |
87 |
InstallerProviderLocator.getProviderManager() |
88 |
.createInstallerProviderServices(); |
89 |
initialize(pluginsDirectory); |
90 |
} |
91 |
|
92 |
private void initialize(File pluginsDirectory) |
93 |
throws MakePluginPackageServiceException {
|
94 |
if ((!pluginsDirectory.exists()) || (!pluginsDirectory.isDirectory())) {
|
95 |
throw new MakePluginPackageServiceException( |
96 |
"The plugins directory does't exists or not is a directory");
|
97 |
} |
98 |
this.pluginsDirectory = pluginsDirectory;
|
99 |
|
100 |
// Read all the installed plugins
|
101 |
String[] plugins = pluginsDirectory.list(); |
102 |
|
103 |
if (plugins != null) { |
104 |
for (int i = 0; i < plugins.length; i++) { |
105 |
String pluginDirectoryName = plugins[i];
|
106 |
File pluginDirectoryFile =
|
107 |
getAbsolutePluginPackageDirectory(pluginDirectoryName); |
108 |
DefaultPackageInfo packageInfo = new DefaultPackageInfo();
|
109 |
|
110 |
installerProviderServices.readPackageInfo(new File( |
111 |
pluginDirectoryFile.getAbsolutePath()), packageInfo); |
112 |
|
113 |
// Checks if the ant file exists
|
114 |
File antFile = getAntFile(pluginDirectoryFile);
|
115 |
if (antFile.exists()) {
|
116 |
try {
|
117 |
packageInfo.setAnScript(readFileAsString(antFile |
118 |
.getAbsolutePath())); |
119 |
} catch (IOException e) { |
120 |
logger.error("Not possible to read the ant file");
|
121 |
} |
122 |
} |
123 |
|
124 |
// Checks if there are files to copy
|
125 |
File copiedFilesDirectory =
|
126 |
getCopiedFilesDirectory(pluginDirectoryFile); |
127 |
if (copiedFilesDirectory.exists()) {
|
128 |
loadCopiedFiles(copiedFilesDirectory, copiedFilesDirectory, |
129 |
packageInfo); |
130 |
} |
131 |
|
132 |
installerInfos.add(packageInfo); |
133 |
directories.put(packageInfo, pluginDirectoryName); |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
private void loadCopiedFiles(File file, File filesDirectory, |
139 |
PackageInfo packageInfo) throws MakePluginPackageServiceException {
|
140 |
if (file.isDirectory()) {
|
141 |
File[] files = file.listFiles(); |
142 |
for (int i = 0; i < files.length; i++) { |
143 |
loadCopiedFiles(files[i], filesDirectory, packageInfo); |
144 |
} |
145 |
} else {
|
146 |
// Removing the plugin prefix
|
147 |
|
148 |
String pluginFileName =
|
149 |
file.getAbsolutePath().substring( |
150 |
filesDirectory.getAbsolutePath().length(), |
151 |
file.getAbsolutePath().length()); |
152 |
|
153 |
// Ading the root directory
|
154 |
File pluginFile =
|
155 |
new File(pluginsDirectory.getParentFile().getParentFile() |
156 |
.getAbsolutePath() |
157 |
+ pluginFileName); |
158 |
|
159 |
packageInfo.addFileToCopy(pluginFile); |
160 |
} |
161 |
} |
162 |
|
163 |
public void createPackageSet(PackageInfo packageInfo, |
164 |
OutputStream packageStream) throws MakePluginPackageServiceException { |
165 |
|
166 |
LOG.debug("Creating a package set of the package info: \n{0}",
|
167 |
packageInfo); |
168 |
|
169 |
writeAdditionalFiles(packageInfo); |
170 |
String pluginFileName = manager.getPackageFileName(packageInfo);
|
171 |
installerProviderServices.compressPackageSet( |
172 |
getAbsolutePluginPackageDirectory(packageInfo), pluginFileName, |
173 |
packageStream); |
174 |
} |
175 |
|
176 |
private void writeAdditionalFiles(PackageInfo packageInfo) |
177 |
throws MakePluginPackageServiceException {
|
178 |
// Write the package.info file
|
179 |
writePackageInfo(packageInfo); |
180 |
|
181 |
if (packageInfo.getAntScript() != null) { |
182 |
// Create the ant file
|
183 |
writeAntFile(packageInfo); |
184 |
|
185 |
// Copy the selected files
|
186 |
writeSelectedFiles(packageInfo); |
187 |
} |
188 |
} |
189 |
|
190 |
public void createPackage(PackageInfo packageInfo, |
191 |
OutputStream packageStream) throws MakePluginPackageServiceException { |
192 |
|
193 |
LOG.debug("Creating package of the package info: \n{0}", packageInfo);
|
194 |
|
195 |
writeAdditionalFiles(packageInfo); |
196 |
installerProviderServices.compressPackage( |
197 |
getAbsolutePluginPackageDirectory(packageInfo), packageStream); |
198 |
} |
199 |
|
200 |
private void writePackageInfo(PackageInfo packageInfo) |
201 |
throws MakePluginPackageServiceException {
|
202 |
writePackageInfo(packageInfo, |
203 |
getAbsolutePluginPackageDirectory(packageInfo)); |
204 |
} |
205 |
|
206 |
public void writePackageInfo(PackageInfo packageInfo, File folder) |
207 |
throws MakePluginPackageServiceException {
|
208 |
installerProviderServices.writePackageInfo(folder, packageInfo); |
209 |
} |
210 |
|
211 |
private void writeAntFile(PackageInfo packageInfo) |
212 |
throws MakePluginPackageServiceException {
|
213 |
try {
|
214 |
ByteArrayInputStream in =
|
215 |
new ByteArrayInputStream(packageInfo.getAntScript().getBytes()); |
216 |
OutputStream out =
|
217 |
new FileOutputStream(getAbsoluteAntFile(packageInfo)); |
218 |
|
219 |
// Copy the bits from instream to outstream
|
220 |
byte[] buf = new byte[1024]; |
221 |
int len;
|
222 |
while ((len = in.read(buf)) > 0) { |
223 |
out.write(buf, 0, len);
|
224 |
} |
225 |
in.close(); |
226 |
out.close(); |
227 |
} catch (IOException e) { |
228 |
throw new MakePluginPackageServiceException( |
229 |
"Exception writing the ant file");
|
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
private void writeSelectedFiles(PackageInfo packageInfo) |
235 |
throws MakePluginPackageServiceException {
|
236 |
try {
|
237 |
String copiedFilesDirectoryName =
|
238 |
getAbsoulteCopiedFilesDirectoryName(packageInfo); |
239 |
|
240 |
// Deleting the previous files folder
|
241 |
File copiedFilesDirectoryfile = new File(copiedFilesDirectoryName); |
242 |
deleteDirectory(copiedFilesDirectoryfile); |
243 |
|
244 |
// It works if the plugins directory is in the folder
|
245 |
// "gvSIG/extensiones"
|
246 |
String applicationDirectory =
|
247 |
pluginsDirectory.getParentFile().getParent(); |
248 |
|
249 |
for (int i = 0; i < packageInfo.getFileToCopySize(); i++) { |
250 |
String sourceFile =
|
251 |
packageInfo.getFileToCopyAt(i).getAbsolutePath(); |
252 |
|
253 |
// Create the final path
|
254 |
String destFile =
|
255 |
sourceFile.substring(applicationDirectory.length(), |
256 |
sourceFile.length()); |
257 |
destFile = copiedFilesDirectoryName + destFile; |
258 |
|
259 |
// Copy the files
|
260 |
copy(new File(sourceFile), new File(destFile)); |
261 |
} |
262 |
} catch (IOException e) { |
263 |
throw new MakePluginPackageServiceException( |
264 |
"Exception copying the files");
|
265 |
} |
266 |
} |
267 |
|
268 |
static public boolean deleteDirectory(File path) { |
269 |
if (path.exists()) {
|
270 |
File[] files = path.listFiles(); |
271 |
for (int i = 0; i < files.length; i++) { |
272 |
if (files[i].isDirectory()) {
|
273 |
deleteDirectory(files[i]); |
274 |
} else {
|
275 |
files[i].delete(); |
276 |
} |
277 |
} |
278 |
} |
279 |
return (path.delete());
|
280 |
} |
281 |
|
282 |
void copy(File sourceLocation, File targetLocation) throws IOException { |
283 |
if (sourceLocation.isDirectory()) {
|
284 |
if (!targetLocation.exists()) {
|
285 |
targetLocation.mkdir(); |
286 |
} |
287 |
|
288 |
String[] children = sourceLocation.list(); |
289 |
for (int i = 0; i < children.length; i++) { |
290 |
copy(new File(sourceLocation, children[i]), new File( |
291 |
targetLocation, children[i])); |
292 |
} |
293 |
} else {
|
294 |
targetLocation.getParentFile().mkdirs(); |
295 |
|
296 |
InputStream in = new FileInputStream(sourceLocation); |
297 |
OutputStream out = new FileOutputStream(targetLocation); |
298 |
|
299 |
// Copy the bits from instream to outstream
|
300 |
byte[] buf = new byte[1024]; |
301 |
int len;
|
302 |
while ((len = in.read(buf)) > 0) { |
303 |
out.write(buf, 0, len);
|
304 |
} |
305 |
in.close(); |
306 |
out.close(); |
307 |
} |
308 |
} |
309 |
|
310 |
private String getPluginPackageDirectory(PackageInfo packageInfo) { |
311 |
return directories.get(packageInfo);
|
312 |
} |
313 |
|
314 |
private String getCopiedFilesDirectoryName(PackageInfo packageInfo) |
315 |
throws MakePluginPackageServiceException {
|
316 |
return getPluginPackageDirectory(packageInfo) + File.separator |
317 |
+ COPIED_FILES_DIRECTORY_NAME; |
318 |
} |
319 |
|
320 |
private File getCopiedFilesDirectory(File pluginDirectory) |
321 |
throws MakePluginPackageServiceException {
|
322 |
return new File(pluginDirectory.getAbsolutePath() + File.separator |
323 |
+ COPIED_FILES_DIRECTORY_NAME); |
324 |
} |
325 |
|
326 |
private String getAbsoulteCopiedFilesDirectoryName(PackageInfo packageInfo) |
327 |
throws MakePluginPackageServiceException {
|
328 |
return getAbsolutePluginPackageDirectory(packageInfo).getAbsolutePath()
|
329 |
+ File.separator + COPIED_FILES_DIRECTORY_NAME;
|
330 |
} |
331 |
|
332 |
private String getAntFileName(PackageInfo packageInfo) |
333 |
throws MakePluginPackageServiceException {
|
334 |
return getPluginPackageDirectory(packageInfo) + File.separator |
335 |
+ ANT_FILE_NAME; |
336 |
} |
337 |
|
338 |
private File getAntFile(File pluginDirectory) |
339 |
throws MakePluginPackageServiceException {
|
340 |
return new File(pluginDirectory.getAbsolutePath() + File.separator |
341 |
+ ANT_FILE_NAME); |
342 |
} |
343 |
|
344 |
private File getAbsoluteAntFile(PackageInfo packageInfo) |
345 |
throws MakePluginPackageServiceException {
|
346 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator |
347 |
+ getAntFileName(packageInfo)); |
348 |
} |
349 |
|
350 |
private File getAbsolutePluginPackageDirectory(PackageInfo packageInfo) |
351 |
throws MakePluginPackageServiceException {
|
352 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator |
353 |
+ getPluginPackageDirectory(packageInfo)); |
354 |
} |
355 |
|
356 |
private File getAbsolutePluginPackageDirectory(String pluginDirectory) |
357 |
throws MakePluginPackageServiceException {
|
358 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator |
359 |
+ pluginDirectory); |
360 |
} |
361 |
|
362 |
public Manager getManager() {
|
363 |
return this.manager; |
364 |
} |
365 |
|
366 |
public PackageInfo getPluginPackageInfo(int index) { |
367 |
if (index >= installerInfos.size()) {
|
368 |
return null; |
369 |
} |
370 |
return installerInfos.get(index);
|
371 |
} |
372 |
|
373 |
public PackageInfo getPluginPackageInfo(String code) { |
374 |
for (int i = 0; i < getPluginPackageCount(); i++) { |
375 |
if (installerInfos.get(i).getCode().equals(code)) {
|
376 |
return installerInfos.get(i);
|
377 |
} |
378 |
} |
379 |
return null; |
380 |
} |
381 |
|
382 |
public int getPluginPackageCount() { |
383 |
return installerInfos.size();
|
384 |
} |
385 |
|
386 |
public String getDefaultAntScript() |
387 |
throws MakePluginPackageServiceException {
|
388 |
try {
|
389 |
return readFileAsString(getClass().getClassLoader()
|
390 |
.getResource(ANT_FILE_NAME).getFile()); |
391 |
} catch (IOException e) { |
392 |
throw new MakePluginPackageServiceException( |
393 |
"Impossible to read the default ant file", e);
|
394 |
} |
395 |
} |
396 |
|
397 |
private String readFileAsString(String filePath) throws java.io.IOException { |
398 |
StringBuffer fileData = new StringBuffer(1000); |
399 |
BufferedReader reader = new BufferedReader(new FileReader(filePath)); |
400 |
char[] buf = new char[1024]; |
401 |
int numRead = 0; |
402 |
while ((numRead = reader.read(buf)) != -1) { |
403 |
String readData = String.valueOf(buf, 0, numRead); |
404 |
fileData.append(readData); |
405 |
buf = new char[1024]; |
406 |
} |
407 |
reader.close(); |
408 |
return fileData.toString();
|
409 |
} |
410 |
|
411 |
public File getPluginsDirectory() { |
412 |
return pluginsDirectory;
|
413 |
} |
414 |
|
415 |
public PackageInfo[] getInstalledPackages() |
416 |
throws MakePluginPackageServiceException {
|
417 |
return installerInfos.toArray(new PackageInfo[installerInfos.size()]); |
418 |
} |
419 |
} |