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 @ 40936

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

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

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

    
37
import com.sardak.antform.AntForm;
38
import com.sardak.antform.AntMenu;
39

    
40
import org.apache.tools.ant.Project;
41
import org.apache.tools.ant.ProjectHelper;
42
import org.gvsig.installer.lib.api.PackageInfo;
43
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
44
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
45
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
46
import org.gvsig.installer.lib.spi.InstallerProviderManager;
47
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
48
import org.gvsig.tools.service.spi.AbstractProvider;
49
import org.gvsig.tools.service.spi.ProviderServices;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

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

    
59
        private static Logger logger = LoggerFactory.getLogger(PluginInstallerExecutionProvider.class);
60
        private static final String ANT_FILE_NAME = "install.xml";
61
        private File pluginsDirectory = null;
62
        private File applicationDirectory = null;
63
        private File decompressDir = null;
64
        private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator
65
                        .getProviderManager();
66

    
67
        public PluginInstallerExecutionProvider(ProviderServices providerServices) {
68
                super(providerServices);
69
        }
70

    
71
        public class InstallerPluginsDirectoryNotFoundException extends
72
                        InstallPackageServiceException {
73

    
74
                private static final long serialVersionUID = 4416143986837955880L;
75

    
76
                private static final String message = "Plugins directory not found";
77

    
78
                private static final String KEY = "plugins_directory_not_found";
79

    
80
                public InstallerPluginsDirectoryNotFoundException() {
81
                        super(message, KEY, serialVersionUID);
82
                }
83

    
84
        }
85

    
86
        public void install(File applicationDirectory, InputStream inputStream,
87
                        PackageInfo packageInfo) throws InstallPackageServiceException {
88

    
89
                this.applicationDirectory = applicationDirectory;
90
                pluginsDirectory = new File(applicationDirectory + File.separator
91
                                + "gvSIG" + File.separator + "extensiones");
92
                logger.info("Installing package '"+packageInfo.getCode()+"' in '"+pluginsDirectory+"'.");
93
                try {
94
                        if (!pluginsDirectory.exists()) {
95
                                logger.warn("Can install package '"+packageInfo.getCode()+"', install folder '"+pluginsDirectory+"' does not exists");
96
                                throw new InstallerPluginsDirectoryNotFoundException();
97
                        }
98

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

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

    
107
                        if (antFile.exists()) {
108
                                executeAntFile(antFile);
109
                        }
110

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

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

    
131
                logger.info("putting off the installation of package '"+packageInfo.getCode()+"'.");
132
                this.applicationDirectory = applicationDirectory;
133

    
134
                File updateDirectory = new File(applicationDirectory + File.separator
135
                                + "update");
136

    
137
                if (!updateDirectory.exists()) {
138
                        updateDirectory.mkdir();
139
                }
140

    
141
                File filesDirectory = new File(updateDirectory + File.separator
142
                                + "files");
143
                if (!filesDirectory.exists()) {
144
                        filesDirectory.mkdir();
145
                }
146

    
147
                File scriptsFile = new File(updateDirectory + File.separator
148
                                + "scripts.lst");
149

    
150
                decompressDir = new File(filesDirectory.toString() + File.separator
151
                                + packageInfo.getCode());
152
                if (decompressDir.exists()) {
153
                        deleteDir(decompressDir);
154
                }
155

    
156
                InstallPackageProviderServices installerProviderServices = installerProviderManager
157
                                .createInstallerProviderServices();
158
                installerProviderServices.decompress(inputStream, filesDirectory);
159

    
160
                File antFile = new File(decompressDir + File.separator + "install"
161
                                + File.separator + ANT_FILE_NAME);
162

    
163
                if (antFile.exists()) {
164
                        if (!scriptsFile.exists()) {
165
                                scriptsFile.createNewFile();
166
                        }
167
                        FileWriter writer = new FileWriter(scriptsFile, true);
168
                        BufferedWriter out = new BufferedWriter(writer);
169
                        String str = antFile.toString();
170
                        out.append(str + "\n");
171
                        out.close();
172
                }
173

    
174
        }
175

    
176
        private void executeAntFile(File file) {
177
                Project p = new Project();
178
                p.setUserProperty("ant.file", file.getAbsolutePath());
179
                p.setUserProperty("gvsig_dir", applicationDirectory.getAbsolutePath());
180
                p.setUserProperty("extensions_dir", pluginsDirectory.getAbsolutePath());
181
                p.setBaseDir(file.getParentFile());
182
                p.addTaskDefinition("antform", AntForm.class);
183
                p.addTaskDefinition("antmenu", AntMenu.class);
184
                p.init();
185
                ProjectHelper helper = ProjectHelper.getProjectHelper();
186
                p.addReference("ant.projectHelper", helper);
187
                helper.parse(p, file);
188
                p.executeTarget(p.getDefaultTarget());
189
        }
190

    
191
        private boolean deleteDir(File dir) {
192
                if (dir.isDirectory()) {
193
                        String[] children = dir.list();
194
                        for (int i = 0; i < children.length; i++) {
195
                                boolean success = deleteDir(new File(dir, children[i]));
196
                                if (!success) {
197
                                        return false;
198
                                }
199
                        }
200
                }
201
                // The directory is now empty so delete it
202
                return dir.delete();
203
        }
204

    
205
}