Revision 32420
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 | ||
---|---|---|
40 | 40 |
import org.gvsig.installer.lib.api.InstallerInfo; |
41 | 41 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionService; |
42 | 42 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException; |
43 |
import org.gvsig.installer.lib.impl.DefaultInstallerInfo; |
|
44 | 43 |
import org.gvsig.installer.lib.impl.DefaultInstallerManager; |
45 | 44 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
46 | 45 |
import org.gvsig.installer.lib.spi.execution.InstallerExecutionProvider; |
... | ... | |
53 | 52 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a> |
54 | 53 |
*/ |
55 | 54 |
public class DefaultInstallerExecutionService implements InstallerExecutionService{ |
56 |
private Map<InstallerInfo, InputStream> inputStreams = null;
|
|
57 |
private Map<InstallerInfo, String> zipEntries = null; |
|
55 |
private Map<InstallerInfo, File> installFilesMap = null;
|
|
56 |
private Map<InstallerInfo, String> zipEntriesMap = null;
|
|
58 | 57 |
private List<InstallerInfo> installerInfos = null; |
59 | 58 |
private Manager manager; |
60 | 59 |
private int pluginToInstallIndex = -1; |
... | ... | |
64 | 63 |
public DefaultInstallerExecutionService(DefaultInstallerManager manager) { |
65 | 64 |
super(); |
66 | 65 |
this.manager = manager; |
67 |
inputStreams = new HashMap<InstallerInfo, InputStream>();
|
|
66 |
installFilesMap = new HashMap<InstallerInfo, File>();
|
|
68 | 67 |
installerInfos = new ArrayList<InstallerInfo>(); |
69 |
zipEntries = new HashMap<InstallerInfo, String>(); |
|
68 |
zipEntriesMap = new HashMap<InstallerInfo, String>();
|
|
70 | 69 |
} |
71 | 70 |
|
72 | 71 |
public void executeInstaller() |
... | ... | |
74 | 73 |
if (pluginToInstallIndex == -1){ |
75 | 74 |
throw new InstallerExecutionServiceException("not_installer_selected"); |
76 | 75 |
} |
76 |
if (!installFilesMap.containsKey(installerInfos.get(pluginToInstallIndex))){ |
|
77 |
throw new InstallerExecutionServiceException("file_not_found_use_execute_installer_with_stream"); |
|
78 |
} |
|
79 |
File file = installFilesMap.get(installerInfos.get(pluginToInstallIndex)); |
|
80 |
try { |
|
81 |
executeInstaller(new FileInputStream(file)); |
|
82 |
} catch (FileNotFoundException e) { |
|
83 |
throw new InstallerExecutionServiceException("file_not_found"); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
public void executeInstaller(InputStream inputStream) throws InstallerExecutionServiceException{ |
|
88 |
if (pluginToInstallIndex == -1){ |
|
89 |
throw new InstallerExecutionServiceException("not_installer_selected"); |
|
90 |
} |
|
77 | 91 |
if ((applicationDirectory == null) || (!applicationDirectory.exists())){ |
78 | 92 |
throw new InstallerExecutionServiceException("not_valid_application_directory_selected"); |
79 | 93 |
} |
80 |
if (!inputStreams.containsKey(installerInfos.get(pluginToInstallIndex))){ |
|
81 |
throw new InstallerExecutionServiceException("stream_not_found"); |
|
82 |
} |
|
83 | 94 |
//Execute the installer |
84 | 95 |
InstallerExecutionProvider installerExecutionProvider = createProvider(pluginToInstallIndex); |
85 | 96 |
installerExecutionProvider.setApplicationDirectory(applicationDirectory); |
86 | 97 |
// |
87 | 98 |
Decompress decompress = new Decompress(); |
88 | 99 |
InstallerInfo instalerInfo = installerInfos.get(pluginToInstallIndex); |
89 |
InputStream is = decompress.searchPlugin(inputStreams.get(instalerInfo),zipEntries.get(instalerInfo));
|
|
100 |
InputStream is = decompress.searchPlugin(inputStream,zipEntriesMap.get(instalerInfo));
|
|
90 | 101 |
installerExecutionProvider.setInstaller(is); |
91 |
installerExecutionProvider.install(); |
|
92 |
} |
|
102 |
installerExecutionProvider.install();
|
|
103 |
}
|
|
93 | 104 |
|
94 | 105 |
private InstallerExecutionProvider createProvider(int index) throws InstallerExecutionServiceException { |
95 | 106 |
InstallerProviderManager installerProviderManager = (InstallerProviderManager)((DefaultInstallerManager)manager).getProviderManager(); |
... | ... | |
110 | 121 |
} |
111 | 122 |
|
112 | 123 |
public void addInstaller(InputStream inputStream) throws InstallerExecutionServiceException { |
113 |
Decompress decompress = new Decompress(); |
|
114 |
int from = installerInfos.size(); |
|
115 |
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); |
|
116 |
decompress.readInstallInfo(bufferedInputStream, installerInfos, zipEntries); |
|
117 |
int to = installerInfos.size(); |
|
118 |
for (int i=from ; i<to ; i++){ |
|
119 |
inputStreams.put(installerInfos.get(i), bufferedInputStream); |
|
120 |
} |
|
124 |
Decompress decompress = new Decompress(); |
|
125 |
decompress.readInstallInfo(inputStream, installerInfos, zipEntriesMap); |
|
121 | 126 |
} |
122 | 127 |
|
123 | 128 |
public void setApplicationDirectory(File applicationDirectory) throws InstallerExecutionServiceException{ |
... | ... | |
134 | 139 |
File installDirectory = getInstallDirectory(); |
135 | 140 |
if (installDirectory.exists()){ |
136 | 141 |
File[] files = installDirectory.listFiles(); |
142 |
int from = installerInfos.size(); |
|
137 | 143 |
for (int i=0 ; i<files.length ; i++){ |
138 |
try { |
|
139 |
addInstaller(new FileInputStream(files[i])); |
|
144 |
try {
|
|
145 |
addInstaller(new FileInputStream(files[i]));
|
|
140 | 146 |
} catch (FileNotFoundException e) { |
141 | 147 |
logger.error("File not found", e); |
142 | 148 |
} |
149 |
} |
|
150 |
for (int i=0 ; i<files.length ; i++){ |
|
151 |
installFilesMap.put(installerInfos.get(from+i), files[i]); |
|
143 | 152 |
} |
144 | 153 |
} |
145 | 154 |
} |
146 | 155 |
} |
147 | 156 |
|
148 | 157 |
public void deleteInstallers() { |
149 |
inputStreams.clear();
|
|
158 |
installFilesMap.clear();
|
|
150 | 159 |
installerInfos.clear(); |
151 | 160 |
} |
152 | 161 |
|
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 | ||
---|---|---|
83 | 83 |
public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException { |
84 | 84 |
option = OPTION_DECOMPRESS; |
85 | 85 |
this.outputDirectory = outputDirectory; |
86 |
|
|
87 |
is.mark(0); |
|
86 |
|
|
88 | 87 |
decompressFolderOfPlugins(is); |
89 | 88 |
} |
90 | 89 |
|
91 | 90 |
public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{ |
92 | 91 |
option = OPTION_READ_INSTALLINFO; |
93 | 92 |
this.readedIinstallerInfos = installerInfos; |
94 |
this.zipEntriesMap = zipEntriesMap; |
|
95 |
is.mark(0); |
|
93 |
this.zipEntriesMap = zipEntriesMap; |
|
96 | 94 |
decompressFolderOfPlugins(is); |
97 | 95 |
} |
98 | 96 |
|
... | ... | |
109 | 107 |
public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{ |
110 | 108 |
ZipEntry entry = null; |
111 | 109 |
|
112 |
try { |
|
113 |
is.reset(); |
|
110 |
try { |
|
114 | 111 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
115 | 112 |
|
116 | 113 |
while((entry = zipInputStream.getNextEntry()) != null) { |
... | ... | |
121 | 118 |
} |
122 | 119 |
zipInputStream.closeEntry(); |
123 | 120 |
|
124 |
//zipInputStream.close();
|
|
121 |
zipInputStream.close(); |
|
125 | 122 |
} catch (Exception e) { |
126 | 123 |
throw new InstallerExecutionServiceException("Error reading the plugin", e); |
127 | 124 |
} |
... | ... | |
158 | 155 |
readPlugin(new ZipInputStream(zipInputStream), entry.getName()); |
159 | 156 |
} |
160 | 157 |
zipInputStream.closeEntry(); |
161 |
} |
|
162 |
zipInputStream.closeEntry(); |
|
163 |
|
|
164 |
//zipInputStream.close(); |
|
158 |
} |
|
159 |
zipInputStream.close(); |
|
160 |
|
|
165 | 161 |
} catch (Exception e) { |
166 | 162 |
throw new InstallerExecutionServiceException("Error reading the plugin", e); |
167 | 163 |
} |
... | ... | |
182 | 178 |
readedIinstallerInfos.add(installerInfo); |
183 | 179 |
} |
184 | 180 |
zipInputStream.closeEntry(); |
185 |
} |
|
181 |
} |
|
182 |
//zipInputStream.close(); |
|
186 | 183 |
|
187 | 184 |
if (installerInfoNumber == zipEntriesMap.size()){ |
188 | 185 |
InstallerInfo installerInfo = new DefaultInstallerInfo(); |
... | ... | |
222 | 219 |
fos.flush(); |
223 | 220 |
fos.close(); |
224 | 221 |
} |
225 |
zipInputStream.closeEntry(); |
|
222 |
//zipInputStream.closeEntry();
|
|
226 | 223 |
} |
227 | 224 |
} |
228 | 225 |
|
branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/InstallerServiceTest.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.installer.lib.impl; |
29 | 29 |
|
30 |
import java.io.BufferedReader; |
|
30 |
import java.io.BufferedOutputStream; |
|
31 |
import java.io.DataOutputStream; |
|
31 | 32 |
import java.io.File; |
32 | 33 |
import java.io.FileInputStream; |
33 | 34 |
import java.io.FileOutputStream; |
34 |
import java.io.FileReader; |
|
35 | 35 |
import java.io.IOException; |
36 | 36 |
import java.io.InputStream; |
37 | 37 |
import java.io.OutputStream; |
... | ... | |
42 | 42 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a> |
43 | 43 |
*/ |
44 | 44 |
public abstract class InstallerServiceTest extends AbstractLibraryAutoInitTestCase{ |
45 |
private boolean isCopied = false; |
|
46 | 45 |
|
47 |
|
|
48 | 46 |
public File getApplicationDirectory() throws IOException{ |
49 | 47 |
File templateFile = new File(getClass().getClassLoader().getResource("application").getFile()); |
50 | 48 |
File applicationDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "tmp_gvsig_installer"); |
51 | 49 |
|
52 |
if (!isCopied){ |
|
53 |
if (applicationDirectory.exists()){ |
|
54 |
deleteDir(applicationDirectory); |
|
55 |
} |
|
56 |
copy(templateFile, applicationDirectory); |
|
57 |
isCopied = true; |
|
50 |
if (applicationDirectory.exists()){ |
|
51 |
deleteDir(applicationDirectory); |
|
58 | 52 |
} |
59 |
|
|
53 |
copy(templateFile, applicationDirectory); |
|
54 |
|
|
60 | 55 |
return applicationDirectory; |
61 | 56 |
} |
57 |
|
|
58 |
public File getTemporalFile(){ |
|
59 |
return new File(System.getProperty("java.io.tmpdir") + File.separator + "installer" + Math.random()); |
|
60 |
} |
|
62 | 61 |
|
63 | 62 |
public File getPluginsDirectory() throws IOException{ |
64 | 63 |
File applicationDirectory = getApplicationDirectory(); |
... | ... | |
106 | 105 |
} |
107 | 106 |
} |
108 | 107 |
|
108 |
public void writeToFile(InputStream is, File file) { |
|
109 |
try { |
|
110 |
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); |
|
111 |
InputStream is2 = is; |
|
112 |
boolean again = true; |
|
113 |
while(again) { |
|
114 |
if(is2.read() > -1) { |
|
115 |
out.writeByte(is.read()); |
|
116 |
} |
|
117 |
else again = false; |
|
118 |
} |
|
119 |
is.close(); |
|
120 |
out.close(); |
|
121 |
} |
|
122 |
catch(IOException e) { |
|
123 |
System.err.println("Error Writing/Reading Streams."); |
|
124 |
} |
|
125 |
} |
|
109 | 126 |
|
127 |
|
|
110 | 128 |
} |
111 | 129 |
|
branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/execution/InstallerExecutionServiceTest.java | ||
---|---|---|
30 | 30 |
import java.io.File; |
31 | 31 |
import java.io.FileInputStream; |
32 | 32 |
import java.io.FileNotFoundException; |
33 |
import java.io.IOException; |
|
33 | 34 |
|
34 | 35 |
import junit.framework.Assert; |
35 | 36 |
|
37 |
import org.gvsig.installer.lib.api.InstallerInfo; |
|
36 | 38 |
import org.gvsig.installer.lib.api.InstallerLocator; |
39 |
import org.gvsig.installer.lib.api.creation.InstallerCreationService; |
|
40 |
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException; |
|
37 | 41 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionService; |
38 | 42 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException; |
43 |
import org.gvsig.installer.lib.impl.InstallerServiceTest; |
|
39 | 44 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
45 |
import org.gvsig.tools.locator.LocatorException; |
|
40 | 46 |
|
41 | 47 |
/** |
42 | 48 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a> |
43 | 49 |
*/ |
44 |
public class InstallerExecutionServiceTest extends AbstractLibraryAutoInitTestCase{
|
|
50 |
public class InstallerExecutionServiceTest extends InstallerServiceTest{
|
|
45 | 51 |
|
46 | 52 |
@Override |
47 | 53 |
protected void doSetUp() throws Exception { |
... | ... | |
49 | 55 |
|
50 | 56 |
} |
51 | 57 |
|
52 |
public void testReadFiles() throws InstallerExecutionServiceException, FileNotFoundException{ |
|
53 |
File file = new File(getClass().getClassLoader().getResource("installation.zip").getFile()); |
|
54 |
File installationFile = new File(getClass().getClassLoader().getResource("application").getFile()); |
|
55 |
|
|
58 |
public void testReadPlugins() throws LocatorException, InstallerCreationServiceException, InstallerExecutionServiceException, IOException{ |
|
59 |
File applicationDirectory = super.getApplicationDirectory(); |
|
60 |
|
|
56 | 61 |
InstallerExecutionService installerExecutionService = |
57 |
InstallerLocator.getInstallerManager().getInstallerExecutionService(); |
|
58 |
installerExecutionService.addInstaller(new FileInputStream(file)); |
|
59 |
installerExecutionService.setApplicationDirectory(installationFile); |
|
62 |
InstallerLocator.getInstallerManager().getInstallerExecutionService(); |
|
63 |
installerExecutionService.setApplicationDirectory(applicationDirectory); |
|
64 |
installerExecutionService.addInstallersFromInstallDirectory(); |
|
65 |
|
|
66 |
Assert.assertEquals(3, installerExecutionService.getPluginsSize()); |
|
67 |
|
|
68 |
for (int i=0 ; i<3 ; i++){ |
|
69 |
InstallerInfo installerInfo = installerExecutionService.getPluginInfoAt(i); |
|
70 |
if (installerInfo.getCode().equals("org.gvsig.plugin1")){ |
|
71 |
assertNotNull(installerInfo.getState()); |
|
72 |
}else if (installerInfo.getCode().equals("org.gvsig.plugin2")){ |
|
73 |
assertEquals("RC1", installerInfo.getState()); |
|
74 |
}else if (installerInfo.getCode().equals("org.gvsig.plugin3")){ |
|
75 |
assertEquals("RC1", installerInfo.getState()); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
60 | 79 |
|
80 |
public void testInstallPluginFromApplication() throws InstallerExecutionServiceException, IOException, LocatorException, InstallerCreationServiceException{ |
|
81 |
File applicationDirectory = super.getApplicationDirectory(); |
|
61 | 82 |
|
83 |
InstallerExecutionService installerExecutionService = |
|
84 |
InstallerLocator.getInstallerManager().getInstallerExecutionService(); |
|
85 |
installerExecutionService.setApplicationDirectory(applicationDirectory); |
|
86 |
installerExecutionService.addInstallersFromInstallDirectory(); |
|
87 |
installerExecutionService.setPluginToInstall("org.gvsig.plugin1"); |
|
88 |
installerExecutionService.executeInstaller(); |
|
62 | 89 |
|
90 |
Assert.assertEquals(3, installerExecutionService.getPluginsSize()); |
|
63 | 91 |
|
92 |
//Reading the excution and check that the file is installed |
|
93 |
InstallerCreationService installerCreationService = |
|
94 |
InstallerLocator.getInstallerManager().getInstallerCreationService(); |
|
95 |
installerCreationService.setApplicationDirectory(applicationDirectory); |
|
96 |
Assert.assertEquals(4, installerCreationService.getPluginsSize()); |
|
64 | 97 |
} |
98 |
|
|
99 |
public void testInstallPluginFromExternalWithOutStreamException() throws InstallerExecutionServiceException, IOException, LocatorException, InstallerCreationServiceException{ |
|
100 |
File applicationDirectory = super.getApplicationDirectory(); |
|
101 |
|
|
102 |
InstallerExecutionService installerExecutionService = |
|
103 |
InstallerLocator.getInstallerManager().getInstallerExecutionService(); |
|
104 |
installerExecutionService.setApplicationDirectory(applicationDirectory); |
|
105 |
installerExecutionService.setPluginToInstall("org.gvsig.plugin1"); |
|
106 |
|
|
107 |
Exception exc = null; |
|
108 |
try{ |
|
109 |
installerExecutionService.executeInstaller(); |
|
110 |
}catch(InstallerExecutionServiceException e){ |
|
111 |
exc = e; |
|
112 |
} |
|
113 |
assertNotNull(exc); |
|
114 |
|
|
115 |
} |
|
116 |
|
|
117 |
public void testInstallPluginFromExternal() throws InstallerExecutionServiceException, IOException, LocatorException, InstallerCreationServiceException{ |
|
118 |
File applicationDirectory = super.getApplicationDirectory(); |
|
119 |
File externalInstallationFile = new File(applicationDirectory.getAbsoluteFile() + File.separator + "install" + File.separator + "installer1.zip"); |
|
120 |
|
|
121 |
InstallerExecutionService installerExecutionService = |
|
122 |
InstallerLocator.getInstallerManager().getInstallerExecutionService(); |
|
123 |
installerExecutionService.setApplicationDirectory(applicationDirectory); |
|
124 |
installerExecutionService.addInstaller(new FileInputStream(externalInstallationFile)); |
|
125 |
installerExecutionService.setPluginToInstall("org.gvsig.plugin1"); |
|
126 |
|
|
127 |
Assert.assertEquals(3, installerExecutionService.getPluginsSize()); |
|
128 |
|
|
129 |
installerExecutionService.executeInstaller(new FileInputStream(externalInstallationFile)); |
|
130 |
|
|
131 |
//Reading the excution and check that the file is installed |
|
132 |
InstallerCreationService installerCreationService = |
|
133 |
InstallerLocator.getInstallerManager().getInstallerCreationService(); |
|
134 |
installerCreationService.setApplicationDirectory(applicationDirectory); |
|
135 |
Assert.assertEquals(4, installerCreationService.getPluginsSize()); |
|
136 |
|
|
137 |
} |
|
65 | 138 |
|
66 | 139 |
} |
67 | 140 |
|
branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/creation/InstallerCreationServiceTest.java | ||
---|---|---|
67 | 67 |
|
68 | 68 |
Assert.assertEquals(3, installerCreationService.getPluginsSize()); |
69 | 69 |
|
70 |
int pluginsNumber = 0; |
|
70 | 71 |
for (int i=0 ; i<3 ; i++){ |
71 | 72 |
InstallerInfo installerInfo = installerCreationService.getPluginInfoAt(i); |
72 | 73 |
if (installerInfo.getCode().equals("org.gvsig.wms")){ |
73 | 74 |
assertNotNull(installerInfo.getState()); |
75 |
pluginsNumber++; |
|
74 | 76 |
}else if (installerInfo.getCode().equals("org.gvsig.wfs")){ |
75 | 77 |
assertEquals("RC2", installerInfo.getState()); |
78 |
pluginsNumber++; |
|
76 | 79 |
}else if (installerInfo.getCode().equals("org.gvsig.wcs")){ |
77 | 80 |
assertEquals("RC1", installerInfo.getState()); |
81 |
pluginsNumber++; |
|
78 | 82 |
} |
79 | 83 |
} |
84 |
|
|
85 |
Assert.assertEquals(3, pluginsNumber); |
|
80 | 86 |
} |
81 | 87 |
|
82 | 88 |
public void testCreateInstallerWithOutSelectAPluginException() throws IOException, LocatorException, InstallerCreationServiceException{ |
branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.api/src/main/java/org/gvsig/installer/lib/api/execution/InstallerExecutionService.java | ||
---|---|---|
75 | 75 |
* @throws InstallerExecutionServiceException |
76 | 76 |
*/ |
77 | 77 |
public void executeInstaller() throws InstallerExecutionServiceException; |
78 |
|
|
79 |
public void executeInstaller(InputStream is) throws InstallerExecutionServiceException; |
|
78 | 80 |
|
79 | 81 |
public void setPluginToInstall(int index); |
80 | 82 |
|
Also available in: Unified diff