Revision 35914

View differences:

tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/resources/org/gvsig/installer/lib/impl/creation/install.xml
1
<project name="org.gvsig.plugin1" default="main" basedir=".">
2
	<target name="main" depends="copy_files"/>
3
    <target name="copy_files">
4
    	<copy todir="${gvsig_dir}">
5
    		<fileset dir="./files" includes="**"/>
6
		</copy>
7
	</target>
8
</project>
0 9

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.installer.lib.impl.DefaultInstallerLibrary
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/Download.java
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
package org.gvsig.installer.lib.impl.utils;
23

  
24
import java.io.BufferedInputStream;
25
import java.io.BufferedOutputStream;
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.net.URL;
30
import java.net.URLConnection;
31
import java.util.Date;
32
import java.util.StringTokenizer;
33

  
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

  
37
import org.gvsig.tools.task.AbstractMonitorableTask;
38
import org.gvsig.tools.task.SimpleTaskStatus;
39

  
40
/**
41
 * @author gvSIG Team
42
 * @version $Id$
43
 * 
44
 */
45
public class Download extends AbstractMonitorableTask {
46

  
47
    private static final Logger LOG = LoggerFactory.getLogger(Download.class);
48
    private boolean isMyTaskStatus;
49

  
50
    /**
51
     * @param taskName
52
     */
53
    public Download(SimpleTaskStatus taskStatus) {
54
        this();
55
        this.taskStatus = taskStatus;
56
        isMyTaskStatus = false;
57
    }
58

  
59
    public Download() {
60
        super("Downloading...");
61
        isMyTaskStatus = true;
62
    }
63

  
64
    public File downloadFile(URL url, String defaultFileName)
65
        throws IOException {
66

  
67
        URL downloadURL = url;
68

  
69
        // check if the URL ends with '/' and append the file name
70
        if (defaultFileName != null) {
71
            String urlStr = url.toString();
72
            if (urlStr.endsWith("/")) {
73
                urlStr = urlStr.concat(defaultFileName);
74
                downloadURL = new URL(urlStr);
75
            }
76
        }
77

  
78
        URLConnection connection = downloadURL.openConnection();
79
        String fileName = getFileName(connection);
80

  
81
        if (LOG.isDebugEnabled()) {
82
            Date date = new Date(connection.getLastModified());
83
            LOG.debug(
84
                "Downloading file {} from URL {}, with last modified date: {}",
85
                new Object[] { fileName, downloadURL, date });
86
        }
87

  
88
        String fileNamePrefix = fileName;
89
        String fileNameSuffix = "zip";
90
        int dotPosition = fileName.lastIndexOf('.');
91
        if ((dotPosition > -1) && (dotPosition < fileName.length() - 1)) {
92
            fileNamePrefix = fileName.substring(0, dotPosition);
93
            fileNameSuffix = fileName.substring(dotPosition);
94
        }
95

  
96
        BufferedInputStream bis =
97
            new BufferedInputStream(downloadURL.openStream());
98

  
99
        File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix);
100

  
101
        BufferedOutputStream bos =
102
            new BufferedOutputStream(new FileOutputStream(localFile));
103

  
104
        this.taskStatus.setRangeOfValues(0, connection.getContentLength());
105
        try {
106
            byte[] data = new byte[1024];
107
            int count = 0;
108
            long totalCount = 0; // total bytes readed
109
            while ((count = bis.read(data, 0, 1024)) >= 0) {
110
                bos.write(data, 0, count);
111
                totalCount += count;
112

  
113
                this.taskStatus.setCurValue(totalCount);
114

  
115
                if (this.taskStatus.isCancellationRequested()) {
116
                    return null;
117
                }
118
            }
119
            if (isMyTaskStatus) {
120
                this.taskStatus.terminate();
121
                this.taskStatus.remove();
122
            }
123
            return localFile;
124
        } finally {
125
            bis.close();
126
            bos.flush();
127
            bos.close();
128
        }
129

  
130
    }
131

  
132
    /**
133
     * Returns the file name associated to an url connection.<br />
134
     * The result is not a path but just a file name.
135
     * 
136
     * @param urlConnection
137
     *            - the url connection
138
     * @return the file name
139
     * 
140
     * @throws IOException
141
     *             Signals that an I/O exception has occurred.
142
     */
143
    private String getFileName(URLConnection urlConnection) throws IOException {
144
        String fileName = null;
145

  
146
        String contentDisposition =
147
            urlConnection.getHeaderField("content-disposition");
148

  
149
        if (contentDisposition != null) {
150
            fileName =
151
                extractFileNameFromContentDisposition(contentDisposition);
152
        }
153

  
154
        // if the file name cannot be extracted from the content-disposition
155
        // header, using the url.getFilename() method
156
        if (fileName == null) {
157
            StringTokenizer st =
158
                new StringTokenizer(urlConnection.getURL().getFile(), "/");
159
            while (st.hasMoreTokens()) {
160
                fileName = st.nextToken();
161
            }
162
        }
163

  
164
        return fileName;
165
    }
166

  
167
    /**
168
     * Extract the file name from the content disposition header.
169
     * <p>
170
     * See <a
171
     * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
172
     * //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
173
     * information regarding the headers in HTML.
174
     * 
175
     * @param contentDisposition
176
     *            - the content-disposition header. Cannot be <code>null>/code>.
177
     * @return the file name, or <code>null</code> if the content-disposition
178
     *         header does not contain the filename attribute.
179
     */
180
    private String extractFileNameFromContentDisposition(
181
        String contentDisposition) {
182
        String[] attributes = contentDisposition.split(";");
183

  
184
        for (String a : attributes) {
185
            if (a.toLowerCase().contains("filename")) {
186
                // The attribute is the file name. The filename is between
187
                // quotes.
188
                return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"'));
189
            }
190
        }
191

  
192
        // not found
193
        return null;
194

  
195
    }
196
}
0 197

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/Compress.java
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.lib.impl.utils;
29

  
30
import java.io.BufferedInputStream;
31
import java.io.BufferedOutputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.IOException;
35
import java.io.OutputStream;
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.zip.ZipEntry;
39
import java.util.zip.ZipOutputStream;
40

  
41
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
42
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
43

  
44
/**
45
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
46
 */
47
public class Compress {
48

  
49
//    private static final int BUFFER = 2048;
50

  
51
    public void compressPluginAsPackageSet(File file, String fileName,
52
        OutputStream os) throws MakePluginPackageServiceException {
53
        List<File> files = new ArrayList<File>();
54
        List<String> fileNames = new ArrayList<String>();
55
        files.add(file);
56
        fileNames.add(fileName);
57
        compressPluginsAsPackageSet(files, fileNames, os);
58
    }
59

  
60
    public void compressPluginsAsPackageSet(File directory, OutputStream os)
61
        throws MakePluginPackageServiceException {
62
        File[] files = directory.listFiles();
63
        List<File> filesArray = new ArrayList<File>();
64
        List<String> fileNamesArray = new ArrayList<String>();
65
        for (int i = 0; i < files.length; i++) {
66
            filesArray.add(files[i]);
67
            fileNamesArray.add(files[i].getName());
68
        }
69
        compressPluginsAsPackageSet(filesArray, fileNamesArray, os);
70
    }
71

  
72
    public void compressPluginsAsPackageSet(List<File> files,
73
        List<String> fileNames, OutputStream os)
74
        throws MakePluginPackageServiceException {
75
        try {
76
            ZipOutputStream zos =
77
                new ZipOutputStream(new BufferedOutputStream(os));
78

  
79
            for (int i = 0; i < files.size(); i++) {
80
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
81
                zos.putNextEntry(zipEntry);
82
                compressPluginFiles(files.get(i), zos);
83
                zos.closeEntry();
84
            }
85
            zos.close();
86
        } catch (Exception e) {
87
            throw new MakePluginPackageServiceException(
88
                "Error compressing as package set the plugin files: " + files,
89
                e);
90
        }
91
    }
92

  
93
    public void compressPluginAsPackage(File folder, OutputStream os)
94
        throws MakePluginPackageServiceException {
95
        ZipOutputStream zos = new ZipOutputStream(os);
96
        try {
97
            int parentFileLenght = folder.getParentFile().toString().length();
98
            compressPluginFile(folder, parentFileLenght, zos);
99
            zos.flush();
100
            zos.close();
101
        } catch (IOException e) {
102
            throw new MakePluginPackageServiceException(
103
                "Error compressing as package the plugin folder: " + folder, e);
104
        }
105
    }
106

  
107
    public void compressPluginAsPackageIndex(File folder, OutputStream os)
108
        throws MakePluginPackageServiceException {
109
        ZipOutputStream zos = new ZipOutputStream(os);
110
        try {
111
            int parentFileLength = folder.getParentFile().toString().length();
112
            String folderName =
113
                folder.toString()
114
                    .substring(parentFileLength + 1, folder.toString().length());
115
            
116
            File infoFile =
117
                new File(folder, getPackageInfoFileName() + ".index");
118

  
119
            byte[] buf = new byte[1024];
120
            int len;
121

  
122
            ZipEntry zipEntry =
123
                new ZipEntry(folderName + File.separator
124
                    + getPackageInfoFileName());
125
            zos.putNextEntry(zipEntry);
126

  
127
            FileInputStream fin = new FileInputStream(infoFile);
128
            BufferedInputStream in = new BufferedInputStream(fin);
129
            while ((len = in.read(buf)) >= 0) {
130
                zos.write(buf, 0, len);
131
            }
132
            in.close();
133
            zos.closeEntry();
134
            zos.flush();
135
            zos.close();
136
        } catch (IOException e) {
137
            throw new MakePluginPackageServiceException(
138
                "Error compressing as package index the plugin folder: "
139
                    + folder, e);
140
        }
141
    }
142

  
143
    private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos)
144
        throws IOException {
145
        int parentFileLenght = fileOrFolder.getParentFile().toString().length();
146
        ZipOutputStream zosPlugin = new ZipOutputStream(zos);
147
        compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin);
148
        zosPlugin.finish();
149
    }
150

  
151
    private void compressPluginFile(File file, int parenFileLength,
152
        ZipOutputStream zosPlugin) throws IOException {
153
        String fileName =
154
            file.toString()
155
                .substring(parenFileLength, file.toString().length());
156
        if (File.separatorChar != '/') {
157
            fileName = fileName.replace(File.separatorChar, '/');
158
        }
159

  
160
        if (file.isDirectory()) {
161
            // Remove the subversion folders
162
            if (!file.getName().toUpperCase().equals(".SVN")) {
163
                // Adding the files
164
                String[] fileNames = file.list();
165
                if (fileNames != null) {
166
                    for (int i = 0; i < fileNames.length; i++) {
167
                        compressPluginFile(new File(file, fileNames[i]),
168
                            parenFileLength, zosPlugin);
169
                    }
170
                }
171
            }
172
        } else {
173
            byte[] buf = new byte[1024];
174
            int len;
175

  
176
            ZipEntry zipEntry = new ZipEntry(fileName);
177
            zosPlugin.putNextEntry(zipEntry);
178

  
179
            FileInputStream fin = new FileInputStream(file);
180
            BufferedInputStream in = new BufferedInputStream(fin);
181
            while ((len = in.read(buf)) >= 0) {
182
                zosPlugin.write(buf, 0, len);
183
            }
184
            in.close();
185
            zosPlugin.closeEntry();
186
        }
187
    }
188

  
189
    private String getPackageInfoFileName() {
190
        return InstallerProviderLocator.getProviderManager()
191
            .getPackageInfoFileName();
192
    }
193
}
0 194

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/DeleteFile.java
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
package org.gvsig.installer.lib.impl.utils;
23

  
24
import java.io.File;
25

  
26

  
27
/**
28
 * @author gvSIG Team
29
 * @version $Id$
30
 *
31
 */
32
public class DeleteFile {
33

  
34
    
35
    public DeleteFile() {
36

  
37
    }
38
    
39
    public boolean delete(File dir) {
40
        if (dir.isDirectory()) {
41
            String[] children = dir.list();
42
            for (int i = 0; i < children.length; i++) {
43
                boolean success = delete(new File(dir, children[i]));
44
                if (!success) {
45
                    return false;
46
                }
47
            }
48
        }
49
        return dir.delete();
50
    }
51
    
52
}
0 53

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/Decompress.java
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.lib.impl.utils;
29

  
30
import java.io.ByteArrayInputStream;
31
import java.io.ByteArrayOutputStream;
32
import java.io.File;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.Map;
39
import java.util.zip.ZipEntry;
40
import java.util.zip.ZipException;
41
import java.util.zip.ZipInputStream;
42

  
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

  
46
import org.gvsig.installer.lib.api.PackageInfo;
47
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
48
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
50
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
51
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.task.SimpleTaskStatus;
54
import org.gvsig.tools.task.TaskStatusManager;
55

  
56
/**
57
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
58
 */
59
public class Decompress {
60

  
61
    private static final int OPTION_DECOMPRESS = 1;
62
    private static final int OPTION_READ_INSTALLINFO = 2;
63
    private static final int OPTION_INSTALL = 3;
64

  
65
    private int option = 0;
66

  
67
    private int BUFFER = 2048;
68

  
69
    private static final Logger logger =
70
        LoggerFactory.getLogger(Decompress.class);
71

  
72
    private List<PackageInfo> readedIinstallerInfos = null;
73
    private File outputDirectory = null;
74
    private List<PackageInfo> selectedInstallerInfos = null;
75
    private Map<PackageInfo, String> zipEntriesMap = null;
76
    private List<String> defaultSelectedPackets = null;
77

  
78
    public Decompress() {
79

  
80
    }
81

  
82
    public void decompressPlugins(InputStream is, File outputDirectory)
83
        throws InstallPackageServiceException {
84
        option = OPTION_DECOMPRESS;
85
        this.outputDirectory = outputDirectory;
86

  
87
        decompressFolderOfPlugins(is);
88
    }
89

  
90
    public void readPackageSetInstallInfos(InputStream is,
91
        List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap)
92
        throws InstallPackageServiceException {
93
        option = OPTION_READ_INSTALLINFO;
94
        this.readedIinstallerInfos = installerInfos;
95
        this.zipEntriesMap = zipEntriesMap;
96
        decompressFolderOfPlugins(is);
97
    }
98

  
99
    public void readPackageInstallInfo(InputStream is,
100
        List<PackageInfo> installerInfos,
101
        Map<PackageInfo, String> zipEntriesMap, String name)
102
        throws InstallPackageServiceException {
103
        option = OPTION_READ_INSTALLINFO;
104
        this.readedIinstallerInfos = installerInfos;
105
        this.zipEntriesMap = zipEntriesMap;
106
        decompressFolderOfPluginFromPackage(is, name);
107
    }
108

  
109
    public void decompressPlugin(InputStream is, File outputDirectory)
110
        throws InstallPackageServiceException {
111
        try {
112
            option = OPTION_DECOMPRESS;
113
            this.outputDirectory = outputDirectory;
114
            decompressPlugin(is);
115
        } catch (Exception e) {
116
            throw new InstallPackageServiceException(
117
                "Error reading the plugin", e);
118
        }
119
    }
120

  
121
    public InputStream searchPlugin(InputStream is, String zipEntry)
122
        throws InstallPackageServiceException {
123
        ZipEntry entry = null;
124

  
125
        try {
126
            ZipInputStream zipInputStream = new ZipInputStream(is);
127

  
128
            while ((entry = zipInputStream.getNextEntry()) != null) {
129
                if (entry.getName().equals(zipEntry)) {
130
                    return zipInputStream;
131
                }
132
                zipInputStream.closeEntry();
133
            }
134
            zipInputStream.closeEntry();
135

  
136
            zipInputStream.close();
137
        } catch (Exception e) {
138
            throw new InstallPackageServiceException(
139
                "Error reading the plugin", e);
140
        }
141
        return null;
142
    }
143

  
144
    public void installFromStream(InputStream is, PackageInfo installerInfo)
145
        throws InstallPackageServiceException {
146
        option = OPTION_INSTALL;
147
        this.selectedInstallerInfos = new ArrayList<PackageInfo>();
148
        this.selectedInstallerInfos.add(installerInfo);
149
        decompressFolderOfPlugins(is);
150
    }
151

  
152
    public void installFromStream(InputStream is,
153
        List<PackageInfo> installerInfos) throws InstallPackageServiceException {
154
        option = OPTION_INSTALL;
155
        this.selectedInstallerInfos = installerInfos;
156
        decompressFolderOfPlugins(is);
157
    }
158

  
159
    private void decompressFolderOfPlugins(InputStream is)
160
        throws InstallPackageServiceException {
161
        ZipInputStream zipInputStream = new ZipInputStream(is);
162
        ZipEntry entry = null;
163

  
164
        try {
165
            while ((entry = zipInputStream.getNextEntry()) != null) {
166
                if (entry.getName().equals("defaultPackages")) {
167
                    int count;
168
                    byte data[] = new byte[BUFFER];
169
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
170

  
171
                    while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
172
                        out.write(data, 0, count);
173
                    }
174

  
175
                    String str = out.toString();
176
                    String lineas[] = str.split("\\n");
177
                    List<String> defaultPackagesList = new ArrayList<String>();
178

  
179
                    for (int i = 0; i < lineas.length; i++) {
180
                        defaultPackagesList.add(lineas[i]);
181
                    }
182

  
183
                    defaultSelectedPackets = defaultPackagesList;
184
                    out.flush();
185
                } else {
186
                    logger.debug("Extracting all Plugins, plugin: " + entry);
187
                    if (option == OPTION_INSTALL) {
188

  
189
                    } else {
190
                        if (option == OPTION_DECOMPRESS) {
191
                            decompressPlugin(zipInputStream);
192
                        } else
193
                            if (option == OPTION_READ_INSTALLINFO) {
194
                                readPlugin(zipInputStream, entry.getName());
195
                            }
196
                    }
197
                }
198
                zipInputStream.closeEntry();
199
            }
200
            zipInputStream.close();
201

  
202
        } catch (Exception e) {
203
            throw new InstallPackageServiceException(
204
                "Error reading the plugin", e);
205
        }
206
    }
207
    
208
    private void decompressFolderOfPluginFromPackage(InputStream is, String name)
209
        throws InstallPackageServiceException {
210

  
211
        try {
212
            if (option == OPTION_INSTALL) {
213

  
214
            } else {
215
                if (option == OPTION_DECOMPRESS) {
216
                    decompressPlugin(is);
217
                } else {
218
                    if (option == OPTION_READ_INSTALLINFO) {
219
                        readPlugin(is, name);
220
                    }
221
                }
222
            }
223

  
224
        } catch (Exception e) {
225
            throw new InstallPackageServiceException(
226
                "Error reading the plugin", e);
227
        }
228
    }
229
    
230
    private void readPlugin(InputStream is, String zipEntryName)
231
        throws ZipException, IOException, InstallerInfoFileException {
232
        ZipEntry entry = null;
233
        int installerInfoNumber = zipEntriesMap.size();
234
        String packageInfoName = getPackageInfoFileName();
235
        String unixPackageInfoPath = "/".concat(packageInfoName);
236
        String windowsPackageInfoPath = "\\".concat(packageInfoName);
237
        ZipInputStream zis = new ZipInputStream(is);
238
        while ((entry = zis.getNextEntry()) != null) {
239
            logger.debug("Extracting: " + entry.getName());
240

  
241
            String name = entry.getName();
242
            // Just in case, but we are going to create them always using
243
            // the unix file separator
244
            if (name.endsWith(unixPackageInfoPath)
245
                || name.endsWith(windowsPackageInfoPath)) {
246
                PackageInfo installerInfo = readInstallInfo(zis);
247
                zipEntriesMap.put(installerInfo, zipEntryName);
248
                readedIinstallerInfos.add(installerInfo);
249
            }
250
            zis.closeEntry();
251
        }
252
        // Don't close the stream as if it is a zip file contained 
253
        // into another zip file, closing of the child Zip?nputStream
254
        // will close also the parent's.
255
//        zis.close();
256

  
257
        if (installerInfoNumber == zipEntriesMap.size()) {
258
            PackageInfo installerInfo = new DefaultPackageInfo();
259
            installerInfo.setCode(zipEntryName);
260
            installerInfo.setName(zipEntryName);
261
            zipEntriesMap.put(installerInfo, zipEntryName);
262
            readedIinstallerInfos.add(installerInfo);
263
        }
264
    }
265

  
266
    private void decompressPlugin(InputStream inputStream)
267
        throws ZipException, IOException, InstallerInfoFileException {
268
        ZipInputStream zis = null;
269
        ZipEntry entry = null;
270
        byte data[] = new byte[BUFFER];
271
        int count = 0;
272

  
273
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
274
        SimpleTaskStatus taskStatus =
275
            manager.creteDefaultSimpleTaskStatus("Uncompressing...");
276
        manager.add(taskStatus);
277
        long readed = 0;
278

  
279
        // // First read all the contents size
280
        // zis = new ZipInputStream(inputStream);
281
        // while ((entry = zis.getNextEntry()) != null) {
282
        // count += entry.getSize();
283
        // }
284
        // // zis.close();
285
        // taskStatus.setRangeOfValues(0, count);
286

  
287
        // Return the stream to the initial position
288
        zis = new ZipInputStream(inputStream);
289
        while ((entry = zis.getNextEntry()) != null) {
290
            String entryName = entry.getName();
291
            taskStatus.message(entryName);
292

  
293
            if (File.separatorChar != '/') {
294
                entryName = entryName.replace('/', File.separatorChar);
295
            }
296
            logger.debug("Extracting: " + entryName);
297

  
298
            File file =
299
                new File(outputDirectory.getAbsolutePath() + File.separator
300
                    + entryName);
301
            if (file.exists()) {
302
                delete(file);
303
            }
304
            if (entry.isDirectory()) {
305
                file.mkdirs();
306
            } else {
307
                createParentFolder(file);
308
                FileOutputStream fos = new FileOutputStream(file);
309
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
310
                    fos.write(data, 0, count);
311
                    readed += count;
312
                    taskStatus.setCurValue(readed);
313
                }
314
                fos.flush();
315
                fos.close();
316
            }
317
        }
318
        zis.close();
319
        taskStatus.remove();
320
    }
321

  
322
    private void createParentFolder(File file) {
323
        File parentFile = file.getParentFile();
324
        if (!parentFile.exists()) {
325
            parentFile.mkdirs();
326
        }
327
    }
328

  
329
    public boolean delete(File dir) {
330
        if (dir.isDirectory()) {
331
            String[] children = dir.list();
332
            for (int i = 0; i < children.length; i++) {
333
                boolean success = delete(new File(dir, children[i]));
334
                if (!success) {
335
                    return false;
336
                }
337
            }
338
        }
339
        return dir.delete();
340
    }
341

  
342
    public PackageInfo readInstallerInfo(InputStream is)
343
        throws InstallerInfoFileException {
344
        try {
345
            return readInstallInfo(new ZipInputStream(is));
346
        } catch (IOException e) {
347
            throw new InstallerInfoFileException("error_reading_installerinfo",
348
                e);
349
        }
350
    }
351

  
352
    private PackageInfo readInstallInfo(ZipInputStream zipInputStream)
353
        throws IOException, InstallerInfoFileException {
354
        int count;
355
        byte data[] = new byte[BUFFER];
356

  
357
        ByteArrayOutputStream out = new ByteArrayOutputStream();
358

  
359
        while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
360
            out.write(data, 0, count);
361
        }
362
        out.flush();
363

  
364
        DefaultPackageInfo installerInfo = new DefaultPackageInfo();
365
        InstallerInfoFileReader installerInfoFileReader =
366
            new InstallerInfoFileReader();
367
        installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(
368
            out.toByteArray()));
369

  
370
        return installerInfo;
371
    }
372

  
373
    private String getPackageInfoFileName() {
374
        return InstallerProviderLocator.getProviderManager()
375
            .getPackageInfoFileName();
376
    }
377
    
378
    public List<String> getDefaultSelectedPackages() {
379
        return defaultSelectedPackets;
380
    }
381
}
0 382

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/DefaultInstallerProviderServices.java
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.lib.impl;
29

  
30
import java.io.File;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.util.List;
34
import java.util.Map;
35

  
36
import org.gvsig.installer.lib.api.PackageInfo;
37
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
38
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
39
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
40
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter;
41
import org.gvsig.installer.lib.impl.utils.Compress;
42
import org.gvsig.installer.lib.impl.utils.Decompress;
43
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
44
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
45
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
46
import org.gvsig.tools.service.spi.AbstractProviderServices;
47

  
48
/**
49
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
50
 */
51
public class DefaultInstallerProviderServices extends AbstractProviderServices
52
		implements InstallPackageProviderServices {
53
    
54
    private List<String> defaultSelectedPacketsIDs = null;
55

  
56
//	private static final Logger LOG = LoggerFactory
57
//			.getLogger(DefaultInstallerProviderServices.class);
58

  
59
	public void decompress(InputStream is, File outputDirectory)
60
			throws InstallPackageServiceException {
61
		Decompress decompress = new Decompress();
62
		decompress.decompressPlugin(is, outputDirectory);
63
	}
64

  
65
	public PackageInfo readCompressedPackageInfo(InputStream is)
66
			throws InstallPackageServiceException {
67
		Decompress decompress = new Decompress();
68
		try {
69
			return decompress.readInstallerInfo(is);
70
		} catch (InstallerInfoFileException e) {
71
			throw new InstallPackageServiceException(
72
					"Exception reading a compressed file", e);
73
		}
74
	}
75

  
76
	public void compressPackageSet(File folder, String fileName, OutputStream os)
77
			throws MakePluginPackageServiceException {
78
		Compress compress = new Compress();
79
		compress.compressPluginAsPackageSet(folder, fileName, os);
80
	}
81

  
82
	public void compressPackage(File folder, OutputStream os)
83
			throws MakePluginPackageServiceException {
84
		Compress compress = new Compress();
85
		compress.compressPluginAsPackage(folder, os);
86
	}
87

  
88
    public void compressPackageIndex(File folder, OutputStream os)
89
        throws MakePluginPackageServiceException {
90
        Compress compress = new Compress();
91
        compress.compressPluginAsPackageIndex(folder, os);
92
    }
93

  
94
	public void readPackageInfo(File directory, PackageInfo installerInfo)
95
			throws InstallerInfoFileException {
96
		File installInfoFile = new File(directory + File.separator
97
 + getPackageInfoFileName());
98
		if (installInfoFile.exists()) {
99
			InstallerInfoFileReader reader = new InstallerInfoFileReader();
100
			reader.read(installerInfo, installInfoFile.getAbsolutePath());
101
		}
102

  
103
		if (installerInfo.getCode() == null) {
104
			installerInfo.setCode(directory.getName());
105
		}
106
		if (installerInfo.getName() == null) {
107
			installerInfo.setName(directory.getName());
108
		}
109
	}
110

  
111
    private String getPackageInfoFileName() {
112
        return InstallerProviderLocator.getProviderManager()
113
            .getPackageInfoFileName();
114
    }
115

  
116
    public void writePackageInfo(File directory, PackageInfo packageInfo)
117
			throws InstallerInfoFileException {
118
        writePackageInfoFile(directory, getPackageInfoFileName(), packageInfo);
119
	}
120

  
121
    public void writePackageInfoForIndex(File directory, PackageInfo packageInfo)
122
        throws InstallerInfoFileException {
123
        writePackageInfoFile(directory, getPackageInfoFileName() + ".index",
124
            packageInfo);
125
    }
126

  
127
    private void writePackageInfoFile(File directory, String fileName,
128
        PackageInfo installerInfo) throws InstallerInfoFileException {
129
        if (!directory.exists()) {
130
            throw new InstallerInfoFileException("The directory doesn't exist");
131
        }
132
        InstallerInfoFileWriter installerInfoFileWriter =
133
            new InstallerInfoFileWriter();
134
        installerInfoFileWriter.write(installerInfo, directory + File.separator
135
            + fileName);
136
    }
137

  
138
    public InputStream searchPackage(InputStream is, String zipEntry)
139
			throws InstallPackageServiceException {
140
		Decompress decompress = new Decompress();
141
		return decompress.searchPlugin(is, zipEntry);
142
	}
143

  
144
	public void readPackageSetInfo(InputStream is,
145
			List<PackageInfo> installerInfos,
146
			Map<PackageInfo, String> zipEntriesMap)
147
			throws InstallPackageServiceException {
148
		Decompress decompress = new Decompress();
149
		decompress
150
				.readPackageSetInstallInfos(is, installerInfos, zipEntriesMap);
151
		
152
		defaultSelectedPacketsIDs = decompress.getDefaultSelectedPackages();
153
	}
154

  
155
	public void readPackageInfo(InputStream is,
156
			List<PackageInfo> installerInfos,
157
			Map<PackageInfo, String> zipEntriesMap, String name)
158
			throws InstallPackageServiceException {
159

  
160
		Decompress decompress = new Decompress();
161
		decompress.readPackageInstallInfo(is, installerInfos, zipEntriesMap,
162
				name);
163
    }
164

  
165
    public List<String> getDefaultSelectedPackagesIDs() {
166
        return defaultSelectedPacketsIDs;
167
    }
168
}
0 169

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/execution/NotInstallerExecutionProviderException.java
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.lib.impl.execution;
29

  
30
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
31
import org.gvsig.tools.service.ServiceException;
32

  
33
/**
34
 * Exception thrown when the provider doesn't implements the
35
 * {@link InstallPackageProvider} interface
36
 * 
37
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
38
 */
39
public class NotInstallerExecutionProviderException extends ServiceException {
40

  
41
    private static final long serialVersionUID = -2159095938700834790L;
42

  
43
    private final static String MESSAGE_KEY = "_ProviderNotRegisteredException";
44
    private final static String MESSAGE_FORMAT =
45
        "The provider '%(providerName)' does not implement the InstallerExecutionProvider interface";
46

  
47
    /**
48
     * Creates a new {@link NotInstallerExecutionProviderException}.
49
     * 
50
     * @param name
51
     *            the name of the provider that doesn't implements the
52
     *            {@link InstallPackageProvider} interface
53
     */
54
    public NotInstallerExecutionProviderException(String name) {
55
        super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
56
        setValue("providerName", name);
57
    }
58
}
0 59

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/execution/DefaultInstallPackageService.java
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.lib.impl.execution;
29

  
30
import java.io.BufferedInputStream;
31
import java.io.File;
32
import java.io.FileFilter;
33
import java.io.FileInputStream;
34
import java.io.FileNotFoundException;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.net.URL;
38
import java.util.ArrayList;
39
import java.util.HashMap;
40
import java.util.List;
41
import java.util.Map;
42

  
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

  
46
import org.gvsig.installer.lib.api.InstallerManager;
47
import org.gvsig.installer.lib.api.PackageInfo;
48
import org.gvsig.installer.lib.api.execution.InstallPackageService;
49
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
50
import org.gvsig.installer.lib.impl.DefaultInstallerManager;
51
import org.gvsig.installer.lib.impl.utils.Download;
52
import org.gvsig.installer.lib.spi.InstallPackageProviderServices;
53
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
54
import org.gvsig.installer.lib.spi.InstallerProviderManager;
55
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
56
import org.gvsig.tools.service.Manager;
57
import org.gvsig.tools.service.ServiceException;
58
import org.gvsig.tools.task.SimpleTaskStatus;
59

  
60
/**
61
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
62
 */
63

  
64
public class DefaultInstallPackageService extends Thread implements
65
    InstallPackageService {
66

  
67
    private static final Logger LOG =
68
        LoggerFactory.getLogger(DefaultInstallPackageService.class);
69

  
70
    private Map<PackageInfo, File> packageInfoFileMap = null;
71
    private Map<PackageInfo, String> zipEntriesMap = null;
72
    private List<PackageInfo> packageInfos = null;
73
    private InstallerManager manager;
74
    private InstallPackageProviderServices installerProviderServices = null;
75

  
76
    public DefaultInstallPackageService(DefaultInstallerManager manager) {
77
        super();
78
        this.manager = manager;
79
        this.reset();
80
    }
81
    
82
    public void reset() {
83
        packageInfoFileMap = new HashMap<PackageInfo, File>();
84
        packageInfos = new ArrayList<PackageInfo>();
85
        zipEntriesMap = new HashMap<PackageInfo, String>();
86
        installerProviderServices =
87
            InstallerProviderLocator.getProviderManager()
88
                .createInstallerProviderServices();
89
    }
90

  
91
    public void installPackage(File applicationDirectory,
92
        PackageInfo packageInfo) throws InstallPackageServiceException {
93
        if (!applicationDirectory.exists()) {
94
            throw new InstallPackageServiceException(
95
                "application_directory_not_found");
96
        }
97
        if (!packageInfoFileMap.containsKey(packageInfo)) {
98
            throw new InstallPackageServiceException("package_not_found");
99
        }
100

  
101
        InstallPackageProvider installerExecutionProvider =
102
            createProvider(packageInfo);
103

  
104
        // Get the package or package set file
105
        File file = packageInfoFileMap.get(packageInfo);
106
        if (file == null) {
107
            if (packageInfo.getDownloadURL() == null) {
108
                throw new InstallPackageServiceException("package_not_found");
109
            }
110
            this.downloadPackage(packageInfo);
111
            file = packageInfoFileMap.get(packageInfo);
112
        }
113

  
114
        // Open and install the package or package set file
115
        try {
116
            InputStream packageStream;
117
            InputStream fis = new FileInputStream(file);
118
            InputStream bis = new BufferedInputStream(fis);
119
            if (isPackage(file)) {
120
                packageStream = bis;
121
            } else {
122
                if (!isPackageSet(file)) {
123
                    LOG.warn("Trying to install a package file ({0}) "
124
                        + "without a known file extension. Will try "
125
                        + "to install it as a package set", file);
126
                }
127
                packageStream =
128
                    installerProviderServices.searchPackage(bis, zipEntriesMap
129
                        .get(packageInfo));
130
            }
131
            installerExecutionProvider.install(applicationDirectory,
132
                packageStream, packageInfo);
133

  
134
            packageStream.close();
135
            if (bis != packageStream) {
136
                bis.close();
137
            }
138
            fis.close();
139
        } catch (FileNotFoundException e) {
140
            throw new InstallPackageServiceException("Package file not found: "
141
                + file);
142
        } catch (IOException e) {
143
            throw new InstallPackageServiceException(
144
                "IO error installing the package file: " + file, e);
145
        }
146

  
147
    }
148

  
149
    public void installPackage(File applicationDirectory, String packageCode)
150
        throws InstallPackageServiceException {
151
        PackageInfo packageInfo = getPackageInfo(packageCode);
152
        if (packageInfo == null) {
153
            throw new InstallPackageServiceException(
154
                "The package doesn't exist");
155
        }
156
        installPackage(applicationDirectory, packageInfo);
157
    }
158

  
159
    private InstallPackageProvider createProvider(PackageInfo packageInfo)
160
        throws InstallPackageServiceException {
161
        InstallerProviderManager installerProviderManager =
162
            (InstallerProviderManager) ((DefaultInstallerManager) manager)
163
                .getProviderManager();
164

  
165
        try {
166
            return installerProviderManager.createExecutionProvider(packageInfo
167
                .getType());
168
        } catch (ServiceException e) {
169
            throw new InstallPackageServiceException(
170
                "Error creating the provider", e);
171
        }
172
    }
173

  
174
    public PackageInfo getPackageInfo(int index) {
175
        if (index >= packageInfos.size()) {
176
            return null;
177
        }
178
        return packageInfos.get(index);
179
    }
180

  
181
    public PackageInfo getPackageInfo(String packageCode) {
182
        for (int i = 0; i < getPackageCount(); i++) {
183
            if (packageInfos.get(i).getCode().equals(packageCode)) {
184
                return packageInfos.get(i);
185
            }
186
        }
187
        return null;
188
    }
189

  
190
    public void addBundle(File bundle) throws InstallPackageServiceException {
191
        
192
        if (!bundle.exists()) {
193
            throw new InstallPackageServiceException(
194
                "Only an existing file is supported");
195
        }
196
        
197
        int packageInfoCount = packageInfos.size();
198
        
199
        FileInputStream fis;
200
        try {
201
            fis = new FileInputStream(bundle);
202
        } catch (FileNotFoundException e) {
203
            throw new InstallPackageServiceException("Package file not found: "
204
                + bundle, e);
205
        }
206
        BufferedInputStream bis = new BufferedInputStream(fis);
207
        if (isPackage(bundle)) {
208
            installerProviderServices.readPackageInfo(bis, packageInfos,
209
                zipEntriesMap, bundle.getName());
210
        } else {
211
            if (!isPackageSet(bundle)) {
212
                LOG.warn("Trying to add a package file ({0}) without a known "
213
                    + "file extension. Will try to add it as a package set",
214
                    bundle);
215
            }
216
            installerProviderServices.readPackageSetInfo(fis, packageInfos,
217
                zipEntriesMap);
218
        }
219
        try {
220
            bis.close();
221
            fis.close();
222
        } catch (IOException e) {
223
            LOG.warn("Error closing the input streams of the package file: "
224
                + bundle, e);
225
        }
226

  
227
        for (int i = packageInfoCount; i < packageInfos.size(); i++) {
228
            packageInfoFileMap.put(packageInfos.get(i), bundle);
229
        }
230
    }
231

  
232
    private boolean isPackageSet(File file) {
233
        return file.getName().endsWith(
234
            manager.getDefaultPackageSetFileExtension());
235
    }
236

  
237
    private boolean isPackage(File file) {
238
        return file.getName()
239
            .endsWith(manager.getDefaultPackageFileExtension());
240
    }
241

  
242
    public void addBundle(URL bundleURL) throws InstallPackageServiceException {
243
        File bundle = downloadFile(bundleURL, "packages.gvspki");
244
        addBundle(bundle);
245
    }
246

  
247
    private File downloadFile(URL bundleURL, String defaultFileName)
248
        throws InstallPackageServiceException {
249
        try {
250
            Download download = new Download();
251
            return download.downloadFile(bundleURL, defaultFileName);
252
        } catch (IOException e) {
253
            throw new InstallPackageServiceException(
254
                "Bundle file download error from URL: " + bundleURL, e);
255
        }
256
    }
257

  
258
    public void addBundlesFromDirectory(File directory)
259
        throws InstallPackageServiceException {
260
        if (!directory.isDirectory()) {
261
            throw new InstallPackageServiceException(
262
                "The application directory has to be a directory");
263
        }
264
        List<File> files = new ArrayList<File>();
265
        
266
        listRecursively(directory, new FileFilter() {
267

  
268
            private String packageExt =
269
                manager.getDefaultPackageFileExtension();
270
            private String packageSetExt =
271
                manager.getDefaultPackageSetFileExtension();
272

  
273
            public boolean accept(File file) {
274
                String name = file.getName().toLowerCase();
275
                return file.isDirectory() || name.endsWith(packageExt)
276
                    || name.endsWith(packageSetExt);
277
            }
278
        }, files);
279
        for (int i = 0; i < files.size(); i++) {
280
            if (files.get(i).isFile()) {
281
                addBundle(files.get(i));
282
            }
283
        }
284
    }
285

  
286
    private void listRecursively(File fileOrDir, FileFilter filter, List<File> files) {
287
        files.add(fileOrDir);
288
        
289
        if (fileOrDir.isDirectory()) {
290

  
291
            File[] dirContents = fileOrDir.listFiles(filter);
292

  
293
            for (File f : dirContents) {
294
                listRecursively(f, filter, files); // Recursively list.
295
            }
296
        } else {
297
            files.add(fileOrDir);
298
        }
299
    }
300

  
301
    public int getPackageCount() {
302
        if (packageInfos == null) {
303
            return 0;
304
        }
305
        return packageInfos.size();
306
    }
307
    
308
    public Manager getManager() {
309
        return this.manager;
310
    }
311

  
312
    public void downloadPackage(PackageInfo packageInfo)
313
        throws InstallPackageServiceException {
314
        this.downloadPackage(packageInfo, null);
315
    }
316

  
317
    public void downloadPackage(PackageInfo packageInfo,
318
        SimpleTaskStatus taskStatus) throws InstallPackageServiceException {
319
        File file = packageInfo.downloadFile(taskStatus);
320
        this.packageInfoFileMap.put(packageInfo, file);
321
    }
322

  
323
    public List<String> getDefaultSelectedPackagesIDs() {
324
        return installerProviderServices.getDefaultSelectedPackagesIDs();
325
    }
326

  
327
    
328
}
0 329

  
tags/v2_0_0_Build_2030/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/creation/DefaultMakePluginPackageService.java
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
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff