Revision 32500

View differences:

branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/InstallerProviderServices.java
30 30
import java.io.File;
31 31
import java.io.InputStream;
32 32
import java.io.OutputStream;
33
import java.util.List;
34
import java.util.Map;
33 35

  
34 36
import org.gvsig.installer.lib.api.InstallerInfo;
35 37
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
......
83 85
	public void readInstallInfo(File directory, InstallerInfo installerInfo) throws InstallerInfoFileException;
84 86
	
85 87
	/**
88
	 * It reads an installer and parses all the install.info files.
89
	 * @param is
90
	 * The installer.
91
	 * @param installerInfos
92
	 * A list of the information of the plugins to install.
93
	 * @param zipEntriesMap
94
	 * A map to retrieve the zipEntry name for each plugin. This information is necessary to
95
	 * select the plugin to decompress.
96
	 * @throws InstallerExecutionServiceException
97
	 * If there is a problem reading the installer.
98
	 */
99
	public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException;
100
	
101
	/**
86 102
	 * Writes the install.info file in a concrete directory.
87 103
	 * @param directory
88 104
	 * The directory.
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/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.gvsig.installer.lib.api.InstallerInfo;
44
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
45
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
46
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService;
47
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
48
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
49
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
50
import org.gvsig.installer.lib.spi.InstallerProviderManager;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

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

  
62
	private int option = 0;
63

  
64
	private int BUFFER = 2048;
65

  
66
	private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
67

  
68
	private List<InstallerInfo> readedIinstallerInfos = null;	
69
	private File outputDirectory = null;	
70
	private List<InstallerInfo> selectedInstallerInfos = null;
71
	private Map<InstallerInfo, String> zipEntriesMap = null;
72

  
73
	private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
74

  
75
	public Decompress(){		
76

  
77
	}
78

  
79
	public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
80
		option = OPTION_DECOMPRESS;
81
		this.outputDirectory = outputDirectory;
82
		
83
		decompressFolderOfPlugins(is);
84
	}
85

  
86
	public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{
87
		option = OPTION_READ_INSTALLINFO;
88
		this.readedIinstallerInfos = installerInfos;
89
		this.zipEntriesMap = zipEntriesMap;	
90
		decompressFolderOfPlugins(is);			
91
	}	
92

  
93
	public void decompressPlugin(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
94
		try{
95
			option = OPTION_DECOMPRESS;
96
			this.outputDirectory = outputDirectory;				
97
			decompressPlugin(new ZipInputStream(is), "");
98
		} catch (Exception e) {
99
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
100
		} 	
101
	}	
102

  
103
	public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{
104
		ZipEntry entry = null;		
105

  
106
		try {		
107
			ZipInputStream zipInputStream = new ZipInputStream(is);	
108

  
109
			while((entry = zipInputStream.getNextEntry()) != null) {	
110
				if (entry.getName().equals(zipEntry)){
111
					return zipInputStream;
112
				}				
113
				zipInputStream.closeEntry();
114
			}
115
			zipInputStream.closeEntry();
116

  
117
			zipInputStream.close();			
118
		} catch (Exception e) {
119
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
120
		} 	
121
		return null;	
122
	}
123

  
124
	public void installFromStream(InputStream is, InstallerInfo installerInfo) throws InstallerExecutionServiceException{
125
		option = OPTION_INSTALL;
126
		this.selectedInstallerInfos = new ArrayList<InstallerInfo>();
127
		this.selectedInstallerInfos.add(installerInfo);
128
		decompressFolderOfPlugins(is);
129
	}
130

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

  
137

  
138
	private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException {
139
		ZipInputStream zipInputStream = new ZipInputStream(is);	
140
		ZipEntry entry = null;		
141

  
142
		try {			
143

  
144
			while((entry = zipInputStream.getNextEntry()) != null) {	
145
				logger.debug("Extracting all Plugins, plugin: " + entry);
146
				if (option == OPTION_INSTALL){
147

  
148
				}else if (option == OPTION_DECOMPRESS){				
149
					decompressPlugin(new ZipInputStream(zipInputStream), entry.getName());
150
				}else if (option == OPTION_READ_INSTALLINFO){			
151
					readPlugin(new ZipInputStream(zipInputStream), entry.getName());
152
				}
153
				zipInputStream.closeEntry();
154
			}			
155
			zipInputStream.close();	
156
			
157
		} catch (Exception e) {
158
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
159
		} 			
160
	}
161

  
162
	private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
163
		ZipEntry entry = null;
164
		byte data[] = new byte[BUFFER];		
165
		int count;
166
		int installerInfoNumber = zipEntriesMap.size();
167

  
168
		while((entry = zipInputStream.getNextEntry()) != null) {			        
169
			logger.debug("Extracting: " + entry.getName());	
170

  
171
			if (entry.getName().endsWith(File.separator + "install.info")){
172
				InstallerInfo installerInfo = readInstallInfo(zipInputStream);
173
				zipEntriesMap.put(installerInfo, zipEntryName);			
174
				readedIinstallerInfos.add(installerInfo);					
175
			}
176
			zipInputStream.closeEntry();
177
		}			
178
		//zipInputStream.close();	
179
		
180
		if (installerInfoNumber == zipEntriesMap.size()){
181
			InstallerInfo installerInfo = new DefaultInstallerInfo();
182
			installerInfo.setCode(zipEntryName);
183
			installerInfo.setName(zipEntryName);
184
			zipEntriesMap.put(installerInfo, zipEntryName);
185
			readedIinstallerInfos.add(installerInfo);		
186
		}
187
	}
188

  
189

  
190
	private void decompressPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
191
		ZipEntry entry = null;
192
		byte data[] = new byte[BUFFER];		
193
		int count;
194
		int installerInfoNumber = 0;
195

  
196
		if (zipEntriesMap != null){
197
			installerInfoNumber = zipEntriesMap.size();
198
		}
199

  
200
		while((entry = zipInputStream.getNextEntry()) != null) {			        
201
			logger.debug("Extracting: " + entry.getName());	
202

  
203
			File file = new File(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
204
			if (file.exists()){
205
				delete(file);
206
			}
207
			if (entry.isDirectory()){
208
				file.mkdir();
209
			}else{
210
				FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
211

  
212
				while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
213
					fos.write(data, 0, count);
214
				}
215
				fos.flush();		
216
				fos.close();	
217
			}
218
			//zipInputStream.closeEntry();
219
		}
220
	}	
221

  
222
	public boolean delete(File dir) { 
223
		if (dir.isDirectory()){
224
			String[] children = dir.list(); 
225
			for (int i=0; i<children.length; i++) { 
226
				boolean success = delete(new File(dir, children[i]));
227
				if (!success) { 
228
					return false; 
229
				}
230
			} 
231
		}
232
		return dir.delete(); 
233
	}
234

  
235
	public InstallerInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{
236
		try {
237
			return readInstallInfo(new ZipInputStream(is));
238
		} catch (IOException e) {
239
			throw new InstallerInfoFileException("error_reading_installerinfo", e);
240
		}
241
	}
242

  
243
	private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
244
		int count;
245
		byte data[] = new byte[BUFFER];
246

  
247
		ByteArrayOutputStream out = new ByteArrayOutputStream();
248

  
249
		while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
250
			out.write(data, 0, count);
251
		}
252
		out.flush();
253

  
254
		DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
255
		InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
256
		installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
257

  
258
		return installerInfo;
259
	}	
260
}
261

  
0 262

  
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/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.InstallerCreationServiceException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

  
45
/**
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public class Compress {
49
	static final int BUFFER = 2048;
50
	private static final Logger logger = LoggerFactory.getLogger(Compress.class);
51
		
52
	public Compress() {
53
		super();		
54
	}
55

  
56
	public void compressPlugin(File file, String fileName, OutputStream os) throws InstallerCreationServiceException {
57
		ArrayList<File> files = new ArrayList<File>();
58
		ArrayList<String> fileNames = new ArrayList<String>();
59
		files.add(file);
60
		fileNames.add(fileName);
61
		compressPlugins(files, fileNames, os);
62
	}
63

  
64
	public void compressPlugins(List<File> files, List<String> fileNames, OutputStream os) throws InstallerCreationServiceException {
65
		try{
66
			ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
67
			byte[] buf = new byte[1024];
68
			int len;
69
			
70
			for (int i=0 ; i<files.size() ; i++){
71
				File file = files.get(i);				
72
				
73
				int parentFileLenght = file.getParentFile().toString().length();
74
				String pluginName = file.toString().substring(parentFileLenght, file.toString().length());				
75
				
76
				ZipEntry zipEntry = new ZipEntry(fileNames.get(i));				
77
				zos.putNextEntry(zipEntry);								
78
					
79
				ZipOutputStream zosPlugin = new ZipOutputStream(zos);				
80
				compressPluginFile(file, parentFileLenght, zosPlugin);				
81
				zosPlugin.finish();
82
				
83
				zos.closeEntry();
84
	
85
				zos.close();				
86
			}
87
			zos.close();
88
		} catch (Exception e) {
89
			logger.error("Error compressing the plugin");
90
			throw new InstallerCreationServiceException("Error writing the plugin", e);
91
		}
92
	}
93

  
94

  
95
	private void compressPluginFile(File file, int parenFileLength, ZipOutputStream zosPlugin) throws IOException  {		
96
		if (file.isDirectory()) {
97
			String[] fileNames = file.list();
98
			if (fileNames != null) {
99
				for (int i=0; i<fileNames.length; i++)  {
100
					compressPluginFile(new File(file, fileNames[i]), parenFileLength, zosPlugin);
101
				}
102
			}
103
		}else{
104
			byte[] buf = new byte[1024];
105
			int len;
106

  
107
			String fileName = file.toString().substring(parenFileLength, file.toString().length());
108
			
109
			ZipEntry zipEntry = new ZipEntry(fileName);      
110
			zosPlugin.putNextEntry(zipEntry);   
111
			
112
			FileInputStream fin = new FileInputStream(file);
113
			BufferedInputStream in = new BufferedInputStream(fin);			    
114
			while ((len = in.read(buf)) >= 0) {
115
				zosPlugin.write(buf, 0, len);
116
			}        
117
			in.close();      
118
			zosPlugin.closeEntry();
119
		}
120
	}
121
}
122

  
0 123

  
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/DefaultInstallerProviderServices.java
30 30
import java.io.File;
31 31
import java.io.InputStream;
32 32
import java.io.OutputStream;
33
import java.util.List;
34
import java.util.Map;
33 35

  
34 36
import org.gvsig.installer.lib.api.InstallerInfo;
35 37
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
36 38
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
37
import org.gvsig.installer.lib.impl.creation.Compress;
38
import org.gvsig.installer.lib.impl.execution.Decompress;
39 39
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
40 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;
41 43
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
42 44
import org.gvsig.installer.lib.spi.InstallerProviderServices;
43 45
import org.gvsig.tools.service.spi.AbstractProviderServices;
......
93 95
		return decompress.searchPlugin(is, zipEntry);	
94 96
	}
95 97

  
98
	public void readInstallInfo(InputStream is,
99
			List<InstallerInfo> installerInfos,
100
			Map<InstallerInfo, String> zipEntriesMap)
101
			throws InstallerExecutionServiceException {
102
		Decompress decompress = new Decompress();			
103
		decompress.readInstallInfo(is, installerInfos, zipEntriesMap);			
104
	}
105

  
96 106
}
97 107

  
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
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.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.gvsig.installer.lib.api.InstallerInfo;
44
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
45
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
46
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService;
47
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
48
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
49
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
50
import org.gvsig.installer.lib.spi.InstallerProviderManager;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

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

  
62
	private int option = 0;
63

  
64
	private int BUFFER = 2048;
65

  
66
	private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
67

  
68
	private List<InstallerInfo> readedIinstallerInfos = null;	
69
	private File outputDirectory = null;	
70
	private List<InstallerInfo> selectedInstallerInfos = null;
71
	private Map<InstallerInfo, String> zipEntriesMap = null;
72

  
73
	private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
74

  
75
	public Decompress(){		
76

  
77
	}
78

  
79
	public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
80
		option = OPTION_DECOMPRESS;
81
		this.outputDirectory = outputDirectory;
82
		
83
		decompressFolderOfPlugins(is);
84
	}
85

  
86
	public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{
87
		option = OPTION_READ_INSTALLINFO;
88
		this.readedIinstallerInfos = installerInfos;
89
		this.zipEntriesMap = zipEntriesMap;	
90
		decompressFolderOfPlugins(is);			
91
	}	
92

  
93
	public void decompressPlugin(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
94
		try{
95
			option = OPTION_DECOMPRESS;
96
			this.outputDirectory = outputDirectory;				
97
			decompressPlugin(new ZipInputStream(is), "");
98
		} catch (Exception e) {
99
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
100
		} 	
101
	}	
102

  
103
	public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{
104
		ZipEntry entry = null;		
105

  
106
		try {		
107
			ZipInputStream zipInputStream = new ZipInputStream(is);	
108

  
109
			while((entry = zipInputStream.getNextEntry()) != null) {	
110
				if (entry.getName().equals(zipEntry)){
111
					return zipInputStream;
112
				}				
113
				zipInputStream.closeEntry();
114
			}
115
			zipInputStream.closeEntry();
116

  
117
			zipInputStream.close();			
118
		} catch (Exception e) {
119
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
120
		} 	
121
		return null;	
122
	}
123

  
124
	public void installFromStream(InputStream is, InstallerInfo installerInfo) throws InstallerExecutionServiceException{
125
		option = OPTION_INSTALL;
126
		this.selectedInstallerInfos = new ArrayList<InstallerInfo>();
127
		this.selectedInstallerInfos.add(installerInfo);
128
		decompressFolderOfPlugins(is);
129
	}
130

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

  
137

  
138
	private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException {
139
		ZipInputStream zipInputStream = new ZipInputStream(is);	
140
		ZipEntry entry = null;		
141

  
142
		try {			
143

  
144
			while((entry = zipInputStream.getNextEntry()) != null) {	
145
				logger.debug("Extracting all Plugins, plugin: " + entry);
146
				if (option == OPTION_INSTALL){
147

  
148
				}else if (option == OPTION_DECOMPRESS){				
149
					decompressPlugin(new ZipInputStream(zipInputStream), entry.getName());
150
				}else if (option == OPTION_READ_INSTALLINFO){			
151
					readPlugin(new ZipInputStream(zipInputStream), entry.getName());
152
				}
153
				zipInputStream.closeEntry();
154
			}			
155
			zipInputStream.close();	
156
			
157
		} catch (Exception e) {
158
			throw new InstallerExecutionServiceException("Error reading the plugin", e);
159
		} 			
160
	}
161

  
162
	private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
163
		ZipEntry entry = null;
164
		byte data[] = new byte[BUFFER];		
165
		int count;
166
		int installerInfoNumber = zipEntriesMap.size();
167

  
168
		while((entry = zipInputStream.getNextEntry()) != null) {			        
169
			logger.debug("Extracting: " + entry.getName());	
170

  
171
			if (entry.getName().endsWith(File.separator + DefaultInstallerCreationService.INSTALLINFO_FILE_NAME)){
172
				InstallerInfo installerInfo = readInstallInfo(zipInputStream);
173
				zipEntriesMap.put(installerInfo, zipEntryName);			
174
				readedIinstallerInfos.add(installerInfo);					
175
			}
176
			zipInputStream.closeEntry();
177
		}			
178
		//zipInputStream.close();	
179
		
180
		if (installerInfoNumber == zipEntriesMap.size()){
181
			InstallerInfo installerInfo = new DefaultInstallerInfo();
182
			installerInfo.setCode(zipEntryName);
183
			installerInfo.setName(zipEntryName);
184
			zipEntriesMap.put(installerInfo, zipEntryName);
185
			readedIinstallerInfos.add(installerInfo);		
186
		}
187
	}
188

  
189

  
190
	private void decompressPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
191
		ZipEntry entry = null;
192
		byte data[] = new byte[BUFFER];		
193
		int count;
194
		int installerInfoNumber = 0;
195

  
196
		if (zipEntriesMap != null){
197
			installerInfoNumber = zipEntriesMap.size();
198
		}
199

  
200
		while((entry = zipInputStream.getNextEntry()) != null) {			        
201
			logger.debug("Extracting: " + entry.getName());	
202

  
203
			File file = new File(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
204
			if (file.exists()){
205
				delete(file);
206
			}
207
			if (entry.isDirectory()){
208
				file.mkdir();
209
			}else{
210
				FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
211

  
212
				while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
213
					fos.write(data, 0, count);
214
				}
215
				fos.flush();		
216
				fos.close();	
217
			}
218
			//zipInputStream.closeEntry();
219
		}
220
	}	
221

  
222
	public boolean delete(File dir) { 
223
		if (dir.isDirectory()){
224
			String[] children = dir.list(); 
225
			for (int i=0; i<children.length; i++) { 
226
				boolean success = delete(new File(dir, children[i]));
227
				if (!success) { 
228
					return false; 
229
				}
230
			} 
231
		}
232
		return dir.delete(); 
233
	}
234

  
235
	public InstallerInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{
236
		try {
237
			return readInstallInfo(new ZipInputStream(is));
238
		} catch (IOException e) {
239
			throw new InstallerInfoFileException("error_reading_installerinfo", e);
240
		}
241
	}
242

  
243
	private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
244
		int count;
245
		byte data[] = new byte[BUFFER];
246

  
247
		ByteArrayOutputStream out = new ByteArrayOutputStream();
248

  
249
		while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
250
			out.write(data, 0, count);
251
		}
252
		out.flush();
253

  
254
		DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
255
		InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
256
		installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
257

  
258
		return installerInfo;
259
	}	
260
}
261

  
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/DefaultInstallerExecutionService.java
121 121
	}
122 122

  
123 123
	public void addInstaller(InputStream inputStream) throws InstallerExecutionServiceException {
124
		Decompress decompress = new Decompress();			
125
		decompress.readInstallInfo(inputStream, installerInfos, zipEntriesMap);		
124
		installerProviderServices.readInstallInfo(inputStream, installerInfos, zipEntriesMap);
126 125
	}	
127 126

  
128 127
	public void setApplicationDirectory(File applicationDirectory) throws InstallerExecutionServiceException{
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/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.creation;
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.InstallerCreationServiceException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

  
45
/**
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public class Compress {
49
	static final int BUFFER = 2048;
50
	private static final Logger logger = LoggerFactory.getLogger(Compress.class);
51
	
52
	public void compressPlugin(File file, String fileName, OutputStream os) throws InstallerCreationServiceException {
53
		ArrayList<File> files = new ArrayList<File>();
54
		ArrayList<String> fileNames = new ArrayList<String>();
55
		files.add(file);
56
		fileNames.add(fileName);
57
		compressPlugins(files, fileNames, os);
58
	}
59

  
60
	public void compressPlugins(List<File> files, List<String> fileNames, OutputStream os) throws InstallerCreationServiceException {
61
		try{
62
			ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
63
			byte[] buf = new byte[1024];
64
			int len;
65
			
66
			for (int i=0 ; i<files.size() ; i++){
67
				File file = files.get(i);				
68
				
69
				int parentFileLenght = file.getParentFile().toString().length();
70
				String pluginName = file.toString().substring(parentFileLenght, file.toString().length());				
71
				
72
				ZipEntry zipEntry = new ZipEntry(fileNames.get(i));				
73
				zos.putNextEntry(zipEntry);								
74
					
75
				ZipOutputStream zosPlugin = new ZipOutputStream(zos);				
76
				compressPluginFile(file, parentFileLenght, zosPlugin);				
77
				zosPlugin.finish();
78
				
79
				zos.closeEntry();
80
	
81
				zos.close();				
82
			}
83
			zos.close();
84
		} catch (Exception e) {
85
			logger.error("Error compressing the plugin");
86
			throw new InstallerCreationServiceException("Error writing the plugin", e);
87
		}
88
	}
89

  
90

  
91
	private void compressPluginFile(File file, int parenFileLength, ZipOutputStream zosPlugin) throws IOException  {		
92
		if (file.isDirectory()) {
93
			String[] fileNames = file.list();
94
			if (fileNames != null) {
95
				for (int i=0; i<fileNames.length; i++)  {
96
					compressPluginFile(new File(file, fileNames[i]), parenFileLength, zosPlugin);
97
				}
98
			}
99
		}else{
100
			byte[] buf = new byte[1024];
101
			int len;
102

  
103
			String fileName = file.toString().substring(parenFileLength, file.toString().length());
104
			
105
			ZipEntry zipEntry = new ZipEntry(fileName);      
106
			zosPlugin.putNextEntry(zipEntry);   
107
			
108
			FileInputStream fin = new FileInputStream(file);
109
			BufferedInputStream in = new BufferedInputStream(fin);			    
110
			while ((len = in.read(buf)) >= 0) {
111
				zosPlugin.write(buf, 0, len);
112
			}        
113
			in.close();      
114
			zosPlugin.closeEntry();
115
		}
116
	}
117
}
118

  
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/DefaultInstallerCreationService.java
56 56
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
57 57
 */
58 58
public class DefaultInstallerCreationService implements InstallerCreationService {
59
	public static final String INSTALLINFO_FILE_NAME = "install.info";
60 59
	public static final String ANT_FILE_NAME = "install.xml";
61 60
	public static final String COPIED_FILES_DIRECTORY_NAME = "files";
62 61
	private File applicationDirectory;
......
261 260
		return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
262 261
	}
263 262

  
264
	private String getInstallerInfoFileName() throws InstallerCreationServiceException{
265
		return getInstallerInfoFileName(getPluginToInstallDirectory());
266
	}
267

  
268 263
	private String getAntFileName() throws InstallerCreationServiceException{
269 264
		return getAntFileName(getPluginToInstallDirectory());
270 265
	}
271 266

  
272 267
	private String getCopiedFilesDirectoryName(String pluginDirectory) throws InstallerCreationServiceException{
273 268
		return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
274
	}
269
	}	
275 270

  
276
	private String getInstallerInfoFileName(String pluginDirectory) throws InstallerCreationServiceException{
277
		return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME;
278
	}
279

  
280 271
	private String getAntFileName(String pluginDirectory) throws InstallerCreationServiceException{
281 272
		return pluginDirectory + File.separator + ANT_FILE_NAME;
282 273
	}

Also available in: Unified diff