Revision 37599 branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/execution/DefaultInstallPackageService.java

View differences:

DefaultInstallPackageService.java
65 65
 */
66 66

  
67 67
public class DefaultInstallPackageService extends Thread implements
68
    InstallPackageService {
68
		InstallPackageService {
69 69

  
70
    private static final Logger LOG =
71
        LoggerFactory.getLogger(DefaultInstallPackageService.class);
70
	private static final Logger LOG = LoggerFactory
71
			.getLogger(DefaultInstallPackageService.class);
72 72

  
73
    private Map<PackageInfo, File> packageInfoFileMap = null;
74
    private Map<PackageInfo, String> zipEntriesMap = null;
75
    private List<PackageInfo> packageInfos = null;
76
    private InstallerManager manager;
77
    private InstallPackageProviderServices installerProviderServices = null;
73
	private Map<PackageInfo, File> packageInfoFileMap = null;
74
	private Map<PackageInfo, String> zipEntriesMap = null;
75
	private List<PackageInfo> packageInfos = null;
76
	private InstallerManager manager;
77
	private InstallPackageProviderServices installerProviderServices = null;
78 78

  
79
    public DefaultInstallPackageService(DefaultInstallerManager manager) {
80
        super();
81
        this.manager = manager;
82
        this.reset();
83
    }
79
	public DefaultInstallPackageService(DefaultInstallerManager manager) {
80
		super();
81
		this.manager = manager;
82
		this.reset();
83
	}
84 84

  
85
    public void reset() {
86
        packageInfoFileMap = new HashMap<PackageInfo, File>();
87
        packageInfos = new ArrayList<PackageInfo>();
88
        zipEntriesMap = new HashMap<PackageInfo, String>();
89
        installerProviderServices =
90
            InstallerProviderLocator.getProviderManager()
91
                .createInstallerProviderServices();
92
    }
85
	public void reset() {
86
		packageInfoFileMap = new HashMap<PackageInfo, File>();
87
		packageInfos = new ArrayList<PackageInfo>();
88
		zipEntriesMap = new HashMap<PackageInfo, String>();
89
		installerProviderServices = InstallerProviderLocator
90
				.getProviderManager().createInstallerProviderServices();
91
	}
93 92

  
94
    public class InstallerApplicationDirectoryNotFoundException extends
95
        InstallPackageServiceException {
93
	public class InstallerApplicationDirectoryNotFoundException extends
94
			InstallPackageServiceException {
96 95

  
97
        private static final long serialVersionUID = -1130408094135962456L;
96
		private static final long serialVersionUID = -1130408094135962456L;
98 97

  
99
        private static final String message =
100
            "Aplication directory '%(directory)s' not found";
98
		private static final String message = "Aplication directory '%(directory)s' not found";
101 99

  
102
        private static final String KEY =
103
            "_aplication_directory_XdirectoryX_not_found";
100
		private static final String KEY = "_aplication_directory_XdirectoryX_not_found";
104 101

  
105
        public InstallerApplicationDirectoryNotFoundException(File file) {
106
            super(message, KEY, serialVersionUID);
107
            setValue("directory", file.toString());
108
        }
102
		public InstallerApplicationDirectoryNotFoundException(File file) {
103
			super(message, KEY, serialVersionUID);
104
			setValue("directory", file.toString());
105
		}
109 106

  
110
    }
107
	}
111 108

  
112
    public class InstallerNoDirectoryException extends
113
        InstallPackageServiceException {
109
	public class InstallerNoDirectoryException extends
110
			InstallPackageServiceException {
114 111

  
115
        private static final long serialVersionUID = -8685263049644983769L;
112
		private static final long serialVersionUID = -8685263049644983769L;
116 113

  
117
        private static final String message =
118
            "'%(directory)s' is not a directory";
114
		private static final String message = "'%(directory)s' is not a directory";
119 115

  
120
        private static final String KEY = "_XdirectoryX_is_not_a_directory";
116
		private static final String KEY = "_XdirectoryX_is_not_a_directory";
121 117

  
122
        public InstallerNoDirectoryException(File file) {
123
            super(message, KEY, serialVersionUID);
124
            setValue("directory", file.toString());
125
        }
118
		public InstallerNoDirectoryException(File file) {
119
			super(message, KEY, serialVersionUID);
120
			setValue("directory", file.toString());
121
		}
126 122

  
127
    }
123
	}
128 124

  
129
    public class InstallerFileNotFoundException extends
130
        InstallPackageServiceException {
125
	public class InstallerFileNotFoundException extends
126
			InstallPackageServiceException {
131 127

  
132
        private static final long serialVersionUID = 556517830330132149L;
128
		private static final long serialVersionUID = 556517830330132149L;
133 129

  
134
        private static final String message = "File '%(file)s' not found";
130
		private static final String message = "File '%(file)s' not found";
135 131

  
136
        private static final String KEY = "_file_XfileX_not_found";
132
		private static final String KEY = "_file_XfileX_not_found";
137 133

  
138
        public InstallerFileNotFoundException(File file) {
139
            super(message, KEY, serialVersionUID);
140
            setValue("file", file.toString());
141
        }
134
		public InstallerFileNotFoundException(File file) {
135
			super(message, KEY, serialVersionUID);
136
			setValue("file", file.toString());
137
		}
142 138

  
143
    }
139
	}
144 140

  
145
    public class InstallerBundleNotFoundException extends
146
        InstallPackageServiceException {
141
	public class InstallerBundleNotFoundException extends
142
			InstallPackageServiceException {
147 143

  
148
        private static final long serialVersionUID = 5065410511582625301L;
144
		private static final long serialVersionUID = 5065410511582625301L;
149 145

  
150
        private static final String message = "File '%(file)s' not found";
146
		private static final String message = "File '%(file)s' not found";
151 147

  
152
        private static final String KEY = "_file_XfileX_not_found";
148
		private static final String KEY = "_file_XfileX_not_found";
153 149

  
154
        public InstallerBundleNotFoundException(File file,
155
            FileNotFoundException e) {
156
            super(message, e, KEY, serialVersionUID);
157
            setValue("file", file.toString());
158
        }
150
		public InstallerBundleNotFoundException(File file,
151
				FileNotFoundException e) {
152
			super(message, e, KEY, serialVersionUID);
153
			setValue("file", file.toString());
154
		}
159 155

  
160
    }
156
	}
161 157

  
162
    public class InstallerIOException extends InstallPackageServiceException {
158
	public class InstallerIOException extends InstallPackageServiceException {
163 159

  
164
        private static final long serialVersionUID = 3153613550157712363L;
160
		private static final long serialVersionUID = 3153613550157712363L;
165 161

  
166
        private static final String message =
167
            "IO error installing the file '%(file)s'";
162
		private static final String message = "IO error installing the file '%(file)s'";
168 163

  
169
        private static final String KEY = "_IO_error installing_file_XfileX_";
164
		private static final String KEY = "_IO_error installing_file_XfileX_";
170 165

  
171
        public InstallerIOException(File file, IOException e) {
172
            super(message, e, KEY, serialVersionUID);
173
            setValue("file", file.toString());
174
        }
166
		public InstallerIOException(File file, IOException e) {
167
			super(message, e, KEY, serialVersionUID);
168
			setValue("file", file.toString());
169
		}
175 170

  
176
    }
171
	}
177 172

  
178
    public class InstallerFileDownloadException extends
179
        InstallPackageServiceException {
173
	public class InstallerFileDownloadException extends
174
			InstallPackageServiceException {
180 175

  
181
        private static final long serialVersionUID = 8640183295766490512L;
176
		private static final long serialVersionUID = 8640183295766490512L;
182 177

  
183
        private static final String message = "File '%(url)s' download error";
178
		private static final String message = "File '%(url)s' download error";
184 179

  
185
        private static final String KEY = "_File_XurlX_download_error";
180
		private static final String KEY = "_File_XurlX_download_error";
186 181

  
187
        public InstallerFileDownloadException(URL url, IOException e) {
188
            super(message, e, KEY, serialVersionUID);
189
            setValue("url", url.toString());
190
        }
182
		public InstallerFileDownloadException(URL url, IOException e) {
183
			super(message, e, KEY, serialVersionUID);
184
			setValue("url", url.toString());
185
		}
191 186

  
192
    }
187
	}
193 188

  
194
    public class InstallerPackageNotFoundException extends
195
        InstallPackageServiceException {
189
	public class InstallerPackageNotFoundException extends
190
			InstallPackageServiceException {
196 191

  
197
        private static final long serialVersionUID = 1726608498886963868L;
192
		private static final long serialVersionUID = 1726608498886963868L;
198 193

  
199
        private static final String message = "Package not found";
194
		private static final String message = "Package not found";
200 195

  
201
        private static final String KEY = "_package_not_found";
196
		private static final String KEY = "_package_not_found";
202 197

  
203
        public InstallerPackageNotFoundException() {
204
            super(message, KEY, serialVersionUID);
205
        }
198
		public InstallerPackageNotFoundException() {
199
			super(message, KEY, serialVersionUID);
200
		}
206 201

  
207
    }
202
	}
208 203

  
209
    public class InstallerNoPackageException extends
210
        InstallPackageServiceException {
204
	public class InstallerNoPackageException extends
205
			InstallPackageServiceException {
211 206

  
212
        private static final long serialVersionUID = -2292735515704746966L;
207
		private static final long serialVersionUID = -2292735515704746966L;
213 208

  
214
        private static final String message = "Package does not exist";
209
		private static final String message = "Package does not exist";
215 210

  
216
        private static final String KEY = "_package__does_not_exist";
211
		private static final String KEY = "_package__does_not_exist";
217 212

  
218
        public InstallerNoPackageException() {
219
            super(message, KEY, serialVersionUID);
220
        }
213
		public InstallerNoPackageException() {
214
			super(message, KEY, serialVersionUID);
215
		}
221 216

  
222
    }
217
	}
223 218

  
224
    public class InstallerProviderCreationException extends
225
        InstallPackageServiceException {
219
	public class InstallerProviderCreationException extends
220
			InstallPackageServiceException {
226 221

  
227
        private static final long serialVersionUID = -7985786807492393584L;
222
		private static final long serialVersionUID = -7985786807492393584L;
228 223

  
229
        private static final String message = "Error creating the provider";
224
		private static final String message = "Error creating the provider";
230 225

  
231
        private static final String KEY = "_Error_creating_the_provider";
226
		private static final String KEY = "_Error_creating_the_provider";
232 227

  
233
        public InstallerProviderCreationException(ServiceException e) {
234
            super(message, e, KEY, serialVersionUID);
235
        }
228
		public InstallerProviderCreationException(ServiceException e) {
229
			super(message, e, KEY, serialVersionUID);
230
		}
236 231

  
237
    }
232
	}
238 233

  
239
    public void installPackage(File applicationDirectory,
240
        PackageInfo packageInfo) throws InstallPackageServiceException {
241
        if (!applicationDirectory.exists()) {
242
            throw new InstallerApplicationDirectoryNotFoundException(
243
                applicationDirectory);
244
        }
245
        if (!packageInfoFileMap.containsKey(packageInfo)) {
246
            throw new InstallerPackageNotFoundException();
247
        }
234
	public void installPackage(File applicationDirectory,
235
			PackageInfo packageInfo) throws InstallPackageServiceException {
236
		if (!applicationDirectory.exists()) {
237
			throw new InstallerApplicationDirectoryNotFoundException(
238
					applicationDirectory);
239
		}
240
		if (!packageInfoFileMap.containsKey(packageInfo)) {
241
			throw new InstallerPackageNotFoundException();
242
		}
248 243

  
249
        InstallPackageProvider installerExecutionProvider =
250
            createProvider(packageInfo);
244
		InstallPackageProvider installerExecutionProvider = createProvider(packageInfo);
251 245

  
252
        // Get the package or package set file
253
        File file = packageInfoFileMap.get(packageInfo);
254
        if (file == null) {
255
            if (packageInfo.getDownloadURL() == null) {
256
                throw new InstallerPackageNotFoundException();
257
            }
258
            this.downloadPackage(packageInfo);
259
            file = packageInfoFileMap.get(packageInfo);
260
        }
246
		// Get the package or package set file
247
		File file = packageInfoFileMap.get(packageInfo);
248
		if (file == null) {
249
			if (packageInfo.getDownloadURL() == null) {
250
				throw new InstallerPackageNotFoundException();
251
			}
252
			this.downloadPackage(packageInfo);
253
			file = packageInfoFileMap.get(packageInfo);
254
		}
261 255

  
262
        // Open and install the package or package set file
263
        try {
256
		// Open and install the package or package set file
257
		try {
264 258

  
265
            InputStream packageStream;
266
            InputStream fis = new FileInputStream(file);
267
            InputStream bis = new BufferedInputStream(fis);
268
            if (isPackage(file)) {
269
                packageStream = bis;
270
            } else {
271
                if (!isPackageSet(file)) {
272
                    LOG.warn("Trying to install a package file ({0}) "
273
                        + "without a known file extension. Will try "
274
                        + "to install it as a package set", file);
275
                }
276
                packageStream =
277
                    installerProviderServices.searchPackage(bis, zipEntriesMap
278
                        .get(packageInfo));
279
            }
259
			InputStream packageStream;
260
			InputStream fis = new FileInputStream(file);
261
			InputStream bis = new BufferedInputStream(fis);
262
			if (isPackage(file)) {
263
				packageStream = bis;
264
			} else {
265
				if (!isPackageSet(file)) {
266
					LOG.warn("Trying to install a package file ({0}) "
267
							+ "without a known file extension. Will try "
268
							+ "to install it as a package set", file);
269
				}
270
				packageStream = installerProviderServices.searchPackage(bis,
271
						zipEntriesMap.get(packageInfo));
272
			}
280 273

  
281
            try {
282
                installerExecutionProvider.install(applicationDirectory,
283
                    packageStream, packageInfo);
284
            } catch (InstallPackageServiceException e) {
274
			try {
275
				installerExecutionProvider.install(applicationDirectory,
276
						packageStream, packageInfo);
277
			} catch (InstallPackageServiceException e) {
285 278

  
286
                packageStream.close();
287
                if (bis != packageStream) {
288
                    bis.close();
289
                }
290
                fis.close();
279
				packageStream.close();
280
				if (bis != packageStream) {
281
					bis.close();
282
				}
283
				fis.close();
291 284

  
292
                // if fails the installation, zip files need to be reopen
293
                // the package will be installed into the update folder for
294
                // installation after a gvSIG restart
285
				// if fails the installation, zip files need to be reopen
286
				// the package will be installed into the update folder for
287
				// installation after a gvSIG restart
295 288

  
296
                fis = new FileInputStream(file);
297
                bis = new BufferedInputStream(fis);
298
                if (isPackage(file)) {
299
                    packageStream = bis;
300
                } else {
301
                    if (!isPackageSet(file)) {
302
                        LOG.warn("Trying to install a package file ({0}) "
303
                            + "without a known file extension. Will try "
304
                            + "to install it as a package set", file);
305
                    }
306
                    packageStream =
307
                        installerProviderServices.searchPackage(bis,
308
                            zipEntriesMap.get(packageInfo));
309
                }
310
                installerExecutionProvider.installLater(applicationDirectory,
311
                    packageStream, packageInfo);
312
            }
289
				fis = new FileInputStream(file);
290
				bis = new BufferedInputStream(fis);
291
				if (isPackage(file)) {
292
					packageStream = bis;
293
				} else {
294
					if (!isPackageSet(file)) {
295
						LOG.warn("Trying to install a package file ({0}) "
296
								+ "without a known file extension. Will try "
297
								+ "to install it as a package set", file);
298
					}
299
					packageStream = installerProviderServices.searchPackage(
300
							bis, zipEntriesMap.get(packageInfo));
301
				}
302
				installerExecutionProvider.installLater(applicationDirectory,
303
						packageStream, packageInfo);
304
			}
313 305

  
314
            packageStream.close();
315
            if (bis != packageStream) {
316
                bis.close();
317
            }
318
            fis.close();
306
			packageStream.close();
307
			if (bis != packageStream) {
308
				bis.close();
309
			}
310
			fis.close();
319 311

  
320
        } catch (FileNotFoundException e) {
321
            throw new InstallerFileNotFoundException(file);
322
        } catch (IOException e) {
323
            throw new InstallerIOException(file, e);
324
        }
312
		} catch (FileNotFoundException e) {
313
			throw new InstallerFileNotFoundException(file);
314
		} catch (IOException e) {
315
			throw new InstallerIOException(file, e);
316
		}
325 317

  
326
    }
318
	}
327 319

  
328
    public void installPackage(File applicationDirectory, String packageCode)
329
        throws InstallPackageServiceException {
330
        PackageInfo packageInfo = getPackageInfo(packageCode);
331
        if (packageInfo == null) {
332
            throw new InstallerNoPackageException();
333
        }
334
        installPackage(applicationDirectory, packageInfo);
335
    }
320
	public void installPackage(File applicationDirectory, String packageCode)
321
			throws InstallPackageServiceException {
322
		PackageInfo packageInfo = getPackageInfo(packageCode);
323
		if (packageInfo == null) {
324
			throw new InstallerNoPackageException();
325
		}
326
		installPackage(applicationDirectory, packageInfo);
327
	}
336 328

  
337
    private InstallPackageProvider createProvider(PackageInfo packageInfo)
338
        throws InstallPackageServiceException {
339
        InstallerProviderManager installerProviderManager =
340
            (InstallerProviderManager) ((DefaultInstallerManager) manager)
341
                .getProviderManager();
329
	private InstallPackageProvider createProvider(PackageInfo packageInfo)
330
			throws InstallPackageServiceException {
331
		InstallerProviderManager installerProviderManager = (InstallerProviderManager) ((DefaultInstallerManager) manager)
332
				.getProviderManager();
342 333

  
343
        try {
344
            return installerProviderManager.createExecutionProvider(packageInfo
345
                .getType());
346
        } catch (ServiceException e) {
347
            throw new InstallerProviderCreationException(e);
348
        }
349
    }
334
		try {
335
			return installerProviderManager.createExecutionProvider(packageInfo
336
					.getType());
337
		} catch (ServiceException e) {
338
			throw new InstallerProviderCreationException(e);
339
		}
340
	}
350 341

  
351
    public PackageInfo getPackageInfo(int index) {
352
        if (index >= packageInfos.size()) {
353
            return null;
354
        }
355
        return packageInfos.get(index);
356
    }
342
	public PackageInfo getPackageInfo(int index) {
343
		if (index >= packageInfos.size()) {
344
			return null;
345
		}
346
		return packageInfos.get(index);
347
	}
357 348

  
358
    public PackageInfo getPackageInfo(String packageCode) {
359
        for (int i = 0; i < getPackageCount(); i++) {
360
            if (packageInfos.get(i).getCode().equals(packageCode)) {
361
                return packageInfos.get(i);
362
            }
363
        }
364
        return null;
365
    }
349
	public PackageInfo getPackageInfo(String packageCode) {
350
		for (int i = 0; i < getPackageCount(); i++) {
351
			if (packageInfos.get(i).getCode().equals(packageCode)) {
352
				return packageInfos.get(i);
353
			}
354
		}
355
		return null;
356
	}
366 357

  
367
    public void addBundle(File bundle) throws InstallPackageServiceException {
358
	public void addBundle(File bundle) throws InstallPackageServiceException {
368 359

  
369
        if (!bundle.exists()) {
370
            throw new InstallPackageServiceException();
371
        }
360
		if (!bundle.exists()) {
361
			throw new InstallPackageServiceException();
362
		}
372 363

  
373
        int packageInfoCount = packageInfos.size();
364
		int packageInfoCount = packageInfos.size();
374 365

  
375
        FileInputStream fis;
376
        try {
377
            fis = new FileInputStream(bundle);
378
        } catch (FileNotFoundException e) {
379
            throw new InstallerBundleNotFoundException(bundle, e);
380
        }
381
        BufferedInputStream bis = new BufferedInputStream(fis);
382
        if (isPackage(bundle)) {
383
            installerProviderServices.readPackageInfo(bis, packageInfos,
384
                zipEntriesMap, bundle.getName());
385
        } else {
386
            if (!isPackageSet(bundle)) {
387
                LOG.warn("Trying to add a package file ({0}) without a known "
388
                    + "file extension. Will try to add it as a package set",
389
                    bundle);
390
            }
391
            installerProviderServices.readPackageSetInfo(fis, packageInfos,
392
                zipEntriesMap);
393
        }
394
        try {
395
            bis.close();
396
            fis.close();
397
        } catch (IOException e) {
398
            LOG.warn("Error closing the input streams of the package file: "
399
                + bundle, e);
400
        }
366
		FileInputStream fis;
367
		try {
368
			fis = new FileInputStream(bundle);
369
		} catch (FileNotFoundException e) {
370
			throw new InstallerBundleNotFoundException(bundle, e);
371
		}
372
		BufferedInputStream bis = new BufferedInputStream(fis);
373
		if (isPackage(bundle)) {
374
			installerProviderServices.readPackageInfo(bis, packageInfos,
375
					zipEntriesMap, bundle.getName());
376
		} else {
377
			if (!isPackageSet(bundle)) {
378
				LOG
379
						.warn(
380
								"Trying to add a package file ({0}) without a known "
381
										+ "file extension. Will try to add it as a package set",
382
								bundle);
383
			}
384
			installerProviderServices.readPackageSetInfo(fis, packageInfos,
385
					zipEntriesMap);
386
		}
387
		try {
388
			bis.close();
389
			fis.close();
390
		} catch (IOException e) {
391
			LOG.warn("Error closing the input streams of the package file: "
392
					+ bundle, e);
393
		}
401 394

  
402
        for (int i = packageInfoCount; i < packageInfos.size(); i++) {
403
            packageInfoFileMap.put(packageInfos.get(i), bundle);
404
        }
405
    }
395
		for (int i = packageInfoCount; i < packageInfos.size(); i++) {
396
			packageInfoFileMap.put(packageInfos.get(i), bundle);
397
		}
398
	}
406 399

  
407
    private boolean isPackageSet(File file) {
408
        return file.getName().endsWith(
409
            manager.getDefaultPackageSetFileExtension());
410
    }
400
	private boolean isPackageSet(File file) {
401
		return file.getName().endsWith(
402
				manager.getDefaultPackageSetFileExtension());
403
	}
411 404

  
412
    private boolean isPackage(File file) {
413
        return file.getName()
414
            .endsWith(manager.getDefaultPackageFileExtension());
415
    }
405
	private boolean isPackage(File file) {
406
		return file.getName()
407
				.endsWith(manager.getDefaultPackageFileExtension());
408
	}
416 409

  
417
    // public void addBundle(URL bundleURL) throws
418
    // InstallPackageServiceException {
419
    // File bundle = downloadFile(bundleURL, "packages.gvspki");
420
    // addBundle(bundle);
421
    // }
410
	// public void addBundle(URL bundleURL) throws
411
	// InstallPackageServiceException {
412
	// File bundle = downloadFile(bundleURL, "packages.gvspki");
413
	// addBundle(bundle);
414
	// }
422 415

  
423
    public void addBundle(URL bundleURL) throws InstallPackageServiceException {
424
        String packageFileName = "packages.gvspki";
425
        File bundle;
426
        if (bundleURL.toString().endsWith(packageFileName)) {
427
            bundle = downloadFile(bundleURL, packageFileName);
428
            addBundle(bundle);
429
        } else {
416
	public void addBundle(URL bundleURL) throws InstallPackageServiceException {
417
		String packageFileName = "packages.gvspki";
418
		File bundle;
419
		if (bundleURL.toString().endsWith(packageFileName)) {
420
			bundle = downloadFile(bundleURL, packageFileName);
421
			addBundle(bundle);
422
		} else {
430 423

  
431
            manager.setDownloadBaseURL(bundleURL);
424
			manager.setDownloadBaseURL(bundleURL);
432 425

  
433
            String urlString = bundleURL.toString();
434
            if (!urlString.endsWith("/")) {
435
                urlString += "/";
436
            }
437
            urlString +=
438
                ("dists/" + manager.getVersion() + "/" + packageFileName);
426
			String urlString = bundleURL.toString();
427
			if (!urlString.endsWith("/")) {
428
				urlString += "/";
429
			}
430
			urlString += ("dists/" + manager.getVersion() + "/" + packageFileName);
439 431

  
440
            URL completeURL;
441
            try {
442
                completeURL = new URL(urlString);
443
            } catch (MalformedURLException e) {
444
                // TODO Auto-generated catch block
445
                e.printStackTrace();
446
                return;
447
            }
432
			URL completeURL;
433
			try {
434
				completeURL = new URL(urlString);
435
			} catch (MalformedURLException e) {
436
				// TODO Auto-generated catch block
437
				e.printStackTrace();
438
				return;
439
			}
448 440

  
449
            bundle = downloadFile(completeURL, packageFileName);
450
            addBundle(bundle);
451
        }
441
			bundle = downloadFile(completeURL, packageFileName);
442
			addBundle(bundle);
443
		}
452 444

  
453
    }
445
	}
454 446

  
455
    private File downloadFile(URL bundleURL, String defaultFileName)
456
        throws InstallPackageServiceException {
457
        try {
458
            Download download = new Download();
459
            return download.downloadFile(bundleURL, defaultFileName);
460
        } catch (IOException e) {
461
            throw new InstallerFileDownloadException(bundleURL, e);
462
        }
463
    }
447
	private File downloadFile(URL bundleURL, String defaultFileName)
448
			throws InstallPackageServiceException {
449
		try {
450
			Download download = new Download();
451
			return download.downloadFile(bundleURL, defaultFileName);
452
		} catch (IOException e) {
453
			throw new InstallerFileDownloadException(bundleURL, e);
454
		}
455
	}
464 456

  
465
    public void addBundlesFromDirectory(File directory)
466
        throws InstallPackageServiceException {
467
        if (!directory.isDirectory()) {
468
            throw new InstallerNoDirectoryException(directory);
469
        }
470
        List<File> files = new ArrayList<File>();
457
	public void addBundlesFromDirectory(File directory)
458
			throws InstallPackageServiceException {
459
		if (!directory.isDirectory()) {
460
			throw new InstallerNoDirectoryException(directory);
461
		}
462
		List<File> files = new ArrayList<File>();
471 463

  
472
        listRecursively(directory, new FileFilter() {
464
		listRecursively(directory, new FileFilter() {
473 465

  
474
            private String packageExt =
475
                manager.getDefaultPackageFileExtension();
476
            private String packageSetExt =
477
                manager.getDefaultPackageSetFileExtension();
466
			private String packageExt = manager
467
					.getDefaultPackageFileExtension();
468
			private String packageSetExt = manager
469
					.getDefaultPackageSetFileExtension();
478 470

  
479
            public boolean accept(File file) {
480
                String name = file.getName().toLowerCase();
481
                return file.isDirectory() || name.endsWith(packageExt)
482
                    || name.endsWith(packageSetExt);
483
            }
484
        }, files);
485
        for (int i = 0; i < files.size(); i++) {
486
            if (files.get(i).isFile()) {
487
                addBundle(files.get(i));
488
            }
489
        }
490
    }
471
			public boolean accept(File file) {
472
				String name = file.getName().toLowerCase();
473
				return file.isDirectory() || name.endsWith(packageExt)
474
						|| name.endsWith(packageSetExt);
475
			}
476
		}, files);
477
		for (int i = 0; i < files.size(); i++) {
478
			if (files.get(i).isFile()) {
479
				addBundle(files.get(i));
480
			}
481
		}
482
	}
491 483

  
492
    private void listRecursively(File fileOrDir, FileFilter filter,
493
        List<File> files) {
494
        files.add(fileOrDir);
484
	private void listRecursively(File fileOrDir, FileFilter filter,
485
			List<File> files) {
486
		files.add(fileOrDir);
495 487

  
496
        if (fileOrDir.isDirectory()) {
488
		if (fileOrDir.isDirectory()) {
497 489

  
498
            File[] dirContents = fileOrDir.listFiles(filter);
490
			File[] dirContents = fileOrDir.listFiles(filter);
499 491

  
500
            for (File f : dirContents) {
501
                listRecursively(f, filter, files); // Recursively list.
502
            }
503
        } else {
504
            files.add(fileOrDir);
505
        }
506
    }
492
			for (File f : dirContents) {
493
				listRecursively(f, filter, files); // Recursively list.
494
			}
495
		} else {
496
			files.add(fileOrDir);
497
		}
498
	}
507 499

  
508
    public int getPackageCount() {
509
        if (packageInfos == null) {
510
            return 0;
511
        }
512
        return packageInfos.size();
513
    }
500
	public int getPackageCount() {
501
		if (packageInfos == null) {
502
			return 0;
503
		}
504
		return packageInfos.size();
505
	}
514 506

  
515
    public Manager getManager() {
516
        return this.manager;
517
    }
507
	public Manager getManager() {
508
		return this.manager;
509
	}
518 510

  
519
    public void downloadPackage(PackageInfo packageInfo)
520
        throws InstallPackageServiceException {
521
        this.downloadPackage(packageInfo, null);
522
    }
511
	public void downloadPackage(PackageInfo packageInfo)
512
			throws InstallPackageServiceException {
513
		this.downloadPackage(packageInfo, null);
514
	}
523 515

  
524
    public void downloadPackage(PackageInfo packageInfo,
525
        SimpleTaskStatus taskStatus) throws InstallPackageServiceException {
526
        File file = packageInfo.downloadFile(taskStatus);
527
        this.packageInfoFileMap.put(packageInfo, file);
528
    }
516
	public void downloadPackage(PackageInfo packageInfo,
517
			SimpleTaskStatus taskStatus) throws InstallPackageServiceException {
518
		File file = packageInfo.downloadFile(taskStatus);
519
		this.packageInfoFileMap.put(packageInfo, file);
520
	}
529 521

  
530
    public List<String> getDefaultSelectedPackagesIDs() {
531
        return installerProviderServices.getDefaultSelectedPackagesIDs();
532
    }
522
	public List<String> getDefaultSelectedPackagesIDs() {
523
		return installerProviderServices.getDefaultSelectedPackagesIDs();
524
	}
533 525

  
534
    public List<String> getCategories() {
535
        Set<String> categories = new HashSet<String>();
526
	public List<String> getCategories() {
527
		Set<String> categories = new HashSet<String>();
536 528

  
537
        for (int i = 0; i < getPackageCount(); i++) {
538
            categories.addAll(packageInfos.get(i).getCategories());
539
        }
529
		for (int i = 0; i < getPackageCount(); i++) {
530
			categories.addAll(packageInfos.get(i).getCategories());
531
		}
540 532

  
541
        return new ArrayList<String>(categories);
542
    }
533
		return new ArrayList<String>(categories);
534
	}
543 535

  
544
    public List<String> getTypes() {
545
        Set<String> types = new HashSet<String>();
536
	public List<String> getTypes() {
537
		Set<String> types = new HashSet<String>();
546 538

  
547
        for (int i = 0; i < getPackageCount(); i++) {
548
            types.add(packageInfos.get(i).getType());
549
        }
539
		for (int i = 0; i < getPackageCount(); i++) {
540
			types.add(packageInfos.get(i).getType());
541
		}
550 542

  
551
        return new ArrayList<String>(types);
552
    }
543
		return new ArrayList<String>(types);
544
	}
553 545

  
554 546
}

Also available in: Unified diff