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 @ 33732
History | View | Annotate | Download (6.69 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.net.URI; |
35 |
import java.util.ArrayList; |
36 |
import java.util.HashMap; |
37 |
import java.util.List; |
38 |
import java.util.Map; |
39 |
|
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
import org.gvsig.installer.lib.api.PackageInfo; |
44 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
45 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
46 |
import org.gvsig.installer.lib.impl.DefaultInstallerManager; |
47 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
48 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
49 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
50 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
51 |
import org.gvsig.tools.service.Manager; |
52 |
import org.gvsig.tools.service.ServiceException; |
53 |
|
54 |
/**
|
55 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
56 |
*/
|
57 |
public class DefaultInstallPackageService implements InstallPackageService { |
58 |
|
59 |
private Map<PackageInfo, URI> packageInfoUriMap = null; |
60 |
private Map<PackageInfo, String> zipEntriesMap = null; |
61 |
private List<PackageInfo> packageInfos = null; |
62 |
private Manager manager;
|
63 |
private static final Logger logger = LoggerFactory |
64 |
.getLogger(DefaultInstallPackageService.class); |
65 |
private InstallPackageProviderServices installerProviderServices = null; |
66 |
|
67 |
public DefaultInstallPackageService(DefaultInstallerManager manager) {
|
68 |
super();
|
69 |
this.manager = manager;
|
70 |
packageInfoUriMap = new HashMap<PackageInfo, URI>(); |
71 |
packageInfos = new ArrayList<PackageInfo>(); |
72 |
zipEntriesMap = new HashMap<PackageInfo, String>(); |
73 |
installerProviderServices = |
74 |
InstallerProviderLocator.getProviderManager() |
75 |
.createInstallerProviderServices(); |
76 |
} |
77 |
|
78 |
public void installPackage(File applicationDirectory, |
79 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
80 |
if (!applicationDirectory.exists()) {
|
81 |
throw new InstallPackageServiceException( |
82 |
"application_directory_not_found");
|
83 |
} |
84 |
if (!packageInfoUriMap.containsKey(packageInfo)) {
|
85 |
throw new InstallPackageServiceException("package_not_found"); |
86 |
} |
87 |
|
88 |
InputStream bundleStream = null; |
89 |
|
90 |
try {
|
91 |
File file = new File(packageInfoUriMap.get(packageInfo)); |
92 |
bundleStream = new FileInputStream(file); |
93 |
} catch (FileNotFoundException e) { |
94 |
throw new InstallPackageServiceException("not_uri_selected"); |
95 |
} |
96 |
|
97 |
InstallPackageProvider installerExecutionProvider = |
98 |
createProvider(packageInfo); |
99 |
|
100 |
InputStream packageStream =
|
101 |
installerProviderServices.searchPackage(bundleStream, |
102 |
zipEntriesMap.get(packageInfo)); |
103 |
installerExecutionProvider.install(applicationDirectory, packageStream); |
104 |
} |
105 |
|
106 |
public void installPackage(File applicationDirectory, String packageCode) |
107 |
throws InstallPackageServiceException {
|
108 |
PackageInfo packageInfo = getPackageInfo(packageCode); |
109 |
if (packageInfo == null) { |
110 |
throw new InstallPackageServiceException( |
111 |
"The package doesn't exist");
|
112 |
} |
113 |
installPackage(applicationDirectory, packageInfo); |
114 |
} |
115 |
|
116 |
private InstallPackageProvider createProvider(PackageInfo packageInfo)
|
117 |
throws InstallPackageServiceException {
|
118 |
InstallerProviderManager installerProviderManager = |
119 |
(InstallerProviderManager) ((DefaultInstallerManager) manager) |
120 |
.getProviderManager(); |
121 |
|
122 |
try {
|
123 |
return installerProviderManager.createExecutionProvider(packageInfo
|
124 |
.getType()); |
125 |
} catch (ServiceException e) {
|
126 |
throw new InstallPackageServiceException( |
127 |
"Error creating the provider", e);
|
128 |
} |
129 |
} |
130 |
|
131 |
public PackageInfo getPackageInfo(int index) { |
132 |
if (index >= packageInfos.size()) {
|
133 |
return null; |
134 |
} |
135 |
return packageInfos.get(index);
|
136 |
} |
137 |
|
138 |
public PackageInfo getPackageInfo(String packageCode) { |
139 |
for (int i = 0; i < getPackageCount(); i++) { |
140 |
if (packageInfos.get(i).getCode().equals(packageCode)) {
|
141 |
return packageInfos.get(i);
|
142 |
} |
143 |
} |
144 |
return null; |
145 |
} |
146 |
|
147 |
public void addBundle(URI inputURI) throws InstallPackageServiceException { |
148 |
File file = new File(inputURI); |
149 |
if (!file.exists()) {
|
150 |
throw new InstallPackageServiceException( |
151 |
"Only URI form existing file is supported");
|
152 |
} |
153 |
try {
|
154 |
int packageInfoCount = packageInfos.size();
|
155 |
installerProviderServices.readPackageInfo( |
156 |
new FileInputStream(file), packageInfos, zipEntriesMap); |
157 |
for (int i = packageInfoCount; i < packageInfos.size(); i++) { |
158 |
packageInfoUriMap.put(packageInfos.get(i), inputURI); |
159 |
} |
160 |
} catch (FileNotFoundException e) { |
161 |
logger.error("File not found", e);
|
162 |
} |
163 |
} |
164 |
|
165 |
public void addBundlesFromDirectory(File directory) |
166 |
throws InstallPackageServiceException {
|
167 |
if (!directory.isDirectory()) {
|
168 |
throw new InstallPackageServiceException( |
169 |
"The application directory has to be a directory");
|
170 |
} |
171 |
File[] files = directory.listFiles(); |
172 |
for (int i = 0; i < files.length; i++) { |
173 |
addBundle(files[i].toURI()); |
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 |
} |