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 / execution / Decompress.java @ 32411

History | View | Annotate | Download (8.79 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.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.HashMap;
38
import java.util.List;
39
import java.util.Map;
40
import java.util.zip.ZipEntry;
41
import java.util.zip.ZipException;
42
import java.util.zip.ZipInputStream;
43

    
44
import org.gvsig.installer.lib.api.InstallerInfo;
45
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
46
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
47
import org.gvsig.installer.lib.impl.DefaultInstallerManager;
48
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
50
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
51
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
52
import org.gvsig.installer.lib.spi.InstallerProviderManager;
53
import org.gvsig.installer.lib.spi.execution.InstallerExecutionProvider;
54
import org.gvsig.tools.service.ServiceException;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
/**
59
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
60
 */
61
public class Decompress {
62
        private static final int OPTION_DECOMPRESS = 1;
63
        private static final int OPTION_READ_INSTALLINFO = 2;
64
        private static final int OPTION_INSTALL = 3;
65

    
66
        private int option = 0;
67

    
68
        private int BUFFER = 2048;
69

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

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

    
77
        private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
78

    
79
        public Decompress(){                
80

    
81
        }
82

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

    
87
                is.mark(0);        
88
                decompressFolderOfPlugins(is);
89
        }
90

    
91
        public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{
92
                option = OPTION_READ_INSTALLINFO;
93
                this.readedIinstallerInfos = installerInfos;
94
                this.zipEntriesMap = zipEntriesMap;
95
                is.mark(0);                
96
                decompressFolderOfPlugins(is);                        
97
        }        
98

    
99
        public void decompressPlugin(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
100
                try{
101
                        option = OPTION_DECOMPRESS;
102
                        this.outputDirectory = outputDirectory;                                
103
                        decompressPlugin(new ZipInputStream(is), "");
104
                } catch (Exception e) {
105
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
106
                }         
107
        }        
108

    
109
        public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{
110
                ZipEntry entry = null;                
111

    
112
                try {                        
113
                        is.reset();
114
                        ZipInputStream zipInputStream = new ZipInputStream(is);        
115

    
116
                        while((entry = zipInputStream.getNextEntry()) != null) {        
117
                                if (entry.getName().equals(zipEntry)){
118
                                        return zipInputStream;
119
                                }                                
120
                                zipInputStream.closeEntry();
121
                        }
122
                        zipInputStream.closeEntry();
123

    
124
                        //zipInputStream.close();                        
125
                } catch (Exception e) {
126
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
127
                }         
128
                return null;        
129
        }
130

    
131
        public void installFromStream(InputStream is, InstallerInfo installerInfo) throws InstallerExecutionServiceException{
132
                option = OPTION_INSTALL;
133
                this.selectedInstallerInfos = new ArrayList<InstallerInfo>();
134
                this.selectedInstallerInfos.add(installerInfo);
135
                decompressFolderOfPlugins(is);
136
        }
137

    
138
        public void installFromStream(InputStream is, List<InstallerInfo> installerInfos) throws InstallerExecutionServiceException{
139
                option = OPTION_INSTALL;
140
                this.selectedInstallerInfos = installerInfos;                
141
                decompressFolderOfPlugins(is);
142
        }        
143

    
144

    
145
        private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException {
146
                ZipInputStream zipInputStream = new ZipInputStream(is);        
147
                ZipEntry entry = null;                
148

    
149
                try {                        
150

    
151
                        while((entry = zipInputStream.getNextEntry()) != null) {        
152
                                logger.debug("Extracting all Plugins, plugin: " + entry);
153
                                if (option == OPTION_INSTALL){
154

    
155
                                }else if (option == OPTION_DECOMPRESS){                                
156
                                        decompressPlugin(new ZipInputStream(zipInputStream), entry.getName());
157
                                }else if (option == OPTION_READ_INSTALLINFO){                        
158
                                        readPlugin(new ZipInputStream(zipInputStream), entry.getName());
159
                                }
160
                                zipInputStream.closeEntry();
161
                        }
162
                        zipInputStream.closeEntry();
163

    
164
                        //zipInputStream.close();                        
165
                } catch (Exception e) {
166
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
167
                }                         
168
        }
169

    
170
        private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
171
                ZipEntry entry = null;
172
                byte data[] = new byte[BUFFER];                
173
                int count;
174
                int installerInfoNumber = zipEntriesMap.size();
175

    
176
                while((entry = zipInputStream.getNextEntry()) != null) {                                
177
                        logger.debug("Extracting: " + entry.getName());        
178

    
179
                        if (entry.getName().endsWith(File.separator + DefaultInstallerCreationService.INSTALLINFO_FILE_NAME)){
180
                                InstallerInfo installerInfo = readInstallInfo(zipInputStream);
181
                                zipEntriesMap.put(installerInfo, zipEntryName);                        
182
                                readedIinstallerInfos.add(installerInfo);                                        
183
                        }
184
                        zipInputStream.closeEntry();
185
                }        
186
                
187
                if (installerInfoNumber == zipEntriesMap.size()){
188
                        InstallerInfo installerInfo = new DefaultInstallerInfo();
189
                        installerInfo.setCode(zipEntryName);
190
                        installerInfo.setName(zipEntryName);
191
                        zipEntriesMap.put(installerInfo, zipEntryName);
192
                        readedIinstallerInfos.add(installerInfo);                
193
                }
194
        }
195

    
196

    
197
        private void decompressPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
198
                ZipEntry entry = null;
199
                byte data[] = new byte[BUFFER];                
200
                int count;
201
                int installerInfoNumber = 0;
202

    
203
                if (zipEntriesMap != null){
204
                        installerInfoNumber = zipEntriesMap.size();
205
                }
206

    
207
                while((entry = zipInputStream.getNextEntry()) != null) {                                
208
                        logger.debug("Extracting: " + entry.getName());        
209

    
210
                        File file = new File(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
211
                        if (file.exists()){
212
                                delete(file);
213
                        }
214
                        if (entry.isDirectory()){
215
                                file.mkdir();
216
                        }else{
217
                                FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
218

    
219
                                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
220
                                        fos.write(data, 0, count);
221
                                }
222
                                fos.flush();                
223
                                fos.close();        
224
                        }
225
                        zipInputStream.closeEntry();
226
                }
227
        }        
228

    
229
        public boolean delete(File dir) { 
230
                if (dir.isDirectory()){
231
                        String[] children = dir.list(); 
232
                        for (int i=0; i<children.length; i++) { 
233
                                boolean success = delete(new File(dir, children[i]));
234
                                if (!success) { 
235
                                        return false; 
236
                                }
237
                        } 
238
                }
239
                return dir.delete(); 
240
        }
241

    
242
        public InstallerInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{
243
                try {
244
                        return readInstallInfo(new ZipInputStream(is));
245
                } catch (IOException e) {
246
                        throw new InstallerInfoFileException("error_reading_installerinfo", e);
247
                }
248
        }
249

    
250
        private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
251
                int count;
252
                byte data[] = new byte[BUFFER];
253

    
254
                ByteArrayOutputStream out = new ByteArrayOutputStream();
255

    
256
                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
257
                        out.write(data, 0, count);
258
                }
259
                out.flush();
260

    
261
                DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
262
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
263
                installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
264

    
265
                return installerInfo;
266
        }        
267
}
268