|
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 |
package org.gvsig.andamiupdater;
|
|
23 |
|
|
24 |
import java.io.BufferedReader;
|
|
25 |
import java.io.DataInputStream;
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.FileInputStream;
|
|
28 |
import java.io.FileOutputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.io.InputStreamReader;
|
|
32 |
import java.io.OutputStream;
|
|
33 |
|
|
34 |
import org.apache.tools.ant.Project;
|
|
35 |
import org.apache.tools.ant.ProjectHelper;
|
|
36 |
|
|
37 |
import org.gvsig.andami.Launcher;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* @author gvSIG Team
|
|
41 |
* @version $Id$
|
|
42 |
*
|
|
43 |
*/
|
|
44 |
public class Updater {
|
|
45 |
|
|
46 |
private String[] args;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* @param args
|
|
50 |
*/
|
|
51 |
public Updater(String[] args) {
|
|
52 |
this.args = args;
|
|
53 |
}
|
|
54 |
|
|
55 |
/**
|
|
56 |
* @param args
|
|
57 |
* @throws Exception
|
|
58 |
*/
|
|
59 |
public static void main(String[] args) throws Exception {
|
|
60 |
Updater updater = new Updater(args);
|
|
61 |
updater.copyFiles();
|
|
62 |
updater.executeScripts();
|
|
63 |
updater.removeAll();
|
|
64 |
updater.launchApp(args);
|
|
65 |
}
|
|
66 |
|
|
67 |
private File getApplicationDirectory() {
|
|
68 |
String appDir = new File("").getAbsolutePath();
|
|
69 |
return new File(appDir);
|
|
70 |
}
|
|
71 |
|
|
72 |
private File getPluginsDirectory() {
|
|
73 |
return new File(getApplicationDirectory(), this.args[1]);
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* @param args
|
|
78 |
* @throws Exception
|
|
79 |
*/
|
|
80 |
private void launchApp(String[] args) throws Exception {
|
|
81 |
Launcher.main(args);
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
*
|
|
86 |
*/
|
|
87 |
private void removeAll() {
|
|
88 |
File updateDirectory =
|
|
89 |
new File(getApplicationDirectory().toString() + File.separator
|
|
90 |
+ "update");
|
|
91 |
if (updateDirectory.exists()) {
|
|
92 |
deleteDir(updateDirectory);
|
|
93 |
}
|
|
94 |
|
|
95 |
}
|
|
96 |
|
|
97 |
private boolean deleteDir(File dir) {
|
|
98 |
if (dir.isDirectory()) {
|
|
99 |
String[] children = dir.list();
|
|
100 |
for (int i = 0; i < children.length; i++) {
|
|
101 |
boolean success = deleteDir(new File(dir, children[i]));
|
|
102 |
if (!success) {
|
|
103 |
return false;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
// The directory is now empty so delete it
|
|
109 |
return dir.delete();
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Reads every line of appDirectory/update/scripts.lst and executes the ant
|
|
114 |
* file that this line points to
|
|
115 |
*/
|
|
116 |
private void executeScripts() {
|
|
117 |
|
|
118 |
String antFilesScriptPath =
|
|
119 |
getApplicationDirectory() + File.separator + "update"
|
|
120 |
+ File.separator + "scripts.lst";
|
|
121 |
File antFilesScript = new File(antFilesScriptPath);
|
|
122 |
|
|
123 |
try {
|
|
124 |
|
|
125 |
FileInputStream fstream = new FileInputStream(antFilesScript);
|
|
126 |
|
|
127 |
// Get the object of DataInputStream
|
|
128 |
DataInputStream in = new DataInputStream(fstream);
|
|
129 |
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
|
130 |
String strLine;
|
|
131 |
|
|
132 |
// Read File Line By Line
|
|
133 |
while ((strLine = br.readLine()) != null) {
|
|
134 |
// Print the content on the console
|
|
135 |
File antFile = new File(strLine);
|
|
136 |
executeAntFile(antFile);
|
|
137 |
}
|
|
138 |
|
|
139 |
// Close the input stream
|
|
140 |
in.close();
|
|
141 |
} catch (Exception e) {// Catch exception if any
|
|
142 |
System.err.println("Error: " + e.getMessage());
|
|
143 |
}
|
|
144 |
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Copies all files and folders from gvSIG/update to gvSIG/extensions
|
|
149 |
*
|
|
150 |
* @throws IOException
|
|
151 |
*
|
|
152 |
*/
|
|
153 |
private void copyFiles() throws IOException {
|
|
154 |
File updateFolder =
|
|
155 |
new File(getApplicationDirectory().toString() + File.separator
|
|
156 |
+ "update");
|
|
157 |
File filesFolder =
|
|
158 |
new File(updateFolder.toString() + File.separator + "files");
|
|
159 |
File pluginsDirectory = getPluginsDirectory();
|
|
160 |
|
|
161 |
if (!updateFolder.exists()) {
|
|
162 |
// TODO: si no existe el directorio update es que no deberia haberse
|
|
163 |
// ejecutado este launcher
|
|
164 |
System.err.println("Error copying update files.");
|
|
165 |
System.err.println("The directory " + updateFolder.toString() + " does not exist!");
|
|
166 |
} else {
|
|
167 |
|
|
168 |
String[] childrenDirs = filesFolder.list();
|
|
169 |
|
|
170 |
if (childrenDirs != null) {
|
|
171 |
for (int i = 0; i < childrenDirs.length; i++) {
|
|
172 |
File sourceLocation =
|
|
173 |
new File(filesFolder, childrenDirs[i]);
|
|
174 |
File targetLocation =
|
|
175 |
new File(pluginsDirectory, childrenDirs[i]);
|
|
176 |
// lo copio todo
|
|
177 |
copyFilesAndFolders(sourceLocation, targetLocation);
|
|
178 |
}
|
|
179 |
} else {
|
|
180 |
System.err.println("Error copying update files.");
|
|
181 |
System.err.println("The directory " + updateFolder.toString() + " is empty!");
|
|
182 |
}
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
private boolean copyFilesAndFolders(File sourceLocation, File targetLocation)
|
|
187 |
throws IOException {
|
|
188 |
|
|
189 |
if (sourceLocation.isDirectory()) {
|
|
190 |
if (!targetLocation.exists()) {
|
|
191 |
targetLocation.mkdir();
|
|
192 |
}
|
|
193 |
|
|
194 |
String[] children = sourceLocation.list();
|
|
195 |
if (children != null) {
|
|
196 |
for (int i = 0; i < children.length; i++) {
|
|
197 |
copyFilesAndFolders(new File(sourceLocation, children[i]),
|
|
198 |
new File(targetLocation, children[i]));
|
|
199 |
}
|
|
200 |
}
|
|
201 |
} else {
|
|
202 |
InputStream in = new FileInputStream(sourceLocation);
|
|
203 |
OutputStream out = new FileOutputStream(targetLocation);
|
|
204 |
|
|
205 |
// Copy the bits from instream to outstream
|
|
206 |
byte[] buf = new byte[1024];
|
|
207 |
int len;
|
|
208 |
while ((len = in.read(buf)) > 0) {
|
|
209 |
out.write(buf, 0, len);
|
|
210 |
}
|
|
211 |
in.close();
|
|
212 |
out.close();
|
|
213 |
}
|
|
214 |
|
|
215 |
return true;
|
|
216 |
}
|
|
217 |
|
|
218 |
private void executeAntFile(File file) {
|
|
219 |
Project p = new Project();
|
|
220 |
p.setUserProperty("ant.file", file.getAbsolutePath());
|
|
221 |
p.setProperty("gvsig_dir", getApplicationDirectory().getAbsolutePath());
|
|
222 |
p
|
|
223 |
.setProperty("extensions_dir", getPluginsDirectory()
|
|
224 |
.getAbsolutePath());
|
|
225 |
p.init();
|
|
226 |
ProjectHelper helper = ProjectHelper.getProjectHelper();
|
|
227 |
p.addReference("ant.projectHelper", helper);
|
|
228 |
helper.parse(p, file);
|
|
229 |
p.executeTarget(p.getDefaultTarget());
|
|
230 |
}
|
|
231 |
|
|
232 |
}
|
0 |
233 |
|