Revision 33175

View differences:

tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<artifactId>org.gvsig.installer.lib.spi</artifactId>
5
	<packaging>jar</packaging>
6
	<name>org.gvsig.installer.lib.spi</name>
7
	<parent>
8
		<groupId>org.gvsig</groupId>
9
		<artifactId>org.gvsig.installer.lib</artifactId>
10
		<version>1.0.0-SNAPSHOT</version>
11
	</parent>
12
	<dependencies>
13
		<dependency>
14
			<groupId>org.gvsig</groupId>
15
			<artifactId>org.gvsig.installer.lib.api</artifactId>
16
			<version>1.0.0-SNAPSHOT</version>
17
		</dependency>
18
		<dependency>
19
			<groupId>org.gvsig</groupId>
20
			<artifactId>org.gvsig.tools.lib</artifactId>
21
		</dependency>
22
	</dependencies>
23
</project>
0 24

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/InstallerProviderLocator.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.spi;
29

  
30
import org.gvsig.tools.locator.BaseLocator;
31
import org.gvsig.tools.locator.Locator;
32
import org.gvsig.tools.locator.LocatorException;
33

  
34
/**
35
 * This Locator provides the entry point for the gvSIG {@link InstallerProviderManager}
36
 * 
37
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
38
 */
39
public class InstallerProviderLocator extends BaseLocator{
40
	private static final String LOCATOR_NAME = "Installer.provider.locator";
41
	public static final String PROVIDER_MANAGER_NAME = "Installer.provider.manager";
42
	public static final String PROVIDER_MANAGER_DESCRIPTION = "Installer Manager";
43

  
44
	/**
45
	 * Unique instance.
46
	 */
47
	private static final InstallerProviderLocator instance = new InstallerProviderLocator();
48

  
49
	/**
50
	 * Return the singleton instance.
51
	 *
52
	 * @return the singleton instance
53
	 */
54
	public static InstallerProviderLocator getInstance() {
55
		return instance;
56
	}
57

  
58
	/**
59
	 * Return the Locator's name
60
	 *
61
	 * @return a String with the Locator's name
62
	 */
63
	public String getLocatorName() {
64
		return LOCATOR_NAME;
65
	}
66

  
67
	/**
68
	 * Return a reference to InstallerProviderManager.
69
	 *
70
	 * @return a reference to InstallerProviderManager
71
	 * @throws LocatorException
72
	 *             if there is no access to the class or the class cannot be
73
	 *             instantiated
74
	 * @see Locator#get(String)
75
	 */
76
	public static InstallerProviderManager getProviderManager() throws LocatorException {
77
		return (InstallerProviderManager) getInstance().get(PROVIDER_MANAGER_NAME);
78
	}
79

  
80
	/**
81
	 * Registers the Class implementing the PersistenceManager interface.
82
	 *
83
	 * @param clazz
84
	 *            implementing the PersistenceManager interface
85
	 */
86
	@SuppressWarnings(value = "unchecked")
87
	public static void registerInstallerProviderManager(Class clazz) {
88
		getInstance().register(PROVIDER_MANAGER_NAME, PROVIDER_MANAGER_DESCRIPTION, clazz);
89
	}
90
	
91
}
0 92

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/InstallPackageProviderServices.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.spi;
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.tools.service.spi.ProviderServices;
40

  
41
/**
42
 * Services that can be used by the providers to create or exceute the bundle. It
43
 * contains methods to compress and to decompress files and methods to 
44
 * read an install a package file from a directory.
45
 * 
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public interface InstallPackageProviderServices extends ProviderServices{
49

  
50
	/**
51
	 * It decompress an input stream and write it on a directory.
52
	 * @param is
53
	 * The input stream to decompress
54
	 * @param outputDirectory.
55
	 * The output directory.
56
	 * @throws InstallPackageServiceException
57
	 * If there is a problem decompressing the stream.
58
	 */
59
	public void decompress(InputStream is, File outputDirectory) throws InstallPackageServiceException;
60
	
61
	/**
62
	 * Compress a directory like an zipped outputstream with a concrete name.
63
	 * @param directory
64
	 * the directory to compress.
65
	 * @param fileName
66
	 * name of the zip entry that has the output file.
67
	 * The name that have to have 
68
	 * @param os
69
	 * output stream to write the output.
70
	 * @throws MakePluginPackageServiceException
71
	 * if there is any problem compressing. 
72
	 */
73
	public void compress(File directory, String fileName, OutputStream os) throws MakePluginPackageServiceException;
74
	
75
	/**
76
	 * Reads the package.info file from a directory a fills the the properties
77
	 * of the {@link PackageInfo} object.
78
	 * @param directory
79
	 * the root directory that contains the installinfo file
80
	 * @param installInfo
81
	 * the installinfo file that has to be filled.
82
	 * @throws InstallerInfoFileException.
83
	 * if there is any problem reading the file
84
	 */
85
	public void readPackageInfo(File directory, PackageInfo installerInfo) throws InstallerInfoFileException;
86
	
87
	/**
88
	 * Reads the package.info file from a bundle fills the the properties
89
	 * of the {@link PackageInfo} objects.
90
	 * @param is
91
	 * the input stream of a bundle.
92
	 * @param packageInfos
93
	 * a list of the information of the packages to install.
94
	 * @param zipEntriesMap
95
	 * a map to retrieve the zipEntry for every package. This information is necessary to
96
	 * select the plugin to decompress.
97
	 * @throws InstallPackageServiceException
98
	 * if there is a problem reading the bundle.
99
	 */
100
	public void readPackageInfo(InputStream is, List<PackageInfo> packageInfos, Map<PackageInfo, String> zipEntriesMap) throws InstallPackageServiceException;
101
	
102
	/**
103
	 * Writes the package.info file in a concrete directory.
104
	 * @param directory
105
	 * the directory.
106
	 * @param packageInfo
107
	 * the information to write.
108
	 * @throws InstallerInfoFileException
109
	 * if there is any problem writing the package.info file. 
110
	 */
111
	public void writePackageInfo(File directory, PackageInfo packageInfo) throws InstallerInfoFileException;
112
	
113
	/**
114
	 * It search a package inside an installer file by the zip name and returns
115
	 * the stream in this position ready to decompres.
116
	 * @param is
117
	 * the input stream of a bundle.
118
	 * @param zipEntry
119
	 * the name of the zip entry.
120
	 * @return
121
	 * the input stream ready to install.
122
	 * @throws InstallPackageServiceException
123
	 * if there is a problem reading the stream 
124
	 */
125
	public InputStream searchPackage(InputStream is, String zipEntry) throws InstallPackageServiceException; 
126
	
127
	/**
128
	 * It reads a compressed file and retrieve the package information.
129
	 * @param is
130
	 * the compressed file
131
	 * @return
132
	 * the information of the package
133
	 * @throws InstallPackageServiceException
134
	 * if there is a problem decompressing the file.
135
	 */
136
	public PackageInfo readCompressedPackageInfo(InputStream is) throws InstallPackageServiceException; 
137
}
138

  
0 139

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/InstallerProviderManager.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.spi;
29

  
30
import org.gvsig.installer.lib.api.PackageInfo;
31
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider;
32
import org.gvsig.tools.service.ServiceException;
33
import org.gvsig.tools.service.spi.ProviderManager;
34

  
35
/**
36
 * <p>
37
 * The installation process install packages in gvSIG. These packages has a type, 
38
 * that can be a plugin, theme, translation, etc. For every type of package
39
 * the installation process needs a provider for installing the
40
 * packages. 
41
 * </p>
42
 * <p>
43
 * All the packages to install have to have some install properties defined
44
 * by the {@link PackageInfo} class. This class is just a set of properties 
45
 * and there is a property named <b>type</b> that can be retrieved by the
46
 * {@link PackageInfo#getType()} method that defines the package type.
47
 * This property is used to create a {@link InstallPackageProvider}
48
 * that is used to install the selected package.
49
 * </p>
50
 * <p>
51
 * This manager provides the functionality to register and to create 
52
 * a providers for every package.
53
 * </p>
54
 * 
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public interface InstallerProviderManager extends ProviderManager {
58
	
59
	/**
60
	 * Creates a new provider to execute an installer to add a new package in gvSIG. 
61
	 * @param providerName
62
	 * the provider name used on the registration of the provider. This name is the
63
	 * type attribute defined by {@link PackageInfo}.
64
	 * @return
65
	 * a provider that can be used to install a package.
66
	 * @throws ServiceException
67
	 * if the provider doesn't exist or if there is a problem creating the provider.
68
	 */
69
	public InstallPackageProvider createExecutionProvider(String providerName) throws ServiceException;
70
	
71
	/**
72
	 * Creates the services that be used for the providers to execute or
73
	 * create a new bundle.
74
	 * @return
75
	 * the services used to create or execute an bundle.
76
	 */
77
	public InstallPackageProviderServices createInstallerProviderServices();
78
	
79
}
80

  
0 81

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/execution/InstallPackageProvider.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.spi.execution;
29

  
30
import java.io.File;
31
import java.io.InputStream;
32

  
33
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
34

  
35

  
36
/**
37
 * <p>
38
 * Provider that manage the installation process of a concrete package. There
39
 * is a different provider for each package type that is supported. The provider
40
 * just have a method to install the package in gvSIG
41
 * </p>
42
 * 
43
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
44
 */
45
public interface InstallPackageProvider {
46

  
47
	/**
48
	 * This method install a package in a valid gvSIG directory.
49
	 * @param applicationDirectory
50
	 * the directory where gvSIG is located.
51
	 * @param inputStream
52
	 * the stream that contains the package information.
53
	 * @throws InstallPackageServiceException
54
	 * if there is a problem reading the stream or installing the package.
55
	 */
56
	public void install(File applicationDirectory, InputStream inputStream) throws InstallPackageServiceException;
57

  
58
}
59

  
0 60

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.spi/src/main/java/org/gvsig/installer/lib/spi/InstallerInfoFileException.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.spi;
29

  
30
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
31
import org.gvsig.tools.exception.BaseException;
32

  
33
/**
34
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
35
 */
36
public class InstallerInfoFileException extends MakePluginPackageServiceException {
37
	private static final long serialVersionUID = -5388898489108649788L;
38

  
39
	private static final String KEY = "install_infofile_exception";
40
	
41
	/**
42
     * @see BaseException#BaseException(String, String, long)
43
     */
44
    public InstallerInfoFileException(String message) {
45
    	 super(message, KEY, serialVersionUID);
46
    }
47
    
48
    /**
49
     * @see BaseException#BaseException(String, Throwable)
50
     */
51
    public InstallerInfoFileException(String message, Throwable cause) {
52
    	 super(message, cause,  KEY, serialVersionUID);
53
    }
54
    
55
    /**
56
     * @see BaseException#BaseException(String, String, long)
57
     */
58
    public InstallerInfoFileException(String message, String key, long code) {
59
   	    super(message, key, code);
60
   }
61
    
62
}
63

  
0 64

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/InstallerServiceTest.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.BufferedOutputStream;
31
import java.io.DataOutputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.OutputStream;
38

  
39
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
40

  
41
/**
42
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
43
 */
44
public abstract class InstallerServiceTest extends AbstractLibraryAutoInitTestCase{
45

  
46
	public File getApplicationDirectory() throws IOException{
47
		File templateFile = new File(getClass().getClassLoader().getResource("application").getFile());
48
		File applicationDirectory = new File(System.getProperty("java.io.tmpdir") +  File.separator + "tmp_gvsig_installer");		
49

  
50
		if (applicationDirectory.exists()){
51
			deleteDir(applicationDirectory);
52
		}
53
		copy(templateFile, applicationDirectory);
54
		
55
		return applicationDirectory;
56
	}
57
	
58
	public File getTemporalFile(){
59
		return new File(System.getProperty("java.io.tmpdir") + File.separator + "installer" + Math.random());
60
	}
61

  
62
	public File getInstallersDirectory() throws IOException
63
	{
64
		File applicationDirectory = getApplicationDirectory();
65
		return new File(applicationDirectory + File.separator + "install");	
66
	}
67
	
68
	public File getPluginsDirectory() throws IOException{
69
		File applicationDirectory = getApplicationDirectory();
70
		return new File(applicationDirectory + File.separator + "gvSIG" + File.separator + "extensiones");
71
	}
72

  
73
	public boolean deleteDir(File dir) { 
74
		if (dir.isDirectory()) {
75
			String[] children = dir.list();
76
			for (int i=0; i<children.length; i++) {
77
				boolean success = deleteDir(new File(dir, children[i]));
78
				if (!success) { 
79
					return false; 
80
				} 
81
			}
82
		}
83
		return dir.delete();		 
84
	}
85

  
86
	public void copy(File sourceLocation , File targetLocation) throws IOException { 
87
		if (sourceLocation.isDirectory()) {
88
			if (!targetLocation.exists()) {
89
				targetLocation.mkdir();
90
			}
91

  
92
			String[] children = sourceLocation.list();
93
			for (int i=0; i<children.length; i++) {
94
				copy(new File(sourceLocation, children[i]),
95
						new File(targetLocation, children[i]));
96
			}
97
		} else {
98
			targetLocation.getParentFile().mkdirs();
99

  
100
			InputStream in = new FileInputStream(sourceLocation);
101
			OutputStream out = new FileOutputStream(targetLocation);
102

  
103
			// Copy the bits from instream to outstream
104
			byte[] buf = new byte[1024];
105
			int len;
106
			while ((len = in.read(buf)) > 0) {
107
				out.write(buf, 0, len);
108
			}
109
			in.close();
110
			out.close();
111
		}		 
112
	}
113
	
114
	public void writeToFile(InputStream is, File file) {
115
		try {
116
			DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
117
			InputStream is2 = is;
118
			boolean again = true;
119
			while(again) {
120
				if(is2.read() > -1) {
121
					out.writeByte(is.read());
122
				}
123
				else again = false;
124
			}
125
			is.close();
126
			out.close();
127
		}
128
		catch(IOException e) {
129
			System.err.println("Error Writing/Reading Streams.");
130
		}
131
	}
132
	
133
	
134
}
135

  
0 136

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/execution/InstallPackageServiceTest.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.File;
31
import java.io.IOException;
32

  
33
import junit.framework.Assert;
34

  
35
import org.gvsig.installer.lib.api.InstallerLocator;
36
import org.gvsig.installer.lib.api.PackageInfo;
37
import org.gvsig.installer.lib.api.creation.MakePluginPackageService;
38
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
39
import org.gvsig.installer.lib.api.execution.InstallPackageService;
40
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
41
import org.gvsig.installer.lib.impl.InstallerServiceTest;
42
import org.gvsig.tools.locator.LocatorException;
43

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

  
49
	@Override
50
	protected void doSetUp() throws Exception {
51
		// TODO Auto-generated method stub
52
		
53
	}
54
	
55
	public void testReadPlugins() throws LocatorException, MakePluginPackageServiceException, InstallPackageServiceException, IOException{
56
		File installersDirectory = super.getInstallersDirectory();		
57
				
58
		InstallPackageService installPackageService = 
59
			InstallerLocator.getInstallerManager().getInstallPackageService();		
60
		installPackageService.addBundlesFromDirectory(installersDirectory);
61
				
62
		Assert.assertEquals(3, installPackageService.getPackageCount());
63
		
64
		int pluginsNumber = 0;
65
		for (int i=0 ; i<3 ; i++){
66
			PackageInfo packageInfo = installPackageService.getPackageInfo(i);
67
			if (packageInfo.getCode().equals("org.gvsig.plugin1")){
68
				assertNotNull(packageInfo.getState());
69
				pluginsNumber++;
70
			}else if (packageInfo.getCode().equals("org.gvsig.plugin2")){
71
				assertEquals("RC1", packageInfo.getState());
72
				pluginsNumber++;
73
			}else if (packageInfo.getCode().equals("org.gvsig.plugin3")){
74
				assertEquals("Testing", packageInfo.getState());
75
				pluginsNumber++;
76
			}
77
		}
78
		
79
		Assert.assertEquals(3, pluginsNumber);
80
	}
81
	
82
	public void testInstallPluginFromApplication() throws InstallPackageServiceException, IOException, LocatorException, MakePluginPackageServiceException{
83
		File installersDirectory = super.getInstallersDirectory();		
84
		File pluginsDirectory = super.getPluginsDirectory();
85
		File applicationDirectory = super.getApplicationDirectory();
86
		
87
		InstallPackageService installerExecutionService = 
88
			InstallerLocator.getInstallerManager().getInstallPackageService();		
89
		installerExecutionService.addBundlesFromDirectory(installersDirectory);
90
		
91
		PackageInfo packageInfo = installerExecutionService.getPackageInfo("org.gvsig.plugin1");
92
		installerExecutionService.installPackage(applicationDirectory, packageInfo);
93
		
94
		Assert.assertEquals(3, installerExecutionService.getPackageCount());
95
		
96
		//Reading the excution and check that the file is installed
97
		MakePluginPackageService installerCreationService = 
98
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);	
99
		Assert.assertEquals(4, installerCreationService.getPluginPackageCount());
100
	}	
101
	
102
	
103
	public void testInstallPluginFromExternal() throws InstallPackageServiceException, IOException, LocatorException, MakePluginPackageServiceException{
104
		File applicationDirectory = super.getApplicationDirectory();
105
		File externalInstallationFile = new File(applicationDirectory.getAbsoluteFile() + File.separator + "install" + File.separator + "bundle1.zip");
106
		File pluginsDirectory = super.getPluginsDirectory();
107
		
108
		InstallPackageService installerExecutionService = 
109
			InstallerLocator.getInstallerManager().getInstallPackageService();		
110
		installerExecutionService.addBundle(externalInstallationFile.toURI());
111
		
112
		PackageInfo packageInfo = installerExecutionService.getPackageInfo("org.gvsig.plugin1");
113
		
114
		Assert.assertEquals(3, installerExecutionService.getPackageCount());
115
		
116
		installerExecutionService.installPackage(applicationDirectory, packageInfo);
117
		
118
		//Reading the excution and check that the file is installed
119
		MakePluginPackageService installerCreationService = 
120
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
121
		Assert.assertEquals(4, installerCreationService.getPluginPackageCount());	
122
	}
123
	
124
//	public void testInstallPluginWithAntScript() throws InstallPackageServiceException, LocatorException, MakePluginPackageServiceException, IOException{
125
//		File applicationDirectory = super.getApplicationDirectory();
126
//		File installersDirectory = super.getInstallersDirectory();		
127
//		File pluginsDirectory = super.getPluginsDirectory();
128
//		
129
//		InstallPackageService installerExecutionService = 
130
//			InstallerLocator.getInstallerManager().getInstallPackageService();		
131
//		installerExecutionService.addBundlesFromDirectory(installersDirectory);
132
//
133
//		PackageInfo packageInfo = installerExecutionService.getPackageInfo("org.gvsig.plugin2");				
134
//		
135
//		Assert.assertEquals(3, installerExecutionService.getPackageCount());
136
//		
137
//		installerExecutionService.installPackage(applicationDirectory, packageInfo);		
138
//		
139
//		//Reading the excution and check that the file is installed
140
//		MakePluginPackageService installerCreationService = 
141
//			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
142
//		
143
//		Assert.assertEquals(5, installerCreationService.getPluginPackageCount());	
144
//	}
145

  
146
}
147

  
0 148

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/creation/MakePluginPackageServiceTest.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.File;
31
import java.io.FileOutputStream;
32
import java.io.IOException;
33

  
34
import junit.framework.Assert;
35

  
36
import org.gvsig.installer.lib.api.InstallerLocator;
37
import org.gvsig.installer.lib.api.PackageInfo;
38
import org.gvsig.installer.lib.api.creation.MakePluginPackageService;
39
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
40
import org.gvsig.installer.lib.api.execution.InstallPackageService;
41
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
42
import org.gvsig.installer.lib.impl.InstallerServiceTest;
43
import org.gvsig.tools.locator.LocatorException;
44

  
45

  
46
/**
47
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
48
 */
49
public class MakePluginPackageServiceTest extends InstallerServiceTest{
50
	
51
	@Override
52
	protected void doSetUp() throws Exception {
53
		
54
	}	
55

  
56
	public void testReadPlugins() throws LocatorException, MakePluginPackageServiceException, InstallPackageServiceException, IOException{
57
		File pluginsDirectory = super.getPluginsDirectory();		
58
				
59
		MakePluginPackageService makePluginPackageService = 
60
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
61
			
62
		Assert.assertEquals(3, makePluginPackageService.getPluginPackageCount());
63
		
64
		int pluginsNumber = 0;
65
		for (int i=0 ; i<3 ; i++){
66
			PackageInfo packageInfo = makePluginPackageService.getPluginPackageInfo(i);
67
			if (packageInfo.getCode().equals("org.gvsig.wms")){
68
				assertNotNull(packageInfo.getState());
69
				pluginsNumber++;
70
			}else if (packageInfo.getCode().equals("org.gvsig.wfs")){
71
				assertEquals("RC2", packageInfo.getState());
72
				pluginsNumber++;
73
			}else if (packageInfo.getCode().equals("org.gvsig.wcs")){
74
				assertEquals("RC1", packageInfo.getState());
75
				pluginsNumber++;
76
			}
77
		}
78
		
79
		Assert.assertEquals(3, pluginsNumber);
80
	}
81

  
82
	public void testMakePluginPackage() throws IOException, LocatorException, MakePluginPackageServiceException, InstallPackageServiceException{
83
		File pluginsDirectory = super.getPluginsDirectory();		
84
		File installerFile = super.getTemporalFile();
85
		
86
		
87
		MakePluginPackageService makePluginPackageService = 
88
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
89
				
90
		PackageInfo packageInfo = makePluginPackageService.getPluginPackageInfo("org.gvsig.wfs");
91
		
92
		assertEquals("1.0.0", packageInfo.getVersion());
93
		
94
		//Change the version to check that the installation process works fine
95
		packageInfo.setVersion("1.0.1");
96

  
97
		//Compress the plugin and create the installer compressed file
98
		FileOutputStream os = new FileOutputStream(installerFile);		
99
		makePluginPackageService.createPluginPackage(packageInfo, os);
100
		
101
		//decompress the plugin and read the plugin information		
102
		InstallPackageService installPackageService = 
103
		InstallerLocator.getInstallerManager().getInstallPackageService();
104
		
105
		installPackageService.addBundle(installerFile.toURI());
106
		assertEquals(1, installPackageService.getPackageCount());
107
		PackageInfo newInstallerInfo = makePluginPackageService.getPluginPackageInfo(0);
108
		assertEquals("1.0.1", newInstallerInfo.getVersion());
109
	}	
110
	
111
	public void testCreateInstallerWithAntFile() throws IOException, LocatorException, MakePluginPackageServiceException, InstallPackageServiceException{
112
		File pluginsDirectory = super.getPluginsDirectory();		
113
		File installerFile = super.getTemporalFile();
114
		
115
		MakePluginPackageService makePluginPackageService = 
116
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
117
		
118
		PackageInfo packageInfo = makePluginPackageService.getPluginPackageInfo("org.gvsig.wfs");
119
		packageInfo.setAnScript(makePluginPackageService.getDefaultAntScript());	
120
		
121
		//Compress the plugin and create the installer compressed file
122
		FileOutputStream os = new FileOutputStream(installerFile);		
123
		makePluginPackageService.createPluginPackage(packageInfo, os);
124
		
125
		//decompress the plugin and read the plugin information	
126
		InstallPackageService installPackageService = 
127
			InstallerLocator.getInstallerManager().getInstallPackageService();
128
		installPackageService.addBundle(installerFile.toURI());
129
		assertEquals(1, installPackageService.getPackageCount());		
130
		PackageInfo newInstallerInfo = makePluginPackageService.getPluginPackageInfo(0);
131
		assertTrue(newInstallerInfo.getAntScript().startsWith("<project"));		
132
	}	
133
	
134
	public void testCreateInstallerWithFiles() throws IOException, LocatorException, MakePluginPackageServiceException, InstallPackageServiceException{
135
		File pluginsDirectory = super.getPluginsDirectory();		
136
		File installerFile = super.getTemporalFile();	
137
		File resource = new File (pluginsDirectory + File.separator + "org.gvsig.wms" + File.separator + "resources2.txt");
138
		
139
		MakePluginPackageService makePluginPackageService = 
140
			InstallerLocator.getInstallerManager().getMakePluginPackageService(pluginsDirectory);
141
			
142
		PackageInfo packageInfo = makePluginPackageService.getPluginPackageInfo("org.gvsig.wcs");
143
		packageInfo.addFileToCopy(resource);
144
		
145
		//Compress the plugin and create the installer compressed file
146
		FileOutputStream os = new FileOutputStream(installerFile);		
147
		makePluginPackageService.createPluginPackage(packageInfo, os);
148
		
149
		//decompress the plugin and read the plugin information	
150
		InstallPackageService installPackageService = 
151
			InstallerLocator.getInstallerManager().getInstallPackageService();
152
		installPackageService.addBundle(installerFile.toURI());
153
		assertEquals(1, installPackageService.getPackageCount());		
154
		PackageInfo newInstallerInfo = makePluginPackageService.getPluginPackageInfo(0);
155
		assertEquals(1, newInstallerInfo.getFileToCopySize());		
156
	}		
157
}
158
	
0 159

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/info/InstallerInfoFileWriterTest.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.info;
29

  
30
import java.io.ByteArrayInputStream;
31
import java.io.ByteArrayOutputStream;
32

  
33
import org.gvsig.installer.lib.api.PackageInfo;
34
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
35
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
36
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
37
import org.gvsig.tools.locator.LocatorException;
38

  
39
/**
40
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
41
 */
42
public class InstallerInfoFileWriterTest extends AbstractLibraryAutoInitTestCase{
43
	
44
	@Override
45
	protected void doSetUp() throws Exception {
46
		// TODO Auto-generated method stub
47
		
48
	}	
49

  
50
	public void testWriteFile() throws LocatorException, InstallerInfoFileException {
51
		DefaultPackageInfo installerInfo = new DefaultPackageInfo();
52
			
53
		installerInfo.setCode("org.gvsig.myplugin");
54
		installerInfo.setName("My name");
55
		installerInfo.setDescription("My description");
56
		installerInfo.setVersion("1.0.0");
57
		installerInfo.setBuild(4);
58
		installerInfo.setState("final");
59
		installerInfo.setOfficial(false);
60
		installerInfo.setType("plugin");
61
		
62
		ByteArrayOutputStream out = new ByteArrayOutputStream();
63
		
64
		InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter();
65
		installerInfoFileWriter.write(installerInfo, out);
66

  
67
		PackageInfo installerInfo2 = new DefaultPackageInfo();
68
		
69
		InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
70
		installerInfoFileReader.read(installerInfo2, new ByteArrayInputStream(out.toByteArray()));		
71
				
72
		assertEquals("org.gvsig.myplugin", installerInfo2.getCode());
73
		assertEquals("My name", installerInfo2.getName());
74
		assertEquals("My description", installerInfo2.getDescription());
75
		assertEquals("1.0.0", installerInfo2.getVersion());
76
		assertEquals(4, installerInfo2.getBuild());
77
		assertEquals("final", installerInfo2.getState());
78
		assertEquals(false, installerInfo2.isOfficial());
79
	}
80
}
81

  
0 82

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/java/org/gvsig/installer/lib/impl/info/InstallerInfoFileReaderTest.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.info;
29

  
30
import java.io.FileInputStream;
31
import java.io.FileNotFoundException;
32

  
33
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
34
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
35
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
36
import org.gvsig.tools.locator.LocatorException;
37

  
38

  
39

  
40
/**
41
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
42
 */
43
public class InstallerInfoFileReaderTest extends AbstractLibraryAutoInitTestCase{
44
	
45

  
46
	@Override
47
	protected void doSetUp() throws Exception {
48
		// TODO Auto-generated method stub
49
		
50
	}
51
	
52
	public void testReadFile() throws LocatorException, InstallerInfoFileException, FileNotFoundException {
53
		FileInputStream fis = new FileInputStream(getClass().getClassLoader().getResource("package.info1").getFile());
54
		
55
		DefaultPackageInfo installerInfo = new DefaultPackageInfo();
56
		
57
		InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
58
		installerInfoFileReader.read(installerInfo, fis);		
59
		
60
		assertEquals(installerInfo.getCode(), "org.gvsig.myplugin");
61
		assertEquals(installerInfo.getName(), "myplugin");
62
		assertEquals(installerInfo.getDescription(), "Test");
63
		assertEquals(installerInfo.getVersion(), "1.0.0");
64
		assertEquals(installerInfo.getBuild(), 1);
65
		assertEquals(installerInfo.getState(), "RC1");
66
		assertEquals(installerInfo.isOfficial(), true);
67
	}
68

  
69
	
70
}
71

  
0 72

  
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/application/gvSIG/extensiones/org.gvsig.wcs/package.info
1
code=org.gvsig.wcs
2
name=myplugin
3
description=Test
4
version=1.0.0
5
build=1
6
state=RC1
7
oficial=true
8
type=plugin
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/application/gvSIG/extensiones/org.gvsig.wfs/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_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/application/gvSIG/extensiones/org.gvsig.wfs/package.info
1
code=org.gvsig.wfs
2
name=myplugin
3
description=Test
4
version=1.0.0
5
build=1
6
state=RC2
7
oficial=true
8
type=plugin
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/package.info1
1
code=org.gvsig.myplugin
2
name=myplugin
3
description=Test
4
version=1.0.0
5
build=1
6
state=RC1
7
oficial=true
8
type=plugin
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/bundle1/org.gvsig.plugin1/package.info
1
code=org.gvsig.plugin1
2
name=myplugin
3
description=Test
4
version=1.0.0
5
build=1
6
state=RC2
7
oficial=true
8
type=plugin
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/bundle1/org.gvsig.plugin2/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_2010/extensions/org.gvsig.installer/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/test/resources/bundle1/org.gvsig.plugin2/package.info
1
code=org.gvsig.plugin2
2
name=myplugin
3
description=Test
4
version=1.0.0
5
build=1
6
state=RC1
7
oficial=true
8
type=plugin
tags/v2_0_0_Build_2010/extensions/org.gvsig.installer/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.PackageInfo;
44
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
45
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
46
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
47
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

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

  
59
	private int option = 0;
60

  
61
	private int BUFFER = 2048;
62

  
63
	private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
64

  
65
	private List<PackageInfo> readedIinstallerInfos = null;	
66
	private File outputDirectory = null;	
67
	private List<PackageInfo> selectedInstallerInfos = null;
68
	private Map<PackageInfo, String> zipEntriesMap = null;
69

  
70
	public Decompress(){		
71

  
72
	}
73

  
74
	public void decompressPlugins(InputStream is, File outputDirectory) throws InstallPackageServiceException {
75
		option = OPTION_DECOMPRESS;
76
		this.outputDirectory = outputDirectory;
77
		
78
		decompressFolderOfPlugins(is);
79
	}
80

  
81
	public void readInstallInfo(InputStream is, List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) throws InstallPackageServiceException{
82
		option = OPTION_READ_INSTALLINFO;
83
		this.readedIinstallerInfos = installerInfos;
84
		this.zipEntriesMap = zipEntriesMap;	
85
		decompressFolderOfPlugins(is);			
86
	}	
87

  
88
	public void decompressPlugin(InputStream is, File outputDirectory) throws InstallPackageServiceException {
89
		try{
90
			option = OPTION_DECOMPRESS;
91
			this.outputDirectory = outputDirectory;				
92
			decompressPlugin(new ZipInputStream(is), "");
93
		} catch (Exception e) {
94
			throw new InstallPackageServiceException("Error reading the plugin", e);
95
		} 	
96
	}	
97

  
98
	public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallPackageServiceException{
99
		ZipEntry entry = null;		
100

  
101
		try {		
102
			ZipInputStream zipInputStream = new ZipInputStream(is);	
103

  
104
			while((entry = zipInputStream.getNextEntry()) != null) {	
105
				if (entry.getName().equals(zipEntry)){
106
					return zipInputStream;
107
				}				
108
				zipInputStream.closeEntry();
109
			}
110
			zipInputStream.closeEntry();
111

  
112
			zipInputStream.close();			
113
		} catch (Exception e) {
114
			throw new InstallPackageServiceException("Error reading the plugin", e);
115
		} 	
116
		return null;	
117
	}
118

  
119
	public void installFromStream(InputStream is, PackageInfo installerInfo) throws InstallPackageServiceException{
120
		option = OPTION_INSTALL;
121
		this.selectedInstallerInfos = new ArrayList<PackageInfo>();
122
		this.selectedInstallerInfos.add(installerInfo);
123
		decompressFolderOfPlugins(is);
124
	}
125

  
126
	public void installFromStream(InputStream is, List<PackageInfo> installerInfos) throws InstallPackageServiceException{
127
		option = OPTION_INSTALL;
128
		this.selectedInstallerInfos = installerInfos;		
129
		decompressFolderOfPlugins(is);
130
	}	
131

  
132

  
133
	private void decompressFolderOfPlugins(InputStream is) throws InstallPackageServiceException {
134
		ZipInputStream zipInputStream = new ZipInputStream(is);	
135
		ZipEntry entry = null;		
136

  
137
		try {			
138

  
139
			while((entry = zipInputStream.getNextEntry()) != null) {	
140
				logger.debug("Extracting all Plugins, plugin: " + entry);
141
				if (option == OPTION_INSTALL){
142

  
143
				}else if (option == OPTION_DECOMPRESS){				
144
					decompressPlugin(new ZipInputStream(zipInputStream), entry.getName());
145
				}else if (option == OPTION_READ_INSTALLINFO){			
146
					readPlugin(new ZipInputStream(zipInputStream), entry.getName());
147
				}
148
				zipInputStream.closeEntry();
149
			}			
150
			zipInputStream.close();	
151
			
152
		} catch (Exception e) {
153
			throw new InstallPackageServiceException("Error reading the plugin", e);
154
		} 			
155
	}
156

  
157
	private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
158
		ZipEntry entry = null;		
159
		int installerInfoNumber = zipEntriesMap.size();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff