Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2035 / applications / _fwAndami-updater / src / main / java / org / gvsig / andamiupdater / Updater.java @ 36274

History | View | Annotate | Download (8.23 KB)

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
import java.lang.reflect.Method;
34

    
35
import com.sardak.antform.AntForm;
36
import com.sardak.antform.AntMenu;
37

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

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

    
48
    private String[] args;
49

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

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

    
69
    private File getApplicationDirectory() {
70
        String appDir = new File("").getAbsolutePath();
71
        return new File(appDir);
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

    
87
            Class launcher_class = Class.forName("com.iver.andami.Launcher");
88

    
89
            Object launcher = launcher_class.newInstance();
90
            Method main =
91
                launcher_class
92
                    .getMethod("main", new Class[] { String[].class });
93

    
94
            main.invoke(launcher, new Object[] { args });
95

    
96
        } catch (ClassNotFoundException e) {
97

    
98
            Class launcher_class = Class.forName("org.gvsig.andami.Launcher");
99
            Object launcher = launcher_class.newInstance();
100
            Method main =
101
                launcher_class
102
                    .getMethod("main", new Class[] { String[].class });
103

    
104
            main.invoke(launcher, new Object[] { args });
105
        }
106
    }
107

    
108
    /**
109
     * 
110
     */
111
    private void removeAll() {
112
        File updateDirectory =
113
            new File(getApplicationDirectory().toString() + File.separator
114
                + "update");
115
        if (updateDirectory.exists()) {
116
            deleteDir(updateDirectory);
117
        }
118

    
119
    }
120

    
121
    private boolean deleteDir(File dir) {
122
        if (dir.isDirectory()) {
123
            String[] children = dir.list();
124
            for (int i = 0; i < children.length; i++) {
125
                boolean success = deleteDir(new File(dir, children[i]));
126
                if (!success) {
127
                    return false;
128
                }
129
            }
130
        }
131

    
132
        // The directory is now empty so delete it
133
        return dir.delete();
134
    }
135

    
136
    /**
137
     * Reads every line of appDirectory/update/scripts.lst and executes the ant
138
     * file that this line points to
139
     */
140
    private void executeScripts() {
141

    
142
        String antFilesScriptPath =
143
            getApplicationDirectory() + File.separator + "update"
144
                + File.separator + "scripts.lst";
145
        File antFilesScript = new File(antFilesScriptPath);
146

    
147
        if (antFilesScript.exists()) {
148
            try {
149

    
150
                FileInputStream fstream = new FileInputStream(antFilesScript);
151

    
152
                // Get the object of DataInputStream
153
                DataInputStream in = new DataInputStream(fstream);
154
                BufferedReader br =
155
                    new BufferedReader(new InputStreamReader(in));
156
                String strLine;
157

    
158
                // Read File Line By Line
159
                while ((strLine = br.readLine()) != null) {
160
                    // Print the content on the console
161
                    File antFile = new File(strLine);
162
                    executeAntFile(antFile);
163
                }
164

    
165
                // Close the input stream
166
                in.close();
167

    
168
            } catch (Exception e) {// Catch exception if any
169
                System.err.println("Error: " + e.getMessage());
170
            }
171

    
172
        }
173

    
174
    }
175

    
176
    /**
177
     * Copies all files and folders from gvSIG/update to gvSIG/extensions
178
     * 
179
     * @throws IOException
180
     * 
181
     */
182
    private void copyFiles() throws IOException {
183
        File updateFolder =
184
            new File(getApplicationDirectory().toString() + File.separator
185
                + "update");
186
        File filesFolder =
187
            new File(updateFolder.toString() + File.separator + "files");
188
        File pluginsDirectory = getPluginsDirectory();
189

    
190
        if (updateFolder.exists()) {
191
            String[] childrenDirs = filesFolder.list();
192

    
193
            if (childrenDirs != null) {
194
                for (int i = 0; i < childrenDirs.length; i++) {
195
                    File sourceLocation =
196
                        new File(filesFolder, childrenDirs[i]);
197
                    File targetLocation =
198
                        new File(pluginsDirectory, childrenDirs[i]);
199
                    // lo copio todo
200
                    copyFilesAndFolders(sourceLocation, targetLocation);
201
                }
202
            }
203
        }
204
    }
205

    
206
    private boolean copyFilesAndFolders(File sourceLocation, File targetLocation)
207
        throws IOException {
208

    
209
        if (sourceLocation.isDirectory()) {
210
            if (!targetLocation.exists()) {
211
                targetLocation.mkdir();
212
            }
213

    
214
            String[] children = sourceLocation.list();
215
            if (children != null) {
216
                for (int i = 0; i < children.length; i++) {
217
                    copyFilesAndFolders(new File(sourceLocation, children[i]),
218
                        new File(targetLocation, children[i]));
219
                }
220
            }
221
        } else {
222
            InputStream in = new FileInputStream(sourceLocation);
223
            OutputStream out = new FileOutputStream(targetLocation);
224

    
225
            // Copy the bits from instream to outstream
226
            byte[] buf = new byte[1024];
227
            int len;
228
            while ((len = in.read(buf)) > 0) {
229
                out.write(buf, 0, len);
230
            }
231
            in.close();
232
            out.close();
233
        }
234

    
235
        return true;
236
    }
237

    
238
    // PRIVATE VOID EXECUTEANTFILE(FILE FILE) {
239
    // PROJECT P = NEW PROJECT();
240
    // P.SETUSERPROPERTY("ANT.FILE", FILE.GETABSOLUTEPATH());
241
    // P.SETPROPERTY("GVSIG_DIR", GETAPPLICATIONDIRECTORY().GETABSOLUTEPATH());
242
    // P
243
    // .SETPROPERTY("EXTENSIONS_DIR", GETPLUGINSDIRECTORY()
244
    // .GETABSOLUTEPATH());
245
    // P.INIT();
246
    // PROJECTHELPER HELPER = PROJECTHELPER.GETPROJECTHELPER();
247
    // P.ADDREFERENCE("ANT.PROJECTHELPER", HELPER);
248
    // HELPER.PARSE(P, FILE);
249
    // P.EXECUTETARGET(P.GETDEFAULTTARGET());
250
    // }
251

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

    
270
}