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 @ 42881
History | View | Annotate | Download (24.8 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.installer.lib.impl; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
31 |
import java.net.URL; |
32 |
import java.text.MessageFormat; |
33 |
import java.util.ArrayList; |
34 |
import java.util.Collection; |
35 |
import java.util.HashMap; |
36 |
import java.util.HashSet; |
37 |
import java.util.Iterator; |
38 |
import java.util.List; |
39 |
import java.util.Map; |
40 |
import java.util.Properties; |
41 |
import java.util.Set; |
42 |
import org.apache.commons.io.IOUtils; |
43 |
import org.apache.commons.lang3.StringUtils; |
44 |
|
45 |
import org.gvsig.installer.lib.api.DependenciesCalculator; |
46 |
import org.gvsig.installer.lib.api.Dependency; |
47 |
import org.gvsig.installer.lib.api.InstallerManager; |
48 |
import org.gvsig.installer.lib.api.PackageInfo; |
49 |
import org.gvsig.installer.lib.api.PackageInfoReader; |
50 |
import org.gvsig.installer.lib.api.PackageInfoWriter; |
51 |
import org.gvsig.installer.lib.api.Version; |
52 |
import org.gvsig.installer.lib.api.creation.MakePackageService; |
53 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageService; |
54 |
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
55 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
56 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
57 |
import org.gvsig.installer.lib.impl.creation.DefaultMakePackageService; |
58 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
59 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter; |
60 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
61 |
import org.gvsig.tools.ToolsLocator; |
62 |
import org.gvsig.tools.dynobject.DynObject; |
63 |
import org.gvsig.tools.exception.BaseException; |
64 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
65 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
66 |
import org.gvsig.tools.packageutils.PackageManager; |
67 |
import org.gvsig.tools.service.AbstractManager; |
68 |
import org.gvsig.tools.service.Service; |
69 |
import org.gvsig.tools.service.ServiceException; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
/**
|
74 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
75 |
*/
|
76 |
public class DefaultInstallerManager extends AbstractManager implements |
77 |
InstallerManager { |
78 |
|
79 |
private static Logger logger = LoggerFactory.getLogger(DefaultInstallerManager.class); |
80 |
|
81 |
private static class LocalRepositoryLocation { |
82 |
|
83 |
private File location; |
84 |
private Set<String> types; |
85 |
|
86 |
public LocalRepositoryLocation(File localtion, String type) { |
87 |
this.location = localtion;
|
88 |
this.types = new HashSet<String>(); |
89 |
this.addType(type);
|
90 |
} |
91 |
|
92 |
public LocalRepositoryLocation(File localtion) { |
93 |
this(localtion, null); |
94 |
} |
95 |
|
96 |
public void addType(String type) { |
97 |
if ( !StringUtils.isBlank(type) ) {
|
98 |
this.types.add(type);
|
99 |
} |
100 |
} |
101 |
|
102 |
public void addType(LocalRepositoryLocation location) { |
103 |
this.types.addAll(location.getTypes());
|
104 |
} |
105 |
|
106 |
public Collection<String> getTypes() { |
107 |
return this.types; |
108 |
} |
109 |
|
110 |
public File getLocation() { |
111 |
return this.location; |
112 |
} |
113 |
|
114 |
public String getDefaultType() { |
115 |
if ( this.types.isEmpty() ) { |
116 |
return null; |
117 |
} |
118 |
return this.types.iterator().next(); |
119 |
} |
120 |
|
121 |
public boolean is(File location) { |
122 |
if ( location.equals(this.location) ) { |
123 |
return true; |
124 |
} |
125 |
return false; |
126 |
} |
127 |
|
128 |
public boolean contains(File file) { |
129 |
if ( file.getAbsolutePath().startsWith(this.location.getAbsolutePath()) ) { |
130 |
return true; |
131 |
} |
132 |
return false; |
133 |
} |
134 |
|
135 |
public boolean contains(PackageInfo packageInfo) { |
136 |
if( !this.support(packageInfo.getType()) ) { |
137 |
return false; |
138 |
} |
139 |
String packageInfoName = packageInfo.getCode() + File.separator + PACKAGE_INFO_FILE_NAME; |
140 |
File packageInfoFile = new File(this.location,packageInfoName); |
141 |
return packageInfoFile.exists();
|
142 |
} |
143 |
|
144 |
public boolean support(String type) { |
145 |
for( String atype : this.types ) { |
146 |
if( atype != null ) { |
147 |
if( atype.equalsIgnoreCase(type) ) {
|
148 |
return true; |
149 |
} |
150 |
} |
151 |
} |
152 |
return false; |
153 |
} |
154 |
} |
155 |
|
156 |
private static class LocalRepositoriesLocations extends ArrayList<LocalRepositoryLocation> { |
157 |
|
158 |
public LocalRepositoryLocation getLocation(File location) { |
159 |
Iterator<LocalRepositoryLocation> it = super.iterator(); |
160 |
while ( it.hasNext() ) {
|
161 |
LocalRepositoryLocation x = it.next(); |
162 |
if ( x.is(location) ) {
|
163 |
return x;
|
164 |
} |
165 |
} |
166 |
return null; |
167 |
} |
168 |
|
169 |
public boolean add(LocalRepositoryLocation location) { |
170 |
LocalRepositoryLocation old = this.getLocation(location.getLocation());
|
171 |
if ( old != null ) { |
172 |
old.addType(location); |
173 |
return true; |
174 |
} |
175 |
return super.add(location); |
176 |
} |
177 |
|
178 |
} |
179 |
|
180 |
private static final String INSTALLER_MANAGER_EXTENSION_POINT = "InstallerManagerExtensionPoint"; |
181 |
private static final String INSTALLER_CREATION_SERVICE_NAME = "InstallerCreationService"; |
182 |
private static final String INSTALLER_EXECUTION_SERVICE_NAME = "InstallerExecutionService"; |
183 |
private ExtensionPointManager extensionPoints = ToolsLocator
|
184 |
.getExtensionPointManager(); |
185 |
|
186 |
private String packageSetNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspks"; |
187 |
private String packageNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspkg"; |
188 |
private String packageIndexNameFormat = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspki"; |
189 |
|
190 |
private URL BaseDownloadURL = null; |
191 |
private Version version = null; |
192 |
private List<LocalRepositoryLocation> localRepositoriesLocation = null; |
193 |
private Map<String,File> defaultRepositoryLocation = null; |
194 |
|
195 |
private String operatingSystemName; |
196 |
private String operatingSystemFamily; |
197 |
private String operatingSystemVersion; |
198 |
|
199 |
public DefaultInstallerManager() {
|
200 |
super(new DefaultInstallerProviderManager()); |
201 |
this.defaultRepositoryLocation = new HashMap<String,File>(); |
202 |
localRepositoriesLocation = new LocalRepositoriesLocations();
|
203 |
} |
204 |
|
205 |
public String getPackageSetNameFormat() { |
206 |
return packageSetNameFormat;
|
207 |
} |
208 |
|
209 |
public void setPackageSetNameFormat(String packageSetNameFormat) { |
210 |
this.packageSetNameFormat = packageSetNameFormat;
|
211 |
} |
212 |
|
213 |
public String getPackageNameFormat() { |
214 |
return packageNameFormat;
|
215 |
} |
216 |
|
217 |
public void setPackageNameFormat(String packageNameFormat) { |
218 |
this.packageNameFormat = packageNameFormat;
|
219 |
} |
220 |
|
221 |
public String getPackageIndexNameFormat() { |
222 |
return packageIndexNameFormat;
|
223 |
} |
224 |
|
225 |
public void setPackageIndexNameFormat(String packageIndexNameFormat) { |
226 |
this.packageIndexNameFormat = packageIndexNameFormat;
|
227 |
} |
228 |
|
229 |
public MakePluginPackageService getMakePluginPackageService()
|
230 |
throws MakePluginPackageServiceException {
|
231 |
ExtensionPoint ep = extensionPoints |
232 |
.add(INSTALLER_MANAGER_EXTENSION_POINT); |
233 |
try {
|
234 |
Object[] args = new Object[]{this}; |
235 |
return (MakePluginPackageService) ep.create(
|
236 |
INSTALLER_CREATION_SERVICE_NAME, args); |
237 |
} catch (Exception e) { |
238 |
throw new MakePluginPackageServiceException( |
239 |
"Exception creating the installer service to create installers",
|
240 |
e); |
241 |
} |
242 |
} |
243 |
|
244 |
public class InstallerCreationException extends |
245 |
InstallPackageServiceException { |
246 |
|
247 |
private static final long serialVersionUID = 759329820705535873L; |
248 |
|
249 |
private static final String message = "Error creating the installer service to install plugins"; |
250 |
|
251 |
private static final String KEY = "_Error_creating_the_installer_service_to_install_plugins"; |
252 |
|
253 |
public InstallerCreationException(Exception e) { |
254 |
super(message, e, KEY, serialVersionUID);
|
255 |
} |
256 |
|
257 |
} |
258 |
|
259 |
public InstallPackageService getInstallPackageService()
|
260 |
throws InstallPackageServiceException {
|
261 |
ExtensionPoint ep = extensionPoints |
262 |
.add(INSTALLER_MANAGER_EXTENSION_POINT); |
263 |
try {
|
264 |
Object[] args = new Object[1]; |
265 |
args[0] = this; |
266 |
return (InstallPackageService) ep.create(
|
267 |
INSTALLER_EXECUTION_SERVICE_NAME, args); |
268 |
} catch (Exception e) { |
269 |
throw new InstallerCreationException(e); |
270 |
} |
271 |
} |
272 |
|
273 |
public void registerMakePluginPackageService( |
274 |
Class<? extends MakePluginPackageService> clazz) { |
275 |
ExtensionPoint extensionPoint = extensionPoints.add( |
276 |
INSTALLER_MANAGER_EXTENSION_POINT, "");
|
277 |
extensionPoint.append(INSTALLER_CREATION_SERVICE_NAME, "", clazz);
|
278 |
} |
279 |
|
280 |
public void registerInstallPackageService( |
281 |
Class<? extends InstallPackageService> clazz) { |
282 |
ExtensionPoint extensionPoint = extensionPoints.add( |
283 |
INSTALLER_MANAGER_EXTENSION_POINT, "");
|
284 |
extensionPoint.append(INSTALLER_EXECUTION_SERVICE_NAME, "", clazz);
|
285 |
} |
286 |
|
287 |
public Service getService(DynObject parameters) throws ServiceException { |
288 |
return null; |
289 |
} |
290 |
|
291 |
public String getPackageSetFileName(PackageInfo info) { |
292 |
Object[] parameters = getPackageNameFormatParameters(info); |
293 |
return MessageFormat.format(getPackageSetNameFormat(), parameters); |
294 |
} |
295 |
|
296 |
public String getPackageFileName(PackageInfo info) { |
297 |
Object[] parameters = getPackageNameFormatParameters(info); |
298 |
return MessageFormat.format(getPackageNameFormat(), parameters); |
299 |
} |
300 |
|
301 |
public String getPackageIndexFileName(PackageInfo info) { |
302 |
Object[] parameters = getPackageNameFormatParameters(info); |
303 |
return MessageFormat.format(getPackageIndexNameFormat(), parameters); |
304 |
} |
305 |
|
306 |
private Object[] getPackageNameFormatParameters(PackageInfo info) { |
307 |
Object[] parameters = new Object[8]; |
308 |
parameters[PACKAGE_FILE_NAME_FIELDS.GVSIG_VERSION] = info |
309 |
.getGvSIGVersion(); |
310 |
parameters[PACKAGE_FILE_NAME_FIELDS.NAME] = info.getCode(); |
311 |
parameters[PACKAGE_FILE_NAME_FIELDS.VERSION] = info.getVersion(); |
312 |
parameters[PACKAGE_FILE_NAME_FIELDS.BUILD] = info.getBuild(); |
313 |
parameters[PACKAGE_FILE_NAME_FIELDS.STATE] = info.getState(); |
314 |
parameters[PACKAGE_FILE_NAME_FIELDS.OS] = info.getOperatingSystem(); |
315 |
parameters[PACKAGE_FILE_NAME_FIELDS.ARCH] = info.getArchitecture(); |
316 |
parameters[PACKAGE_FILE_NAME_FIELDS.JVM] = info.getJavaVM(); |
317 |
return parameters;
|
318 |
} |
319 |
|
320 |
public PackageInfo[] getInstalledPackages(File pluginsDirectory) |
321 |
throws MakePluginPackageServiceException {
|
322 |
MakePluginPackageService service = getMakePluginPackageService(); |
323 |
return service.getInstalledPackages();
|
324 |
} |
325 |
|
326 |
public PackageInfo[] getInstalledPackages() |
327 |
throws MakePluginPackageServiceException {
|
328 |
MakePluginPackageService service = getMakePluginPackageService(); |
329 |
return service.getInstalledPackages();
|
330 |
} |
331 |
|
332 |
public String getDefaultPackageFileExtension() { |
333 |
return "gvspkg"; |
334 |
} |
335 |
|
336 |
public String getDefaultPackageSetFileExtension() { |
337 |
return "gvspks"; |
338 |
} |
339 |
|
340 |
public String getDefaultIndexSetFileExtension() { |
341 |
return "gvspki"; |
342 |
} |
343 |
|
344 |
private void loadOperatingSystemInfo() { |
345 |
String osname = System.getProperty("os.name"); |
346 |
String osversion = System.getProperty("os.version"); |
347 |
if (osname.toLowerCase().startsWith("linux")) { |
348 |
this.operatingSystemFamily = PackageManager.OS.LINUX;
|
349 |
FileInputStream fis = null; |
350 |
try {
|
351 |
Properties p = new Properties(); |
352 |
fis = new FileInputStream("/etc/os-release"); |
353 |
p.load(fis); |
354 |
this.operatingSystemName = p.getProperty("ID", null); |
355 |
this.operatingSystemVersion = p.getProperty("VERSION_ID", null); |
356 |
} catch(Exception ex) { |
357 |
|
358 |
} finally {
|
359 |
IOUtils.closeQuietly(fis); |
360 |
} |
361 |
|
362 |
} else if (osname.toLowerCase().startsWith("window")) { |
363 |
this.operatingSystemFamily = PackageManager.OS.WINDOWS;
|
364 |
String s = osname.replace(" ", ""); |
365 |
s = s.replace("_", ""); |
366 |
s = s.replace("-", ""); |
367 |
this.operatingSystemName = s;
|
368 |
this.operatingSystemVersion = osversion;
|
369 |
|
370 |
} else {
|
371 |
String s = osname.replace(" ", ""); |
372 |
s = s.replace("_", ""); |
373 |
s = s.replace("-", ""); |
374 |
this.operatingSystemFamily = s;
|
375 |
this.operatingSystemName = null; |
376 |
this.operatingSystemVersion = osversion;
|
377 |
} |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public String getOperatingSystem() { |
382 |
if( this.operatingSystemFamily == null ) { |
383 |
loadOperatingSystemInfo(); |
384 |
} |
385 |
StringBuilder builder = new StringBuilder(); |
386 |
builder.append(this.operatingSystemFamily);
|
387 |
if( this.operatingSystemName!=null ) { |
388 |
builder.append("_");
|
389 |
builder.append(this.operatingSystemName);
|
390 |
if( this.operatingSystemVersion!=null ) { |
391 |
builder.append("_");
|
392 |
builder.append(this.operatingSystemVersion);
|
393 |
} |
394 |
} |
395 |
return builder.toString();
|
396 |
} |
397 |
|
398 |
@Override
|
399 |
public String getOperatingSystemFamily() { |
400 |
if( this.operatingSystemFamily == null ) { |
401 |
loadOperatingSystemInfo(); |
402 |
} |
403 |
return this.operatingSystemFamily; |
404 |
} |
405 |
|
406 |
@Override
|
407 |
public String getOperatingSystemName() { |
408 |
if( this.operatingSystemFamily == null ) { |
409 |
loadOperatingSystemInfo(); |
410 |
} |
411 |
return this.operatingSystemName; |
412 |
} |
413 |
|
414 |
@Override
|
415 |
public String getOperatingSystemVersion() { |
416 |
if( this.operatingSystemFamily == null ) { |
417 |
loadOperatingSystemInfo(); |
418 |
} |
419 |
return this.operatingSystemVersion; |
420 |
} |
421 |
|
422 |
@Override
|
423 |
public String getArchitecture() { |
424 |
String osarch = System.getProperty("os.arch"); |
425 |
if (osarch.toLowerCase().startsWith("i386")) { |
426 |
return ARCH.X86;
|
427 |
} |
428 |
if (osarch.toLowerCase().startsWith("i686")) { |
429 |
return ARCH.X86;
|
430 |
} |
431 |
if (osarch.toLowerCase().startsWith("x86_64")) { |
432 |
return ARCH.X86_64;
|
433 |
} |
434 |
if (osarch.toLowerCase().startsWith("x86")) { |
435 |
return ARCH.X86;
|
436 |
} |
437 |
if (osarch.toLowerCase().startsWith("amd64")) { |
438 |
return ARCH.X86_64;
|
439 |
} |
440 |
return osarch;
|
441 |
} |
442 |
|
443 |
public Dependency createDependency(PackageInfo packageInfo) {
|
444 |
return new DefaultDependency(packageInfo); |
445 |
} |
446 |
|
447 |
public Dependency createDependency() {
|
448 |
return new DefaultDependency(); |
449 |
} |
450 |
|
451 |
public DependenciesCalculator createDependenciesCalculator(
|
452 |
InstallPackageService installService) { |
453 |
return new DefaultDependenciesCalculator(installService); |
454 |
} |
455 |
|
456 |
public Version createVersion() {
|
457 |
if ( version == null ) { |
458 |
return new DefaultVersion(); |
459 |
} |
460 |
Version v = null;
|
461 |
try {
|
462 |
v = (Version) version.clone(); |
463 |
} catch (CloneNotSupportedException e) { |
464 |
// Version clone can't trow exception
|
465 |
} |
466 |
return v;
|
467 |
} |
468 |
|
469 |
public PackageInfoReader getDefaultPackageInfoReader() {
|
470 |
return new InstallerInfoFileReader(); |
471 |
} |
472 |
|
473 |
public PackageInfoWriter getDefaultPackageInfoWriter() {
|
474 |
return new InstallerInfoFileWriter(); |
475 |
} |
476 |
|
477 |
public MakePackageService createMakePackage(File packageFolder, |
478 |
PackageInfo packageInfo) { |
479 |
return new DefaultMakePackageService(this, packageFolder, packageInfo); |
480 |
} |
481 |
|
482 |
public PackageInfo createPackageInfo() {
|
483 |
return new DefaultPackageInfo(); |
484 |
} |
485 |
|
486 |
public PackageInfo createPackageInfo(InputStream stream) throws BaseException { |
487 |
PackageInfo pkg = new DefaultPackageInfo();
|
488 |
PackageInfoReader reader = this.getDefaultPackageInfoReader();
|
489 |
reader.read(pkg, stream); |
490 |
return pkg;
|
491 |
} |
492 |
|
493 |
public PackageInfo createPackageInfo(File file) throws BaseException { |
494 |
FileInputStream fis = null; |
495 |
PackageInfo pkg = null;
|
496 |
try {
|
497 |
fis = new FileInputStream(file); |
498 |
pkg = this.createPackageInfo(fis);
|
499 |
fis.close(); |
500 |
} catch (Exception ex) { |
501 |
|
502 |
} finally {
|
503 |
try {
|
504 |
fis.close(); |
505 |
} catch (IOException ex) { |
506 |
//
|
507 |
} |
508 |
} |
509 |
return pkg;
|
510 |
} |
511 |
|
512 |
public URL getDownloadBaseURL() { |
513 |
return this.BaseDownloadURL; |
514 |
} |
515 |
|
516 |
public String getVersion() { |
517 |
return this.version.toString(); |
518 |
} |
519 |
|
520 |
public void setVersion(Version version) { |
521 |
try {
|
522 |
this.version = (Version) version.clone();
|
523 |
} catch (CloneNotSupportedException e) { |
524 |
// This should not happen
|
525 |
} |
526 |
|
527 |
} |
528 |
|
529 |
public Version getVersionEx() {
|
530 |
try {
|
531 |
return (Version) this.version.clone(); |
532 |
} catch (CloneNotSupportedException e) { |
533 |
// This should not happen
|
534 |
return null; |
535 |
} |
536 |
} |
537 |
|
538 |
public void setDownloadBaseURL(URL url) { |
539 |
this.BaseDownloadURL = url;
|
540 |
} |
541 |
|
542 |
public void setVersion(String version) { |
543 |
if ( this.version == null ) { |
544 |
this.version = new DefaultVersion(); |
545 |
} |
546 |
this.version.parse(version);
|
547 |
} |
548 |
|
549 |
public File getDefaultLocalAddonRepository() { |
550 |
File f = this.defaultRepositoryLocation.get("plugin"); |
551 |
return f;
|
552 |
} |
553 |
|
554 |
public void setDefaultLocalAddonRepository(File defaultAddonsRepository) { |
555 |
this.defaultRepositoryLocation.put("plugin", defaultAddonsRepository); |
556 |
this.localRepositoriesLocation.add(new LocalRepositoryLocation(defaultAddonsRepository, "plugin")); |
557 |
} |
558 |
|
559 |
public void addLocalAddonRepository(File path) { |
560 |
this.addLocalAddonRepository(path, "plugin"); |
561 |
} |
562 |
|
563 |
public File getDefaultLocalAddonRepository(String packageType) { |
564 |
return this.getDefaultLocalAddonRepository(packageType,ACCESS_READ); |
565 |
} |
566 |
|
567 |
public boolean needAdminRights() { |
568 |
List<File> folders = getLocalAddonRepositories(); |
569 |
for( File folder : folders) { |
570 |
if( !canWrite(folder) ) {
|
571 |
return true; |
572 |
} |
573 |
} |
574 |
return false; |
575 |
} |
576 |
|
577 |
private boolean canWrite(File f) { |
578 |
if( !f.canWrite() ) {
|
579 |
return false; |
580 |
} |
581 |
// Esto requiere java 1.7 o superior y aun debemos ejecutar con 1.6
|
582 |
// Path path = FileSystems.getDefault().getPath(f.getAbsolutePath());
|
583 |
// boolean b = Files.isWritable(path);
|
584 |
// return b;
|
585 |
|
586 |
// En MS Windows File.canWrite retorna true aunque luego no se pueden crear
|
587 |
// escribir en esa carpeta, asi que probamos a crear una carpeta para
|
588 |
// asegurarnos si se puede escribir realmente.
|
589 |
File f2 = new File(f,"test.dir"); |
590 |
if( f2.mkdir() ) {
|
591 |
f2.delete(); |
592 |
return true; |
593 |
} |
594 |
return false; |
595 |
} |
596 |
|
597 |
public File getDefaultLocalAddonRepository(String packageType, int access) { |
598 |
File f = this.defaultRepositoryLocation.get(packageType); |
599 |
switch(access) {
|
600 |
case ACCESS_WRITE:
|
601 |
if( canWrite(f) ) {
|
602 |
return f;
|
603 |
} |
604 |
break;
|
605 |
case ACCESS_READ:
|
606 |
default:
|
607 |
if( f.canRead()) {
|
608 |
return f;
|
609 |
} |
610 |
break;
|
611 |
} |
612 |
List<File> repositoriesLocaltions = this.getLocalAddonRepositories(packageType); |
613 |
for( File repositoryLocation : repositoriesLocaltions ) { |
614 |
switch(access) {
|
615 |
case ACCESS_WRITE:
|
616 |
if( canWrite(repositoryLocation) ) {
|
617 |
return repositoryLocation;
|
618 |
} |
619 |
break;
|
620 |
case ACCESS_READ:
|
621 |
default:
|
622 |
if( repositoryLocation.canRead()) {
|
623 |
return repositoryLocation;
|
624 |
} |
625 |
break;
|
626 |
} |
627 |
} |
628 |
return null; |
629 |
} |
630 |
|
631 |
public void setDefaultLocalAddonRepository(File defaultAddonsRepository, String packageType) { |
632 |
this.defaultRepositoryLocation.put(packageType, defaultAddonsRepository);
|
633 |
this.localRepositoriesLocation.add(new LocalRepositoryLocation(defaultAddonsRepository, packageType)); |
634 |
} |
635 |
|
636 |
public void addLocalAddonRepository(File path, String type) { |
637 |
localRepositoriesLocation.add(new LocalRepositoryLocation(path, type));
|
638 |
} |
639 |
|
640 |
public String getDefaultLocalRepositoryType(File file) { |
641 |
Iterator<LocalRepositoryLocation> it = localRepositoriesLocation.iterator();
|
642 |
while ( it.hasNext() ) {
|
643 |
LocalRepositoryLocation location = it.next(); |
644 |
if ( location.contains(file) ) {
|
645 |
return location.getDefaultType();
|
646 |
} |
647 |
} |
648 |
return null; |
649 |
} |
650 |
|
651 |
public List<File> getLocalAddonRepositories() { |
652 |
return this.getLocalAddonRepositories(null); |
653 |
} |
654 |
|
655 |
public List<File> getLocalAddonRepositories(String type) { |
656 |
List<File> l = new ArrayList<File>(); |
657 |
Iterator<LocalRepositoryLocation> it = localRepositoriesLocation.iterator();
|
658 |
while ( it.hasNext() ) {
|
659 |
LocalRepositoryLocation location = it.next(); |
660 |
if( type==null || location.support(type) ) { |
661 |
l.add(location.getLocation()); |
662 |
} |
663 |
} |
664 |
return l;
|
665 |
} |
666 |
|
667 |
public List<File> getAddonFolders() { |
668 |
return this.getAddonFolders(null); |
669 |
} |
670 |
|
671 |
public List<File> getAddonFolders(String type) { |
672 |
List<File> addonFolders = new ArrayList<File>(); |
673 |
|
674 |
// Para cada directorio en la lista de repositorios locales
|
675 |
List<File> localAddonRepositories = this.getLocalAddonRepositories(type); |
676 |
for ( int i = 0; i < localAddonRepositories.size(); i++ ) { |
677 |
File repoPath = localAddonRepositories.get(i);
|
678 |
if ( repoPath.isDirectory() && repoPath.exists() ) {
|
679 |
File[] folderRepoList = repoPath.listFiles(); |
680 |
|
681 |
// recorrer los directorios que haya dentro
|
682 |
for ( int j = 0; j < folderRepoList.length; j++ ) { |
683 |
File addonFolder = folderRepoList[j];
|
684 |
if ( addonFolder.isDirectory() ) {
|
685 |
File pkginfofile = new File(addonFolder, "package.info"); |
686 |
if ( pkginfofile.exists() ) {
|
687 |
addonFolders.add(addonFolder); |
688 |
} |
689 |
} |
690 |
|
691 |
} |
692 |
} |
693 |
} |
694 |
|
695 |
return addonFolders;
|
696 |
} |
697 |
|
698 |
public File getAddonFolder(String code) { |
699 |
List<File> packagePaths = this.getAddonFolders(); |
700 |
for ( int i = 0; i < packagePaths.size(); i++ ) { |
701 |
try {
|
702 |
File pkgfile = new File(packagePaths.get(i), "package.info"); |
703 |
PackageInfo pkg = this.createPackageInfo(pkgfile);
|
704 |
if ( pkg.getCode().equalsIgnoreCase(code) ) {
|
705 |
return packagePaths.get(i);
|
706 |
} |
707 |
} catch (Exception ex) { |
708 |
|
709 |
} |
710 |
} |
711 |
return null; |
712 |
} |
713 |
|
714 |
public List<byte[]> getPublicKeys() { |
715 |
byte[] rawkey; |
716 |
try {
|
717 |
InputStream is = this.getClass().getResourceAsStream("/org/gvsig/installer/lib/keys/key.public"); |
718 |
rawkey = new byte[is.available()]; |
719 |
is.read(rawkey); |
720 |
is.close(); |
721 |
} catch (IOException e) { |
722 |
return null; |
723 |
} |
724 |
List<byte[]> keys = new ArrayList<byte[]>(); |
725 |
keys.add(rawkey); |
726 |
return keys;
|
727 |
} |
728 |
|
729 |
public boolean hasProviderToThisPackage(PackageInfo packageInfo) { |
730 |
InstallerProviderManager provmgr = (InstallerProviderManager) this.getProviderManager();
|
731 |
try {
|
732 |
return provmgr.getProviderFactory(packageInfo.getType()) != null; |
733 |
} catch (Exception e) { |
734 |
return false; |
735 |
} |
736 |
} |
737 |
|
738 |
} |