svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami.updater / src / main / java / org / gvsig / andamiupdater / Updater.java @ 42681
History | View | Annotate | Download (8.55 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.andamiupdater; |
24 |
|
25 |
import java.io.BufferedReader; |
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.lang.reflect.Method; |
29 |
|
30 |
import com.sardak.antform.AntForm; |
31 |
import com.sardak.antform.AntMenu; |
32 |
import java.io.Closeable; |
33 |
import java.io.FileInputStream; |
34 |
import java.io.FileOutputStream; |
35 |
import java.io.FileReader; |
36 |
import java.io.InputStream; |
37 |
import java.io.OutputStream; |
38 |
import static java.lang.System.in; |
39 |
import java.util.logging.Level; |
40 |
import java.util.logging.Logger; |
41 |
|
42 |
import org.apache.tools.ant.Project; |
43 |
import org.apache.tools.ant.ProjectHelper; |
44 |
|
45 |
/**
|
46 |
* @author gvSIG Team
|
47 |
* @version $Id$
|
48 |
*
|
49 |
*/
|
50 |
public class Updater { |
51 |
|
52 |
private String[] args; |
53 |
|
54 |
/**
|
55 |
* @param args
|
56 |
*/
|
57 |
public Updater(String[] args) { |
58 |
this.args = args;
|
59 |
} |
60 |
|
61 |
/**
|
62 |
* @param args
|
63 |
* @throws Exception
|
64 |
*/
|
65 |
public static void main(String[] args) throws Exception { |
66 |
Updater updater = new Updater(args);
|
67 |
updater.copyFiles(); |
68 |
updater.executeScripts(); |
69 |
updater.removeAll(); |
70 |
updater.launchApp(args); |
71 |
} |
72 |
|
73 |
private File getApplicationDirectory() { |
74 |
return new File("").getAbsoluteFile(); |
75 |
} |
76 |
|
77 |
private File getPluginsDirectory() { |
78 |
return new File(getApplicationDirectory(), this.args[1]); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* @param args
|
83 |
* @throws Exception
|
84 |
*/
|
85 |
@SuppressWarnings("unchecked") |
86 |
private void launchApp(String[] args) throws Exception { |
87 |
|
88 |
try {
|
89 |
Class launcher_class = Class.forName("org.gvsig.andami.Launcher"); |
90 |
Object launcher = launcher_class.newInstance();
|
91 |
Method main = launcher_class.getMethod("main", new Class[]{String[].class}); |
92 |
main.invoke(launcher, new Object[]{args}); |
93 |
|
94 |
} catch (ClassNotFoundException e) { |
95 |
Class launcher_class = Class.forName("com.iver.andami.Launcher"); |
96 |
Object launcher = launcher_class.newInstance();
|
97 |
Method main = launcher_class.getMethod("main", new Class[]{String[].class}); |
98 |
main.invoke(launcher, new Object[]{args}); |
99 |
} |
100 |
} |
101 |
|
102 |
/**
|
103 |
*
|
104 |
*/
|
105 |
private void removeAll() { |
106 |
File updateDirectory = new File(getApplicationDirectory(), "update"); |
107 |
if (updateDirectory.exists()) {
|
108 |
deleteDir(updateDirectory); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
private boolean deleteDir(File folder) { |
114 |
if (folder == null) { |
115 |
return true; |
116 |
} |
117 |
try {
|
118 |
if (folder.isDirectory()) {
|
119 |
String[] children = folder.list(); |
120 |
for (String children1 : children) { |
121 |
boolean success = deleteDir(new File(folder, children1)); |
122 |
if (!success) {
|
123 |
return false; |
124 |
} |
125 |
} |
126 |
} |
127 |
return folder.delete();
|
128 |
} catch (Throwable th) { |
129 |
this.warning("Can't remove '" + folder.getAbsolutePath() + "'.", th); |
130 |
return false; |
131 |
} |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Reads every line of appDirectory/update/scripts.lst and executes the ant
|
136 |
* file that this line points to
|
137 |
*/
|
138 |
private void executeScripts() { |
139 |
File updateFolder = new File(getApplicationDirectory(), "update"); |
140 |
File antFilesScript = new File(updateFolder, "scripts.lst"); |
141 |
|
142 |
if (antFilesScript.exists()) {
|
143 |
BufferedReader reader = null; |
144 |
try {
|
145 |
reader = new BufferedReader(new FileReader(antFilesScript)); |
146 |
String line;
|
147 |
while ((line = reader.readLine()) != null) { |
148 |
File antFile = new File(line); |
149 |
try {
|
150 |
executeAntFile(antFile); |
151 |
} catch (Throwable th) { |
152 |
this.warning("Error running file '" + antFile.getAbsolutePath() + "'.", th); |
153 |
} |
154 |
} |
155 |
} catch (Throwable th) { |
156 |
this.warning("Error running ant scripts.", th); |
157 |
} finally {
|
158 |
closeQuietly(reader, antFilesScript); |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Copies all files and folders from gvSIG/update to gvSIG/extensions
|
167 |
*
|
168 |
* @throws IOException
|
169 |
*
|
170 |
*/
|
171 |
private void copyFiles() throws IOException { |
172 |
File updateFolder = new File(getApplicationDirectory(), "update"); |
173 |
File filesFolder = new File(updateFolder, "files"); |
174 |
File pluginsDirectory = getPluginsDirectory();
|
175 |
|
176 |
if (updateFolder.exists()) {
|
177 |
String[] childrenDirs = filesFolder.list(); |
178 |
|
179 |
if (childrenDirs != null) { |
180 |
for (String childrenDir : childrenDirs) { |
181 |
File sourceLocation = new File(filesFolder, childrenDir); |
182 |
File targetLocation = new File(pluginsDirectory, childrenDir); |
183 |
try {
|
184 |
copyFilesAndFolders(sourceLocation, targetLocation); |
185 |
} catch (Throwable th) { |
186 |
this.warning("Can't copy files to '" + targetLocation.getAbsolutePath() + "'.", th); |
187 |
} |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
private boolean copyFilesAndFolders(File source, File target) |
194 |
throws IOException { |
195 |
|
196 |
if (source.isDirectory()) {
|
197 |
if (!target.exists()) {
|
198 |
target.mkdir(); |
199 |
} |
200 |
|
201 |
String[] children = source.list(); |
202 |
if (children != null) { |
203 |
for (String children1 : children) { |
204 |
copyFilesAndFolders( |
205 |
new File(source, children1), |
206 |
new File(target, children1) |
207 |
); |
208 |
} |
209 |
} |
210 |
} else {
|
211 |
InputStream in = null; |
212 |
OutputStream out = null; |
213 |
try {
|
214 |
in = new FileInputStream(source); |
215 |
out = new FileOutputStream(target); |
216 |
|
217 |
// Copy the bits from instream to outstream
|
218 |
byte[] buf = new byte[1024]; |
219 |
int len;
|
220 |
while ((len = in.read(buf)) > 0) { |
221 |
out.write(buf, 0, len);
|
222 |
} |
223 |
in.close(); |
224 |
out.close(); |
225 |
} catch (Throwable th) { |
226 |
this.warning("Can't copy '" + source + "' to '" + target + "'.", th); |
227 |
} finally {
|
228 |
closeQuietly(in, source); |
229 |
closeQuietly(out, target); |
230 |
} |
231 |
} |
232 |
|
233 |
return true; |
234 |
} |
235 |
|
236 |
private void closeQuietly(Closeable closeable, File f) { |
237 |
if (closeable == null) { |
238 |
return;
|
239 |
} |
240 |
try {
|
241 |
closeable.close(); |
242 |
} catch (IOException ex) { |
243 |
if (f == null) { |
244 |
this.warning("Can't close file '" + closeable.toString() + "'.", ex); |
245 |
} else {
|
246 |
this.warning("Can't close file '" + f.getAbsolutePath() + "'.", ex); |
247 |
} |
248 |
} |
249 |
} |
250 |
|
251 |
private void executeAntFile(File file) { |
252 |
Project p = new Project();
|
253 |
p.setUserProperty("ant.file", file.getAbsolutePath());
|
254 |
p.setUserProperty("gvsig_dir", getApplicationDirectory().getAbsolutePath());
|
255 |
p.setUserProperty("extensions_dir", getPluginsDirectory().getAbsolutePath());
|
256 |
p.init(); |
257 |
p.setBaseDir(file.getParentFile()); |
258 |
p.addTaskDefinition("antform", AntForm.class);
|
259 |
p.addTaskDefinition("antmenu", AntMenu.class);
|
260 |
p.init(); |
261 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
262 |
p.addReference("ant.projectHelper", helper);
|
263 |
helper.parse(p, file); |
264 |
p.executeTarget(p.getDefaultTarget()); |
265 |
} |
266 |
|
267 |
private void warning(String message, Throwable th) { |
268 |
System.err.println(message);
|
269 |
th.printStackTrace(); |
270 |
} |
271 |
|
272 |
} |