Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.prov / org.gvsig.installer.prov.plugin / src / main / java / org / gvsig / installer / prov / plugin / execution / PluginInstallerExecutionProvider.java @ 41706

History | View | Annotate | Download (8.58 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {Prodevelop}   {Task}
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
import org.gvsig.installer.lib.api.PackageInfo;
39
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
40
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
41
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
42
import org.gvsig.installer.lib.spi.InstallerProviderManager;
43
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
44
import org.gvsig.tools.service.spi.AbstractProvider;
45
import org.gvsig.tools.service.spi.ProviderServices;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
import com.sardak.antform.AntForm;
50
import com.sardak.antform.AntMenu;
51
import org.gvsig.installer.lib.api.InstallerLocator;
52
import org.gvsig.installer.lib.api.InstallerManager;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public class PluginInstallerExecutionProvider extends AbstractProvider
58
        implements InstallPackageProvider {
59

    
60
    private static Logger logger = LoggerFactory.getLogger(PluginInstallerExecutionProvider.class);
61
    private static final String ANT_FILE_NAME = "install.xml";
62

    
63
    public PluginInstallerExecutionProvider(ProviderServices providerServices) {
64
        super(providerServices);
65
    }
66

    
67
    public class InstallerPluginsDirectoryNotFoundException extends
68
            InstallPackageServiceException {
69

    
70
        private static final long serialVersionUID = 4416143986837955880L;
71

    
72
        private static final String message = "Plugins directory not found";
73

    
74
        private static final String KEY = "plugins_directory_not_found";
75

    
76
        public InstallerPluginsDirectoryNotFoundException() {
77
            super(message, KEY, serialVersionUID);
78
        }
79

    
80
    }
81

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

    
85
        InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
86
        InstallerManager installerManager = InstallerLocator.getInstallerManager();
87
        
88
        File pluginsDirectory = installerManager.getDefaultLocalAddonRepository(
89
                "plugin", 
90
                InstallerManager.ACCESS_WRITE
91
        );
92

    
93
        logger.info("Installing package '" + packageInfo.getCode() + "' in '" + pluginsDirectory + "'.");
94
        try {
95
            if ( !pluginsDirectory.exists() ) {
96
                logger.warn("Can install package '" + packageInfo.getCode() + "', install folder '" + pluginsDirectory + "' does not exists");
97
                throw new InstallerPluginsDirectoryNotFoundException();
98
            }
99

    
100
            InstallPackageProviderServices installerProviderServices = installerProviderManager
101
                    .createInstallerProviderServices();
102

    
103
            installerProviderServices.decompress(inputStream, pluginsDirectory);
104
            File antFile = new File(pluginsDirectory + File.separator
105
                    + packageInfo.getCode() + File.separator + "install"
106
                    + File.separator + ANT_FILE_NAME);
107

    
108
            if ( antFile.exists() ) {
109
                executeAntFile(applicationDirectory, pluginsDirectory, antFile);
110
            }
111

    
112
        } catch (Exception e) {
113
            try {
114
                logger.warn("Can install package '" + packageInfo.getCode() + "'.", e);
115
                // if there is an exception, installLater is called
116
                installLater(applicationDirectory, inputStream, packageInfo);
117
            } catch (IOException e1) {
118
                logger.warn("Can install package '" + packageInfo.getCode() + "'.", e1);
119
                throw new InstallPackageServiceException(e1);
120
            }
121
        }
122
    }
123

    
124
    /**
125
     * This function will is called when an exception is produced during the
126
     * installation of a plugin. It will prepare the package to be installed on
127
     * the next gvSIG restart.
128
     */
129
    public void installLater(File applicationDirectory,
130
            InputStream inputStream, PackageInfo packageInfo)
131
            throws InstallPackageServiceException, IOException {
132

    
133
        InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
134
        File decompressDir = null;
135
        
136
        logger.info("Delayed the package installation (" + packageInfo.getCode() + ").");
137

    
138
        File updateDirectory = new File(applicationDirectory + File.separator
139
                + "update");
140

    
141
        if ( !updateDirectory.exists() ) {
142
            forceMkdir(updateDirectory);
143
        }
144

    
145
        File filesDirectory = new File(updateDirectory + File.separator
146
                + "files");
147
        if ( !filesDirectory.exists() ) {
148
            forceMkdir(filesDirectory);
149
        }
150

    
151
        File scriptsFile = new File(updateDirectory + File.separator
152
                + "scripts.lst");
153

    
154
        decompressDir = new File(filesDirectory.toString() + File.separator
155
                + packageInfo.getCode());
156
        if ( decompressDir.exists() ) {
157
            deleteDir(decompressDir);
158
        }
159

    
160
        InstallPackageProviderServices installerProviderServices = installerProviderManager
161
                .createInstallerProviderServices();
162
        installerProviderServices.decompress(inputStream, filesDirectory);
163

    
164
        File antFile = new File(decompressDir + File.separator + "install"
165
                + File.separator + ANT_FILE_NAME);
166

    
167
        if ( antFile.exists() ) {
168
            if ( !scriptsFile.exists() ) {
169
                forceMkdir(scriptsFile);
170
                scriptsFile.createNewFile();
171
            }
172
            FileWriter writer = new FileWriter(scriptsFile, true);
173
            BufferedWriter out = new BufferedWriter(writer);
174
            String str = antFile.toString();
175
            out.append(str + "\n");
176
            out.close();
177
        }
178

    
179
    }
180

    
181
    private void forceMkdir(File file) throws IOException {
182
        if ( file.isDirectory() ) {
183
            org.apache.commons.io.FileUtils.forceMkdir(file);
184
        } else {
185
            org.apache.commons.io.FileUtils.forceMkdir(file.getParentFile());
186
        }
187
    }
188

    
189
    private void executeAntFile(File applicationDirectory, File pluginsDirectory, File file) {
190
        logger.info("Running install script '" + file.getAbsolutePath() + "'.");
191
        try {
192
            Project p = new Project();
193
            p.setUserProperty("ant.file", file.getAbsolutePath());
194
            p.setUserProperty("gvsig_dir", applicationDirectory.getAbsolutePath());
195
            p.setUserProperty("extensions_dir", pluginsDirectory.getAbsolutePath());
196
            p.setBaseDir(file.getParentFile());
197
            p.addTaskDefinition("antform", AntForm.class);
198
            p.addTaskDefinition("antmenu", AntMenu.class);
199
            p.init();
200
            ProjectHelper helper = ProjectHelper.getProjectHelper();
201
            p.addReference("ant.projectHelper", helper);
202
            helper.parse(p, file);
203
            p.executeTarget(p.getDefaultTarget());
204
        } catch (Exception ex) {
205
            logger.warn("Problems executing installation script.", ex);
206
        }
207
    }
208

    
209
    private boolean deleteDir(File dir) {
210
        if ( dir.isDirectory() ) {
211
            String[] children = dir.list();
212
            for ( int i = 0; i < children.length; i++ ) {
213
                boolean success = deleteDir(new File(dir, children[i]));
214
                if ( !success ) {
215
                    return false;
216
                }
217
            }
218
        }
219
        // The directory is now empty so delete it
220
        return dir.delete();
221
    }
222

    
223
}