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 / DefaultInstallerManager.java @ 41620
History | View | Annotate | Download (18.5 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 |
package org.gvsig.installer.lib.impl; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.FileInputStream; |
32 |
import java.io.FileNotFoundException; |
33 |
import java.io.IOException; |
34 |
import java.io.InputStream; |
35 |
import java.net.URL; |
36 |
import java.text.MessageFormat; |
37 |
import java.util.ArrayList; |
38 |
import java.util.Collection; |
39 |
import java.util.HashSet; |
40 |
import java.util.Iterator; |
41 |
import java.util.List; |
42 |
import java.util.Set; |
43 |
import java.util.logging.Level; |
44 |
import java.util.logging.Logger; |
45 |
import org.apache.commons.lang3.StringUtils; |
46 |
|
47 |
import org.gvsig.installer.lib.api.DependenciesCalculator; |
48 |
import org.gvsig.installer.lib.api.Dependency; |
49 |
import org.gvsig.installer.lib.api.InstallerManager; |
50 |
import org.gvsig.installer.lib.api.PackageInfo; |
51 |
import org.gvsig.installer.lib.api.PackageInfoReader; |
52 |
import org.gvsig.installer.lib.api.PackageInfoWriter; |
53 |
import org.gvsig.installer.lib.api.Version; |
54 |
import org.gvsig.installer.lib.api.creation.MakePackageService; |
55 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageService; |
56 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
57 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
58 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
59 |
import org.gvsig.installer.lib.impl.creation.DefaultMakePackageService; |
60 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
61 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter; |
62 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
63 |
import org.gvsig.tools.ToolsLocator; |
64 |
import org.gvsig.tools.dynobject.DynObject; |
65 |
import org.gvsig.tools.exception.BaseException; |
66 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
67 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
68 |
import org.gvsig.tools.service.AbstractManager; |
69 |
import org.gvsig.tools.service.Service; |
70 |
import org.gvsig.tools.service.ServiceException; |
71 |
|
72 |
/**
|
73 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
74 |
*/
|
75 |
public class DefaultInstallerManager extends AbstractManager implements |
76 |
InstallerManager { |
77 |
|
78 |
private static class LocalRepositoryLocation { |
79 |
|
80 |
private File location; |
81 |
private Set<String> types; |
82 |
|
83 |
public LocalRepositoryLocation(File localtion, String type) { |
84 |
this.location = localtion;
|
85 |
this.types = new HashSet<String>(); |
86 |
this.addType(type);
|
87 |
} |
88 |
|
89 |
public LocalRepositoryLocation(File localtion) { |
90 |
this(localtion, null); |
91 |
} |
92 |
|
93 |
public void addType(String type) { |
94 |
if ( !StringUtils.isBlank(type) ) {
|
95 |
this.types.add(type);
|
96 |
} |
97 |
} |
98 |
|
99 |
public void addType(LocalRepositoryLocation location) { |
100 |
this.types.addAll(location.getTypes());
|
101 |
} |
102 |
|
103 |
public Collection<String> getTypes() { |
104 |
return this.types; |
105 |
} |
106 |
|
107 |
public File getLocation() { |
108 |
return this.location; |
109 |
} |
110 |
|
111 |
public String getDefaultType() { |
112 |
if ( this.types.isEmpty() ) { |
113 |
return null; |
114 |
} |
115 |
return this.types.iterator().next(); |
116 |
} |
117 |
|
118 |
public boolean is(File location) { |
119 |
if ( location.equals(this.location) ) { |
120 |
return true; |
121 |
} |
122 |
return false; |
123 |
} |
124 |
|
125 |
public boolean contains(File file) { |
126 |
if ( file.getAbsolutePath().startsWith(this.location.getAbsolutePath()) ) { |
127 |
return true; |
128 |
} |
129 |
return false; |
130 |
} |
131 |
} |
132 |
|
133 |
private static class LocalRepositoriesLocations extends ArrayList<LocalRepositoryLocation> { |
134 |
|
135 |
public LocalRepositoryLocation getLocation(File location) { |
136 |
Iterator<LocalRepositoryLocation> it = super.iterator(); |
137 |
while ( it.hasNext() ) {
|
138 |
LocalRepositoryLocation x = it.next(); |
139 |
if ( x.is(location) ) {
|
140 |
return x;
|
141 |
} |
142 |
} |
143 |
return null; |
144 |
} |
145 |
|
146 |
public boolean add(LocalRepositoryLocation location) { |
147 |
LocalRepositoryLocation old = this.getLocation(location.getLocation());
|
148 |
if ( old != null ) { |
149 |
old.addType(location); |
150 |
return true; |
151 |
} |
152 |
return super.add(location); |
153 |
} |
154 |
|
155 |
} |
156 |
|
157 |
private static final String INSTALLER_MANAGER_EXTENSION_POINT = "InstallerManagerExtensionPoint"; |
158 |
private static final String INSTALLER_CREATION_SERVICE_NAME = "InstallerCreationService"; |
159 |
private static final String INSTALLER_EXECUTION_SERVICE_NAME = "InstallerExecutionService"; |
160 |
private ExtensionPointManager extensionPoints = ToolsLocator
|
161 |
.getExtensionPointManager(); |
162 |
|
163 |
private String packageSetNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspks"; |
164 |
private String packageNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspkg"; |
165 |
private String packageIndexNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspki"; |
166 |
|
167 |
private URL BaseDownloadURL = null; |
168 |
private Version version = null; |
169 |
private List<LocalRepositoryLocation> localRepositoriesLocation = null; |
170 |
private File defaultRepositoryLocation = null; |
171 |
|
172 |
public DefaultInstallerManager() {
|
173 |
super(new DefaultInstallerProviderManager()); |
174 |
localRepositoriesLocation = new LocalRepositoriesLocations();
|
175 |
} |
176 |
|
177 |
public String getPackageSetNameFormat() { |
178 |
return packageSetNameFormat;
|
179 |
} |
180 |
|
181 |
public void setPackageSetNameFormat(String packageSetNameFormat) { |
182 |
this.packageSetNameFormat = packageSetNameFormat;
|
183 |
} |
184 |
|
185 |
public String getPackageNameFormat() { |
186 |
return packageNameFormat;
|
187 |
} |
188 |
|
189 |
public void setPackageNameFormat(String packageNameFormat) { |
190 |
this.packageNameFormat = packageNameFormat;
|
191 |
} |
192 |
|
193 |
public String getPackageIndexNameFormat() { |
194 |
return packageIndexNameFormat;
|
195 |
} |
196 |
|
197 |
public void setPackageIndexNameFormat(String packageIndexNameFormat) { |
198 |
this.packageIndexNameFormat = packageIndexNameFormat;
|
199 |
} |
200 |
|
201 |
public MakePluginPackageService getMakePluginPackageService()
|
202 |
throws MakePluginPackageServiceException {
|
203 |
ExtensionPoint ep = extensionPoints |
204 |
.add(INSTALLER_MANAGER_EXTENSION_POINT); |
205 |
try {
|
206 |
Object[] args = new Object[]{this}; |
207 |
return (MakePluginPackageService) ep.create(
|
208 |
INSTALLER_CREATION_SERVICE_NAME, args); |
209 |
} catch (Exception e) { |
210 |
throw new MakePluginPackageServiceException( |
211 |
"Exception creating the installer service to create installers",
|
212 |
e); |
213 |
} |
214 |
} |
215 |
|
216 |
public class InstallerCreationException extends |
217 |
InstallPackageServiceException { |
218 |
|
219 |
private static final long serialVersionUID = 759329820705535873L; |
220 |
|
221 |
private static final String message = "Error creating the installer service to install plugins"; |
222 |
|
223 |
private static final String KEY = "_Error_creating_the_installer_service_to_install_plugins"; |
224 |
|
225 |
public InstallerCreationException(Exception e) { |
226 |
super(message, e, KEY, serialVersionUID);
|
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
public InstallPackageService getInstallPackageService()
|
232 |
throws InstallPackageServiceException {
|
233 |
ExtensionPoint ep = extensionPoints |
234 |
.add(INSTALLER_MANAGER_EXTENSION_POINT); |
235 |
try {
|
236 |
Object[] args = new Object[1]; |
237 |
args[0] = this; |
238 |
return (InstallPackageService) ep.create(
|
239 |
INSTALLER_EXECUTION_SERVICE_NAME, args); |
240 |
} catch (Exception e) { |
241 |
throw new InstallerCreationException(e); |
242 |
} |
243 |
} |
244 |
|
245 |
public void registerMakePluginPackageService( |
246 |
Class<? extends MakePluginPackageService> clazz) { |
247 |
ExtensionPoint extensionPoint = extensionPoints.add( |
248 |
INSTALLER_MANAGER_EXTENSION_POINT, "");
|
249 |
extensionPoint.append(INSTALLER_CREATION_SERVICE_NAME, "", clazz);
|
250 |
} |
251 |
|
252 |
public void registerInstallPackageService( |
253 |
Class<? extends InstallPackageService> clazz) { |
254 |
ExtensionPoint extensionPoint = extensionPoints.add( |
255 |
INSTALLER_MANAGER_EXTENSION_POINT, "");
|
256 |
extensionPoint.append(INSTALLER_EXECUTION_SERVICE_NAME, "", clazz);
|
257 |
} |
258 |
|
259 |
public Service getService(DynObject parameters) throws ServiceException { |
260 |
return null; |
261 |
} |
262 |
|
263 |
public String getPackageSetFileName(PackageInfo info) { |
264 |
Object[] parameters = getPackageNameFormatParameters(info); |
265 |
return MessageFormat.format(getPackageSetNameFormat(), parameters); |
266 |
} |
267 |
|
268 |
public String getPackageFileName(PackageInfo info) { |
269 |
Object[] parameters = getPackageNameFormatParameters(info); |
270 |
return MessageFormat.format(getPackageNameFormat(), parameters); |
271 |
} |
272 |
|
273 |
public String getPackageIndexFileName(PackageInfo info) { |
274 |
Object[] parameters = getPackageNameFormatParameters(info); |
275 |
return MessageFormat.format(getPackageIndexNameFormat(), parameters); |
276 |
} |
277 |
|
278 |
private Object[] getPackageNameFormatParameters(PackageInfo info) { |
279 |
Object[] parameters = new Object[8]; |
280 |
parameters[PACKAGE_FILE_NAME_FIELDS.GVSIG_VERSION] = info |
281 |
.getGvSIGVersion(); |
282 |
parameters[PACKAGE_FILE_NAME_FIELDS.NAME] = info.getCode(); |
283 |
parameters[PACKAGE_FILE_NAME_FIELDS.VERSION] = info.getVersion(); |
284 |
parameters[PACKAGE_FILE_NAME_FIELDS.BUILD] = info.getBuild(); |
285 |
parameters[PACKAGE_FILE_NAME_FIELDS.STATE] = info.getState(); |
286 |
parameters[PACKAGE_FILE_NAME_FIELDS.OS] = info.getOperatingSystem(); |
287 |
parameters[PACKAGE_FILE_NAME_FIELDS.ARCH] = info.getArchitecture(); |
288 |
parameters[PACKAGE_FILE_NAME_FIELDS.JVM] = info.getJavaVM(); |
289 |
return parameters;
|
290 |
} |
291 |
|
292 |
public PackageInfo[] getInstalledPackages(File pluginsDirectory) |
293 |
throws MakePluginPackageServiceException {
|
294 |
MakePluginPackageService service = getMakePluginPackageService(); |
295 |
return service.getInstalledPackages();
|
296 |
} |
297 |
|
298 |
public PackageInfo[] getInstalledPackages() |
299 |
throws MakePluginPackageServiceException {
|
300 |
MakePluginPackageService service = getMakePluginPackageService(); |
301 |
return service.getInstalledPackages();
|
302 |
} |
303 |
|
304 |
public String getDefaultPackageFileExtension() { |
305 |
return "gvspkg"; |
306 |
} |
307 |
|
308 |
public String getDefaultPackageSetFileExtension() { |
309 |
return "gvspks"; |
310 |
} |
311 |
|
312 |
public String getDefaultIndexSetFileExtension() { |
313 |
return "gvspki"; |
314 |
} |
315 |
|
316 |
public String getOperatingSystem() { |
317 |
String osname = System.getProperty("os.name"); |
318 |
if ( osname.toLowerCase().startsWith("linux") ) { |
319 |
return InstallerManager.OS.LINUX;
|
320 |
} |
321 |
if ( osname.toLowerCase().startsWith("window") ) { |
322 |
return InstallerManager.OS.WINDOWS;
|
323 |
} |
324 |
return osname;
|
325 |
} |
326 |
|
327 |
public String getArchitecture() { |
328 |
String osarch = System.getProperty("os.arch"); |
329 |
if ( osarch.toLowerCase().startsWith("i386") ) { |
330 |
return InstallerManager.ARCH.X86;
|
331 |
} |
332 |
if ( osarch.toLowerCase().startsWith("x86") ) { |
333 |
return InstallerManager.ARCH.X86;
|
334 |
} |
335 |
if ( osarch.toLowerCase().startsWith("amd64") ) { |
336 |
return InstallerManager.ARCH.X86_64;
|
337 |
} |
338 |
return osarch;
|
339 |
} |
340 |
|
341 |
public Dependency createDependency(PackageInfo packageInfo) {
|
342 |
return new DefaultDependency(packageInfo); |
343 |
} |
344 |
|
345 |
public Dependency createDependency() {
|
346 |
return new DefaultDependency(); |
347 |
} |
348 |
|
349 |
public DependenciesCalculator createDependenciesCalculator(
|
350 |
InstallPackageService installService) { |
351 |
return new DefaultDependenciesCalculator(installService); |
352 |
} |
353 |
|
354 |
public Version createVersion() {
|
355 |
if ( version == null ) { |
356 |
return new DefaultVersion(); |
357 |
} |
358 |
Version v = null;
|
359 |
try {
|
360 |
v = (Version) version.clone(); |
361 |
} catch (CloneNotSupportedException e) { |
362 |
// Version clone can't trow exception
|
363 |
} |
364 |
return v;
|
365 |
} |
366 |
|
367 |
public PackageInfoReader getDefaultPackageInfoReader() {
|
368 |
return new InstallerInfoFileReader(); |
369 |
} |
370 |
|
371 |
public PackageInfoWriter getDefaultPackageInfoWriter() {
|
372 |
return new InstallerInfoFileWriter(); |
373 |
} |
374 |
|
375 |
public MakePackageService createMakePackage(File packageFolder, |
376 |
PackageInfo packageInfo) { |
377 |
return new DefaultMakePackageService(this, packageFolder, packageInfo); |
378 |
} |
379 |
|
380 |
public PackageInfo createPackageInfo() {
|
381 |
return new DefaultPackageInfo(); |
382 |
} |
383 |
|
384 |
public PackageInfo createPackageInfo(InputStream stream) throws BaseException { |
385 |
PackageInfo pkg = new DefaultPackageInfo();
|
386 |
PackageInfoReader reader = this.getDefaultPackageInfoReader();
|
387 |
reader.read(pkg, stream); |
388 |
return pkg;
|
389 |
} |
390 |
|
391 |
public PackageInfo createPackageInfo(File file) throws BaseException { |
392 |
FileInputStream fis = null; |
393 |
PackageInfo pkg = null;
|
394 |
try {
|
395 |
fis = new FileInputStream(file); |
396 |
pkg = this.createPackageInfo(fis);
|
397 |
fis.close(); |
398 |
} catch (Exception ex) { |
399 |
|
400 |
} finally {
|
401 |
try {
|
402 |
fis.close(); |
403 |
} catch (IOException ex) { |
404 |
//
|
405 |
} |
406 |
} |
407 |
return pkg;
|
408 |
} |
409 |
|
410 |
public URL getDownloadBaseURL() { |
411 |
return this.BaseDownloadURL; |
412 |
} |
413 |
|
414 |
public String getVersion() { |
415 |
return this.version.toString(); |
416 |
} |
417 |
|
418 |
public void setVersion(Version version) { |
419 |
try {
|
420 |
this.version = (Version) version.clone();
|
421 |
} catch (CloneNotSupportedException e) { |
422 |
// This should not happen
|
423 |
} |
424 |
|
425 |
} |
426 |
|
427 |
public Version getVersionEx() {
|
428 |
try {
|
429 |
return (Version) this.version.clone(); |
430 |
} catch (CloneNotSupportedException e) { |
431 |
// This should not happen
|
432 |
return null; |
433 |
} |
434 |
} |
435 |
|
436 |
public void setDownloadBaseURL(URL url) { |
437 |
this.BaseDownloadURL = url;
|
438 |
} |
439 |
|
440 |
public void setVersion(String version) { |
441 |
if ( this.version == null ) { |
442 |
this.version = new DefaultVersion(); |
443 |
} |
444 |
this.version.parse(version);
|
445 |
} |
446 |
|
447 |
public File getDefaultLocalAddonRepository() { |
448 |
return this.defaultRepositoryLocation; |
449 |
} |
450 |
|
451 |
public void setDefaultLocalAddonRepository(File defaultAddonsRepository) { |
452 |
this.defaultRepositoryLocation = defaultAddonsRepository;
|
453 |
this.localRepositoriesLocation.add(new LocalRepositoryLocation(defaultAddonsRepository)); |
454 |
} |
455 |
|
456 |
public void addLocalAddonRepository(File path) { |
457 |
this.addLocalAddonRepository(path, null); |
458 |
} |
459 |
|
460 |
public void addLocalAddonRepository(File path, String type) { |
461 |
localRepositoriesLocation.add(new LocalRepositoryLocation(path, type));
|
462 |
} |
463 |
|
464 |
public String getDefaultLocalRepositoryType(File file) { |
465 |
Iterator<LocalRepositoryLocation> it = localRepositoriesLocation.iterator();
|
466 |
while ( it.hasNext() ) {
|
467 |
LocalRepositoryLocation location = it.next(); |
468 |
if ( location.contains(file) ) {
|
469 |
return location.getDefaultType();
|
470 |
} |
471 |
} |
472 |
return null; |
473 |
} |
474 |
|
475 |
public List<File> getLocalAddonRepositories() { |
476 |
List<File> l = new ArrayList<File>(); |
477 |
Iterator<LocalRepositoryLocation> it = localRepositoriesLocation.iterator();
|
478 |
while ( it.hasNext() ) {
|
479 |
l.add(it.next().getLocation()); |
480 |
} |
481 |
return l;
|
482 |
} |
483 |
|
484 |
public List<File> getAddonFolders() { |
485 |
List<File> addonFolders = new ArrayList<File>(); |
486 |
|
487 |
// Para cada directorio en repositoriesPathsList
|
488 |
List<File> localAddonRepositories = this.getLocalAddonRepositories(); |
489 |
for ( int i = 0; i < localAddonRepositories.size(); i++ ) { |
490 |
File repoPath = localAddonRepositories.get(i);
|
491 |
if ( repoPath.isDirectory() && repoPath.exists() ) {
|
492 |
File[] folderRepoList = repoPath.listFiles(); |
493 |
|
494 |
// recorrer los directorios que haya dentro
|
495 |
for ( int j = 0; j < folderRepoList.length; j++ ) { |
496 |
File addonFolder = folderRepoList[j];
|
497 |
if ( addonFolder.isDirectory() ) {
|
498 |
File pkginfofile = new File(addonFolder, "package.info"); |
499 |
if ( pkginfofile.exists() ) {
|
500 |
addonFolders.add(addonFolder); |
501 |
} |
502 |
} |
503 |
|
504 |
} |
505 |
} |
506 |
} |
507 |
|
508 |
return addonFolders;
|
509 |
} |
510 |
|
511 |
public File getAddonFolder(String code) { |
512 |
List<File> packagePaths = this.getAddonFolders(); |
513 |
for ( int i = 0; i < packagePaths.size(); i++ ) { |
514 |
try {
|
515 |
File pkgfile = new File(packagePaths.get(i), "package.info"); |
516 |
PackageInfo pkg = this.createPackageInfo(pkgfile);
|
517 |
if ( pkg.getCode().equalsIgnoreCase(code) ) {
|
518 |
return packagePaths.get(i);
|
519 |
} |
520 |
} catch (Exception ex) { |
521 |
|
522 |
} |
523 |
} |
524 |
return null; |
525 |
} |
526 |
|
527 |
public List<byte[]> getPublicKeys() { |
528 |
byte[] rawkey; |
529 |
try {
|
530 |
InputStream is = this.getClass().getResourceAsStream("/org/gvsig/installer/lib/keys/key.public"); |
531 |
rawkey = new byte[is.available()]; |
532 |
is.read(rawkey); |
533 |
is.close(); |
534 |
} catch (IOException e) { |
535 |
return null; |
536 |
} |
537 |
List<byte[]> keys = new ArrayList<byte[]>(); |
538 |
keys.add(rawkey); |
539 |
return keys;
|
540 |
} |
541 |
|
542 |
public boolean hasProviderToThisPackage(PackageInfo packageInfo) { |
543 |
InstallerProviderManager provmgr = (InstallerProviderManager) this.getProviderManager();
|
544 |
try {
|
545 |
return provmgr.getProviderFactory(packageInfo.getType()) != null; |
546 |
} catch (Exception e) { |
547 |
return false; |
548 |
} |
549 |
} |
550 |
|
551 |
} |