Revision 36183

View differences:

tags/v2_0_0_Build_2032/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

  
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

  
tags/v2_0_0_Build_2032/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
	</dependencies>
30
	<build>
31
        <plugins>
32
           <plugin>
33
                <groupId>org.apache.maven.plugins</groupId>
34
                <artifactId>maven-jar-plugin</artifactId>
35
                <configuration>
36
                    <archive>
37
                        <manifest>
38
                            <addClasspath>true</addClasspath>
39
                            <mainClass>org.gvsig.andamiupdater.Updater</mainClass>
40
                        </manifest>
41
                    </archive>
42
                </configuration>
43
            </plugin>
44
        </plugins>
45
    </build>
46
    <profiles>
47
    	<profile>
48
            <id>install-extension</id>
49
            <activation>
50
                <activeByDefault>true</activeByDefault>
51
                <property>
52
                    <name>install-extension</name>
53
                </property>
54
            </activation>
55
            <build>
56
                <plugins>
57
                    <plugin>
58
                        <groupId>org.apache.maven.plugins
59
						</groupId>
60
                        <artifactId>maven-dependency-plugin
61
						</artifactId>
62
                        <executions>
63
                            <execution>
64
                                <id>copy-own-dependencies-for-andamiupdater
65
								</id>
66
                                <phase>install</phase>
67
                                <goals>
68
                                    <goal>copy-dependencies</goal>
69
                                </goals>
70
                                <configuration>
71
                                    <outputDirectory>${andami.lib.dir}</outputDirectory>
72
                                    <overWriteReleases>true</overWriteReleases>
73
                                    <overWriteSnapshots>true</overWriteSnapshots>
74
                                    <overWriteIfNewer>true</overWriteIfNewer>
75
                                    <includeScope>runtime</includeScope>
76
                                </configuration>
77
                            </execution>
78
                            <execution>
79
                                <id>copy-andamiupdater</id>
80
                                <phase>install</phase>
81
                                <goals>
82
                                    <goal>copy</goal>
83
                                </goals>
84
                                <configuration>
85
                                    <artifactItems>
86
                                        <artifactItem>
87
                                            <groupId>org.gvsig</groupId>
88
                                            <artifactId>org.gvsig.andamiupdater</artifactId>
89
                                            <version>1.0.0-SNAPSHOT</version>
90
                                            <type>jar</type>
91
                                            <overWrite>true</overWrite>
92
                                        </artifactItem>
93
                                    </artifactItems>
94
                                    <outputDirectory>${andami.lib.dir}</outputDirectory>
95
                                    <overWriteReleases>true</overWriteReleases>
96
                                    <overWriteSnapshots>false</overWriteSnapshots>
97
                                    <overWriteIfNewer>true</overWriteIfNewer>
98
                                    <excludeTransitive>false</excludeTransitive>
99
                                </configuration>
100
                            </execution>
101
                        </executions>
102
                    </plugin>
103
                </plugins>
104
            </build>
105
        </profile>
106
     </profiles>
107
</project>
0 108

  

Also available in: Unified diff