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 |
/* 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.creation; |
29 |
|
30 |
import java.io.BufferedReader; |
31 |
import java.io.ByteArrayInputStream; |
32 |
import java.io.File; |
33 |
import java.io.FileInputStream; |
34 |
import java.io.FileOutputStream; |
35 |
import java.io.FileReader; |
36 |
import java.io.IOException; |
37 |
import java.io.InputStream; |
38 |
import java.io.OutputStream; |
39 |
import java.util.ArrayList; |
40 |
import java.util.HashMap; |
41 |
import java.util.List; |
42 |
import java.util.Map; |
43 |
|
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 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
49 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter; |
50 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
51 |
import org.gvsig.installer.lib.spi.InstallerProviderServices; |
52 |
import org.gvsig.tools.service.Manager; |
53 |
import org.slf4j.Logger; |
54 |
|
55 |
/**
|
56 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
57 |
*/
|
58 |
public class DefaultInstallerCreationService implements InstallerCreationService { |
59 |
public static final String ANT_FILE_NAME = "install.xml"; |
60 |
public static final String COPIED_FILES_DIRECTORY_NAME = "files"; |
61 |
private File applicationDirectory; |
62 |
private File pluginsDirectory; |
63 |
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class); |
64 |
private Manager manager = null; |
65 |
private List<InstallerInfo> installerInfos = null; |
66 |
private Map<InstallerInfo, String> directories = null; |
67 |
private InstallerInfo pluginToInstall = null; |
68 |
protected List<File> selectedFiles = null; |
69 |
protected String antScript = null; |
70 |
private InstallerProviderServices installerProviderServices = null; |
71 |
|
72 |
public DefaultInstallerCreationService(Manager manager) {
|
73 |
super();
|
74 |
this.manager = manager;
|
75 |
installerInfos = new ArrayList<InstallerInfo>(); |
76 |
directories = new HashMap<InstallerInfo, String>(); |
77 |
selectedFiles = new ArrayList<File>(); |
78 |
installerProviderServices = InstallerProviderLocator.getProviderManager().createInstallerProviderServices(); |
79 |
} |
80 |
|
81 |
public void createInstaller(OutputStream installerStream) |
82 |
throws InstallerCreationServiceException {
|
83 |
if (applicationDirectory == null){ |
84 |
throw new InstallerCreationServiceException("The application directory has to be set"); |
85 |
} |
86 |
|
87 |
//Write the install.info file
|
88 |
writeInstallInfo(); |
89 |
|
90 |
//Create the ant file
|
91 |
String antScript = getSelectedInstallerInfo().getAntScript();
|
92 |
if (antScript != null){ |
93 |
writeAntFile(antScript); |
94 |
} |
95 |
|
96 |
//Copy the selected files
|
97 |
writeSelectedFiles(); |
98 |
|
99 |
|
100 |
InstallerInfo infInstallerInfo = getSelectedPlugin(); |
101 |
String pluginFileName = getSelectedPlugin().getCode() + "-" + infInstallerInfo.getVersion() + "-" + infInstallerInfo.getBuild(); |
102 |
installerProviderServices.compress(getAbsoluteSelectePluginFile(), pluginFileName, installerStream); |
103 |
} |
104 |
|
105 |
|
106 |
private void writeInstallInfo() throws InstallerCreationServiceException{ |
107 |
installerProviderServices.writeInstallInfo(getAbsoluteSelectePluginFile(), getSelectedInstallerInfo()); |
108 |
} |
109 |
|
110 |
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 |
InstallerInfo installerInfo = getSelectedInstallerInfo(); |
118 |
for (int i=0 ; i<installerInfo.getFileToCopySize() ; i++){ |
119 |
String destFile = installerInfo.getFileToCopyAt(i).getAbsolutePath();
|
120 |
|
121 |
String sourceFile = applicationDirectory + File.separator + destFile; |
122 |
|
123 |
//Create the final path
|
124 |
destFile = pluginsDirectory + File.separator + copiedFilesDirectory + File.separator + destFile; |
125 |
|
126 |
|
127 |
//Copy the files
|
128 |
copy(new File(sourceFile), new File(destFile)); |
129 |
} |
130 |
}catch(IOException e){ |
131 |
throw new InstallerCreationServiceException("Exception copying the files"); |
132 |
} |
133 |
} |
134 |
|
135 |
private void writeAntFile(String content) throws InstallerCreationServiceException { |
136 |
try{
|
137 |
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes()); |
138 |
OutputStream out = new FileOutputStream(getAbsoluteAntFile()); |
139 |
|
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 |
} |
152 |
|
153 |
|
154 |
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 |
public void setApplicationDirectory(File applicationDirectory) throws InstallerCreationServiceException { |
199 |
if (!applicationDirectory.isDirectory()){
|
200 |
throw new InstallerCreationServiceException("The application directory has to be a directory"); |
201 |
} |
202 |
this.applicationDirectory = applicationDirectory;
|
203 |
|
204 |
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 |
} |
208 |
|
209 |
//Read all the installed plugins
|
210 |
String[] plugins = pluginsDirectory.list(); |
211 |
|
212 |
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 |
|
219 |
installerProviderServices.readInstallInfo(new File(pluginDirectory.getAbsolutePath()), installerInfo); |
220 |
|
221 |
//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 |
installerInfos.add(installerInfo); |
231 |
directories.put(installerInfo, pluginDirectoryName); |
232 |
} |
233 |
} |
234 |
} |
235 |
|
236 |
private InstallerInfo getSelectedInstallerInfo() throws InstallerCreationServiceException{ |
237 |
if (pluginToInstall != null){ |
238 |
return pluginToInstall;
|
239 |
} |
240 |
throw new InstallerCreationServiceException("There is not a plugin selected"); |
241 |
} |
242 |
|
243 |
private File getAbsoluteSelectePluginFile() throws InstallerCreationServiceException{ |
244 |
return getAbsolutePluginFile(directories.get(getSelectedInstallerInfo()));
|
245 |
} |
246 |
|
247 |
private File getAbsolutePluginFile(String selectePluginName) throws InstallerCreationServiceException{ |
248 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator + selectePluginName); |
249 |
} |
250 |
|
251 |
private String getPluginToInstallDirectory() throws InstallerCreationServiceException{ |
252 |
return directories.get(getSelectedInstallerInfo());
|
253 |
} |
254 |
|
255 |
private File getAbsoluteAntFile() throws InstallerCreationServiceException{ |
256 |
return new File(pluginsDirectory.getAbsolutePath() + File.separator + ANT_FILE_NAME); |
257 |
} |
258 |
|
259 |
private String getCopiedFilesDirectoryName() throws InstallerCreationServiceException{ |
260 |
return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
|
261 |
} |
262 |
|
263 |
private String getCopiedFilesDirectoryName(String pluginDirectory) throws InstallerCreationServiceException{ |
264 |
return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME; |
265 |
} |
266 |
|
267 |
private String getAntFileName(String pluginDirectory) throws InstallerCreationServiceException{ |
268 |
return pluginDirectory + File.separator + ANT_FILE_NAME; |
269 |
} |
270 |
|
271 |
|
272 |
private void loadAntFile(File antFile) throws IOException, InstallerCreationServiceException { |
273 |
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 |
getSelectedInstallerInfo().setAnScript(fileData.toString()); |
284 |
} |
285 |
|
286 |
private void loadCopiedFiles(File file) throws InstallerCreationServiceException{ |
287 |
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 |
|
296 |
//Ading the root directory
|
297 |
pluginFile = new File(getPluginToInstallDirectory()).getParentFile().getAbsolutePath() + pluginFile; |
298 |
|
299 |
getSelectedInstallerInfo().addFileToCopy(new File(pluginFile)); |
300 |
} |
301 |
} |
302 |
|
303 |
public Manager getManager() {
|
304 |
return this.manager; |
305 |
} |
306 |
|
307 |
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 |
if (index > installerInfos.size()){
|
317 |
throw new InstallerCreationServiceException("The plugin doesn't exist"); |
318 |
} |
319 |
pluginToInstall = installerInfos.get(index); |
320 |
} |
321 |
|
322 |
public void setSelectedPlugin(String code) throws InstallerCreationServiceException { |
323 |
int selectedCode = -1;; |
324 |
for (int i=0 ; i<getPluginsSize() ; i++){ |
325 |
if (installerInfos.get(i).getCode().equals(code)){
|
326 |
selectedCode = i; |
327 |
break;
|
328 |
} |
329 |
} |
330 |
if (selectedCode == -1){ |
331 |
throw new InstallerCreationServiceException("The plugin doesn't exist"); |
332 |
} |
333 |
pluginToInstall = installerInfos.get(selectedCode); |
334 |
} |
335 |
|
336 |
public InstallerInfo getSelectedPlugin() {
|
337 |
try {
|
338 |
return getSelectedInstallerInfo();
|
339 |
} catch (InstallerCreationServiceException e) {
|
340 |
//there is not a selected plugin
|
341 |
return null; |
342 |
} |
343 |
} |
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 |
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 |
} |
369 |
|