Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / creation / DefaultMakePluginPackageService.java @ 33743

History | View | Annotate | Download (14.2 KB)

1 32269 jpiera
/* gvSIG. Geographic Information System of the Valencian Government
2 32285 jpiera
 *
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 32269 jpiera
23
/*
24 32285 jpiera
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27
28 32269 jpiera
package org.gvsig.installer.lib.impl.creation;
29
30 32411 jpiera
import java.io.BufferedReader;
31 32296 jpiera
import java.io.ByteArrayInputStream;
32 32269 jpiera
import java.io.File;
33 32285 jpiera
import java.io.FileInputStream;
34 32290 jpiera
import java.io.FileOutputStream;
35 32411 jpiera
import java.io.FileReader;
36 32290 jpiera
import java.io.IOException;
37
import java.io.InputStream;
38 32269 jpiera
import java.io.OutputStream;
39 32411 jpiera
import java.util.ArrayList;
40
import java.util.HashMap;
41
import java.util.List;
42
import java.util.Map;
43 32269 jpiera
44 33686 cordinyana
import org.slf4j.Logger;
45
46
import org.gvsig.installer.lib.api.InstallerManager;
47 32562 jpiera
import org.gvsig.installer.lib.api.PackageInfo;
48
import org.gvsig.installer.lib.api.creation.MakePluginPackageService;
49
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
50
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
51 32627 jpiera
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
52 32498 jpiera
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
53 32400 jpiera
import org.gvsig.tools.service.Manager;
54 32269 jpiera
55
/**
56
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
57
 */
58 33686 cordinyana
public class DefaultMakePluginPackageService implements
59
    MakePluginPackageService {
60 32562 jpiera
61 33686 cordinyana
    public static final String ANT_FILE_NAME = "install.xml";
62
    public static final String COPIED_FILES_DIRECTORY_NAME = "files";
63
    private File pluginsDirectory;
64
    private static final Logger logger = org.slf4j.LoggerFactory
65
        .getLogger(DefaultMakePluginPackageService.class);
66
    private final InstallerManager manager;
67 32562 jpiera
68 33686 cordinyana
    private List<PackageInfo> installerInfos = null;
69
    private Map<PackageInfo, String> directories = null;
70 32562 jpiera
71 33686 cordinyana
    protected List<File> selectedFiles = null;
72
    protected String antScript = null;
73
    private InstallPackageProviderServices installerProviderServices = null;
74 32269 jpiera
75 33686 cordinyana
    public DefaultMakePluginPackageService(InstallerManager manager,
76
        File pluginsDirectory) throws MakePluginPackageServiceException {
77
        super();
78
        this.manager = manager;
79
        installerInfos = new ArrayList<PackageInfo>();
80
        directories = new HashMap<PackageInfo, String>();
81
        selectedFiles = new ArrayList<File>();
82
        installerProviderServices =
83
            InstallerProviderLocator.getProviderManager()
84
                .createInstallerProviderServices();
85
        initialize(pluginsDirectory);
86
    }
87 32290 jpiera
88 33686 cordinyana
    private void initialize(File pluginsDirectory)
89
        throws MakePluginPackageServiceException {
90
        if ((!pluginsDirectory.exists()) || (!pluginsDirectory.isDirectory())) {
91
            throw new MakePluginPackageServiceException(
92
                "The plugins directory does't exists or not is a directory");
93
        }
94
        this.pluginsDirectory = pluginsDirectory;
95 32562 jpiera
96 33686 cordinyana
        // Read all the installed plugins
97
        String[] plugins = pluginsDirectory.list();
98 32562 jpiera
99 33686 cordinyana
        if (plugins != null) {
100
            for (int i = 0; i < plugins.length; i++) {
101
                String pluginDirectoryName = plugins[i];
102
                File pluginDirectoryFile =
103
                    getAbsolutePluginPackageDirectory(pluginDirectoryName);
104
                DefaultPackageInfo packageInfo = new DefaultPackageInfo();
105 32562 jpiera
106 33686 cordinyana
                installerProviderServices.readPackageInfo(new File(
107
                    pluginDirectoryFile.getAbsolutePath()), packageInfo);
108 32597 jpiera
109 33686 cordinyana
                // Checks if the ant file exists
110
                File antFile = getAntFile(pluginDirectoryFile);
111
                if (antFile.exists()) {
112
                    try {
113
                        packageInfo.setAnScript(readFileAsString(antFile
114
                            .getAbsolutePath()));
115
                    } catch (IOException e) {
116
                        logger.error("Not possible to read the ant file");
117
                    }
118
                }
119 32597 jpiera
120 33686 cordinyana
                // Checks if there are files to copy
121
                File copiedFilesDirectory =
122
                    getCopiedFilesDirectory(pluginDirectoryFile);
123
                if (copiedFilesDirectory.exists()) {
124
                    loadCopiedFiles(copiedFilesDirectory, copiedFilesDirectory,
125
                        packageInfo);
126
                }
127 32597 jpiera
128 33686 cordinyana
                installerInfos.add(packageInfo);
129
                directories.put(packageInfo, pluginDirectoryName);
130
            }
131
        }
132
    }
133 32597 jpiera
134 33686 cordinyana
    private void loadCopiedFiles(File file, File filesDirectory,
135
        PackageInfo packageInfo) throws MakePluginPackageServiceException {
136
        if (file.isDirectory()) {
137
            File[] files = file.listFiles();
138
            for (int i = 0; i < files.length; i++) {
139
                loadCopiedFiles(files[i], filesDirectory, packageInfo);
140
            }
141
        } else {
142
            // Removing the plugin prefix
143 32597 jpiera
144 33686 cordinyana
            String pluginFileName =
145
                file.getAbsolutePath().substring(
146
                    filesDirectory.getAbsolutePath().length(),
147
                    file.getAbsolutePath().length());
148 32597 jpiera
149 33686 cordinyana
            // Ading the root directory
150
            File pluginFile =
151
                new File(pluginsDirectory.getParentFile().getParentFile()
152
                    .getAbsolutePath()
153
                    + pluginFileName);
154 32562 jpiera
155 33686 cordinyana
            packageInfo.addFileToCopy(pluginFile);
156
        }
157
    }
158 32562 jpiera
159 33686 cordinyana
    public void createPluginPackage(PackageInfo packageInfo,
160
        OutputStream installerStream) throws MakePluginPackageServiceException {
161 32562 jpiera
162 33686 cordinyana
        // Write the package.info file
163
        writePackageInfo(packageInfo);
164 32285 jpiera
165 33686 cordinyana
        if (packageInfo.getAntScript() != null) {
166
            // Create the ant file
167
            writeAntFile(packageInfo);
168 32423 jpiera
169 33686 cordinyana
            // Copy the selected files
170
            writeSelectedFiles(packageInfo);
171
        }
172 32296 jpiera
173 33686 cordinyana
        String pluginFileName = manager.getPackageBundleFileName(packageInfo);
174
        installerProviderServices.compress(
175
            getAbsolutePluginPackageDirectory(packageInfo), pluginFileName,
176
            installerStream);
177
    }
178 32562 jpiera
179 33686 cordinyana
    private void writePackageInfo(PackageInfo packageInfo)
180
        throws MakePluginPackageServiceException {
181 33697 cordinyana
        writePackageInfo(packageInfo,
182
            getAbsolutePluginPackageDirectory(packageInfo));
183 33686 cordinyana
    }
184 32562 jpiera
185 33697 cordinyana
    public void writePackageInfo(PackageInfo packageInfo, File folder)
186
        throws MakePluginPackageServiceException {
187
        installerProviderServices.writePackageInfo(folder, packageInfo);
188
    }
189
190 33686 cordinyana
    private void writeAntFile(PackageInfo packageInfo)
191
        throws MakePluginPackageServiceException {
192
        try {
193
            ByteArrayInputStream in =
194
                new ByteArrayInputStream(packageInfo.getAntScript().getBytes());
195
            OutputStream out =
196
                new FileOutputStream(getAbsoluteAntFile(packageInfo));
197 32562 jpiera
198 33686 cordinyana
            // Copy the bits from instream to outstream
199
            byte[] buf = new byte[1024];
200
            int len;
201
            while ((len = in.read(buf)) > 0) {
202
                out.write(buf, 0, len);
203
            }
204
            in.close();
205
            out.close();
206
        } catch (IOException e) {
207
            throw new MakePluginPackageServiceException(
208
                "Exception writing the ant file");
209
        }
210 32285 jpiera
211 33686 cordinyana
    }
212 32296 jpiera
213 33686 cordinyana
    private void writeSelectedFiles(PackageInfo packageInfo)
214
        throws MakePluginPackageServiceException {
215
        try {
216
            String copiedFilesDirectoryName =
217
                getAbsoulteCopiedFilesDirectoryName(packageInfo);
218 32597 jpiera
219 33686 cordinyana
            // Deleting the previous files folder
220
            File copiedFilesDirectoryfile = new File(copiedFilesDirectoryName);
221
            deleteDirectory(copiedFilesDirectoryfile);
222 32296 jpiera
223 33686 cordinyana
            // It works if the plugins directory is in the folder
224
            // "gvSIG/extensiones"
225
            String applicationDirectory =
226
                pluginsDirectory.getParentFile().getParent();
227 32562 jpiera
228 33686 cordinyana
            for (int i = 0; i < packageInfo.getFileToCopySize(); i++) {
229
                String sourceFile =
230
                    packageInfo.getFileToCopyAt(i).getAbsolutePath();
231 32290 jpiera
232 33686 cordinyana
                // Create the final path
233
                String destFile =
234
                    sourceFile.substring(applicationDirectory.length(),
235
                        sourceFile.length());
236
                destFile = copiedFilesDirectoryName + destFile;
237 32597 jpiera
238 33686 cordinyana
                // Copy the files
239
                copy(new File(sourceFile), new File(destFile));
240
            }
241
        } catch (IOException e) {
242
            throw new MakePluginPackageServiceException(
243
                "Exception copying the files");
244
        }
245
    }
246 32597 jpiera
247 33686 cordinyana
    static public boolean deleteDirectory(File path) {
248
        if (path.exists()) {
249
            File[] files = path.listFiles();
250
            for (int i = 0; i < files.length; i++) {
251
                if (files[i].isDirectory()) {
252
                    deleteDirectory(files[i]);
253
                } else {
254
                    files[i].delete();
255
                }
256
            }
257
        }
258
        return (path.delete());
259
    }
260 32423 jpiera
261 33686 cordinyana
    void copy(File sourceLocation, File targetLocation) throws IOException {
262
        if (sourceLocation.isDirectory()) {
263
            if (!targetLocation.exists()) {
264
                targetLocation.mkdir();
265
            }
266 32290 jpiera
267 33686 cordinyana
            String[] children = sourceLocation.list();
268
            for (int i = 0; i < children.length; i++) {
269
                copy(new File(sourceLocation, children[i]), new File(
270
                    targetLocation, children[i]));
271
            }
272
        } else {
273
            targetLocation.getParentFile().mkdirs();
274 32423 jpiera
275 33686 cordinyana
            InputStream in = new FileInputStream(sourceLocation);
276
            OutputStream out = new FileOutputStream(targetLocation);
277 32411 jpiera
278 33686 cordinyana
            // Copy the bits from instream to outstream
279
            byte[] buf = new byte[1024];
280
            int len;
281
            while ((len = in.read(buf)) > 0) {
282
                out.write(buf, 0, len);
283
            }
284
            in.close();
285
            out.close();
286
        }
287
    }
288 32411 jpiera
289 33686 cordinyana
    private String getPluginPackageDirectory(PackageInfo packageInfo) {
290
        return directories.get(packageInfo);
291
    }
292 32290 jpiera
293 33686 cordinyana
    private String getCopiedFilesDirectoryName(PackageInfo packageInfo)
294
        throws MakePluginPackageServiceException {
295
        return getPluginPackageDirectory(packageInfo) + File.separator
296
            + COPIED_FILES_DIRECTORY_NAME;
297
    }
298 32290 jpiera
299 33686 cordinyana
    private File getCopiedFilesDirectory(File pluginDirectory)
300
        throws MakePluginPackageServiceException {
301
        return new File(pluginDirectory.getAbsolutePath() + File.separator
302
            + COPIED_FILES_DIRECTORY_NAME);
303
    }
304 32290 jpiera
305 33686 cordinyana
    private String getAbsoulteCopiedFilesDirectoryName(PackageInfo packageInfo)
306
        throws MakePluginPackageServiceException {
307
        return getAbsolutePluginPackageDirectory(packageInfo).getAbsolutePath()
308
            + File.separator + COPIED_FILES_DIRECTORY_NAME;
309
    }
310 32290 jpiera
311 33686 cordinyana
    private String getAntFileName(PackageInfo packageInfo)
312
        throws MakePluginPackageServiceException {
313
        return getPluginPackageDirectory(packageInfo) + File.separator
314
            + ANT_FILE_NAME;
315
    }
316 32290 jpiera
317 33686 cordinyana
    private File getAntFile(File pluginDirectory)
318
        throws MakePluginPackageServiceException {
319
        return new File(pluginDirectory.getAbsolutePath() + File.separator
320
            + ANT_FILE_NAME);
321
    }
322 32290 jpiera
323 33686 cordinyana
    private File getAbsoluteAntFile(PackageInfo packageInfo)
324
        throws MakePluginPackageServiceException {
325
        return new File(pluginsDirectory.getAbsolutePath() + File.separator
326
            + getAntFileName(packageInfo));
327
    }
328 32290 jpiera
329 33686 cordinyana
    private File getAbsolutePluginPackageDirectory(PackageInfo packageInfo)
330
        throws MakePluginPackageServiceException {
331
        return new File(pluginsDirectory.getAbsolutePath() + File.separator
332
            + getPluginPackageDirectory(packageInfo));
333
    }
334 32269 jpiera
335 33686 cordinyana
    private File getAbsolutePluginPackageDirectory(String pluginDirectory)
336
        throws MakePluginPackageServiceException {
337
        return new File(pluginsDirectory.getAbsolutePath() + File.separator
338
            + pluginDirectory);
339
    }
340 32597 jpiera
341 33686 cordinyana
    public Manager getManager() {
342
        return this.manager;
343
    }
344 32597 jpiera
345 33686 cordinyana
    public PackageInfo getPluginPackageInfo(int index) {
346
        if (index >= installerInfos.size()) {
347
            return null;
348
        }
349
        return installerInfos.get(index);
350
    }
351 32290 jpiera
352 33686 cordinyana
    public PackageInfo getPluginPackageInfo(String code) {
353
        for (int i = 0; i < getPluginPackageCount(); i++) {
354
            if (installerInfos.get(i).getCode().equals(code)) {
355
                return installerInfos.get(i);
356
            }
357
        }
358
        return null;
359
    }
360 32423 jpiera
361 33686 cordinyana
    public int getPluginPackageCount() {
362
        return installerInfos.size();
363
    }
364 32597 jpiera
365 33686 cordinyana
    public String getDefaultAntScript()
366
        throws MakePluginPackageServiceException {
367
        try {
368
            return readFileAsString(getClass().getClassLoader()
369
                .getResource(ANT_FILE_NAME).getFile());
370
        } catch (IOException e) {
371
            throw new MakePluginPackageServiceException(
372
                "Impossible to read the default ant file", e);
373
        }
374
    }
375 32269 jpiera
376 33686 cordinyana
    private String readFileAsString(String filePath) throws java.io.IOException {
377
        StringBuffer fileData = new StringBuffer(1000);
378
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
379
        char[] buf = new char[1024];
380
        int numRead = 0;
381
        while ((numRead = reader.read(buf)) != -1) {
382
            String readData = String.valueOf(buf, 0, numRead);
383
            fileData.append(readData);
384
            buf = new char[1024];
385
        }
386
        reader.close();
387
        return fileData.toString();
388
    }
389 32290 jpiera
390 33686 cordinyana
    public File getPluginsDirectory() {
391
        return pluginsDirectory;
392
    }
393 33743 cordinyana
394
    public PackageInfo[] getInstalledPackages()
395
        throws MakePluginPackageServiceException {
396
        return installerInfos.toArray(new PackageInfo[installerInfos.size()]);
397
    }
398 32269 jpiera
}