Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / creation / DefaultInstallerCreationService.java @ 32500

History | View | Annotate | Download (12.4 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.creation;
29

    
30
import java.io.BufferedReader;
31
import java.io.ByteArrayInputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.FileOutputStream;
35
import java.io.FileReader;
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39
import java.util.ArrayList;
40
import java.util.HashMap;
41
import java.util.List;
42
import java.util.Map;
43

    
44
import org.gvsig.installer.lib.api.InstallerInfo;
45
import org.gvsig.installer.lib.api.creation.InstallerCreationService;
46
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
47
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
48
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter;
50
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
51
import org.gvsig.installer.lib.spi.InstallerProviderServices;
52
import org.gvsig.tools.service.Manager;
53
import org.slf4j.Logger;
54

    
55
/**
56
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
57
 */
58
public class DefaultInstallerCreationService implements InstallerCreationService {
59
        public static final String ANT_FILE_NAME = "install.xml";
60
        public static final String COPIED_FILES_DIRECTORY_NAME = "files";
61
        private File applicationDirectory;
62
        private File pluginsDirectory;
63
        private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class);
64
        private Manager manager = null;
65
        private List<InstallerInfo> installerInfos = null;        
66
        private Map<InstallerInfo, String> directories = null;
67
        private int pluginToInstallIndex = -1;
68
        protected List<File> selectedFiles = null;
69
        protected String antScript = null;
70
        private InstallerProviderServices installerProviderServices = null;
71
                
72
        public DefaultInstallerCreationService(Manager manager) {
73
                super();
74
                this.manager = manager;
75
                installerInfos = new ArrayList<InstallerInfo>();        
76
                directories = new HashMap<InstallerInfo, String>();
77
                selectedFiles = new ArrayList<File>();
78
                installerProviderServices = InstallerProviderLocator.getProviderManager().createInstallerProviderServices();
79
        }
80

    
81
        public void createInstaller(OutputStream installerStream)
82
        throws InstallerCreationServiceException {
83
                if (applicationDirectory == null){
84
                        throw new InstallerCreationServiceException("The application directory has to be set");
85
                }
86

    
87
                //Write the install.info file
88
                writeInstallInfo();
89

    
90
                //Create the ant file
91
                String antScript = getSelectedInstallerInfo().getAntScript();
92
                if (antScript != null){
93
                        writeAntFile(antScript);
94
                }
95

    
96
                //Copy the selected files
97
                writeSelectedFiles();
98

    
99
                
100
                InstallerInfo infInstallerInfo = getSelectedPlugin();
101
                String pluginFileName = getSelectedPlugin().getCode() + "-" + infInstallerInfo.getVersion() + "-" + infInstallerInfo.getBuild();
102
                installerProviderServices.compress(getAbsoluteSelectePluginFile(), pluginFileName, installerStream);
103
        }
104

    
105

    
106
        private void writeInstallInfo() throws InstallerCreationServiceException{
107
                installerProviderServices.writeInstallInfo(new File(directories.get(getSelectedInstallerInfo())), getSelectedInstallerInfo());
108
        }
109

    
110
        private void writeSelectedFiles() throws InstallerCreationServiceException {
111
                try{
112
                        String copiedFilesDirectory = getCopiedFilesDirectoryName();
113

    
114
                        //Deleting the previous files folder
115
                        File copiedFilesDirectoryfile = new File(copiedFilesDirectory);
116
                        deleteDirectory(copiedFilesDirectoryfile);
117
                        InstallerInfo installerInfo = getSelectedInstallerInfo();
118
                        for (int i=0 ; i<installerInfo.getFileToCopySize() ; i++){
119
                                String destFile = installerInfo.getFileToCopyAt(i).getAbsolutePath();
120

    
121
                                String sourceFile = applicationDirectory + File.separator + destFile;
122

    
123
                                //Create the final path
124
                                destFile = pluginsDirectory + File.separator + copiedFilesDirectory + File.separator +  destFile;                                
125

    
126

    
127
                                //Copy the files
128
                                copy(new File(sourceFile), new File(destFile));
129
                        }
130
                }catch(IOException e){
131
                        throw new InstallerCreationServiceException("Exception copying the files");
132
                }
133
        }        
134

    
135
        private void writeAntFile(String content) throws InstallerCreationServiceException {
136
                try{
137
                        ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
138
                        OutputStream out = new FileOutputStream(getAbsoluteAntFile());
139

    
140
                        // Copy the bits from instream to outstream
141
                        byte[] buf = new byte[1024];
142
                        int len;
143
                        while ((len = in.read(buf)) > 0) {
144
                                out.write(buf, 0, len);
145
                        }
146
                        in.close();
147
                        out.close();
148
                }catch(IOException e){
149
                        throw new InstallerCreationServiceException("Exception writing the ant file");
150
                }
151
        }
152

    
153

    
154
        static public boolean deleteDirectory(File path) {
155
                if( path.exists() ) {
156
                        File[] files = path.listFiles();
157
                        for(int i=0; i<files.length; i++) {
158
                                if(files[i].isDirectory()) {
159
                                        deleteDirectory(files[i]);
160
                                }
161
                                else {
162
                                        files[i].delete();
163
                                }
164
                        }
165
                }
166
                return( path.delete() );
167
        }
168

    
169

    
170
        void copy(File sourceLocation , File targetLocation) throws IOException { 
171
                if (sourceLocation.isDirectory()) {
172
                        if (!targetLocation.exists()) {
173
                                targetLocation.mkdir();
174
                        }
175

    
176
                        String[] children = sourceLocation.list();
177
                        for (int i=0; i<children.length; i++) {
178
                                copy(new File(sourceLocation, children[i]),
179
                                                new File(targetLocation, children[i]));
180
                        }
181
                } else {
182
                        targetLocation.getParentFile().mkdirs();
183

    
184
                        InputStream in = new FileInputStream(sourceLocation);
185
                        OutputStream out = new FileOutputStream(targetLocation);
186

    
187
                        // Copy the bits from instream to outstream
188
                        byte[] buf = new byte[1024];
189
                        int len;
190
                        while ((len = in.read(buf)) > 0) {
191
                                out.write(buf, 0, len);
192
                        }
193
                        in.close();
194
                        out.close();
195
                }                 
196
        }
197

    
198
        public void setApplicationDirectory(File applicationDirectory) throws InstallerCreationServiceException {
199
                if (!applicationDirectory.isDirectory()){
200
                        throw new InstallerCreationServiceException("The application directory has to be a directory");
201
                }
202
                this.applicationDirectory = applicationDirectory;        
203

    
204
                this.pluginsDirectory = new File(applicationDirectory.getAbsoluteFile() + File.separator + "gvSIG" + File.separator + "extensiones") ;
205
                if ((!pluginsDirectory.exists()) || (!pluginsDirectory.isDirectory())){
206
                        throw new InstallerCreationServiceException("The plugins directory does't exists or not is a directory");
207
                }
208

    
209
                //Read all the installed plugins
210
                String[] plugins = pluginsDirectory.list();
211
                
212
                if (plugins != null) {
213
                        for (int i=0; i<plugins.length; i++) 
214
                        { 
215
                                String pluginDirectoryName = plugins[i];
216
                                File pluginDirectory = getAbsolutePluginFile(pluginDirectoryName);
217
                                DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
218
                                
219
                                installerProviderServices.readInstallInfo(new File(pluginDirectory.getAbsolutePath()), installerInfo);
220
                                                                
221
                                //Checks if the ant file exists
222
                                File antFile = new File(getAntFileName(pluginDirectory.getAbsolutePath()));
223
                                if (antFile.exists()){
224
                                        try {
225
                                                installerInfo.setAnScript(readFileAsString(antFile.getAbsolutePath()));
226
                                        } catch (IOException e) {
227
                                                logger.error("Not possible to read the ant file");
228
                                        }
229
                                }
230
                                installerInfos.add(installerInfo);
231
                                directories.put(installerInfo, pluginDirectoryName);                                
232
                        }
233
                }        
234
        }
235

    
236
        private InstallerInfo getSelectedInstallerInfo() throws InstallerCreationServiceException{
237
                if (pluginToInstallIndex != -1){
238
                        return installerInfos.get(pluginToInstallIndex);
239
                }
240
                throw new InstallerCreationServiceException("There is not a plugin selected");
241
        }
242

    
243
        private File getAbsoluteSelectePluginFile() throws InstallerCreationServiceException{
244
                return getAbsolutePluginFile(directories.get(getSelectedInstallerInfo()));
245
        }
246

    
247
        private File getAbsolutePluginFile(String selectePluginName) throws InstallerCreationServiceException{
248
                return new File(pluginsDirectory.getAbsolutePath() + File.separator + selectePluginName);
249
        }
250

    
251
        private String getPluginToInstallDirectory() throws InstallerCreationServiceException{                
252
                return directories.get(getSelectedInstallerInfo());                
253
        }
254
        
255
        private File getAbsoluteAntFile() throws InstallerCreationServiceException{                
256
                return new File(pluginsDirectory.getAbsolutePath() + File.separator + ANT_FILE_NAME);
257
        }
258

    
259
        private String getCopiedFilesDirectoryName() throws InstallerCreationServiceException{
260
                return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
261
        }
262

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

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

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

    
275

    
276
        private void loadAntFile(File antFile) throws IOException, InstallerCreationServiceException {
277
                StringBuffer fileData = new StringBuffer();
278
                FileInputStream fis = new FileInputStream(antFile);
279
                byte[] buf = new byte[1024];
280
                int numRead = 0;
281
                while ((numRead = fis.read(buf)) != -1) {
282
                        String readData = new String(buf, 0, numRead);
283
                        fileData.append(readData);
284
                        buf = new byte[1024];
285
                }
286
                fis.close();
287
                getSelectedInstallerInfo().setAnScript(fileData.toString());                
288
        }
289

    
290
        private void loadCopiedFiles(File file) throws InstallerCreationServiceException{
291
                if (file.isDirectory()){
292
                        File[] files = file.listFiles();
293
                        for (int i=0 ; i<files.length ; i++){
294
                                loadCopiedFiles(files[i]);
295
                        }
296
                }else{
297
                        //Removing the plugin prefix
298
                        String pluginFile = file.getAbsolutePath().substring(getCopiedFilesDirectoryName().length(), file.getAbsolutePath().length());
299

    
300
                        //Ading the root directory
301
                        pluginFile = new File(getPluginToInstallDirectory()).getParentFile().getAbsolutePath() + pluginFile;
302

    
303
                        getSelectedInstallerInfo().addFileToCopy(new File(pluginFile));
304
                }
305
        }
306

    
307
        public Manager getManager() {
308
                return this.manager;
309
        }
310

    
311
        public InstallerInfo getPluginInfoAt(int index) {
312
                return installerInfos.get(index);
313
        }
314

    
315
        public int getPluginsSize() {
316
                return installerInfos.size();
317
        }
318

    
319
        public void setSelectedPlugin(int index) throws InstallerCreationServiceException {
320
                if (index > installerInfos.size()){
321
                        throw new InstallerCreationServiceException("The plugin doesn't exist");
322
                }
323
                pluginToInstallIndex = index;                        
324
        }
325

    
326
        public void setSelectedPlugin(String code) throws InstallerCreationServiceException {
327
                int selectedCode = -1;;
328
                for (int i=0 ; i<getPluginsSize() ; i++){
329
                        if (installerInfos.get(i).getCode().equals(code)){
330
                                selectedCode = i;
331
                                break;
332
                        }
333
                }
334
                if (selectedCode == -1){
335
                        throw new InstallerCreationServiceException("The plugin doesn't exist");                
336
                }
337
                pluginToInstallIndex = selectedCode;
338
        }
339

    
340
        public InstallerInfo getSelectedPlugin() {
341
                try {
342
                        return getSelectedInstallerInfo();
343
                } catch (InstallerCreationServiceException e) {                        
344
                        //there is not a selected plugin
345
                        return null;
346
                }
347
        }
348

    
349
        public String getDefaultAntScript() throws InstallerCreationServiceException {
350
                try {
351
                        return readFileAsString(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile());
352
                } catch (IOException e) {
353
                        throw new InstallerCreationServiceException("Impossible to read the default ant file", e);
354
                }
355
        }
356

    
357
        private String readFileAsString(String filePath)
358
        throws java.io.IOException{
359
                StringBuffer fileData = new StringBuffer(1000);
360
                BufferedReader reader = new BufferedReader(
361
                                new FileReader(filePath));
362
                char[] buf = new char[1024];
363
                int numRead=0;
364
                while((numRead=reader.read(buf)) != -1){
365
                        String readData = String.valueOf(buf, 0, numRead);
366
                        fileData.append(readData);
367
                        buf = new char[1024];
368
                }
369
                reader.close();
370
                return fileData.toString();
371
        }
372
}
373