Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.prov / org.gvsig.installer.prov.plugin / src / main / java / org / gvsig / installer / prov / plugin / execution / PluginInstallerExecutionProvider.java @ 36249

History | View | Annotate | Download (7.15 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 com.sardak.antform.AntForm;
37
import com.sardak.antform.AntMenu;
38

    
39
import org.apache.tools.ant.Project;
40
import org.apache.tools.ant.ProjectHelper;
41

    
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

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

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

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

    
68
    public class InstallerPluginsDirectoryNotFoundException extends
69
        InstallPackageServiceException {
70

    
71
        private static final long serialVersionUID = 4416143986837955880L;
72

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

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

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

    
81
    }
82

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

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

    
95
            InstallPackageProviderServices installerProviderServices =
96
                installerProviderManager.createInstallerProviderServices();
97

    
98
            installerProviderServices.decompress(inputStream, pluginsDirectory);
99
            File antFile =
100
                new File(pluginsDirectory + File.separator
101
                    + packageInfo.getCode() + File.separator + "install"
102
                    + File.separator + ANT_FILE_NAME);
103

    
104
            if (antFile.exists()) {
105
                executeAntFile(antFile);
106
            }
107

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

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

    
128
        this.applicationDirectory = applicationDirectory;
129

    
130
        File updateDirectory =
131
            new File(applicationDirectory + File.separator + "update");
132

    
133
        if (!updateDirectory.exists()) {
134
            updateDirectory.mkdir();
135
        }
136

    
137
        File filesDirectory =
138
            new File(updateDirectory + File.separator + "files");
139
        if (!filesDirectory.exists()) {
140
            filesDirectory.mkdir();
141
        }
142

    
143
        File scriptsFile =
144
            new File(updateDirectory + File.separator + "scripts.lst");
145

    
146
        decompressDir =
147
            new File(filesDirectory.toString() + File.separator
148
                + packageInfo.getCode());
149
        if (decompressDir.exists()) {
150
            deleteDir(decompressDir);
151
        }
152

    
153
        InstallPackageProviderServices installerProviderServices =
154
            installerProviderManager.createInstallerProviderServices();
155
        installerProviderServices.decompress(inputStream, filesDirectory);
156

    
157
        File antFile =
158
            new File(decompressDir + File.separator + "install"
159
                + File.separator + ANT_FILE_NAME);
160

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

    
172
    }
173

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

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

    
203
}