svn-gvsig-desktop / 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 @ 33743
History | View | Annotate | Download (6.67 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2010 {Prodevelop} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.installer.lib.impl.execution; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.FileInputStream; |
32 |
import java.io.FileNotFoundException; |
33 |
import java.io.InputStream; |
34 |
import java.util.ArrayList; |
35 |
import java.util.HashMap; |
36 |
import java.util.List; |
37 |
import java.util.Map; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
import org.gvsig.installer.lib.api.PackageInfo; |
43 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
44 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
45 |
import org.gvsig.installer.lib.impl.DefaultInstallerManager; |
46 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
47 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
48 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
49 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
50 |
import org.gvsig.tools.service.Manager; |
51 |
import org.gvsig.tools.service.ServiceException; |
52 |
|
53 |
/**
|
54 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
55 |
*/
|
56 |
public class DefaultInstallPackageService implements InstallPackageService { |
57 |
|
58 |
private Map<PackageInfo, File> packageInfoFileMap = null; |
59 |
private Map<PackageInfo, String> zipEntriesMap = null; |
60 |
private List<PackageInfo> packageInfos = null; |
61 |
private Manager manager;
|
62 |
private static final Logger logger = LoggerFactory |
63 |
.getLogger(DefaultInstallPackageService.class); |
64 |
private InstallPackageProviderServices installerProviderServices = null; |
65 |
|
66 |
public DefaultInstallPackageService(DefaultInstallerManager manager) {
|
67 |
super();
|
68 |
this.manager = manager;
|
69 |
packageInfoFileMap = new HashMap<PackageInfo, File>(); |
70 |
packageInfos = new ArrayList<PackageInfo>(); |
71 |
zipEntriesMap = new HashMap<PackageInfo, String>(); |
72 |
installerProviderServices = |
73 |
InstallerProviderLocator.getProviderManager() |
74 |
.createInstallerProviderServices(); |
75 |
} |
76 |
|
77 |
public void installPackage(File applicationDirectory, |
78 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
79 |
if (!applicationDirectory.exists()) {
|
80 |
throw new InstallPackageServiceException( |
81 |
"application_directory_not_found");
|
82 |
} |
83 |
if (!packageInfoFileMap.containsKey(packageInfo)) {
|
84 |
throw new InstallPackageServiceException("package_not_found"); |
85 |
} |
86 |
|
87 |
InputStream bundleStream = null; |
88 |
|
89 |
try {
|
90 |
File file = packageInfoFileMap.get(packageInfo);
|
91 |
bundleStream = new FileInputStream(file); |
92 |
} catch (FileNotFoundException e) { |
93 |
throw new InstallPackageServiceException("not_uri_selected"); |
94 |
} |
95 |
|
96 |
InstallPackageProvider installerExecutionProvider = |
97 |
createProvider(packageInfo); |
98 |
|
99 |
InputStream packageStream =
|
100 |
installerProviderServices.searchPackage(bundleStream, |
101 |
zipEntriesMap.get(packageInfo)); |
102 |
installerExecutionProvider.install(applicationDirectory, packageStream); |
103 |
} |
104 |
|
105 |
public void installPackage(File applicationDirectory, String packageCode) |
106 |
throws InstallPackageServiceException {
|
107 |
PackageInfo packageInfo = getPackageInfo(packageCode); |
108 |
if (packageInfo == null) { |
109 |
throw new InstallPackageServiceException( |
110 |
"The package doesn't exist");
|
111 |
} |
112 |
installPackage(applicationDirectory, packageInfo); |
113 |
} |
114 |
|
115 |
private InstallPackageProvider createProvider(PackageInfo packageInfo)
|
116 |
throws InstallPackageServiceException {
|
117 |
InstallerProviderManager installerProviderManager = |
118 |
(InstallerProviderManager) ((DefaultInstallerManager) manager) |
119 |
.getProviderManager(); |
120 |
|
121 |
try {
|
122 |
return installerProviderManager.createExecutionProvider(packageInfo
|
123 |
.getType()); |
124 |
} catch (ServiceException e) {
|
125 |
throw new InstallPackageServiceException( |
126 |
"Error creating the provider", e);
|
127 |
} |
128 |
} |
129 |
|
130 |
public PackageInfo getPackageInfo(int index) { |
131 |
if (index >= packageInfos.size()) {
|
132 |
return null; |
133 |
} |
134 |
return packageInfos.get(index);
|
135 |
} |
136 |
|
137 |
public PackageInfo getPackageInfo(String packageCode) { |
138 |
for (int i = 0; i < getPackageCount(); i++) { |
139 |
if (packageInfos.get(i).getCode().equals(packageCode)) {
|
140 |
return packageInfos.get(i);
|
141 |
} |
142 |
} |
143 |
return null; |
144 |
} |
145 |
|
146 |
public void addBundle(File bundle) throws InstallPackageServiceException { |
147 |
if (!bundle.exists()) {
|
148 |
throw new InstallPackageServiceException( |
149 |
"Only an existing file is supported");
|
150 |
} |
151 |
try {
|
152 |
int packageInfoCount = packageInfos.size();
|
153 |
installerProviderServices.readPackageInfo(new FileInputStream( |
154 |
bundle), packageInfos, zipEntriesMap); |
155 |
for (int i = packageInfoCount; i < packageInfos.size(); i++) { |
156 |
packageInfoFileMap.put(packageInfos.get(i), bundle); |
157 |
} |
158 |
} catch (FileNotFoundException e) { |
159 |
logger.error("File not found", e);
|
160 |
} |
161 |
} |
162 |
|
163 |
public void addBundlesFromDirectory(File directory) |
164 |
throws InstallPackageServiceException {
|
165 |
if (!directory.isDirectory()) {
|
166 |
throw new InstallPackageServiceException( |
167 |
"The application directory has to be a directory");
|
168 |
} |
169 |
File[] files = directory.listFiles(); |
170 |
for (int i = 0; i < files.length; i++) { |
171 |
if (files[i].isFile()) {
|
172 |
addBundle(files[i]); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
public int getPackageCount() { |
178 |
if (packageInfos == null) { |
179 |
return 0; |
180 |
} |
181 |
return packageInfos.size();
|
182 |
} |
183 |
|
184 |
public Manager getManager() {
|
185 |
return this.manager; |
186 |
} |
187 |
|
188 |
} |