Revision 42681

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami.updater/src/main/java/org/gvsig/andamiupdater/Updater.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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 3
9
 * of the License, or (at your option) any later version.
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 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.
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 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.
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.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.andamiupdater;
25 24

  
26 25
import java.io.BufferedReader;
27
import java.io.DataInputStream;
28 26
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileOutputStream;
31 27
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.OutputStream;
35 28
import java.lang.reflect.Method;
36 29

  
37 30
import com.sardak.antform.AntForm;
38 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;
39 41

  
40 42
import org.apache.tools.ant.Project;
41 43
import org.apache.tools.ant.ProjectHelper;
......
43 45
/**
44 46
 * @author gvSIG Team
45 47
 * @version $Id$
46
 * 
48
 *
47 49
 */
48 50
public class Updater {
49 51

  
......
69 71
    }
70 72

  
71 73
    private File getApplicationDirectory() {
72
        String appDir = new File("").getAbsolutePath();
73
        return new File(appDir);
74
        return new File("").getAbsoluteFile();
74 75
    }
75 76

  
76 77
    private File getPluginsDirectory() {
......
85 86
    private void launchApp(String[] args) throws Exception {
86 87

  
87 88
        try {
88

  
89
            Class launcher_class = Class.forName("com.iver.andami.Launcher");
90

  
89
            Class launcher_class = Class.forName("org.gvsig.andami.Launcher");
91 90
            Object launcher = launcher_class.newInstance();
92
            Method main =
93
                launcher_class
94
                    .getMethod("main", new Class[] { String[].class });
91
            Method main = launcher_class.getMethod("main", new Class[]{String[].class});
92
            main.invoke(launcher, new Object[]{args});
95 93

  
96
            main.invoke(launcher, new Object[] { args });
97

  
98 94
        } catch (ClassNotFoundException e) {
99

  
100
            Class launcher_class = Class.forName("org.gvsig.andami.Launcher");
95
            Class launcher_class = Class.forName("com.iver.andami.Launcher");
101 96
            Object launcher = launcher_class.newInstance();
102
            Method main =
103
                launcher_class
104
                    .getMethod("main", new Class[] { String[].class });
105

  
106
            main.invoke(launcher, new Object[] { args });
97
            Method main = launcher_class.getMethod("main", new Class[]{String[].class});
98
            main.invoke(launcher, new Object[]{args});
107 99
        }
108 100
    }
109 101

  
110 102
    /**
111
     * 
103
     *
112 104
     */
113 105
    private void removeAll() {
114
        File updateDirectory =
115
            new File(getApplicationDirectory().toString() + File.separator
116
                + "update");
106
        File updateDirectory = new File(getApplicationDirectory(), "update");
117 107
        if (updateDirectory.exists()) {
118 108
            deleteDir(updateDirectory);
119 109
        }
120 110

  
121 111
    }
122 112

  
123
    private boolean deleteDir(File dir) {
124
        if (dir.isDirectory()) {
125
            String[] children = dir.list();
126
            for (int i = 0; i < children.length; i++) {
127
                boolean success = deleteDir(new File(dir, children[i]));
128
                if (!success) {
129
                    return false;
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
                    }
130 125
                }
131 126
            }
127
            return folder.delete();
128
        } catch (Throwable th) {
129
            this.warning("Can't remove '" + folder.getAbsolutePath() + "'.", th);
130
            return false;
132 131
        }
133

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

  
138 134
    /**
......
140 136
     * file that this line points to
141 137
     */
142 138
    private void executeScripts() {
139
        File updateFolder = new File(getApplicationDirectory(), "update");
140
        File antFilesScript = new File(updateFolder, "scripts.lst");
143 141

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

  
149 142
        if (antFilesScript.exists()) {
143
            BufferedReader reader = null;
150 144
            try {
151

  
152
                FileInputStream fstream = new FileInputStream(antFilesScript);
153

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

  
160
                // Read File Line By Line
161
                while ((strLine = br.readLine()) != null) {
162
                    // Print the content on the console
163
                    File antFile = new File(strLine);
164
                    executeAntFile(antFile);
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
                    }
165 154
                }
166

  
167
                // Close the input stream
168
                in.close();
169

  
170
            } catch (Exception e) {// Catch exception if any
171
                System.err.println("Error: " + e.getMessage());
155
            } catch (Throwable th) {
156
                this.warning("Error running ant scripts.", th);
157
            } finally {
158
                closeQuietly(reader, antFilesScript);
172 159
            }
173 160

  
174 161
        }
......
177 164

  
178 165
    /**
179 166
     * Copies all files and folders from gvSIG/update to gvSIG/extensions
180
     * 
167
     *
181 168
     * @throws IOException
182
     * 
169
     *
183 170
     */
184 171
    private void copyFiles() throws IOException {
185
        File updateFolder =
186
            new File(getApplicationDirectory().toString() + File.separator
187
                + "update");
188
        File filesFolder =
189
            new File(updateFolder.toString() + File.separator + "files");
172
        File updateFolder = new File(getApplicationDirectory(), "update");
173
        File filesFolder = new File(updateFolder, "files");
190 174
        File pluginsDirectory = getPluginsDirectory();
191 175

  
192 176
        if (updateFolder.exists()) {
193 177
            String[] childrenDirs = filesFolder.list();
194 178

  
195 179
            if (childrenDirs != null) {
196
                for (int i = 0; i < childrenDirs.length; i++) {
197
                    File sourceLocation =
198
                        new File(filesFolder, childrenDirs[i]);
199
                    File targetLocation =
200
                        new File(pluginsDirectory, childrenDirs[i]);
201
                    // lo copio todo
202
                    copyFilesAndFolders(sourceLocation, targetLocation);
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
                    }
203 188
                }
204 189
            }
205 190
        }
206 191
    }
207 192

  
208
    private boolean copyFilesAndFolders(File sourceLocation, File targetLocation)
209
        throws IOException {
193
    private boolean copyFilesAndFolders(File source, File target)
194
            throws IOException {
210 195

  
211
        if (sourceLocation.isDirectory()) {
212
            if (!targetLocation.exists()) {
213
                targetLocation.mkdir();
196
        if (source.isDirectory()) {
197
            if (!target.exists()) {
198
                target.mkdir();
214 199
            }
215 200

  
216
            String[] children = sourceLocation.list();
201
            String[] children = source.list();
217 202
            if (children != null) {
218
                for (int i = 0; i < children.length; i++) {
219
                    copyFilesAndFolders(new File(sourceLocation, children[i]),
220
                        new File(targetLocation, children[i]));
203
                for (String children1 : children) {
204
                    copyFilesAndFolders(
205
                            new File(source, children1),
206
                            new File(target, children1)
207
                    );
221 208
                }
222 209
            }
223 210
        } else {
224
            InputStream in = new FileInputStream(sourceLocation);
225
            OutputStream out = new FileOutputStream(targetLocation);
211
            InputStream in = null;
212
            OutputStream out = null;
213
            try {
214
                in = new FileInputStream(source);
215
                out = new FileOutputStream(target);
226 216

  
227
            // Copy the bits from instream to outstream
228
            byte[] buf = new byte[1024];
229
            int len;
230
            while ((len = in.read(buf)) > 0) {
231
                out.write(buf, 0, len);
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);
232 230
            }
233
            in.close();
234
            out.close();
235 231
        }
236 232

  
237 233
        return true;
238 234
    }
239 235

  
240
    // PRIVATE VOID EXECUTEANTFILE(FILE FILE) {
241
    // PROJECT P = NEW PROJECT();
242
    // P.SETUSERPROPERTY("ANT.FILE", FILE.GETABSOLUTEPATH());
243
    // P.SETPROPERTY("GVSIG_DIR", GETAPPLICATIONDIRECTORY().GETABSOLUTEPATH());
244
    // P
245
    // .SETPROPERTY("EXTENSIONS_DIR", GETPLUGINSDIRECTORY()
246
    // .GETABSOLUTEPATH());
247
    // P.INIT();
248
    // PROJECTHELPER HELPER = PROJECTHELPER.GETPROJECTHELPER();
249
    // P.ADDREFERENCE("ANT.PROJECTHELPER", HELPER);
250
    // HELPER.PARSE(P, FILE);
251
    // P.EXECUTETARGET(P.GETDEFAULTTARGET());
252
    // }
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
    }
253 250

  
254 251
    private void executeAntFile(File file) {
255 252
        Project p = new Project();
256 253
        p.setUserProperty("ant.file", file.getAbsolutePath());
257
        p.setUserProperty("gvsig_dir", getApplicationDirectory()
258
            .getAbsolutePath());
259
        p.setUserProperty("extensions_dir", getPluginsDirectory()
260
            .getAbsolutePath());
254
        p.setUserProperty("gvsig_dir", getApplicationDirectory().getAbsolutePath());
255
        p.setUserProperty("extensions_dir", getPluginsDirectory().getAbsolutePath());
261 256
        p.init();
262 257
        p.setBaseDir(file.getParentFile());
263 258
        p.addTaskDefinition("antform", AntForm.class);
......
269 264
        p.executeTarget(p.getDefaultTarget());
270 265
    }
271 266

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

  
272 272
}

Also available in: Unified diff