Statistics
| Revision:

root / 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 @ 32498

History | View | Annotate | Download (7.22 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.lib.impl.execution;
29

    
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.InputStream;
34
import java.util.ArrayList;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38

    
39
import org.gvsig.installer.lib.api.InstallerInfo;
40
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
41
import org.gvsig.installer.lib.api.execution.InstallerExecutionService;
42
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
43
import org.gvsig.installer.lib.impl.DefaultInstallerManager;
44
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
45
import org.gvsig.installer.lib.spi.InstallerProviderManager;
46
import org.gvsig.installer.lib.spi.InstallerProviderServices;
47
import org.gvsig.installer.lib.spi.execution.InstallerExecutionProvider;
48
import org.gvsig.tools.service.Manager;
49
import org.gvsig.tools.service.ServiceException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
55
 */
56
public class DefaultInstallerExecutionService implements InstallerExecutionService{
57
        private Map<InstallerInfo, File> installFilesMap = null;
58
        private Map<InstallerInfo, String> zipEntriesMap = null;
59
        private List<InstallerInfo> installerInfos = null;        
60
        private Manager manager;        
61
        private int pluginToInstallIndex = -1;
62
        private File applicationDirectory;
63
        private static final Logger logger = LoggerFactory.getLogger(DefaultInstallerExecutionService.class);
64
        private InstallerProviderServices installerProviderServices = null;
65

    
66
        public DefaultInstallerExecutionService(DefaultInstallerManager manager) {
67
                super();
68
                this.manager = manager;
69
                installFilesMap = new HashMap<InstallerInfo, File>();
70
                installerInfos = new ArrayList<InstallerInfo>();
71
                zipEntriesMap = new HashMap<InstallerInfo, String>();
72
                installerProviderServices = InstallerProviderLocator.getProviderManager().createInstallerProviderServices();
73
        }
74

    
75
        public void executeInstaller()
76
        throws InstallerExecutionServiceException {        
77
                if (pluginToInstallIndex == -1){
78
                        throw new InstallerExecutionServiceException("not_installer_selected");
79
                }
80
                if (!installFilesMap.containsKey(installerInfos.get(pluginToInstallIndex))){
81
                        throw new InstallerExecutionServiceException("file_not_found_use_execute_installer_with_stream");
82
                }
83
                File file = installFilesMap.get(installerInfos.get(pluginToInstallIndex));
84
                try {
85
                        executeInstaller(new FileInputStream(file));
86
                } catch (FileNotFoundException e) {
87
                        throw new InstallerExecutionServiceException("file_not_found");
88
                }
89
        }
90
        
91
        public void executeInstaller(InputStream inputStream) throws InstallerExecutionServiceException{
92
                if (pluginToInstallIndex == -1){
93
                        throw new InstallerExecutionServiceException("not_installer_selected");
94
                }
95
                if ((applicationDirectory == null) || (!applicationDirectory.exists())){
96
                        throw new InstallerExecutionServiceException("not_valid_application_directory_selected");
97
                }
98
                //Execute the installer
99
                InstallerExecutionProvider installerExecutionProvider = createProvider(pluginToInstallIndex);
100
                InstallerInfo instalerInfo = installerInfos.get(pluginToInstallIndex);
101
                InputStream is = installerProviderServices.searchPlugin(inputStream,zipEntriesMap.get(instalerInfo));        
102
                installerExecutionProvider.install(applicationDirectory, is);                        
103
        }        
104

    
105
        private InstallerExecutionProvider createProvider(int index) throws InstallerExecutionServiceException {
106
                InstallerProviderManager installerProviderManager = (InstallerProviderManager)((DefaultInstallerManager)manager).getProviderManager();                
107

    
108
                try {
109
                        return installerProviderManager.createExecutionProvider(installerInfos.get(index).getType());                                
110
                } catch (ServiceException e) {
111
                        throw new InstallerExecutionServiceException("Error creating the provider", e);
112
                }                        
113
        }                
114

    
115

    
116
        public InstallerInfo getPluginInfoAt(int index) {
117
                if (installerInfos == null){
118
                        return null;
119
                }
120
                return installerInfos.get(index);        
121
        }
122

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

    
128
        public void setApplicationDirectory(File applicationDirectory) throws InstallerExecutionServiceException{
129
                if (!applicationDirectory.isDirectory()){
130
                        throw new InstallerExecutionServiceException("The application directory has to be a directory");
131
                }
132
                this.applicationDirectory = applicationDirectory;                
133
        }
134

    
135

    
136
        public void addInstallersFromInstallDirectory()
137
        throws InstallerExecutionServiceException {
138
                if (applicationDirectory != null){
139
                        File installDirectory = getInstallDirectory();
140
                        if (installDirectory.exists()){
141
                                File[] files = installDirectory.listFiles();
142
                                int from = installerInfos.size();                
143
                                for (int i=0 ; i<files.length ; i++){
144
                                        try {                                                
145
                                                addInstaller(new FileInputStream(files[i]));                                                
146
                                        } catch (FileNotFoundException e) {
147
                                                logger.error("File not found", e);
148
                                        }
149
                                }                                        
150
                                for (int i=0 ; i<files.length ; i++){
151
                                        installFilesMap.put(installerInfos.get(from+i), files[i]);
152
                                }
153
                        }        
154
                }
155
        }
156

    
157
        public void deleteInstallers() {
158
                installFilesMap.clear();
159
                installerInfos.clear();                
160
        }
161

    
162

    
163

    
164
        private File getInstallDirectory(){
165
                return new File(applicationDirectory.getAbsolutePath() + File.separator + "install");
166
        }
167

    
168
        public int getPluginsSize() {
169
                if (installerInfos == null){
170
                        return 0;
171
                }
172
                return installerInfos.size();
173
        }
174

    
175
        InstallerInfo getSelectedInstallerInfo(){
176
                if (pluginToInstallIndex == -1){
177
                        return null;
178
                }
179
                return installerInfos.get(pluginToInstallIndex);
180
        }
181

    
182
        public void setPluginToInstall(int index) throws InstallerExecutionServiceException{
183
                if (index > installerInfos.size()){
184
                        throw new InstallerExecutionServiceException("The plugin doesn't exist");
185
                }
186
                pluginToInstallIndex = index;
187
        }
188

    
189
        public Manager getManager() {
190
                return this.manager;
191
        }
192

    
193
        public void setPluginToInstall(String code) throws InstallerExecutionServiceException {
194
                int selectedCode = -1;
195
                for (int i=0 ; i<getPluginsSize() ; i++){
196
                        if (installerInfos.get(i).getCode().equals(code)){
197
                                selectedCode = i;
198
                                break;
199
                        }
200
                }
201
                if (selectedCode == -1){
202
                        throw new InstallerExecutionServiceException("The plugin doesn't exist");                
203
                }
204
                pluginToInstallIndex = selectedCode;
205
        }
206

    
207
}
208