Revision 41706 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
PluginInstallerExecutionProvider.java | ||
---|---|---|
10 | 10 |
* |
11 | 11 |
* This program is distributed in the hope that it will be useful, |
12 | 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 |
* GNU General Public License for more details. |
15 | 15 |
* |
16 | 16 |
* You should have received a copy of the GNU General Public License |
17 | 17 |
* along with this program; if not, write to the Free Software |
18 | 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
19 |
* MA 02110-1301, USA.
|
|
19 |
* MA 02110-1301, USA. |
|
20 | 20 |
* |
21 | 21 |
* For any additional information, do not hesitate to contact us |
22 | 22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
... | ... | |
25 | 25 |
* AUTHORS (In addition to CIT): |
26 | 26 |
* 2010 {Prodevelop} {Task} |
27 | 27 |
*/ |
28 |
|
|
29 | 28 |
package org.gvsig.installer.prov.plugin.execution; |
30 | 29 |
|
31 | 30 |
import java.io.BufferedWriter; |
... | ... | |
49 | 48 |
|
50 | 49 |
import com.sardak.antform.AntForm; |
51 | 50 |
import com.sardak.antform.AntMenu; |
51 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
52 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
52 | 53 |
|
53 | 54 |
/** |
54 | 55 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a> |
55 | 56 |
*/ |
56 | 57 |
public class PluginInstallerExecutionProvider extends AbstractProvider |
57 |
implements InstallPackageProvider {
|
|
58 |
implements InstallPackageProvider {
|
|
58 | 59 |
|
59 |
private static Logger logger = LoggerFactory.getLogger(PluginInstallerExecutionProvider.class); |
|
60 |
private static final String ANT_FILE_NAME = "install.xml"; |
|
61 |
private File pluginsDirectory = null; |
|
62 |
private File applicationDirectory = null; |
|
63 |
private File decompressDir = null; |
|
64 |
private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator |
|
65 |
.getProviderManager(); |
|
60 |
private static Logger logger = LoggerFactory.getLogger(PluginInstallerExecutionProvider.class); |
|
61 |
private static final String ANT_FILE_NAME = "install.xml"; |
|
66 | 62 |
|
67 |
public PluginInstallerExecutionProvider(ProviderServices providerServices) {
|
|
68 |
super(providerServices);
|
|
69 |
}
|
|
63 |
public PluginInstallerExecutionProvider(ProviderServices providerServices) {
|
|
64 |
super(providerServices);
|
|
65 |
}
|
|
70 | 66 |
|
71 |
public class InstallerPluginsDirectoryNotFoundException extends
|
|
72 |
InstallPackageServiceException {
|
|
67 |
public class InstallerPluginsDirectoryNotFoundException extends
|
|
68 |
InstallPackageServiceException {
|
|
73 | 69 |
|
74 |
private static final long serialVersionUID = 4416143986837955880L;
|
|
70 |
private static final long serialVersionUID = 4416143986837955880L;
|
|
75 | 71 |
|
76 |
private static final String message = "Plugins directory not found";
|
|
72 |
private static final String message = "Plugins directory not found";
|
|
77 | 73 |
|
78 |
private static final String KEY = "plugins_directory_not_found";
|
|
74 |
private static final String KEY = "plugins_directory_not_found";
|
|
79 | 75 |
|
80 |
public InstallerPluginsDirectoryNotFoundException() {
|
|
81 |
super(message, KEY, serialVersionUID);
|
|
82 |
}
|
|
76 |
public InstallerPluginsDirectoryNotFoundException() {
|
|
77 |
super(message, KEY, serialVersionUID);
|
|
78 |
}
|
|
83 | 79 |
|
84 |
}
|
|
80 |
}
|
|
85 | 81 |
|
86 |
public void install(File applicationDirectory, InputStream inputStream,
|
|
87 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
|
82 |
public void install(File applicationDirectory, InputStream inputStream,
|
|
83 |
PackageInfo packageInfo) throws InstallPackageServiceException {
|
|
88 | 84 |
|
89 |
this.applicationDirectory = applicationDirectory; |
|
90 |
pluginsDirectory = new File(applicationDirectory + File.separator |
|
91 |
+ "gvSIG" + File.separator + "extensiones"); |
|
92 |
logger.info("Installing package '"+packageInfo.getCode()+"' in '"+pluginsDirectory+"'."); |
|
93 |
try { |
|
94 |
if (!pluginsDirectory.exists()) { |
|
95 |
logger.warn("Can install package '"+packageInfo.getCode()+"', install folder '"+pluginsDirectory+"' does not exists"); |
|
96 |
throw new InstallerPluginsDirectoryNotFoundException(); |
|
97 |
} |
|
85 |
InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager(); |
|
86 |
InstallerManager installerManager = InstallerLocator.getInstallerManager(); |
|
87 |
|
|
88 |
File pluginsDirectory = installerManager.getDefaultLocalAddonRepository( |
|
89 |
"plugin", |
|
90 |
InstallerManager.ACCESS_WRITE |
|
91 |
); |
|
98 | 92 |
|
99 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
|
100 |
.createInstallerProviderServices(); |
|
93 |
logger.info("Installing package '" + packageInfo.getCode() + "' in '" + pluginsDirectory + "'."); |
|
94 |
try { |
|
95 |
if ( !pluginsDirectory.exists() ) { |
|
96 |
logger.warn("Can install package '" + packageInfo.getCode() + "', install folder '" + pluginsDirectory + "' does not exists"); |
|
97 |
throw new InstallerPluginsDirectoryNotFoundException(); |
|
98 |
} |
|
101 | 99 |
|
102 |
installerProviderServices.decompress(inputStream, pluginsDirectory); |
|
103 |
File antFile = new File(pluginsDirectory + File.separator |
|
104 |
+ packageInfo.getCode() + File.separator + "install" |
|
105 |
+ File.separator + ANT_FILE_NAME); |
|
100 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
|
101 |
.createInstallerProviderServices(); |
|
106 | 102 |
|
107 |
if (antFile.exists()) { |
|
108 |
executeAntFile(antFile); |
|
109 |
} |
|
103 |
installerProviderServices.decompress(inputStream, pluginsDirectory); |
|
104 |
File antFile = new File(pluginsDirectory + File.separator |
|
105 |
+ packageInfo.getCode() + File.separator + "install" |
|
106 |
+ File.separator + ANT_FILE_NAME); |
|
110 | 107 |
|
111 |
} catch (Exception e) { |
|
112 |
try { |
|
113 |
logger.warn("Can install package '"+packageInfo.getCode()+"'.", e); |
|
114 |
// if there is an exception, installLater is called |
|
115 |
installLater(applicationDirectory, inputStream, packageInfo); |
|
116 |
} catch (IOException e1) { |
|
117 |
logger.warn("Can install package '"+packageInfo.getCode()+"'.", e1); |
|
118 |
throw new InstallPackageServiceException(e1); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
108 |
if ( antFile.exists() ) { |
|
109 |
executeAntFile(applicationDirectory, pluginsDirectory, antFile); |
|
110 |
} |
|
122 | 111 |
|
123 |
/** |
|
124 |
* This function will is called when an exception is produced during the |
|
125 |
* installation of a plugin. It will prepare the package to be installed on |
|
126 |
* the next gvSIG restart. |
|
127 |
*/ |
|
128 |
public void installLater(File applicationDirectory, |
|
129 |
InputStream inputStream, PackageInfo packageInfo) |
|
130 |
throws InstallPackageServiceException, IOException { |
|
112 |
} catch (Exception e) { |
|
113 |
try { |
|
114 |
logger.warn("Can install package '" + packageInfo.getCode() + "'.", e); |
|
115 |
// if there is an exception, installLater is called |
|
116 |
installLater(applicationDirectory, inputStream, packageInfo); |
|
117 |
} catch (IOException e1) { |
|
118 |
logger.warn("Can install package '" + packageInfo.getCode() + "'.", e1); |
|
119 |
throw new InstallPackageServiceException(e1); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
131 | 123 |
|
132 |
logger.info("Delayed the package installation ("+packageInfo.getCode()+")."); |
|
133 |
this.applicationDirectory = applicationDirectory; |
|
124 |
/** |
|
125 |
* This function will is called when an exception is produced during the |
|
126 |
* installation of a plugin. It will prepare the package to be installed on |
|
127 |
* the next gvSIG restart. |
|
128 |
*/ |
|
129 |
public void installLater(File applicationDirectory, |
|
130 |
InputStream inputStream, PackageInfo packageInfo) |
|
131 |
throws InstallPackageServiceException, IOException { |
|
134 | 132 |
|
135 |
File updateDirectory = new File(applicationDirectory + File.separator |
|
136 |
+ "update"); |
|
133 |
InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager(); |
|
134 |
File decompressDir = null; |
|
135 |
|
|
136 |
logger.info("Delayed the package installation (" + packageInfo.getCode() + ")."); |
|
137 | 137 |
|
138 |
if (!updateDirectory.exists()) { |
|
139 |
forceMkdir(updateDirectory); |
|
140 |
} |
|
138 |
File updateDirectory = new File(applicationDirectory + File.separator |
|
139 |
+ "update"); |
|
141 | 140 |
|
142 |
File filesDirectory = new File(updateDirectory + File.separator |
|
143 |
+ "files"); |
|
144 |
if (!filesDirectory.exists()) { |
|
145 |
forceMkdir(filesDirectory); |
|
146 |
} |
|
141 |
if ( !updateDirectory.exists() ) { |
|
142 |
forceMkdir(updateDirectory); |
|
143 |
} |
|
147 | 144 |
|
148 |
File scriptsFile = new File(updateDirectory + File.separator |
|
149 |
+ "scripts.lst"); |
|
145 |
File filesDirectory = new File(updateDirectory + File.separator |
|
146 |
+ "files"); |
|
147 |
if ( !filesDirectory.exists() ) { |
|
148 |
forceMkdir(filesDirectory); |
|
149 |
} |
|
150 | 150 |
|
151 |
decompressDir = new File(filesDirectory.toString() + File.separator |
|
152 |
+ packageInfo.getCode()); |
|
153 |
if (decompressDir.exists()) { |
|
154 |
deleteDir(decompressDir); |
|
155 |
} |
|
151 |
File scriptsFile = new File(updateDirectory + File.separator |
|
152 |
+ "scripts.lst"); |
|
156 | 153 |
|
157 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
|
158 |
.createInstallerProviderServices(); |
|
159 |
installerProviderServices.decompress(inputStream, filesDirectory); |
|
154 |
decompressDir = new File(filesDirectory.toString() + File.separator |
|
155 |
+ packageInfo.getCode()); |
|
156 |
if ( decompressDir.exists() ) { |
|
157 |
deleteDir(decompressDir); |
|
158 |
} |
|
160 | 159 |
|
161 |
File antFile = new File(decompressDir + File.separator + "install" |
|
162 |
+ File.separator + ANT_FILE_NAME); |
|
160 |
InstallPackageProviderServices installerProviderServices = installerProviderManager |
|
161 |
.createInstallerProviderServices(); |
|
162 |
installerProviderServices.decompress(inputStream, filesDirectory); |
|
163 | 163 |
|
164 |
if (antFile.exists()) { |
|
165 |
if (!scriptsFile.exists()) { |
|
166 |
forceMkdir(scriptsFile); |
|
167 |
scriptsFile.createNewFile(); |
|
168 |
} |
|
169 |
FileWriter writer = new FileWriter(scriptsFile, true); |
|
170 |
BufferedWriter out = new BufferedWriter(writer); |
|
171 |
String str = antFile.toString(); |
|
172 |
out.append(str + "\n"); |
|
173 |
out.close(); |
|
174 |
} |
|
164 |
File antFile = new File(decompressDir + File.separator + "install" |
|
165 |
+ File.separator + ANT_FILE_NAME); |
|
175 | 166 |
|
176 |
} |
|
167 |
if ( antFile.exists() ) { |
|
168 |
if ( !scriptsFile.exists() ) { |
|
169 |
forceMkdir(scriptsFile); |
|
170 |
scriptsFile.createNewFile(); |
|
171 |
} |
|
172 |
FileWriter writer = new FileWriter(scriptsFile, true); |
|
173 |
BufferedWriter out = new BufferedWriter(writer); |
|
174 |
String str = antFile.toString(); |
|
175 |
out.append(str + "\n"); |
|
176 |
out.close(); |
|
177 |
} |
|
177 | 178 |
|
178 |
private void forceMkdir(File file) throws IOException { |
|
179 |
if( file.isDirectory() ) { |
|
180 |
org.apache.commons.io.FileUtils.forceMkdir(file); |
|
181 |
} else { |
|
182 |
org.apache.commons.io.FileUtils.forceMkdir(file.getParentFile()); |
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
private void executeAntFile(File file) { |
|
187 |
logger.info("Running install script '"+file.getAbsolutePath()+"'."); |
|
188 |
try { |
|
189 |
Project p = new Project(); |
|
190 |
p.setUserProperty("ant.file", file.getAbsolutePath()); |
|
191 |
p.setUserProperty("gvsig_dir", applicationDirectory.getAbsolutePath()); |
|
192 |
p.setUserProperty("extensions_dir", pluginsDirectory.getAbsolutePath()); |
|
193 |
p.setBaseDir(file.getParentFile()); |
|
194 |
p.addTaskDefinition("antform", AntForm.class); |
|
195 |
p.addTaskDefinition("antmenu", AntMenu.class); |
|
196 |
p.init(); |
|
197 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
|
198 |
p.addReference("ant.projectHelper", helper); |
|
199 |
helper.parse(p, file); |
|
200 |
p.executeTarget(p.getDefaultTarget()); |
|
201 |
} catch(Exception ex) { |
|
202 |
logger.warn("Problems executing installation script.", ex); |
|
203 |
} |
|
204 |
} |
|
179 |
} |
|
205 | 180 |
|
206 |
private boolean deleteDir(File dir) { |
|
207 |
if (dir.isDirectory()) { |
|
208 |
String[] children = dir.list(); |
|
209 |
for (int i = 0; i < children.length; i++) { |
|
210 |
boolean success = deleteDir(new File(dir, children[i])); |
|
211 |
if (!success) { |
|
212 |
return false; |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
// The directory is now empty so delete it |
|
217 |
return dir.delete(); |
|
218 |
} |
|
181 |
private void forceMkdir(File file) throws IOException { |
|
182 |
if ( file.isDirectory() ) { |
|
183 |
org.apache.commons.io.FileUtils.forceMkdir(file); |
|
184 |
} else { |
|
185 |
org.apache.commons.io.FileUtils.forceMkdir(file.getParentFile()); |
|
186 |
} |
|
187 |
} |
|
219 | 188 |
|
189 |
private void executeAntFile(File applicationDirectory, File pluginsDirectory, File file) { |
|
190 |
logger.info("Running install script '" + file.getAbsolutePath() + "'."); |
|
191 |
try { |
|
192 |
Project p = new Project(); |
|
193 |
p.setUserProperty("ant.file", file.getAbsolutePath()); |
|
194 |
p.setUserProperty("gvsig_dir", applicationDirectory.getAbsolutePath()); |
|
195 |
p.setUserProperty("extensions_dir", pluginsDirectory.getAbsolutePath()); |
|
196 |
p.setBaseDir(file.getParentFile()); |
|
197 |
p.addTaskDefinition("antform", AntForm.class); |
|
198 |
p.addTaskDefinition("antmenu", AntMenu.class); |
|
199 |
p.init(); |
|
200 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
|
201 |
p.addReference("ant.projectHelper", helper); |
|
202 |
helper.parse(p, file); |
|
203 |
p.executeTarget(p.getDefaultTarget()); |
|
204 |
} catch (Exception ex) { |
|
205 |
logger.warn("Problems executing installation script.", ex); |
|
206 |
} |
|
207 |
} |
|
208 |
|
|
209 |
private boolean deleteDir(File dir) { |
|
210 |
if ( dir.isDirectory() ) { |
|
211 |
String[] children = dir.list(); |
|
212 |
for ( int i = 0; i < children.length; i++ ) { |
|
213 |
boolean success = deleteDir(new File(dir, children[i])); |
|
214 |
if ( !success ) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
// The directory is now empty so delete it |
|
220 |
return dir.delete(); |
|
221 |
} |
|
222 |
|
|
220 | 223 |
} |
Also available in: Unified diff