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 / DefaultInstallerExecutionService.java @ 32500
History | View | Annotate | Download (7.19 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.gvsig.installer.lib.api.InstallerInfo; |
40 |
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException; |
41 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionService; |
42 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException; |
43 |
import org.gvsig.installer.lib.impl.DefaultInstallerManager; |
44 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
45 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
46 |
import org.gvsig.installer.lib.spi.InstallerProviderServices; |
47 |
import org.gvsig.installer.lib.spi.execution.InstallerExecutionProvider; |
48 |
import org.gvsig.tools.service.Manager; |
49 |
import org.gvsig.tools.service.ServiceException; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
/**
|
54 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
55 |
*/
|
56 |
public class DefaultInstallerExecutionService implements InstallerExecutionService{ |
57 |
private Map<InstallerInfo, File> installFilesMap = null; |
58 |
private Map<InstallerInfo, String> zipEntriesMap = null; |
59 |
private List<InstallerInfo> installerInfos = null; |
60 |
private Manager manager;
|
61 |
private int pluginToInstallIndex = -1; |
62 |
private File applicationDirectory; |
63 |
private static final Logger logger = LoggerFactory.getLogger(DefaultInstallerExecutionService.class); |
64 |
private InstallerProviderServices installerProviderServices = null; |
65 |
|
66 |
public DefaultInstallerExecutionService(DefaultInstallerManager manager) {
|
67 |
super();
|
68 |
this.manager = manager;
|
69 |
installFilesMap = new HashMap<InstallerInfo, File>(); |
70 |
installerInfos = new ArrayList<InstallerInfo>(); |
71 |
zipEntriesMap = new HashMap<InstallerInfo, String>(); |
72 |
installerProviderServices = InstallerProviderLocator.getProviderManager().createInstallerProviderServices(); |
73 |
} |
74 |
|
75 |
public void executeInstaller() |
76 |
throws InstallerExecutionServiceException {
|
77 |
if (pluginToInstallIndex == -1){ |
78 |
throw new InstallerExecutionServiceException("not_installer_selected"); |
79 |
} |
80 |
if (!installFilesMap.containsKey(installerInfos.get(pluginToInstallIndex))){
|
81 |
throw new InstallerExecutionServiceException("file_not_found_use_execute_installer_with_stream"); |
82 |
} |
83 |
File file = installFilesMap.get(installerInfos.get(pluginToInstallIndex));
|
84 |
try {
|
85 |
executeInstaller(new FileInputStream(file)); |
86 |
} catch (FileNotFoundException e) { |
87 |
throw new InstallerExecutionServiceException("file_not_found"); |
88 |
} |
89 |
} |
90 |
|
91 |
public void executeInstaller(InputStream inputStream) throws InstallerExecutionServiceException{ |
92 |
if (pluginToInstallIndex == -1){ |
93 |
throw new InstallerExecutionServiceException("not_installer_selected"); |
94 |
} |
95 |
if ((applicationDirectory == null) || (!applicationDirectory.exists())){ |
96 |
throw new InstallerExecutionServiceException("not_valid_application_directory_selected"); |
97 |
} |
98 |
//Execute the installer
|
99 |
InstallerExecutionProvider installerExecutionProvider = createProvider(pluginToInstallIndex); |
100 |
InstallerInfo instalerInfo = installerInfos.get(pluginToInstallIndex); |
101 |
InputStream is = installerProviderServices.searchPlugin(inputStream,zipEntriesMap.get(instalerInfo));
|
102 |
installerExecutionProvider.install(applicationDirectory, is); |
103 |
} |
104 |
|
105 |
private InstallerExecutionProvider createProvider(int index) throws InstallerExecutionServiceException { |
106 |
InstallerProviderManager installerProviderManager = (InstallerProviderManager)((DefaultInstallerManager)manager).getProviderManager(); |
107 |
|
108 |
try {
|
109 |
return installerProviderManager.createExecutionProvider(installerInfos.get(index).getType());
|
110 |
} catch (ServiceException e) {
|
111 |
throw new InstallerExecutionServiceException("Error creating the provider", e); |
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
public InstallerInfo getPluginInfoAt(int index) { |
117 |
if (installerInfos == null){ |
118 |
return null; |
119 |
} |
120 |
return installerInfos.get(index);
|
121 |
} |
122 |
|
123 |
public void addInstaller(InputStream inputStream) throws InstallerExecutionServiceException { |
124 |
installerProviderServices.readInstallInfo(inputStream, installerInfos, zipEntriesMap); |
125 |
} |
126 |
|
127 |
public void setApplicationDirectory(File applicationDirectory) throws InstallerExecutionServiceException{ |
128 |
if (!applicationDirectory.isDirectory()){
|
129 |
throw new InstallerExecutionServiceException("The application directory has to be a directory"); |
130 |
} |
131 |
this.applicationDirectory = applicationDirectory;
|
132 |
} |
133 |
|
134 |
|
135 |
public void addInstallersFromInstallDirectory() |
136 |
throws InstallerExecutionServiceException {
|
137 |
if (applicationDirectory != null){ |
138 |
File installDirectory = getInstallDirectory();
|
139 |
if (installDirectory.exists()){
|
140 |
File[] files = installDirectory.listFiles(); |
141 |
int from = installerInfos.size();
|
142 |
for (int i=0 ; i<files.length ; i++){ |
143 |
try {
|
144 |
addInstaller(new FileInputStream(files[i])); |
145 |
} catch (FileNotFoundException e) { |
146 |
logger.error("File not found", e);
|
147 |
} |
148 |
} |
149 |
for (int i=0 ; i<files.length ; i++){ |
150 |
installFilesMap.put(installerInfos.get(from+i), files[i]); |
151 |
} |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
public void deleteInstallers() { |
157 |
installFilesMap.clear(); |
158 |
installerInfos.clear(); |
159 |
} |
160 |
|
161 |
|
162 |
|
163 |
private File getInstallDirectory(){ |
164 |
return new File(applicationDirectory.getAbsolutePath() + File.separator + "install"); |
165 |
} |
166 |
|
167 |
public int getPluginsSize() { |
168 |
if (installerInfos == null){ |
169 |
return 0; |
170 |
} |
171 |
return installerInfos.size();
|
172 |
} |
173 |
|
174 |
InstallerInfo getSelectedInstallerInfo(){ |
175 |
if (pluginToInstallIndex == -1){ |
176 |
return null; |
177 |
} |
178 |
return installerInfos.get(pluginToInstallIndex);
|
179 |
} |
180 |
|
181 |
public void setPluginToInstall(int index) throws InstallerExecutionServiceException{ |
182 |
if (index > installerInfos.size()){
|
183 |
throw new InstallerExecutionServiceException("The plugin doesn't exist"); |
184 |
} |
185 |
pluginToInstallIndex = index; |
186 |
} |
187 |
|
188 |
public Manager getManager() {
|
189 |
return this.manager; |
190 |
} |
191 |
|
192 |
public void setPluginToInstall(String code) throws InstallerExecutionServiceException { |
193 |
int selectedCode = -1; |
194 |
for (int i=0 ; i<getPluginsSize() ; i++){ |
195 |
if (installerInfos.get(i).getCode().equals(code)){
|
196 |
selectedCode = i; |
197 |
break;
|
198 |
} |
199 |
} |
200 |
if (selectedCode == -1){ |
201 |
throw new InstallerExecutionServiceException("The plugin doesn't exist"); |
202 |
} |
203 |
pluginToInstallIndex = selectedCode; |
204 |
} |
205 |
|
206 |
} |
207 |
|