Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami.updater / src / main / java / org / gvsig / andamiupdater / Updater.java @ 42753

History | View | Annotate | Download (8.5 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

    
39
import org.apache.tools.ant.Project;
40
import org.apache.tools.ant.ProjectHelper;
41

    
42
/**
43
 * @author gvSIG Team
44
 * @version $Id$
45
 *
46
 */
47
public class Updater {
48

    
49
    private String[] args;
50

    
51
    /**
52
     * @param args
53
     */
54
    public Updater(String[] args) {
55
        this.args = args;
56
    }
57

    
58
    /**
59
     * @param args
60
     * @throws Exception
61
     */
62
    public static void main(String[] args) throws Exception {
63
        Updater updater = new Updater(args);
64
        updater.copyFiles();
65
        updater.executeScripts();
66
        updater.removeAll();
67
        updater.launchApp(args);
68
    }
69

    
70
    private File getApplicationDirectory() {
71
        return new File("").getAbsoluteFile();
72
    }
73

    
74
    private File getPluginsDirectory() {
75
        return new File(getApplicationDirectory(), this.args[1]);
76
    }
77

    
78
    /**
79
     * @param args
80
     * @throws Exception
81
     */
82
    @SuppressWarnings("unchecked")
83
    private void launchApp(String[] args) throws Exception {
84

    
85
        try {
86
            Class launcher_class = Class.forName("org.gvsig.andami.Launcher");
87
            Object launcher = launcher_class.newInstance();
88
            Method main = launcher_class.getMethod("main", new Class[]{String[].class});
89
            main.invoke(launcher, new Object[]{args});
90

    
91
        } catch (ClassNotFoundException e) {
92
            Class launcher_class = Class.forName("com.iver.andami.Launcher");
93
            Object launcher = launcher_class.newInstance();
94
            Method main = launcher_class.getMethod("main", new Class[]{String[].class});
95
            main.invoke(launcher, new Object[]{args});
96
        }
97
    }
98

    
99
    /**
100
     *
101
     */
102
    private void removeAll() {
103
        File updateDirectory = new File(getApplicationDirectory(), "update");
104
        if (updateDirectory.exists()) {
105
            deleteDir(updateDirectory);
106
        }
107

    
108
    }
109

    
110
    private boolean deleteDir(File folder) {
111
        if (folder == null) {
112
            return true;
113
        }
114
        try {
115
            if (folder.isDirectory()) {
116
                String[] children = folder.list();
117
                for (String children1 : children) {
118
                    boolean success = deleteDir(new File(folder, children1));
119
                    if (!success) {
120
                        return false;
121
                    }
122
                }
123
            }
124
            return folder.delete();
125
        } catch (Throwable th) {
126
            this.warning("Can't remove '" + folder.getAbsolutePath() + "'.", th);
127
            return false;
128
        }
129
    }
130

    
131
    /**
132
     * Reads every line of appDirectory/update/scripts.lst and executes the ant
133
     * file that this line points to
134
     */
135
    private void executeScripts() {
136
        File updateFolder = new File(getApplicationDirectory(), "update");
137
        File antFilesScript = new File(updateFolder, "scripts.lst");
138

    
139
        if (antFilesScript.exists()) {
140
            BufferedReader reader = null;
141
            try {
142
                reader = new BufferedReader(new FileReader(antFilesScript));
143
                String line;
144
                while ((line = reader.readLine()) != null) {
145
                    File antFile = new File(line);
146
                    try {
147
                        executeAntFile(antFile);
148
                    } catch (Throwable th) {
149
                        this.warning("Error running file '" + antFile.getAbsolutePath() + "'.", th);
150
                    }
151
                }
152
            } catch (Throwable th) {
153
                this.warning("Error running ant scripts.", th);
154
            } finally {
155
                closeQuietly(reader, antFilesScript);
156
            }
157

    
158
        }
159

    
160
    }
161

    
162
    /**
163
     * Copies all files and folders from gvSIG/update to gvSIG/extensions
164
     *
165
     * @throws IOException
166
     *
167
     */
168
    private void copyFiles() throws IOException {
169
        File updateFolder = new File(getApplicationDirectory(), "update");
170
        File filesFolder = new File(updateFolder, "files");
171
        File pluginsDirectory = getPluginsDirectory();
172

    
173
        if (updateFolder.exists()) {
174
            String[] childrenDirs = filesFolder.list();
175

    
176
            if (childrenDirs != null) {
177
                for (String childrenDir : childrenDirs) {
178
                    File sourceLocation = new File(filesFolder, childrenDir);
179
                    File targetLocation = new File(pluginsDirectory, childrenDir);
180
                    try {
181
                        deleteDir(targetLocation);
182
                        copyFilesAndFolders(sourceLocation, targetLocation);
183
                    } catch (Throwable th) {
184
                        this.warning("Can't copy files to '" + targetLocation.getAbsolutePath() + "'.", th);
185
                    }
186
                }
187
            }
188
        }
189
    }
190

    
191
    private boolean copyFilesAndFolders(File source, File target)
192
            throws IOException {
193

    
194
        if (source.isDirectory()) {
195
            if (!target.exists()) {
196
                target.mkdir();
197
            }
198

    
199
            String[] children = source.list();
200
            if (children != null) {
201
                for (String children1 : children) {
202
                    copyFilesAndFolders(
203
                            new File(source, children1),
204
                            new File(target, children1)
205
                    );
206
                }
207
            }
208
        } else {
209
            InputStream in = null;
210
            OutputStream out = null;
211
            try {
212
                in = new FileInputStream(source);
213
                out = new FileOutputStream(target);
214

    
215
                // Copy the bits from instream to outstream
216
                byte[] buf = new byte[1024];
217
                int len;
218
                while ((len = in.read(buf)) > 0) {
219
                    out.write(buf, 0, len);
220
                }
221
                in.close();
222
                out.close();
223
            } catch (Throwable th) {
224
                this.warning("Can't copy '" + source + "' to '" + target + "'.", th);
225
            } finally {
226
                closeQuietly(in, source);
227
                closeQuietly(out, target);
228
            }
229
        }
230

    
231
        return true;
232
    }
233

    
234
    private void closeQuietly(Closeable closeable, File f) {
235
        if (closeable == null) {
236
            return;
237
        }
238
        try {
239
            closeable.close();
240
        } catch (IOException ex) {
241
            if (f == null) {
242
                this.warning("Can't close file '" + closeable.toString() + "'.", ex);
243
            } else {
244
                this.warning("Can't close file '" + f.getAbsolutePath() + "'.", ex);
245
            }
246
        }
247
    }
248

    
249
    private void executeAntFile(File file) {
250
        Project p = new Project();
251
        p.setUserProperty("ant.file", file.getAbsolutePath());
252
        p.setUserProperty("gvsig_dir", getApplicationDirectory().getAbsolutePath());
253
        p.setUserProperty("extensions_dir", getPluginsDirectory().getAbsolutePath());
254
        p.init();
255
        p.setBaseDir(file.getParentFile());
256
        p.addTaskDefinition("antform", AntForm.class);
257
        p.addTaskDefinition("antmenu", AntMenu.class);
258
        p.init();
259
        ProjectHelper helper = ProjectHelper.getProjectHelper();
260
        p.addReference("ant.projectHelper", helper);
261
        helper.parse(p, file);
262
        p.executeTarget(p.getDefaultTarget());
263
    }
264

    
265
    private void warning(String message, Throwable th) {
266
        System.err.println(message);
267
        th.printStackTrace();
268
    }
269

    
270
}