svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.prov / org.gvsig.installer.prov.plugin / src / main / java / org / gvsig / installer / prov / plugin / execution / PluginInstallerExecutionProvider.java @ 40560
History | View | Annotate | Download (6.35 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2010 {Prodevelop} {Task}
|
27 |
*/
|
28 |
|
29 |
package org.gvsig.installer.prov.plugin.execution; |
30 |
|
31 |
import java.io.BufferedWriter; |
32 |
import java.io.File; |
33 |
import java.io.FileWriter; |
34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
36 |
|
37 |
import com.sardak.antform.AntForm; |
38 |
import com.sardak.antform.AntMenu; |
39 |
|
40 |
import org.apache.tools.ant.Project; |
41 |
import org.apache.tools.ant.ProjectHelper; |
42 |
|
43 |
import org.gvsig.installer.lib.api.PackageInfo; |
44 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
45 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
46 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
47 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
48 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
49 |
import org.gvsig.tools.service.spi.AbstractProvider; |
50 |
import org.gvsig.tools.service.spi.ProviderServices; |
51 |
|
52 |
/**
|
53 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
54 |
*/
|
55 |
public class PluginInstallerExecutionProvider extends AbstractProvider |
56 |
implements InstallPackageProvider {
|
57 |
|
58 |
private static final String ANT_FILE_NAME = "install.xml"; |
59 |
private File pluginsDirectory = null; |
60 |
private File applicationDirectory = null; |
61 |
private File decompressDir = null; |
62 |
private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator |
63 |
.getProviderManager(); |
64 |
|
65 |
public PluginInstallerExecutionProvider(ProviderServices providerServices) {
|
66 |
super(providerServices);
|
67 |
} |
68 |
|
69 |
public class InstallerPluginsDirectoryNotFoundException extends |
70 |
InstallPackageServiceException { |
71 |
|
72 |
private static final long serialVersionUID = 4416143986837955880L; |
73 |
|
74 |
private static final String message = "Plugins directory not found"; |
75 |
|
76 |
private static final String KEY = "plugins_directory_not_found"; |
77 |
|
78 |
public InstallerPluginsDirectoryNotFoundException() {
|
79 |
super(message, KEY, serialVersionUID);
|
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
public void install(File applicationDirectory, InputStream inputStream, |
85 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
86 |
|
87 |
this.applicationDirectory = applicationDirectory;
|
88 |
pluginsDirectory = new File(applicationDirectory + File.separator |
89 |
+ "gvSIG" + File.separator + "extensiones"); |
90 |
try {
|
91 |
if (!pluginsDirectory.exists()) {
|
92 |
throw new InstallerPluginsDirectoryNotFoundException(); |
93 |
} |
94 |
|
95 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
96 |
.createInstallerProviderServices(); |
97 |
|
98 |
installerProviderServices.decompress(inputStream, pluginsDirectory); |
99 |
File antFile = new File(pluginsDirectory + File.separator |
100 |
+ packageInfo.getCode() + File.separator + "install" |
101 |
+ File.separator + ANT_FILE_NAME);
|
102 |
|
103 |
if (antFile.exists()) {
|
104 |
executeAntFile(antFile); |
105 |
} |
106 |
|
107 |
} catch (Exception e) { |
108 |
try {
|
109 |
// if there is an exception, installLater is called
|
110 |
installLater(applicationDirectory, inputStream, packageInfo); |
111 |
} catch (IOException e1) { |
112 |
e1.printStackTrace(); |
113 |
throw new InstallPackageServiceException(e1); |
114 |
} |
115 |
} |
116 |
} |
117 |
|
118 |
/**
|
119 |
* This function will is called when an exception is produced during the
|
120 |
* installation of a plugin. It will prepare the package to be installed on
|
121 |
* the next gvSIG restart.
|
122 |
*/
|
123 |
public void installLater(File applicationDirectory, |
124 |
InputStream inputStream, PackageInfo packageInfo)
|
125 |
throws InstallPackageServiceException, IOException { |
126 |
|
127 |
this.applicationDirectory = applicationDirectory;
|
128 |
|
129 |
File updateDirectory = new File(applicationDirectory + File.separator |
130 |
+ "update");
|
131 |
|
132 |
if (!updateDirectory.exists()) {
|
133 |
updateDirectory.mkdir(); |
134 |
} |
135 |
|
136 |
File filesDirectory = new File(updateDirectory + File.separator |
137 |
+ "files");
|
138 |
if (!filesDirectory.exists()) {
|
139 |
filesDirectory.mkdir(); |
140 |
} |
141 |
|
142 |
File scriptsFile = new File(updateDirectory + File.separator |
143 |
+ "scripts.lst");
|
144 |
|
145 |
decompressDir = new File(filesDirectory.toString() + File.separator |
146 |
+ packageInfo.getCode()); |
147 |
if (decompressDir.exists()) {
|
148 |
deleteDir(decompressDir); |
149 |
} |
150 |
|
151 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
152 |
.createInstallerProviderServices(); |
153 |
installerProviderServices.decompress(inputStream, filesDirectory); |
154 |
|
155 |
File antFile = new File(decompressDir + File.separator + "install" |
156 |
+ File.separator + ANT_FILE_NAME);
|
157 |
|
158 |
if (antFile.exists()) {
|
159 |
if (!scriptsFile.exists()) {
|
160 |
scriptsFile.createNewFile(); |
161 |
} |
162 |
FileWriter writer = new FileWriter(scriptsFile, true); |
163 |
BufferedWriter out = new BufferedWriter(writer); |
164 |
String str = antFile.toString();
|
165 |
out.append(str + "\n");
|
166 |
out.close(); |
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
private void executeAntFile(File file) { |
172 |
Project p = new Project();
|
173 |
p.setUserProperty("ant.file", file.getAbsolutePath());
|
174 |
p.setUserProperty("gvsig_dir", applicationDirectory.getAbsolutePath());
|
175 |
p.setUserProperty("extensions_dir", pluginsDirectory.getAbsolutePath());
|
176 |
p.setBaseDir(file.getParentFile()); |
177 |
p.addTaskDefinition("antform", AntForm.class);
|
178 |
p.addTaskDefinition("antmenu", AntMenu.class);
|
179 |
p.init(); |
180 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
181 |
p.addReference("ant.projectHelper", helper);
|
182 |
helper.parse(p, file); |
183 |
p.executeTarget(p.getDefaultTarget()); |
184 |
} |
185 |
|
186 |
private boolean deleteDir(File dir) { |
187 |
if (dir.isDirectory()) {
|
188 |
String[] children = dir.list(); |
189 |
for (int i = 0; i < children.length; i++) { |
190 |
boolean success = deleteDir(new File(dir, children[i])); |
191 |
if (!success) {
|
192 |
return false; |
193 |
} |
194 |
} |
195 |
} |
196 |
// The directory is now empty so delete it
|
197 |
return dir.delete();
|
198 |
} |
199 |
|
200 |
} |