Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2031 / extensions / org.gvsig.installer / org.gvsig.installer.prov / org.gvsig.installer.prov.plugin / src / main / java / org / gvsig / installer / prov / plugin / execution / PluginInstallerExecutionProvider.java @ 36096

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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.prov.plugin.execution;
29

    
30
import java.io.BufferedWriter;
31
import java.io.File;
32
import java.io.FileWriter;
33
import java.io.IOException;
34
import java.io.InputStream;
35

    
36
import org.apache.tools.ant.Project;
37
import org.apache.tools.ant.ProjectHelper;
38

    
39
import org.gvsig.installer.lib.api.PackageInfo;
40
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
41
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
42
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
43
import org.gvsig.installer.lib.spi.InstallerProviderManager;
44
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
45
import org.gvsig.tools.service.spi.AbstractProvider;
46
import org.gvsig.tools.service.spi.ProviderServices;
47

    
48
/**
49
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
50
 */
51
public class PluginInstallerExecutionProvider extends AbstractProvider
52
    implements InstallPackageProvider {
53

    
54
    private static final String ANT_FILE_NAME = "install.xml";
55
    private File pluginsDirectory = null;
56
    private File applicationDirectory = null;
57
    private File decompressDir = null;
58
    private static final InstallerProviderManager installerProviderManager =
59
        InstallerProviderLocator.getProviderManager();
60

    
61
    public PluginInstallerExecutionProvider(ProviderServices providerServices) {
62
        super(providerServices);
63
    }
64

    
65
    public class InstallerPluginsDirectoryNotFoundException extends
66
        InstallPackageServiceException {
67

    
68
        private static final long serialVersionUID = 4416143986837955880L;
69

    
70
        private static final String message = "Plugins directory not found";
71

    
72
        private static final String KEY = "plugins_directory_not_found";
73

    
74
        public InstallerPluginsDirectoryNotFoundException() {
75
            super(message, KEY, serialVersionUID);
76
        }
77

    
78
    }
79

    
80
    public void install(File applicationDirectory, InputStream inputStream,
81
        PackageInfo packageInfo) throws InstallPackageServiceException {
82

    
83
        this.applicationDirectory = applicationDirectory;
84
        pluginsDirectory =
85
            new File(applicationDirectory + File.separator + "gvSIG"
86
                + File.separator + "extensiones");
87
        try {
88
            if (!pluginsDirectory.exists()) {
89
                throw new InstallerPluginsDirectoryNotFoundException();
90
            }
91

    
92
            InstallPackageProviderServices installerProviderServices =
93
                installerProviderManager.createInstallerProviderServices();
94

    
95
            installerProviderServices.decompress(inputStream, pluginsDirectory);
96
            File antFile =
97
                new File(pluginsDirectory + File.separator
98
                    + packageInfo.getCode() + File.separator + "install"
99
                    + File.separator + ANT_FILE_NAME);
100

    
101
            if (antFile.exists()) {
102
                executeAntFile(antFile);
103
            }
104

    
105
        } catch (Exception e) {
106
            try {
107
                // if there is an exception, installLater is called
108
                installLater(applicationDirectory, inputStream, packageInfo);
109
            } catch (IOException e1) {
110
                e1.printStackTrace();
111
                throw new InstallPackageServiceException(e1);
112
            }
113
        }
114
    }
115

    
116
    /**
117
     * This function will is called when an exception is produced during the
118
     * installation of a plugin. It will prepare the package to be installed on
119
     * the next gvSIG restart.
120
     */
121
    public void installLater(File applicationDirectory,
122
        InputStream inputStream, PackageInfo packageInfo)
123
        throws InstallPackageServiceException, IOException {
124

    
125
        this.applicationDirectory = applicationDirectory;
126

    
127
        File updateDirectory =
128
            new File(applicationDirectory + File.separator + "update");
129

    
130
        if (!updateDirectory.exists()) {
131
            updateDirectory.mkdir();
132
        }
133

    
134
        File filesDirectory =
135
            new File(updateDirectory + File.separator + "files");
136
        if (!filesDirectory.exists()) {
137
            filesDirectory.mkdir();
138
        }
139

    
140
        File scriptsFile =
141
            new File(updateDirectory + File.separator + "scripts.lst");
142

    
143
        decompressDir =
144
            new File(filesDirectory.toString() + File.separator
145
                + packageInfo.getCode());
146
        if (decompressDir.exists()) {
147
            deleteDir(decompressDir);
148
        }
149

    
150
        InstallPackageProviderServices installerProviderServices =
151
            installerProviderManager.createInstallerProviderServices();
152
        installerProviderServices.decompress(inputStream, filesDirectory);
153

    
154
        File antFile =
155
            new File(decompressDir + File.separator + "install"
156
                + File.separator + ANT_FILE_NAME);
157

    
158
        if (antFile.exists()) {
159
            if (!scriptsFile.exists()) {
160
                scriptsFile.createNewFile();
161
            }
162
            FileWriter writer = new FileWriter(scriptsFile, true);
163
            BufferedWriter out = new BufferedWriter(writer);
164
            String str = antFile.toString();
165
            out.append(str + "\n");
166
            out.close();
167
        }
168

    
169
    }
170

    
171
    private void executeAntFile(File file) {
172
        Project p = new Project();
173
        p.setUserProperty("ant.file", file.getAbsolutePath());
174
        p.setProperty("gvsig_dir", applicationDirectory.getAbsolutePath());
175
        p.setProperty("extensions_dir", pluginsDirectory.getAbsolutePath());
176
        p.init();
177
        ProjectHelper helper = ProjectHelper.getProjectHelper();
178
        p.addReference("ant.projectHelper", helper);
179
        helper.parse(p, file);
180
        p.executeTarget(p.getDefaultTarget());
181
    }
182

    
183
    private boolean deleteDir(File dir) {
184
        if (dir.isDirectory()) {
185
            String[] children = dir.list();
186
            for (int i = 0; i < children.length; i++) {
187
                boolean success = deleteDir(new File(dir, children[i]));
188
                if (!success) {
189
                    return false;
190
                }
191
            }
192
        }
193
        // The directory is now empty so delete it
194
        return dir.delete();
195
    }
196

    
197
}