svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / creation / DefaultMakePluginPackageService.java @ 41706
History | View | Annotate | Download (14.4 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 |
* AUTHORS (In addition to CIT):
|
26 |
* 2010 {Prodevelop} {Task}
|
27 |
*/
|
28 |
|
29 |
package org.gvsig.installer.lib.impl.creation; |
30 |
|
31 |
import java.io.BufferedReader; |
32 |
import java.io.ByteArrayInputStream; |
33 |
import java.io.File; |
34 |
import java.io.FileInputStream; |
35 |
import java.io.FileOutputStream; |
36 |
import java.io.FileReader; |
37 |
import java.io.IOException; |
38 |
import java.io.InputStream; |
39 |
import java.io.InputStreamReader; |
40 |
import java.io.OutputStream; |
41 |
import java.net.URL; |
42 |
import java.net.URLConnection; |
43 |
import java.util.ArrayList; |
44 |
import java.util.List; |
45 |
|
46 |
import org.gvsig.installer.lib.api.InstallerManager; |
47 |
import org.gvsig.installer.lib.api.PackageInfo; |
48 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageService; |
49 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
50 |
import org.gvsig.installer.lib.impl.DefaultPackageInfo; |
51 |
import org.gvsig.installer.lib.impl.utils.DeleteFile; |
52 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
53 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
54 |
import org.gvsig.tools.service.Manager; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
/**
|
59 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
60 |
*/
|
61 |
public class DefaultMakePluginPackageService implements |
62 |
MakePluginPackageService { |
63 |
|
64 |
private static final Logger LOG = LoggerFactory |
65 |
.getLogger(DefaultMakePluginPackageService.class); |
66 |
|
67 |
public static final String ANT_FILE_NAME = "install.xml"; |
68 |
public static final String COPIED_FILES_DIRECTORY_NAME = "files"; |
69 |
private static final Logger logger = org.slf4j.LoggerFactory |
70 |
.getLogger(DefaultMakePluginPackageService.class); |
71 |
private final InstallerManager manager; |
72 |
|
73 |
private List<PackageInfo> installerInfos = null; |
74 |
|
75 |
protected String antScript = null; |
76 |
private InstallPackageProviderServices installerProviderServices = null; |
77 |
|
78 |
public DefaultMakePluginPackageService(InstallerManager manager)
|
79 |
throws MakePluginPackageServiceException {
|
80 |
super();
|
81 |
this.manager = manager;
|
82 |
this.installerInfos = new ArrayList<PackageInfo>(); |
83 |
this.installerProviderServices = InstallerProviderLocator
|
84 |
.getProviderManager().createInstallerProviderServices(); |
85 |
initialize(); |
86 |
} |
87 |
|
88 |
private void initialize() throws MakePluginPackageServiceException { |
89 |
|
90 |
List<File> addonFolders = manager.getAddonFolders(); |
91 |
|
92 |
if (addonFolders == null) { |
93 |
throw new MakePluginPackageServiceException( |
94 |
"The addonFolders list is null");
|
95 |
} |
96 |
// else if (addonFolders.size() <= 0) {
|
97 |
// LOG.info("The addonFolders list is empty");
|
98 |
// }
|
99 |
else {
|
100 |
// Read all the installed plugins
|
101 |
for (int i = 0; i < addonFolders.size(); i++) { |
102 |
File pluginDirectoryFile = addonFolders.get(i);
|
103 |
|
104 |
if (!pluginDirectoryFile.exists()) {
|
105 |
pluginDirectoryFile.mkdirs(); |
106 |
LOG.info("Plugins folder created: {}", pluginDirectoryFile);
|
107 |
} |
108 |
|
109 |
DefaultPackageInfo packageInfo = new DefaultPackageInfo();
|
110 |
|
111 |
installerProviderServices.readPackageInfo(new File( |
112 |
pluginDirectoryFile.getAbsolutePath()), packageInfo); |
113 |
|
114 |
// Checks if the ant file exists
|
115 |
File antFile = getAntFile(pluginDirectoryFile);
|
116 |
if (antFile.exists()) {
|
117 |
try {
|
118 |
packageInfo.setAntScript(readFileAsString(antFile |
119 |
.getAbsolutePath())); |
120 |
} catch (IOException e) { |
121 |
logger.error("Not possible to read the ant file");
|
122 |
} |
123 |
} |
124 |
|
125 |
// Checks if there are files to copy
|
126 |
File copiedFilesDirectory = getCopiedFilesDirectory(pluginDirectoryFile);
|
127 |
if (copiedFilesDirectory.exists()) {
|
128 |
loadCopiedFiles(copiedFilesDirectory, copiedFilesDirectory, |
129 |
packageInfo); |
130 |
} |
131 |
|
132 |
installerInfos.add(packageInfo); |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
private void loadCopiedFiles(File file, File filesDirectory, |
138 |
PackageInfo packageInfo) throws MakePluginPackageServiceException {
|
139 |
if (file.isDirectory()) {
|
140 |
File[] files = file.listFiles(); |
141 |
for (int i = 0; i < files.length; i++) { |
142 |
loadCopiedFiles(files[i], filesDirectory, packageInfo); |
143 |
} |
144 |
} else {
|
145 |
// Removing the plugin prefix
|
146 |
|
147 |
String pluginFileName = file.getAbsolutePath().substring(
|
148 |
filesDirectory.getAbsolutePath().length(), |
149 |
file.getAbsolutePath().length()); |
150 |
|
151 |
File pluginFile = new File(System.getProperty("user.dir") |
152 |
+ pluginFileName); |
153 |
|
154 |
packageInfo.getFilesToCopy().add(pluginFile); |
155 |
} |
156 |
} |
157 |
|
158 |
public void createPackageSet(PackageInfo packageInfo, |
159 |
OutputStream packageStream)
|
160 |
throws MakePluginPackageServiceException {
|
161 |
|
162 |
LOG.debug("Creating a package set of the package info: \n{0}",
|
163 |
packageInfo); |
164 |
|
165 |
String pluginFileName = manager.getPackageFileName(packageInfo);
|
166 |
installerProviderServices.compressPackageSet( |
167 |
getAbsolutePluginPackageDirectory(packageInfo), pluginFileName, |
168 |
packageStream); |
169 |
} |
170 |
|
171 |
public void preparePackage(PackageInfo packageInfo, File originalPluginDir) |
172 |
throws MakePluginPackageServiceException {
|
173 |
|
174 |
LOG.debug("Preparing a package set of the package info: \n{0}",
|
175 |
packageInfo); |
176 |
// Write the package.info file
|
177 |
writePackageInfo(packageInfo); |
178 |
|
179 |
if (packageInfo.getAntScript() != null) { |
180 |
// Create the ant file
|
181 |
writeAntFile(packageInfo); |
182 |
|
183 |
// Copy the selected files
|
184 |
writeSelectedFiles(packageInfo); |
185 |
} |
186 |
} |
187 |
|
188 |
public void createPackage(PackageInfo packageInfo, |
189 |
OutputStream packageStream)
|
190 |
throws MakePluginPackageServiceException {
|
191 |
|
192 |
LOG.debug("Creating package of the package info: \n{0}", packageInfo);
|
193 |
installerProviderServices.compressPackage( |
194 |
getAbsolutePluginPackageDirectory(packageInfo), packageStream); |
195 |
} |
196 |
|
197 |
public void createPackageIndex(PackageInfo packageInfo, |
198 |
OutputStream packageStream)
|
199 |
throws MakePluginPackageServiceException {
|
200 |
LOG.debug("Creating package index of the package info: \n{0}",
|
201 |
packageInfo); |
202 |
|
203 |
installerProviderServices.compressPackageIndex( |
204 |
getAbsolutePluginPackageDirectory(packageInfo), packageStream); |
205 |
} |
206 |
|
207 |
private void writePackageInfo(PackageInfo packageInfo) |
208 |
throws MakePluginPackageServiceException {
|
209 |
// conseguir directorio destino
|
210 |
writePackageInfo(packageInfo, |
211 |
getAbsolutePluginPackageDirectory(packageInfo)); |
212 |
} |
213 |
|
214 |
public void writePackageInfo(PackageInfo packageInfo, File folder) |
215 |
throws MakePluginPackageServiceException {
|
216 |
installerProviderServices.writePackageInfo(folder, packageInfo); |
217 |
} |
218 |
|
219 |
public void writePackageInfoForIndex(PackageInfo packageInfo, File folder) |
220 |
throws MakePluginPackageServiceException {
|
221 |
installerProviderServices.writePackageInfoForIndex(folder, packageInfo); |
222 |
} |
223 |
|
224 |
private void createInstallDirInPluginDir(PackageInfo packageInfo) |
225 |
throws MakePluginPackageServiceException {
|
226 |
File pluginDirectory;
|
227 |
pluginDirectory = getAbsolutePluginPackageDirectory(packageInfo); |
228 |
File installdir = new File(pluginDirectory, "install"); |
229 |
installdir.mkdir(); |
230 |
|
231 |
} |
232 |
|
233 |
private void writeAntFile(PackageInfo packageInfo) |
234 |
throws MakePluginPackageServiceException {
|
235 |
try {
|
236 |
createInstallDirInPluginDir(packageInfo); |
237 |
ByteArrayInputStream in = new ByteArrayInputStream(packageInfo |
238 |
.getAntScript().getBytes()); |
239 |
OutputStream out = new FileOutputStream( |
240 |
getAbsoluteAntFile(packageInfo)); |
241 |
|
242 |
// Copy the bits from instream to outstream
|
243 |
byte[] buf = new byte[1024]; |
244 |
int len;
|
245 |
while ((len = in.read(buf)) > 0) { |
246 |
out.write(buf, 0, len);
|
247 |
} |
248 |
in.close(); |
249 |
out.close(); |
250 |
} catch (IOException e) { |
251 |
throw new MakePluginPackageServiceException( |
252 |
"Exception writing the ant file");
|
253 |
} |
254 |
|
255 |
} |
256 |
|
257 |
private void writeSelectedFiles(PackageInfo packageInfo) |
258 |
throws MakePluginPackageServiceException {
|
259 |
try {
|
260 |
createInstallDirInPluginDir(packageInfo); |
261 |
String copiedFilesDirectoryName = getAbsoulteCopiedFilesDirectoryName(packageInfo);
|
262 |
|
263 |
// Deleting the previous files folder
|
264 |
File copiedFilesDirectoryfile = new File(copiedFilesDirectoryName); |
265 |
deleteDirectory(copiedFilesDirectoryfile); |
266 |
|
267 |
// It works if the plugins directory is in the folder
|
268 |
// "gvSIG/extensiones"
|
269 |
String applicationDirectory = System.getProperty("user.dir"); |
270 |
|
271 |
List<File> files = packageInfo.getFilesToCopy(); |
272 |
|
273 |
for (int i = 0; i < files.size(); i++) { |
274 |
String sourceFile = files.get(i).getAbsolutePath();
|
275 |
|
276 |
// Create the final path
|
277 |
String destFile = sourceFile.substring(applicationDirectory
|
278 |
.length(), sourceFile.length()); |
279 |
destFile = copiedFilesDirectoryName + destFile; |
280 |
|
281 |
// Copy the files
|
282 |
copy(new File(sourceFile), new File(destFile)); |
283 |
} |
284 |
} catch (IOException e) { |
285 |
throw new MakePluginPackageServiceException( |
286 |
"Exception copying the files", e);
|
287 |
} |
288 |
} |
289 |
|
290 |
static public boolean deleteDirectory(File path) { |
291 |
if (path.exists()) {
|
292 |
File[] files = path.listFiles(); |
293 |
for (int i = 0; i < files.length; i++) { |
294 |
if (files[i].isDirectory()) {
|
295 |
deleteDirectory(files[i]); |
296 |
} else {
|
297 |
files[i].delete(); |
298 |
} |
299 |
} |
300 |
} |
301 |
return (path.delete());
|
302 |
} |
303 |
|
304 |
private void copy(File sourceLocation, File targetLocation) |
305 |
throws IOException { |
306 |
if (sourceLocation.isDirectory()) {
|
307 |
if (!targetLocation.exists()) {
|
308 |
targetLocation.mkdir(); |
309 |
} |
310 |
|
311 |
String[] children = sourceLocation.list(); |
312 |
for (int i = 0; i < children.length; i++) { |
313 |
copy(new File(sourceLocation, children[i]), new File( |
314 |
targetLocation, children[i])); |
315 |
} |
316 |
} else {
|
317 |
targetLocation.getParentFile().mkdirs(); |
318 |
|
319 |
InputStream in = new FileInputStream(sourceLocation); |
320 |
OutputStream out = new FileOutputStream(targetLocation); |
321 |
|
322 |
// Copy the bits from instream to outstream
|
323 |
byte[] buf = new byte[1024]; |
324 |
int len;
|
325 |
while ((len = in.read(buf)) > 0) { |
326 |
out.write(buf, 0, len);
|
327 |
} |
328 |
in.close(); |
329 |
out.close(); |
330 |
} |
331 |
} |
332 |
|
333 |
private File getCopiedFilesDirectory(File pluginDirectory) |
334 |
throws MakePluginPackageServiceException {
|
335 |
return new File(pluginDirectory.getAbsolutePath() + File.separator |
336 |
+ "install" + File.separator + COPIED_FILES_DIRECTORY_NAME); |
337 |
} |
338 |
|
339 |
private String getAbsoulteCopiedFilesDirectoryName(PackageInfo packageInfo) |
340 |
throws MakePluginPackageServiceException {
|
341 |
return getAbsolutePluginPackageDirectory(packageInfo).getAbsolutePath()
|
342 |
+ File.separator + "install" + File.separator |
343 |
+ COPIED_FILES_DIRECTORY_NAME; |
344 |
} |
345 |
|
346 |
private File getAntFile(File pluginDirectory) |
347 |
throws MakePluginPackageServiceException {
|
348 |
return new File(pluginDirectory.getAbsolutePath() + File.separator |
349 |
+ "install" + File.separator + ANT_FILE_NAME); |
350 |
} |
351 |
|
352 |
private File getAbsoluteAntFile(PackageInfo packageInfo) |
353 |
throws MakePluginPackageServiceException {
|
354 |
String destinationFilePath = manager.getDefaultLocalAddonRepository()
|
355 |
.getAbsolutePath() |
356 |
+ File.separator + packageInfo.getCode();
|
357 |
return new File(destinationFilePath + File.separator + "install" + File.separator + ANT_FILE_NAME); |
358 |
} |
359 |
|
360 |
private File getAbsolutePluginPackageDirectory(PackageInfo packageInfo) |
361 |
throws MakePluginPackageServiceException {
|
362 |
File folder = manager.getAddonFolder(packageInfo.getCode());
|
363 |
// Si existe un addon para ese packageinfo lo usamos, y si no
|
364 |
// devolbemos la carpeta del addon en el repositorio local por defecto
|
365 |
// para ese tipo de addon en el que se pueda escribir para que se
|
366 |
// pueda crear alli el addon.
|
367 |
if( folder == null ) { |
368 |
folder = manager.getDefaultLocalAddonRepository(packageInfo.getType(), InstallerManager.ACCESS_WRITE); |
369 |
folder = new File(folder, packageInfo.getCode()); |
370 |
} |
371 |
return folder.getAbsoluteFile();
|
372 |
} |
373 |
|
374 |
public Manager getManager() {
|
375 |
return this.manager; |
376 |
} |
377 |
|
378 |
public PackageInfo getPluginPackageInfo(int index) { |
379 |
if (index >= installerInfos.size()) {
|
380 |
return null; |
381 |
} |
382 |
return installerInfos.get(index);
|
383 |
} |
384 |
|
385 |
public PackageInfo getPluginPackageInfo(String code) { |
386 |
for (int i = 0; i < getPluginPackageCount(); i++) { |
387 |
if (installerInfos.get(i).getCode() != null |
388 |
&& installerInfos.get(i).getCode().equals(code)) { |
389 |
return installerInfos.get(i);
|
390 |
} |
391 |
} |
392 |
PackageInfo info = new DefaultPackageInfo();
|
393 |
info.setCode(code); |
394 |
return info;
|
395 |
} |
396 |
|
397 |
public int getPluginPackageCount() { |
398 |
return installerInfos.size();
|
399 |
} |
400 |
|
401 |
public String getDefaultAntScript() |
402 |
throws MakePluginPackageServiceException {
|
403 |
try {
|
404 |
URL resource = getClass().getResource(ANT_FILE_NAME);
|
405 |
return readUrlAsString(resource);
|
406 |
} catch (IOException e) { |
407 |
throw new MakePluginPackageServiceException( |
408 |
"Impossible to read the default ant file", e);
|
409 |
} |
410 |
} |
411 |
|
412 |
private String readUrlAsString(URL url) throws java.io.IOException { |
413 |
URLConnection urlConnection = url.openConnection();
|
414 |
InputStream inputStream = urlConnection.getInputStream();
|
415 |
BufferedReader bufferedReader = new BufferedReader( |
416 |
new InputStreamReader(inputStream)); |
417 |
StringBuffer fileData = new StringBuffer(1000); |
418 |
String line = bufferedReader.readLine();
|
419 |
while (line != null) { |
420 |
fileData.append(line + "\n");
|
421 |
line = bufferedReader.readLine(); |
422 |
} |
423 |
bufferedReader.close(); |
424 |
return fileData.toString();
|
425 |
} |
426 |
|
427 |
private String readFileAsString(String filePath) throws java.io.IOException { |
428 |
StringBuffer fileData = new StringBuffer(1000); |
429 |
BufferedReader reader = new BufferedReader(new FileReader(filePath)); |
430 |
char[] buf = new char[1024]; |
431 |
int numRead = 0; |
432 |
while ((numRead = reader.read(buf)) != -1) { |
433 |
String readData = String.valueOf(buf, 0, numRead); |
434 |
fileData.append(readData); |
435 |
buf = new char[1024]; |
436 |
} |
437 |
reader.close(); |
438 |
return fileData.toString();
|
439 |
} |
440 |
|
441 |
public PackageInfo[] getInstalledPackages() |
442 |
throws MakePluginPackageServiceException {
|
443 |
return installerInfos.toArray(new PackageInfo[installerInfos.size()]); |
444 |
} |
445 |
|
446 |
public File getPluginFolder(PackageInfo packageInfo) |
447 |
throws MakePluginPackageServiceException {
|
448 |
return getAbsolutePluginPackageDirectory(packageInfo);
|
449 |
} |
450 |
|
451 |
public boolean deleteDir(File folder) { |
452 |
DeleteFile file = new DeleteFile();
|
453 |
return file.delete(folder);
|
454 |
} |
455 |
|
456 |
} |