Revision 32411 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
DefaultInstallerCreationService.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.installer.lib.impl.creation; |
29 | 29 |
|
30 |
import java.io.BufferedReader; |
|
30 | 31 |
import java.io.ByteArrayInputStream; |
31 | 32 |
import java.io.File; |
32 | 33 |
import java.io.FileInputStream; |
33 | 34 |
import java.io.FileOutputStream; |
35 |
import java.io.FileReader; |
|
34 | 36 |
import java.io.IOException; |
35 | 37 |
import java.io.InputStream; |
36 | 38 |
import java.io.OutputStream; |
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Collection; |
|
41 |
import java.util.HashMap; |
|
42 |
import java.util.Iterator; |
|
43 |
import java.util.List; |
|
44 |
import java.util.ListIterator; |
|
45 |
import java.util.Map; |
|
37 | 46 |
|
47 |
import javax.swing.JCheckBox; |
|
48 |
|
|
38 | 49 |
import org.gvsig.installer.lib.api.InstallerInfo; |
39 | 50 |
import org.gvsig.installer.lib.api.creation.InstallerCreationService; |
40 | 51 |
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException; |
... | ... | |
49 | 60 |
/** |
50 | 61 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a> |
51 | 62 |
*/ |
52 |
public class DefaultInstallerCreationService extends DefaultInstallerInfo implements InstallerCreationService {
|
|
63 |
public class DefaultInstallerCreationService implements InstallerCreationService { |
|
53 | 64 |
public static final String INSTALLINFO_FILE_NAME = "install.info"; |
54 | 65 |
public static final String ANT_FILE_NAME = "install.xml"; |
55 | 66 |
public static final String COPIED_FILES_DIRECTORY_NAME = "files"; |
56 |
private File pluginDirectory; |
|
67 |
private File applicationDirectory; |
|
68 |
private File pluginsDirectory; |
|
57 | 69 |
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class); |
58 | 70 |
private Manager manager = null; |
59 |
|
|
71 |
private List<InstallerInfo> installerInfos = null; |
|
72 |
private Map<InstallerInfo, String> directories = null; |
|
73 |
private int pluginToInstallIndex = -1; |
|
74 |
|
|
60 | 75 |
public DefaultInstallerCreationService(Manager manager) { |
61 | 76 |
super(); |
62 | 77 |
this.manager = manager; |
78 |
installerInfos = new ArrayList<InstallerInfo>(); |
|
79 |
directories = new HashMap<InstallerInfo, String>(); |
|
63 | 80 |
} |
64 | 81 |
|
65 | 82 |
public void createInstaller(OutputStream installerStream) |
66 | 83 |
throws InstallerCreationServiceException { |
67 |
if (pluginDirectory == null){
|
|
68 |
throw new InstallerCreationServiceException("The plugin directory has to be set");
|
|
84 |
if (applicationDirectory == null){
|
|
85 |
throw new InstallerCreationServiceException("The application directory has to be set");
|
|
69 | 86 |
} |
70 | 87 |
|
71 | 88 |
//Write the install.info file |
72 | 89 |
writeInstallInfo(); |
73 | 90 |
|
74 |
//if (isAdvancedMode){ |
|
75 |
//Create the ant file |
|
76 |
if (antScript != null){ |
|
77 |
writeAntFile(); |
|
78 |
}else{ |
|
79 |
File antFileModel = new File(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile()); |
|
80 |
try{ |
|
81 |
copy(antFileModel, new File(getAntFileName())); |
|
82 |
}catch(IOException e){ |
|
83 |
throw new InstallerCreationServiceException("Exception copying the ant file"); |
|
84 |
} |
|
85 |
//} |
|
86 |
|
|
87 |
//Copy the selected files |
|
88 |
writeSelectedFiles(); |
|
91 |
//Create the ant file |
|
92 |
String antScript = getSelectedInstallerInfo().getAntScript(); |
|
93 |
if (antScript != null){ |
|
94 |
writeAntFile(antScript); |
|
89 | 95 |
} |
96 |
|
|
97 |
//Copy the selected files |
|
98 |
writeSelectedFiles(); |
|
90 | 99 |
|
91 | 100 |
Compress compress = new Compress(); |
92 |
compress.compressPlugin(pluginDirectory, installerStream);
|
|
101 |
compress.compressPlugin(getAbsoluteSelectePluginFile(), installerStream);
|
|
93 | 102 |
} |
94 | 103 |
|
95 |
private void writeAntFile() throws InstallerCreationServiceException { |
|
96 |
try{ |
|
97 |
ByteArrayInputStream in = new ByteArrayInputStream(antScript.getBytes()); |
|
98 |
OutputStream out = new FileOutputStream(getAntFileName()); |
|
99 | 104 |
|
100 |
// Copy the bits from instream to outstream |
|
101 |
byte[] buf = new byte[1024]; |
|
102 |
int len; |
|
103 |
while ((len = in.read(buf)) > 0) { |
|
104 |
out.write(buf, 0, len); |
|
105 |
} |
|
106 |
in.close(); |
|
107 |
out.close(); |
|
108 |
}catch(IOException e){ |
|
109 |
throw new InstallerCreationServiceException("Exception writing the ant file"); |
|
110 |
} |
|
105 |
private void writeInstallInfo() throws InstallerCreationServiceException{ |
|
106 |
InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter(); |
|
107 |
installerInfoFileWriter.write(getSelectedInstallerInfo(), new File(directories.get(getSelectedInstallerInfo()))); |
|
111 | 108 |
} |
112 | 109 |
|
113 | 110 |
private void writeSelectedFiles() throws InstallerCreationServiceException { |
... | ... | |
117 | 114 |
//Deleting the previous files folder |
118 | 115 |
File copiedFilesDirectoryfile = new File(copiedFilesDirectory); |
119 | 116 |
deleteDirectory(copiedFilesDirectoryfile); |
120 |
|
|
121 |
String parentDirectory = pluginDirectory.getParent(); |
|
122 |
for (int i=0 ; i<selectedFiles.size() ; i++){ |
|
123 |
String destFile = selectedFiles.get(i).getAbsolutePath(); |
|
124 |
|
|
125 |
//Deleting the root where the gvSIG plugins are located |
|
126 |
destFile = destFile.substring(parentDirectory.length(), destFile.length()); |
|
127 |
|
|
117 |
for (int i=0 ; i<getSelectedInstallerInfo().getFileToCopySize() ; i++){ |
|
118 |
String destFile = getSelectedInstallerInfo().getFileToCopyAt(i).getAbsolutePath(); |
|
119 |
|
|
120 |
String sourceFile = applicationDirectory + File.separator + destFile; |
|
121 |
|
|
128 | 122 |
//Create the final path |
129 |
destFile = copiedFilesDirectory + destFile; |
|
123 |
destFile = pluginsDirectory + File.separator + copiedFilesDirectory + File.separator + destFile; |
|
124 |
|
|
130 | 125 |
|
131 | 126 |
//Copy the files |
132 |
copy(selectedFiles.get(i), new File(destFile));
|
|
127 |
copy(new File(sourceFile), new File(destFile));
|
|
133 | 128 |
} |
134 | 129 |
}catch(IOException e){ |
135 | 130 |
throw new InstallerCreationServiceException("Exception copying the files"); |
136 | 131 |
} |
132 |
} |
|
133 |
|
|
134 |
private void writeAntFile(String content) throws InstallerCreationServiceException { |
|
135 |
try{ |
|
136 |
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes()); |
|
137 |
OutputStream out = new FileOutputStream(getAntFileName()); |
|
138 |
|
|
139 |
// Copy the bits from instream to outstream |
|
140 |
byte[] buf = new byte[1024]; |
|
141 |
int len; |
|
142 |
while ((len = in.read(buf)) > 0) { |
|
143 |
out.write(buf, 0, len); |
|
144 |
} |
|
145 |
in.close(); |
|
146 |
out.close(); |
|
147 |
}catch(IOException e){ |
|
148 |
throw new InstallerCreationServiceException("Exception writing the ant file"); |
|
149 |
} |
|
137 | 150 |
} |
138 | 151 |
|
152 |
|
|
139 | 153 |
static public boolean deleteDirectory(File path) { |
140 | 154 |
if( path.exists() ) { |
141 | 155 |
File[] files = path.listFiles(); |
... | ... | |
180 | 194 |
} |
181 | 195 |
} |
182 | 196 |
|
183 |
public void setPluginDirectory(File pluginDirectory) throws InstallerCreationServiceException {
|
|
184 |
if (!pluginDirectory.isDirectory()){
|
|
185 |
throw new InstallerCreationServiceException("The plugin directory has to be a directory");
|
|
197 |
public void setApplicationDirectory(File applicationDirectory) throws InstallerCreationServiceException {
|
|
198 |
if (!applicationDirectory.isDirectory()){
|
|
199 |
throw new InstallerCreationServiceException("The application directory has to be a directory");
|
|
186 | 200 |
} |
187 |
this.pluginDirectory = pluginDirectory;
|
|
201 |
this.applicationDirectory = applicationDirectory;
|
|
188 | 202 |
|
189 |
//If the install.info file exists |
|
190 |
File file = new File(getInstallerInfoFileName()); |
|
191 |
if (file.exists()){ |
|
192 |
readInstallInfo(); |
|
193 |
}else{ |
|
194 |
setCode(pluginDirectory.getName()); |
|
195 |
setName(pluginDirectory.getName()); |
|
203 |
this.pluginsDirectory = new File(applicationDirectory.getAbsoluteFile() + File.separator + "gvSIG" + File.separator + "extensiones") ; |
|
204 |
if ((!pluginsDirectory.exists()) || (!pluginsDirectory.isDirectory())){ |
|
205 |
throw new InstallerCreationServiceException("The plugins directory does't exists or not is a directory"); |
|
196 | 206 |
} |
207 |
|
|
208 |
//Read all the installed plugins |
|
209 |
String[] plugins = pluginsDirectory.list(); |
|
210 |
InstallerInfoFileReader reader = new InstallerInfoFileReader(); |
|
211 |
if (plugins != null) { |
|
212 |
for (int i=0; i<plugins.length; i++) |
|
213 |
{ |
|
214 |
String pluginDirectoryName = plugins[i]; |
|
215 |
File pluginDirectory = getAbsolutePluginFile(pluginDirectoryName); |
|
216 |
DefaultInstallerInfo installerInfo = new DefaultInstallerInfo(); |
|
217 |
File installInfoFile = new File(getInstallerInfoFileName(pluginDirectory.getAbsolutePath())); |
|
218 |
if (installInfoFile.exists()){ |
|
219 |
reader.read(installerInfo, installInfoFile); |
|
220 |
}else{ |
|
221 |
installerInfo.setCode(pluginDirectoryName); |
|
222 |
installerInfo.setName(pluginDirectoryName); |
|
223 |
} |
|
224 |
installerInfos.add(installerInfo); |
|
225 |
directories.put(installerInfo, pluginDirectoryName); |
|
226 |
} |
|
227 |
} |
|
197 | 228 |
} |
198 | 229 |
|
199 |
private void readInstallInfo() throws InstallerInfoFileException{ |
|
200 |
InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader(); |
|
201 |
installerInfoFileReader.read(this, getInstallerInfoFileName()); |
|
202 |
} |
|
230 |
private InstallerInfo getSelectedInstallerInfo() throws InstallerCreationServiceException{ |
|
231 |
if (pluginToInstallIndex != -1){ |
|
232 |
return installerInfos.get(pluginToInstallIndex); |
|
233 |
} |
|
234 |
throw new InstallerCreationServiceException("There is not a plugin selected"); |
|
235 |
} |
|
203 | 236 |
|
204 |
private void writeInstallInfo() throws InstallerInfoFileException{ |
|
205 |
InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter(); |
|
206 |
installerInfoFileWriter.write(this, new File(getInstallerInfoFileName())); |
|
237 |
private File getAbsoluteSelectePluginFile() throws InstallerCreationServiceException{ |
|
238 |
return getAbsolutePluginFile(directories.get(getSelectedInstallerInfo())); |
|
207 | 239 |
} |
240 |
|
|
241 |
private File getAbsolutePluginFile(String selectePluginName) throws InstallerCreationServiceException{ |
|
242 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator + selectePluginName); |
|
243 |
} |
|
244 |
|
|
245 |
private String getPluginToInstallDirectory() throws InstallerCreationServiceException{ |
|
246 |
return directories.get(getSelectedInstallerInfo()); |
|
247 |
} |
|
208 | 248 |
|
209 |
private String getCopiedFilesDirectoryName(){ |
|
210 |
return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
|
|
249 |
private String getCopiedFilesDirectoryName() throws InstallerCreationServiceException{
|
|
250 |
return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
|
|
211 | 251 |
} |
212 | 252 |
|
213 |
private String getInstallerInfoFileName(){ |
|
214 |
return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME;
|
|
253 |
private String getInstallerInfoFileName() throws InstallerCreationServiceException{
|
|
254 |
return getInstallerInfoFileName(getPluginToInstallDirectory());
|
|
215 | 255 |
} |
216 | 256 |
|
217 |
private String getAntFileName(){ |
|
218 |
return pluginDirectory + File.separator + ANT_FILE_NAME;
|
|
257 |
private String getAntFileName() throws InstallerCreationServiceException{
|
|
258 |
return getAntFileName(getPluginToInstallDirectory());
|
|
219 | 259 |
} |
220 | 260 |
|
221 |
public InstallerInfo getInstallerInfo() {
|
|
222 |
return this;
|
|
261 |
private String getCopiedFilesDirectoryName(String pluginDirectory) throws InstallerCreationServiceException{
|
|
262 |
return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
|
|
223 | 263 |
} |
224 | 264 |
|
225 |
public void loadInstallInfoFromDirectory() throws InstallerCreationServiceException { |
|
226 |
File file = new File(getInstallerInfoFileName()); |
|
227 |
try{ |
|
228 |
if (file.exists()){ |
|
229 |
//Read the install.info |
|
230 |
FileInputStream fis = new FileInputStream(file); |
|
265 |
private String getInstallerInfoFileName(String pluginDirectory) throws InstallerCreationServiceException{ |
|
266 |
return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME; |
|
267 |
} |
|
231 | 268 |
|
232 |
InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader(); |
|
233 |
installerInfoFileReader.read(this, fis); |
|
269 |
private String getAntFileName(String pluginDirectory) throws InstallerCreationServiceException{ |
|
270 |
return getAbsoluteSelectePluginFile().getAbsolutePath() + File.separator + ANT_FILE_NAME; |
|
271 |
} |
|
234 | 272 |
|
235 |
//Load the ant file |
|
236 |
File antFile = new File(getAntFileName()); |
|
237 |
if (antFile.exists()){ |
|
238 |
loadAntFile(antFile); |
|
239 |
} |
|
240 | 273 |
|
241 |
//Load the files to copy |
|
242 |
File copiedFilesDirectoryfile = new File(getCopiedFilesDirectoryName()); |
|
243 |
if (copiedFilesDirectoryfile.exists()){ |
|
244 |
loadCopiedFiles(copiedFilesDirectoryfile); |
|
245 |
} |
|
246 |
} |
|
247 |
}catch(IOException e){ |
|
248 |
throw new InstallerCreationServiceException("Exception copying the files"); |
|
249 |
} |
|
250 |
} |
|
251 |
|
|
252 |
/* (non-Javadoc) |
|
253 |
* @see org.gvsig.installer.lib.impl.DefaultInstallerInfo#getAntScript() |
|
254 |
*/ |
|
255 |
@Override |
|
256 |
public String getAntScript() { |
|
257 |
if (super.getAntScript() != null){ |
|
258 |
return super.getAntScript(); |
|
259 |
} |
|
260 |
//if (isAdvancedMode){ |
|
261 |
try { |
|
262 |
loadAntFile(new File(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile())); |
|
263 |
return antScript; |
|
264 |
} catch (IOException e) { |
|
265 |
logger.error("reading the ant script model", e); |
|
266 |
} |
|
267 |
//} |
|
268 |
return null; |
|
269 |
} |
|
270 |
|
|
271 |
private void loadAntFile(File antFile) throws IOException { |
|
274 |
private void loadAntFile(File antFile) throws IOException, InstallerCreationServiceException { |
|
272 | 275 |
StringBuffer fileData = new StringBuffer(); |
273 | 276 |
FileInputStream fis = new FileInputStream(antFile); |
274 | 277 |
byte[] buf = new byte[1024]; |
... | ... | |
279 | 282 |
buf = new byte[1024]; |
280 | 283 |
} |
281 | 284 |
fis.close(); |
282 |
setAnScript(fileData.toString());
|
|
285 |
getSelectedInstallerInfo().setAnScript(fileData.toString());
|
|
283 | 286 |
} |
284 | 287 |
|
285 |
private void loadCopiedFiles(File file){ |
|
288 |
private void loadCopiedFiles(File file) throws InstallerCreationServiceException{
|
|
286 | 289 |
if (file.isDirectory()){ |
287 | 290 |
File[] files = file.listFiles(); |
288 | 291 |
for (int i=0 ; i<files.length ; i++){ |
... | ... | |
293 | 296 |
String pluginFile = file.getAbsolutePath().substring(getCopiedFilesDirectoryName().length(), file.getAbsolutePath().length()); |
294 | 297 |
|
295 | 298 |
//Ading the root directory |
296 |
pluginFile = pluginDirectory.getParentFile().getAbsolutePath() + pluginFile;
|
|
299 |
pluginFile = new File(getPluginToInstallDirectory()).getParentFile().getAbsolutePath() + pluginFile;
|
|
297 | 300 |
|
298 |
addFileToCopy(new File(pluginFile)); |
|
301 |
getSelectedInstallerInfo().addFileToCopy(new File(pluginFile));
|
|
299 | 302 |
} |
300 | 303 |
} |
301 | 304 |
|
... | ... | |
303 | 306 |
return this.manager; |
304 | 307 |
} |
305 | 308 |
|
309 |
public InstallerInfo getPluginInfoAt(int index) { |
|
310 |
return installerInfos.get(index); |
|
311 |
} |
|
312 |
|
|
313 |
public int getPluginsSize() { |
|
314 |
return installerInfos.size(); |
|
315 |
} |
|
316 |
|
|
317 |
public void setSelectedPlugin(int index) throws InstallerCreationServiceException { |
|
318 |
pluginToInstallIndex = index; |
|
319 |
|
|
320 |
|
|
321 |
//Load the ant file |
|
322 |
File antFile = new File(getAntFileName()); |
|
323 |
if (antFile.exists()){ |
|
324 |
try { |
|
325 |
loadAntFile(antFile); |
|
326 |
} catch (IOException e) { |
|
327 |
throw new InstallerCreationServiceException("Error reading the ant file"); |
|
328 |
} |
|
329 |
} |
|
330 |
|
|
331 |
//Load the files to copy |
|
332 |
File copiedFilesDirectoryfile = new File(getCopiedFilesDirectoryName()); |
|
333 |
if (copiedFilesDirectoryfile.exists()){ |
|
334 |
loadCopiedFiles(copiedFilesDirectoryfile); |
|
335 |
} |
|
336 |
} |
|
337 |
|
|
338 |
public void setSelectedPlugin(String code) { |
|
339 |
pluginToInstallIndex = -1; |
|
340 |
for (int i=0 ; i<getPluginsSize() ; i++){ |
|
341 |
if (installerInfos.get(i).getCode().equals(code)){ |
|
342 |
pluginToInstallIndex = i; |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
|
|
347 |
public InstallerInfo getSelectedPlugin() throws InstallerCreationServiceException { |
|
348 |
return getSelectedInstallerInfo(); |
|
349 |
} |
|
350 |
|
|
351 |
public String getDefaultAntScript() throws InstallerCreationServiceException { |
|
352 |
try { |
|
353 |
return readFileAsString(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile()); |
|
354 |
} catch (IOException e) { |
|
355 |
throw new InstallerCreationServiceException("Impossible to read the default ant file", e); |
|
356 |
} |
|
357 |
} |
|
358 |
|
|
359 |
private String readFileAsString(String filePath) |
|
360 |
throws java.io.IOException{ |
|
361 |
StringBuffer fileData = new StringBuffer(1000); |
|
362 |
BufferedReader reader = new BufferedReader( |
|
363 |
new FileReader(filePath)); |
|
364 |
char[] buf = new char[1024]; |
|
365 |
int numRead=0; |
|
366 |
while((numRead=reader.read(buf)) != -1){ |
|
367 |
String readData = String.valueOf(buf, 0, numRead); |
|
368 |
fileData.append(readData); |
|
369 |
buf = new char[1024]; |
|
370 |
} |
|
371 |
reader.close(); |
|
372 |
return fileData.toString(); |
|
373 |
} |
|
374 |
|
|
306 | 375 |
} |
307 | 376 |
|
Also available in: Unified diff