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 @ 33982
History | View | Annotate | Download (8.21 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.BufferedInputStream; |
31 |
import java.io.File; |
32 |
import java.io.FileInputStream; |
33 |
import java.io.FileNotFoundException; |
34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
36 |
import java.net.URL; |
37 |
import java.util.ArrayList; |
38 |
import java.util.HashMap; |
39 |
import java.util.List; |
40 |
import java.util.Map; |
41 |
|
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
import org.gvsig.installer.lib.api.PackageInfo; |
46 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
47 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
48 |
import org.gvsig.installer.lib.impl.DefaultInstallerManager; |
49 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
50 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
51 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
52 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
53 |
import org.gvsig.tools.service.Manager; |
54 |
import org.gvsig.tools.service.ServiceException; |
55 |
|
56 |
/**
|
57 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
58 |
*/
|
59 |
public class DefaultInstallPackageService implements InstallPackageService { |
60 |
|
61 |
private Map<PackageInfo, File> packageInfoFileMap = null; |
62 |
private Map<PackageInfo, String> zipEntriesMap = null; |
63 |
private List<PackageInfo> packageInfos = null; |
64 |
private Manager manager;
|
65 |
private static final Logger logger = LoggerFactory |
66 |
.getLogger(DefaultInstallPackageService.class); |
67 |
private InstallPackageProviderServices installerProviderServices = null; |
68 |
|
69 |
public DefaultInstallPackageService(DefaultInstallerManager manager) {
|
70 |
super();
|
71 |
this.manager = manager;
|
72 |
packageInfoFileMap = new HashMap<PackageInfo, File>(); |
73 |
packageInfos = new ArrayList<PackageInfo>(); |
74 |
zipEntriesMap = new HashMap<PackageInfo, String>(); |
75 |
installerProviderServices = |
76 |
InstallerProviderLocator.getProviderManager() |
77 |
.createInstallerProviderServices(); |
78 |
} |
79 |
|
80 |
public void installPackage(File applicationDirectory, |
81 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
82 |
if (!applicationDirectory.exists()) {
|
83 |
throw new InstallPackageServiceException( |
84 |
"application_directory_not_found");
|
85 |
} |
86 |
if (!packageInfoFileMap.containsKey(packageInfo)) {
|
87 |
throw new InstallPackageServiceException("package_not_found"); |
88 |
} |
89 |
|
90 |
InputStream bundleStream = null; |
91 |
|
92 |
try {
|
93 |
File file = packageInfoFileMap.get(packageInfo);
|
94 |
bundleStream = new FileInputStream(file); |
95 |
} catch (FileNotFoundException e) { |
96 |
throw new InstallPackageServiceException("not_uri_selected"); |
97 |
} |
98 |
|
99 |
InstallPackageProvider installerExecutionProvider = |
100 |
createProvider(packageInfo); |
101 |
|
102 |
if (packageInfo.getDownloadURL() == null) { |
103 |
InputStream packageStream =
|
104 |
installerProviderServices.searchPackage(bundleStream, |
105 |
zipEntriesMap.get(packageInfo)); |
106 |
installerExecutionProvider.install(applicationDirectory, |
107 |
packageStream); |
108 |
} else {
|
109 |
File downloadedBundle = downloadFile(packageInfo.getDownloadURL());
|
110 |
try {
|
111 |
FileInputStream fis = new FileInputStream(downloadedBundle); |
112 |
BufferedInputStream bis = new BufferedInputStream(fis); |
113 |
installerExecutionProvider.install(applicationDirectory, bis); |
114 |
bis.close(); |
115 |
fis.close(); |
116 |
} catch (FileNotFoundException e) { |
117 |
throw new InstallPackageServiceException( |
118 |
"Downloaded bundle file not found: " + downloadedBundle, e);
|
119 |
} catch (IOException e) { |
120 |
throw new InstallPackageServiceException( |
121 |
"IO error installing the downloaded bundle: "
|
122 |
+ downloadedBundle, e); |
123 |
} |
124 |
} |
125 |
|
126 |
} |
127 |
|
128 |
public void installPackage(File applicationDirectory, String packageCode) |
129 |
throws InstallPackageServiceException {
|
130 |
PackageInfo packageInfo = getPackageInfo(packageCode); |
131 |
if (packageInfo == null) { |
132 |
throw new InstallPackageServiceException( |
133 |
"The package doesn't exist");
|
134 |
} |
135 |
installPackage(applicationDirectory, packageInfo); |
136 |
} |
137 |
|
138 |
private InstallPackageProvider createProvider(PackageInfo packageInfo)
|
139 |
throws InstallPackageServiceException {
|
140 |
InstallerProviderManager installerProviderManager = |
141 |
(InstallerProviderManager) ((DefaultInstallerManager) manager) |
142 |
.getProviderManager(); |
143 |
|
144 |
try {
|
145 |
return installerProviderManager.createExecutionProvider(packageInfo
|
146 |
.getType()); |
147 |
} catch (ServiceException e) {
|
148 |
throw new InstallPackageServiceException( |
149 |
"Error creating the provider", e);
|
150 |
} |
151 |
} |
152 |
|
153 |
public PackageInfo getPackageInfo(int index) { |
154 |
if (index >= packageInfos.size()) {
|
155 |
return null; |
156 |
} |
157 |
return packageInfos.get(index);
|
158 |
} |
159 |
|
160 |
public PackageInfo getPackageInfo(String packageCode) { |
161 |
for (int i = 0; i < getPackageCount(); i++) { |
162 |
if (packageInfos.get(i).getCode().equals(packageCode)) {
|
163 |
return packageInfos.get(i);
|
164 |
} |
165 |
} |
166 |
return null; |
167 |
} |
168 |
|
169 |
public void addBundle(File bundle) throws InstallPackageServiceException { |
170 |
if (!bundle.exists()) {
|
171 |
throw new InstallPackageServiceException( |
172 |
"Only an existing file is supported");
|
173 |
} |
174 |
try {
|
175 |
int packageInfoCount = packageInfos.size();
|
176 |
installerProviderServices.readPackageInfo(new FileInputStream( |
177 |
bundle), packageInfos, zipEntriesMap); |
178 |
for (int i = packageInfoCount; i < packageInfos.size(); i++) { |
179 |
packageInfoFileMap.put(packageInfos.get(i), bundle); |
180 |
} |
181 |
} catch (FileNotFoundException e) { |
182 |
logger.error("File not found", e);
|
183 |
} |
184 |
} |
185 |
|
186 |
public void addBundle(URL bundleURL) throws InstallPackageServiceException { |
187 |
File bundle = downloadFile(bundleURL);
|
188 |
addBundle(bundle); |
189 |
} |
190 |
|
191 |
private File downloadFile(URL bundleURL) |
192 |
throws InstallPackageServiceException {
|
193 |
try {
|
194 |
return InstallerProviderLocator.getProviderManager()
|
195 |
.createInstallerProviderServices().downloadFile(bundleURL); |
196 |
} catch (IOException e) { |
197 |
throw new InstallPackageServiceException( |
198 |
"Bundle file download error from URL: " + bundleURL, e);
|
199 |
} |
200 |
} |
201 |
|
202 |
public void addBundlesFromDirectory(File directory) |
203 |
throws InstallPackageServiceException {
|
204 |
if (!directory.isDirectory()) {
|
205 |
throw new InstallPackageServiceException( |
206 |
"The application directory has to be a directory");
|
207 |
} |
208 |
File[] files = directory.listFiles(); |
209 |
for (int i = 0; i < files.length; i++) { |
210 |
if (files[i].isFile()) {
|
211 |
addBundle(files[i]); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
public int getPackageCount() { |
217 |
if (packageInfos == null) { |
218 |
return 0; |
219 |
} |
220 |
return packageInfos.size();
|
221 |
} |
222 |
|
223 |
public Manager getManager() {
|
224 |
return this.manager; |
225 |
} |
226 |
|
227 |
} |