Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / execution / DefaultInstallPackageService.java @ 34107

History | View | Annotate | Download (10 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.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.spi.InstallPackageProviderServices;
52
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
53
import org.gvsig.installer.lib.spi.InstallerProviderManager;
54
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
55
import org.gvsig.tools.service.Manager;
56
import org.gvsig.tools.service.ServiceException;
57

    
58
/**
59
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
60
 */
61
public class DefaultInstallPackageService implements InstallPackageService {
62

    
63
    private static final Logger LOG = LoggerFactory
64
        .getLogger(DefaultInstallPackageService.class);
65

    
66
    private Map<PackageInfo, File> packageInfoFileMap = null;
67
    private Map<PackageInfo, String> zipEntriesMap = null;
68
    private List<PackageInfo> packageInfos = null;
69
    private InstallerManager manager;
70
    private InstallPackageProviderServices installerProviderServices = null;
71

    
72
    public DefaultInstallPackageService(DefaultInstallerManager manager) {
73
        super();
74
        this.manager = manager;
75
        packageInfoFileMap = new HashMap<PackageInfo, File>();
76
        packageInfos = new ArrayList<PackageInfo>();
77
        zipEntriesMap = new HashMap<PackageInfo, String>();
78
        installerProviderServices =
79
            InstallerProviderLocator.getProviderManager()
80
                .createInstallerProviderServices();
81
    }
82

    
83
    public void installPackage(File applicationDirectory,
84
        PackageInfo packageInfo) throws InstallPackageServiceException {
85
        if (!applicationDirectory.exists()) {
86
            throw new InstallPackageServiceException(
87
                "application_directory_not_found");
88
        }
89
        if (!packageInfoFileMap.containsKey(packageInfo)) {
90
            throw new InstallPackageServiceException("package_not_found");
91
        }
92

    
93
        InstallPackageProvider installerExecutionProvider =
94
            createProvider(packageInfo);
95

    
96
        // Get the package or package set file
97
        File file;
98
        if (packageInfo.getDownloadURL() == null) {
99
            file = packageInfoFileMap.get(packageInfo);
100
        } else {
101
            file = downloadFile(packageInfo.getDownloadURL(), null);
102
        }
103

    
104
        // Open and install the package or package set file
105
        try {
106
            InputStream packageStream;
107
            InputStream fis = new FileInputStream(file);
108
            InputStream bis = new BufferedInputStream(fis);
109
            if (isPackage(file)) {
110
                packageStream = bis;
111
            } else {
112
                if (!isPackageSet(file)) {
113
                    LOG.warn("Trying to install a package file ({0}) "
114
                        + "without a known file extension. Will try "
115
                        + "to install it as a package set", file);
116
                }
117
                packageStream =
118
                    installerProviderServices.searchPackage(bis,
119
                        zipEntriesMap.get(packageInfo));
120
            }
121
            installerExecutionProvider.install(applicationDirectory,
122
                packageStream);
123

    
124
            packageStream.close();
125
            if (bis != packageStream) {
126
                bis.close();
127
            }
128
            fis.close();
129
        } catch (FileNotFoundException e) {
130
            throw new InstallPackageServiceException("Package file not found: "
131
                + file);
132
        } catch (IOException e) {
133
            throw new InstallPackageServiceException(
134
                "IO error installing the package file: " + file, e);
135
        }
136

    
137
    }
138

    
139
    public void installPackage(File applicationDirectory, String packageCode)
140
        throws InstallPackageServiceException {
141
        PackageInfo packageInfo = getPackageInfo(packageCode);
142
        if (packageInfo == null) {
143
            throw new InstallPackageServiceException(
144
                "The package doesn't exist");
145
        }
146
        installPackage(applicationDirectory, packageInfo);
147
    }
148

    
149
    private InstallPackageProvider createProvider(PackageInfo packageInfo)
150
        throws InstallPackageServiceException {
151
        InstallerProviderManager installerProviderManager =
152
            (InstallerProviderManager) ((DefaultInstallerManager) manager)
153
                .getProviderManager();
154

    
155
        try {
156
            return installerProviderManager.createExecutionProvider(packageInfo
157
                .getType());
158
        } catch (ServiceException e) {
159
            throw new InstallPackageServiceException(
160
                "Error creating the provider", e);
161
        }
162
    }
163

    
164
    public PackageInfo getPackageInfo(int index) {
165
        if (index >= packageInfos.size()) {
166
            return null;
167
        }
168
        return packageInfos.get(index);
169
    }
170

    
171
    public PackageInfo getPackageInfo(String packageCode) {
172
        for (int i = 0; i < getPackageCount(); i++) {
173
            if (packageInfos.get(i).getCode().equals(packageCode)) {
174
                return packageInfos.get(i);
175
            }
176
        }
177
        return null;
178
    }
179

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

    
214
        for (int i = packageInfoCount; i < packageInfos.size(); i++) {
215
            packageInfoFileMap.put(packageInfos.get(i), bundle);
216
        }
217
    }
218

    
219
    private boolean isPackageSet(File file) {
220
        return file.getName().endsWith(
221
            manager.getDefaultPackageSetFileExtension());
222
    }
223

    
224
    private boolean isPackage(File file) {
225
        return file.getName()
226
            .endsWith(manager.getDefaultPackageFileExtension());
227
    }
228

    
229
    public void addBundle(URL bundleURL) throws InstallPackageServiceException {
230
        File bundle = downloadFile(bundleURL, "packages.gvspki");
231
        addBundle(bundle);
232
    }
233

    
234
    private File downloadFile(URL bundleURL, String defaultFileName)
235
        throws InstallPackageServiceException {
236
        try {
237
            return InstallerProviderLocator.getProviderManager()
238
                .createInstallerProviderServices()
239
                .downloadFile(bundleURL, defaultFileName);
240
        } catch (IOException e) {
241
            throw new InstallPackageServiceException(
242
                "Bundle file download error from URL: " + bundleURL, e);
243
        }
244
    }
245

    
246
    public void addBundlesFromDirectory(File directory)
247
        throws InstallPackageServiceException {
248
        if (!directory.isDirectory()) {
249
            throw new InstallPackageServiceException(
250
                "The application directory has to be a directory");
251
        }
252
        File[] files = directory.listFiles(new FileFilter() {
253

    
254
            private String packageExt = manager
255
                .getDefaultPackageFileExtension();
256
            private String packageSetExt = manager
257
                .getDefaultPackageSetFileExtension();
258

    
259
            public boolean accept(File file) {
260
                String name = file.getName().toLowerCase();
261
                return name.endsWith(packageExt)
262
                    || name.endsWith(packageSetExt);
263
            }
264
        });
265
        for (int i = 0; i < files.length; i++) {
266
            if (files[i].isFile()) {
267
                addBundle(files[i]);
268
            }
269
        }
270
    }
271

    
272
    public int getPackageCount() {
273
        if (packageInfos == null) {
274
            return 0;
275
        }
276
        return packageInfos.size();
277
    }
278

    
279
    public Manager getManager() {
280
        return this.manager;
281
    }
282

    
283
}