root / 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 / creation / DefaultInstallerCreationService.java @ 32516
History | View | Annotate | Download (12.3 KB)
1 | 32269 | jpiera | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 32285 | jpiera | *
|
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 | 32269 | jpiera | |
23 | /*
|
||
24 | 32285 | jpiera | * AUTHORS (In addition to CIT):
|
25 | * 2010 {Prodevelop} {Task}
|
||
26 | */
|
||
27 | |||
28 | 32269 | jpiera | package org.gvsig.installer.lib.impl.creation; |
29 | |||
30 | 32411 | jpiera | import java.io.BufferedReader; |
31 | 32296 | jpiera | import java.io.ByteArrayInputStream; |
32 | 32269 | jpiera | import java.io.File; |
33 | 32285 | jpiera | import java.io.FileInputStream; |
34 | 32290 | jpiera | import java.io.FileOutputStream; |
35 | 32411 | jpiera | import java.io.FileReader; |
36 | 32290 | jpiera | import java.io.IOException; |
37 | import java.io.InputStream; |
||
38 | 32269 | jpiera | import java.io.OutputStream; |
39 | 32411 | jpiera | import java.util.ArrayList; |
40 | import java.util.HashMap; |
||
41 | import java.util.List; |
||
42 | import java.util.Map; |
||
43 | 32269 | jpiera | |
44 | import org.gvsig.installer.lib.api.InstallerInfo; |
||
45 | import org.gvsig.installer.lib.api.creation.InstallerCreationService; |
||
46 | import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException; |
||
47 | import org.gvsig.installer.lib.impl.DefaultInstallerInfo; |
||
48 | 32285 | jpiera | import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
49 | 32269 | jpiera | import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter; |
50 | 32498 | jpiera | import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
51 | import org.gvsig.installer.lib.spi.InstallerProviderServices; |
||
52 | 32400 | jpiera | import org.gvsig.tools.service.Manager; |
53 | 32296 | jpiera | import org.slf4j.Logger; |
54 | 32269 | jpiera | |
55 | /**
|
||
56 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
||
57 | */
|
||
58 | 32411 | jpiera | public class DefaultInstallerCreationService implements InstallerCreationService { |
59 | 32296 | jpiera | public static final String ANT_FILE_NAME = "install.xml"; |
60 | 32290 | jpiera | public static final String COPIED_FILES_DIRECTORY_NAME = "files"; |
61 | 32411 | jpiera | private File applicationDirectory; |
62 | private File pluginsDirectory; |
||
63 | 32296 | jpiera | private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class); |
64 | 32400 | jpiera | private Manager manager = null; |
65 | 32411 | jpiera | private List<InstallerInfo> installerInfos = null; |
66 | private Map<InstallerInfo, String> directories = null; |
||
67 | 32501 | jpiera | private InstallerInfo pluginToInstall = null; |
68 | 32423 | jpiera | protected List<File> selectedFiles = null; |
69 | protected String antScript = null; |
||
70 | 32498 | jpiera | private InstallerProviderServices installerProviderServices = null; |
71 | |||
72 | 32400 | jpiera | public DefaultInstallerCreationService(Manager manager) {
|
73 | super();
|
||
74 | this.manager = manager;
|
||
75 | 32411 | jpiera | installerInfos = new ArrayList<InstallerInfo>(); |
76 | directories = new HashMap<InstallerInfo, String>(); |
||
77 | 32423 | jpiera | selectedFiles = new ArrayList<File>(); |
78 | 32498 | jpiera | installerProviderServices = InstallerProviderLocator.getProviderManager().createInstallerProviderServices(); |
79 | 32400 | jpiera | } |
80 | 32269 | jpiera | |
81 | public void createInstaller(OutputStream installerStream) |
||
82 | 32285 | jpiera | throws InstallerCreationServiceException {
|
83 | 32411 | jpiera | if (applicationDirectory == null){ |
84 | throw new InstallerCreationServiceException("The application directory has to be set"); |
||
85 | 32269 | jpiera | } |
86 | 32290 | jpiera | |
87 | //Write the install.info file
|
||
88 | 32285 | jpiera | writeInstallInfo(); |
89 | |||
90 | 32411 | jpiera | //Create the ant file
|
91 | String antScript = getSelectedInstallerInfo().getAntScript();
|
||
92 | if (antScript != null){ |
||
93 | writeAntFile(antScript); |
||
94 | 32296 | jpiera | } |
95 | 32423 | jpiera | |
96 | 32411 | jpiera | //Copy the selected files
|
97 | writeSelectedFiles(); |
||
98 | 32296 | jpiera | |
99 | 32498 | jpiera | |
100 | 32451 | jpiera | InstallerInfo infInstallerInfo = getSelectedPlugin(); |
101 | String pluginFileName = getSelectedPlugin().getCode() + "-" + infInstallerInfo.getVersion() + "-" + infInstallerInfo.getBuild(); |
||
102 | 32498 | jpiera | installerProviderServices.compress(getAbsoluteSelectePluginFile(), pluginFileName, installerStream); |
103 | 32269 | jpiera | } |
104 | 32285 | jpiera | |
105 | 32296 | jpiera | |
106 | 32516 | jpiera | private void writeInstallInfo() throws InstallerCreationServiceException{ |
107 | installerProviderServices.writeInstallInfo(getAbsoluteSelectePluginFile(), getSelectedInstallerInfo()); |
||
108 | 32296 | jpiera | } |
109 | |||
110 | 32290 | jpiera | private void writeSelectedFiles() throws InstallerCreationServiceException { |
111 | try{
|
||
112 | String copiedFilesDirectory = getCopiedFilesDirectoryName();
|
||
113 | |||
114 | //Deleting the previous files folder
|
||
115 | File copiedFilesDirectoryfile = new File(copiedFilesDirectory); |
||
116 | deleteDirectory(copiedFilesDirectoryfile); |
||
117 | 32431 | jpiera | InstallerInfo installerInfo = getSelectedInstallerInfo(); |
118 | for (int i=0 ; i<installerInfo.getFileToCopySize() ; i++){ |
||
119 | String destFile = installerInfo.getFileToCopyAt(i).getAbsolutePath();
|
||
120 | 32423 | jpiera | |
121 | 32411 | jpiera | String sourceFile = applicationDirectory + File.separator + destFile; |
122 | 32423 | jpiera | |
123 | 32290 | jpiera | //Create the final path
|
124 | 32411 | jpiera | destFile = pluginsDirectory + File.separator + copiedFilesDirectory + File.separator + destFile; |
125 | 32290 | jpiera | |
126 | 32423 | jpiera | |
127 | 32290 | jpiera | //Copy the files
|
128 | 32411 | jpiera | copy(new File(sourceFile), new File(destFile)); |
129 | 32290 | jpiera | } |
130 | }catch(IOException e){ |
||
131 | throw new InstallerCreationServiceException("Exception copying the files"); |
||
132 | } |
||
133 | 32411 | jpiera | } |
134 | |||
135 | private void writeAntFile(String content) throws InstallerCreationServiceException { |
||
136 | try{
|
||
137 | ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes()); |
||
138 | 32423 | jpiera | OutputStream out = new FileOutputStream(getAbsoluteAntFile()); |
139 | 32411 | jpiera | |
140 | // Copy the bits from instream to outstream
|
||
141 | byte[] buf = new byte[1024]; |
||
142 | int len;
|
||
143 | while ((len = in.read(buf)) > 0) { |
||
144 | out.write(buf, 0, len);
|
||
145 | } |
||
146 | in.close(); |
||
147 | out.close(); |
||
148 | }catch(IOException e){ |
||
149 | throw new InstallerCreationServiceException("Exception writing the ant file"); |
||
150 | } |
||
151 | 32290 | jpiera | } |
152 | |||
153 | 32411 | jpiera | |
154 | 32290 | jpiera | static public boolean deleteDirectory(File path) { |
155 | if( path.exists() ) {
|
||
156 | File[] files = path.listFiles(); |
||
157 | for(int i=0; i<files.length; i++) { |
||
158 | if(files[i].isDirectory()) {
|
||
159 | deleteDirectory(files[i]); |
||
160 | } |
||
161 | else {
|
||
162 | files[i].delete(); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | return( path.delete() );
|
||
167 | } |
||
168 | |||
169 | |||
170 | void copy(File sourceLocation , File targetLocation) throws IOException { |
||
171 | if (sourceLocation.isDirectory()) {
|
||
172 | if (!targetLocation.exists()) {
|
||
173 | targetLocation.mkdir(); |
||
174 | } |
||
175 | |||
176 | String[] children = sourceLocation.list(); |
||
177 | for (int i=0; i<children.length; i++) { |
||
178 | copy(new File(sourceLocation, children[i]), |
||
179 | new File(targetLocation, children[i])); |
||
180 | } |
||
181 | } else {
|
||
182 | targetLocation.getParentFile().mkdirs(); |
||
183 | |||
184 | InputStream in = new FileInputStream(sourceLocation); |
||
185 | OutputStream out = new FileOutputStream(targetLocation); |
||
186 | |||
187 | // Copy the bits from instream to outstream
|
||
188 | byte[] buf = new byte[1024]; |
||
189 | int len;
|
||
190 | while ((len = in.read(buf)) > 0) { |
||
191 | out.write(buf, 0, len);
|
||
192 | } |
||
193 | in.close(); |
||
194 | out.close(); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | 32411 | jpiera | public void setApplicationDirectory(File applicationDirectory) throws InstallerCreationServiceException { |
199 | if (!applicationDirectory.isDirectory()){
|
||
200 | throw new InstallerCreationServiceException("The application directory has to be a directory"); |
||
201 | 32269 | jpiera | } |
202 | 32411 | jpiera | this.applicationDirectory = applicationDirectory;
|
203 | 32285 | jpiera | |
204 | 32411 | jpiera | this.pluginsDirectory = new File(applicationDirectory.getAbsoluteFile() + File.separator + "gvSIG" + File.separator + "extensiones") ; |
205 | if ((!pluginsDirectory.exists()) || (!pluginsDirectory.isDirectory())){
|
||
206 | throw new InstallerCreationServiceException("The plugins directory does't exists or not is a directory"); |
||
207 | 32285 | jpiera | } |
208 | 32411 | jpiera | |
209 | //Read all the installed plugins
|
||
210 | String[] plugins = pluginsDirectory.list(); |
||
211 | 32498 | jpiera | |
212 | 32411 | jpiera | if (plugins != null) { |
213 | for (int i=0; i<plugins.length; i++) |
||
214 | { |
||
215 | String pluginDirectoryName = plugins[i];
|
||
216 | File pluginDirectory = getAbsolutePluginFile(pluginDirectoryName);
|
||
217 | DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
|
||
218 | 32498 | jpiera | |
219 | installerProviderServices.readInstallInfo(new File(pluginDirectory.getAbsolutePath()), installerInfo); |
||
220 | |||
221 | 32423 | jpiera | //Checks if the ant file exists
|
222 | File antFile = new File(getAntFileName(pluginDirectory.getAbsolutePath())); |
||
223 | if (antFile.exists()){
|
||
224 | try {
|
||
225 | installerInfo.setAnScript(readFileAsString(antFile.getAbsolutePath())); |
||
226 | } catch (IOException e) { |
||
227 | logger.error("Not possible to read the ant file");
|
||
228 | } |
||
229 | } |
||
230 | 32411 | jpiera | installerInfos.add(installerInfo); |
231 | directories.put(installerInfo, pluginDirectoryName); |
||
232 | } |
||
233 | } |
||
234 | 32269 | jpiera | } |
235 | |||
236 | 32411 | jpiera | private InstallerInfo getSelectedInstallerInfo() throws InstallerCreationServiceException{ |
237 | 32501 | jpiera | if (pluginToInstall != null){ |
238 | return pluginToInstall;
|
||
239 | 32411 | jpiera | } |
240 | throw new InstallerCreationServiceException("There is not a plugin selected"); |
||
241 | } |
||
242 | 32290 | jpiera | |
243 | 32411 | jpiera | private File getAbsoluteSelectePluginFile() throws InstallerCreationServiceException{ |
244 | return getAbsolutePluginFile(directories.get(getSelectedInstallerInfo()));
|
||
245 | 32269 | jpiera | } |
246 | 32423 | jpiera | |
247 | 32411 | jpiera | private File getAbsolutePluginFile(String selectePluginName) throws InstallerCreationServiceException{ |
248 | return new File(pluginsDirectory.getAbsolutePath() + File.separator + selectePluginName); |
||
249 | } |
||
250 | 32423 | jpiera | |
251 | 32411 | jpiera | private String getPluginToInstallDirectory() throws InstallerCreationServiceException{ |
252 | return directories.get(getSelectedInstallerInfo());
|
||
253 | } |
||
254 | 32423 | jpiera | |
255 | private File getAbsoluteAntFile() throws InstallerCreationServiceException{ |
||
256 | return new File(pluginsDirectory.getAbsolutePath() + File.separator + ANT_FILE_NAME); |
||
257 | } |
||
258 | 32269 | jpiera | |
259 | 32411 | jpiera | private String getCopiedFilesDirectoryName() throws InstallerCreationServiceException{ |
260 | return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
|
||
261 | 32290 | jpiera | } |
262 | |||
263 | 32411 | jpiera | private String getCopiedFilesDirectoryName(String pluginDirectory) throws InstallerCreationServiceException{ |
264 | return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME; |
||
265 | 32500 | jpiera | } |
266 | 32291 | jpiera | |
267 | 32411 | jpiera | private String getAntFileName(String pluginDirectory) throws InstallerCreationServiceException{ |
268 | 32423 | jpiera | return pluginDirectory + File.separator + ANT_FILE_NAME; |
269 | 32411 | jpiera | } |
270 | 32296 | jpiera | |
271 | |||
272 | 32411 | jpiera | private void loadAntFile(File antFile) throws IOException, InstallerCreationServiceException { |
273 | 32296 | jpiera | StringBuffer fileData = new StringBuffer(); |
274 | FileInputStream fis = new FileInputStream(antFile); |
||
275 | byte[] buf = new byte[1024]; |
||
276 | int numRead = 0; |
||
277 | while ((numRead = fis.read(buf)) != -1) { |
||
278 | String readData = new String(buf, 0, numRead); |
||
279 | fileData.append(readData); |
||
280 | buf = new byte[1024]; |
||
281 | } |
||
282 | fis.close(); |
||
283 | 32411 | jpiera | getSelectedInstallerInfo().setAnScript(fileData.toString()); |
284 | 32296 | jpiera | } |
285 | |||
286 | 32411 | jpiera | private void loadCopiedFiles(File file) throws InstallerCreationServiceException{ |
287 | 32293 | jpiera | if (file.isDirectory()){
|
288 | File[] files = file.listFiles(); |
||
289 | for (int i=0 ; i<files.length ; i++){ |
||
290 | loadCopiedFiles(files[i]); |
||
291 | } |
||
292 | }else{
|
||
293 | //Removing the plugin prefix
|
||
294 | String pluginFile = file.getAbsolutePath().substring(getCopiedFilesDirectoryName().length(), file.getAbsolutePath().length());
|
||
295 | 32296 | jpiera | |
296 | 32293 | jpiera | //Ading the root directory
|
297 | 32411 | jpiera | pluginFile = new File(getPluginToInstallDirectory()).getParentFile().getAbsolutePath() + pluginFile; |
298 | 32296 | jpiera | |
299 | 32411 | jpiera | getSelectedInstallerInfo().addFileToCopy(new File(pluginFile)); |
300 | 32293 | jpiera | } |
301 | } |
||
302 | 32296 | jpiera | |
303 | 32400 | jpiera | public Manager getManager() {
|
304 | return this.manager; |
||
305 | } |
||
306 | |||
307 | 32411 | jpiera | public InstallerInfo getPluginInfoAt(int index) { |
308 | return installerInfos.get(index);
|
||
309 | } |
||
310 | |||
311 | public int getPluginsSize() { |
||
312 | return installerInfos.size();
|
||
313 | } |
||
314 | |||
315 | public void setSelectedPlugin(int index) throws InstallerCreationServiceException { |
||
316 | 32451 | jpiera | if (index > installerInfos.size()){
|
317 | throw new InstallerCreationServiceException("The plugin doesn't exist"); |
||
318 | } |
||
319 | 32501 | jpiera | pluginToInstall = installerInfos.get(index); |
320 | 32411 | jpiera | } |
321 | |||
322 | 32451 | jpiera | public void setSelectedPlugin(String code) throws InstallerCreationServiceException { |
323 | int selectedCode = -1;; |
||
324 | 32411 | jpiera | for (int i=0 ; i<getPluginsSize() ; i++){ |
325 | if (installerInfos.get(i).getCode().equals(code)){
|
||
326 | 32451 | jpiera | selectedCode = i; |
327 | break;
|
||
328 | 32411 | jpiera | } |
329 | } |
||
330 | 32451 | jpiera | if (selectedCode == -1){ |
331 | throw new InstallerCreationServiceException("The plugin doesn't exist"); |
||
332 | } |
||
333 | 32501 | jpiera | pluginToInstall = installerInfos.get(selectedCode); |
334 | 32411 | jpiera | } |
335 | |||
336 | 32451 | jpiera | public InstallerInfo getSelectedPlugin() {
|
337 | try {
|
||
338 | return getSelectedInstallerInfo();
|
||
339 | } catch (InstallerCreationServiceException e) {
|
||
340 | //there is not a selected plugin
|
||
341 | return null; |
||
342 | } |
||
343 | 32411 | jpiera | } |
344 | |||
345 | public String getDefaultAntScript() throws InstallerCreationServiceException { |
||
346 | try {
|
||
347 | return readFileAsString(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile());
|
||
348 | } catch (IOException e) { |
||
349 | throw new InstallerCreationServiceException("Impossible to read the default ant file", e); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | 32423 | jpiera | private String readFileAsString(String filePath) |
354 | throws java.io.IOException{
|
||
355 | StringBuffer fileData = new StringBuffer(1000); |
||
356 | BufferedReader reader = new BufferedReader( |
||
357 | new FileReader(filePath)); |
||
358 | char[] buf = new char[1024]; |
||
359 | int numRead=0; |
||
360 | while((numRead=reader.read(buf)) != -1){ |
||
361 | String readData = String.valueOf(buf, 0, numRead); |
||
362 | fileData.append(readData); |
||
363 | buf = new char[1024]; |
||
364 | } |
||
365 | reader.close(); |
||
366 | return fileData.toString();
|
||
367 | } |
||
368 | 32269 | jpiera | } |