Revision 37004

View differences:

tags/v2_0_0_Build_2038/applications/_fwAndami-updater/src/main/java/org/gvsig/andamiupdater/Updater.java
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
}
0 271

  
tags/v2_0_0_Build_2038/applications/_fwAndami-updater/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2
	<modelVersion>4.0.0</modelVersion>
3
	<groupId>org.gvsig</groupId>
4
	<artifactId>org.gvsig.andamiupdater</artifactId>
5
	<packaging>jar</packaging>
6
	<name>org.gvsig.andamiupdater</name>
7
	<version>1.0.0-SNAPSHOT</version>
8
	<parent>
9
        <groupId>org.gvsig</groupId>
10
        <artifactId>gvsig-base-library-pom</artifactId>
11
        <version>2.0-SNAPSHOT</version>
12
    </parent>
13
	<properties>
14
		<build-dir>${basedir}/../build</build-dir>
15
        <andami.lib.dir>${gvsig.install.dir}/lib</andami.lib.dir>
16
    </properties>
17
	<dependencies>
18
		<dependency>
19
	    	<groupId>org.gvsig</groupId>
20
    		<artifactId>org.gvsig.andami</artifactId>
21
		    <version>2.0-SNAPSHOT</version>
22
            <scope>compile</scope>
23
        </dependency>
24
        <dependency>
25
			<groupId>org.apache.ant</groupId>
26
			<artifactId>ant</artifactId>
27
            <scope>compile</scope>
28
		</dependency>
29
        <dependency>
30
            <groupId>com.sardak</groupId>
31
            <artifactId>antform</artifactId>
32
            <scope>compile</scope>
33
        </dependency>
34
        <dependency>
35
            <groupId>ant-contrib</groupId>
36
            <artifactId>ant-contrib</artifactId>
37
            <scope>runtime</scope>
38
        </dependency>
39
	</dependencies>
40
	<build>
41
        <plugins>
42
           <plugin>
43
                <groupId>org.apache.maven.plugins</groupId>
44
                <artifactId>maven-jar-plugin</artifactId>
45
                <configuration>
46
                    <archive>
47
                        <manifest>
48
                            <addClasspath>true</addClasspath>
49
                            <mainClass>org.gvsig.andamiupdater.Updater</mainClass>
50
                        </manifest>
51
                    </archive>
52
                </configuration>
53
            </plugin>
54
        </plugins>
55
    </build>
56
    <profiles>
57
    	<profile>
58
            <id>install-extension</id>
59
            <activation>
60
                <activeByDefault>true</activeByDefault>
61
                <property>
62
                    <name>install-extension</name>
63
                </property>
64
            </activation>
65
            <build>
66
                <plugins>
67
                    <plugin>
68
                        <groupId>org.apache.maven.plugins
69
						</groupId>
70
                        <artifactId>maven-dependency-plugin
71
						</artifactId>
72
                        <executions>
73
                            <execution>
74
                                <id>copy-own-dependencies-for-andamiupdater
75
								</id>
76
                                <phase>install</phase>
77
                                <goals>
78
                                    <goal>copy-dependencies</goal>
79
                                </goals>
80
                                <configuration>
81
                                    <outputDirectory>${andami.lib.dir}</outputDirectory>
82
                                    <overWriteReleases>true</overWriteReleases>
83
                                    <overWriteSnapshots>true</overWriteSnapshots>
84
                                    <overWriteIfNewer>true</overWriteIfNewer>
85
                                    <includeScope>runtime</includeScope>
86
                                </configuration>
87
                            </execution>
88
                            <execution>
89
                                <id>copy-andamiupdater</id>
90
                                <phase>install</phase>
91
                                <goals>
92
                                    <goal>copy</goal>
93
                                </goals>
94
                                <configuration>
95
                                    <artifactItems>
96
                                        <artifactItem>
97
                                            <groupId>org.gvsig</groupId>
98
                                            <artifactId>org.gvsig.andamiupdater</artifactId>
99
                                            <version>1.0.0-SNAPSHOT</version>
100
                                            <type>jar</type>
101
                                            <overWrite>true</overWrite>
102
                                        </artifactItem>
103
                                    </artifactItems>
104
                                    <outputDirectory>${andami.lib.dir}</outputDirectory>
105
                                    <overWriteReleases>true</overWriteReleases>
106
                                    <overWriteSnapshots>false</overWriteSnapshots>
107
                                    <overWriteIfNewer>true</overWriteIfNewer>
108
                                    <excludeTransitive>false</excludeTransitive>
109
                                </configuration>
110
                            </execution>
111
                        </executions>
112
                    </plugin>
113
                </plugins>
114
            </build>
115
        </profile>
116
     </profiles>
117
</project>
0 118

  

Also available in: Unified diff