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 @ 32411

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.Collection;
41
import java.util.HashMap;
42
import java.util.Iterator;
43
import java.util.List;
44
import java.util.ListIterator;
45
import java.util.Map;
46

    
47
import javax.swing.JCheckBox;
48

    
49
import org.gvsig.installer.lib.api.InstallerInfo;
50
import org.gvsig.installer.lib.api.creation.InstallerCreationService;
51
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
52
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
53
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
54
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
55
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter;
56
import org.gvsig.tools.service.AbstractService;
57
import org.gvsig.tools.service.Manager;
58
import org.slf4j.Logger;
59

    
60
/**
61
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
62
 */
63
public class DefaultInstallerCreationService implements InstallerCreationService {
64
        public static final String INSTALLINFO_FILE_NAME = "install.info";
65
        public static final String ANT_FILE_NAME = "install.xml";
66
        public static final String COPIED_FILES_DIRECTORY_NAME = "files";
67
        private File applicationDirectory;
68
        private File pluginsDirectory;
69
        private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class);
70
        private Manager manager = null;
71
        private List<InstallerInfo> installerInfos = null;        
72
        private Map<InstallerInfo, String> directories = null;
73
        private int pluginToInstallIndex = -1;
74

    
75
        public DefaultInstallerCreationService(Manager manager) {
76
                super();
77
                this.manager = manager;
78
                installerInfos = new ArrayList<InstallerInfo>();        
79
                directories = new HashMap<InstallerInfo, String>();
80
        }
81

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

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

    
91
                //Create the ant file
92
                String antScript = getSelectedInstallerInfo().getAntScript();
93
                if (antScript != null){
94
                        writeAntFile(antScript);
95
                }
96
                
97
                //Copy the selected files
98
                writeSelectedFiles();
99

    
100
                Compress compress = new Compress();
101
                compress.compressPlugin(getAbsoluteSelectePluginFile(), installerStream);                        
102
        }
103

    
104

    
105
        private void writeInstallInfo() throws InstallerCreationServiceException{
106
                InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter();
107
                installerInfoFileWriter.write(getSelectedInstallerInfo(), new File(directories.get(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
                        for (int i=0 ; i<getSelectedInstallerInfo().getFileToCopySize() ; i++){
118
                                String destFile = getSelectedInstallerInfo().getFileToCopyAt(i).getAbsolutePath();
119
                                
120
                                String sourceFile = applicationDirectory + File.separator + destFile;
121
                                
122
                                //Create the final path
123
                                destFile = pluginsDirectory + File.separator + copiedFilesDirectory + File.separator +  destFile;                                
124
                                
125

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

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

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

    
152

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

    
168

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

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

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

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

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

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

    
208
                //Read all the installed plugins
209
                String[] plugins = pluginsDirectory.list();
210
                InstallerInfoFileReader reader = new InstallerInfoFileReader();
211
                if (plugins != null) {
212
                        for (int i=0; i<plugins.length; i++) 
213
                        { 
214
                                String pluginDirectoryName = plugins[i];
215
                                File pluginDirectory = getAbsolutePluginFile(pluginDirectoryName);
216
                                DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
217
                                File installInfoFile = new File(getInstallerInfoFileName(pluginDirectory.getAbsolutePath()));
218
                                if (installInfoFile.exists()){
219
                                        reader.read(installerInfo, installInfoFile);
220
                                }else{
221
                                        installerInfo.setCode(pluginDirectoryName);
222
                                        installerInfo.setName(pluginDirectoryName);
223
                                }                                        
224
                                installerInfos.add(installerInfo);
225
                                directories.put(installerInfo, pluginDirectoryName);                                
226
                        }
227
                }        
228
        }
229

    
230
        private InstallerInfo getSelectedInstallerInfo() throws InstallerCreationServiceException{
231
                if (pluginToInstallIndex != -1){
232
                        return installerInfos.get(pluginToInstallIndex);
233
                }
234
                throw new InstallerCreationServiceException("There is not a plugin selected");
235
        }
236

    
237
        private File getAbsoluteSelectePluginFile() throws InstallerCreationServiceException{
238
                return getAbsolutePluginFile(directories.get(getSelectedInstallerInfo()));
239
        }
240
        
241
        private File getAbsolutePluginFile(String selectePluginName) throws InstallerCreationServiceException{
242
                return new File(pluginsDirectory.getAbsolutePath() + File.separator + selectePluginName);
243
        }
244
        
245
        private String getPluginToInstallDirectory() throws InstallerCreationServiceException{                
246
                return directories.get(getSelectedInstallerInfo());                
247
        }
248

    
249
        private String getCopiedFilesDirectoryName() throws InstallerCreationServiceException{
250
                return getCopiedFilesDirectoryName(getPluginToInstallDirectory());
251
        }
252

    
253
        private String getInstallerInfoFileName() throws InstallerCreationServiceException{
254
                return getInstallerInfoFileName(getPluginToInstallDirectory());
255
        }
256

    
257
        private String getAntFileName() throws InstallerCreationServiceException{
258
                return getAntFileName(getPluginToInstallDirectory());
259
        }
260

    
261
        private String getCopiedFilesDirectoryName(String pluginDirectory) throws InstallerCreationServiceException{
262
                return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
263
        }
264

    
265
        private String getInstallerInfoFileName(String pluginDirectory) throws InstallerCreationServiceException{
266
                return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME;
267
        }
268

    
269
        private String getAntFileName(String pluginDirectory) throws InstallerCreationServiceException{
270
                return getAbsoluteSelectePluginFile().getAbsolutePath() + File.separator + ANT_FILE_NAME;
271
        }
272

    
273

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

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

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

    
301
                        getSelectedInstallerInfo().addFileToCopy(new File(pluginFile));
302
                }
303
        }
304

    
305
        public Manager getManager() {
306
                return this.manager;
307
        }
308

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

    
313
        public int getPluginsSize() {
314
                return installerInfos.size();
315
        }
316

    
317
        public void setSelectedPlugin(int index) throws InstallerCreationServiceException {
318
                pluginToInstallIndex = index;        
319

    
320

    
321
                //Load the ant file                                
322
                File antFile = new File(getAntFileName());
323
                if (antFile.exists()){
324
                        try {
325
                                loadAntFile(antFile);
326
                        } catch (IOException e) {
327
                                throw new InstallerCreationServiceException("Error reading the ant file");
328
                        }
329
                }                
330

    
331
                //Load the files to copy                        
332
                File copiedFilesDirectoryfile = new File(getCopiedFilesDirectoryName());
333
                if (copiedFilesDirectoryfile.exists()){
334
                        loadCopiedFiles(copiedFilesDirectoryfile);
335
                }                
336
        }
337

    
338
        public void setSelectedPlugin(String code) {
339
                pluginToInstallIndex = -1;
340
                for (int i=0 ; i<getPluginsSize() ; i++){
341
                        if (installerInfos.get(i).getCode().equals(code)){
342
                                pluginToInstallIndex = i;
343
                        }
344
                }
345
        }
346

    
347
        public InstallerInfo getSelectedPlugin() throws InstallerCreationServiceException {
348
                return getSelectedInstallerInfo();
349
        }
350

    
351
        public String getDefaultAntScript() throws InstallerCreationServiceException {
352
                try {
353
                        return readFileAsString(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile());
354
                } catch (IOException e) {
355
                        throw new InstallerCreationServiceException("Impossible to read the default ant file", e);
356
                }
357
        }
358
        
359
         private String readFileAsString(String filePath)
360
            throws java.io.IOException{
361
                StringBuffer fileData = new StringBuffer(1000);
362
                BufferedReader reader = new BufferedReader(
363
                        new FileReader(filePath));
364
                char[] buf = new char[1024];
365
                int numRead=0;
366
                while((numRead=reader.read(buf)) != -1){
367
                    String readData = String.valueOf(buf, 0, numRead);
368
                    fileData.append(readData);
369
                    buf = new char[1024];
370
                }
371
                reader.close();
372
                return fileData.toString();
373
            }
374

    
375
}
376