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

History | View | Annotate | Download (5.53 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.File;
31
import java.io.FileInputStream;
32
import java.io.FileOutputStream;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36

    
37
import org.gvsig.installer.lib.api.InstallerInfo;
38
import org.gvsig.installer.lib.api.creation.InstallerCreationService;
39
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
40
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
41
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
42
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
43
import org.gvsig.installer.lib.impl.info.InstallerInfoFileWriter;
44

    
45
/**
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public class DefaultInstallerCreationService extends DefaultInstallerInfo implements InstallerCreationService {
49
        public static final String INSTALLINFO_FILE_NAME = "install.info";
50
        public static final String COPIED_FILES_DIRECTORY_NAME = "files";
51
        private File pluginDirectory;
52

    
53

    
54
        public void createInstaller(OutputStream installerStream)
55
        throws InstallerCreationServiceException {
56
                if (pluginDirectory == null){
57
                        throw new InstallerCreationServiceException("The plugin directory has to be set");
58
                }
59

    
60
                //Write the install.info file
61
                writeInstallInfo();
62

    
63
                //Copy the selected files
64
                writeSelectedFiles();
65

    
66
                Compress compress = new Compress();
67
                compress.compressPlugin(pluginDirectory, installerStream);                        
68
        }
69

    
70
        private void writeSelectedFiles() throws InstallerCreationServiceException {
71
                try{
72
                        String copiedFilesDirectory = getCopiedFilesDirectoryName();
73

    
74
                        //Deleting the previous files folder
75
                        File copiedFilesDirectoryfile = new File(copiedFilesDirectory);
76
                        deleteDirectory(copiedFilesDirectoryfile);
77

    
78
                        String parentDirectory = pluginDirectory.getParent();
79
                        for (int i=0 ; i<selectedFiles.size() ; i++){
80
                                String destFile = selectedFiles.get(i).getAbsolutePath();
81

    
82
                                //Deleting the root where the gvSIG plugins are located
83
                                destFile = destFile.substring(parentDirectory.length(), destFile.length());
84

    
85
                                //Create the final path
86
                                destFile = copiedFilesDirectory + destFile;
87

    
88
                                //Copy the files
89
                                copy(selectedFiles.get(i), new File(destFile));
90
                        }
91
                }catch(IOException e){
92
                        throw new InstallerCreationServiceException("Exception copying the files");
93
                }
94
        }
95

    
96
        static public boolean deleteDirectory(File path) {
97
                if( path.exists() ) {
98
                        File[] files = path.listFiles();
99
                        for(int i=0; i<files.length; i++) {
100
                                if(files[i].isDirectory()) {
101
                                        deleteDirectory(files[i]);
102
                                }
103
                                else {
104
                                        files[i].delete();
105
                                }
106
                        }
107
                }
108
                return( path.delete() );
109
        }
110

    
111

    
112
        void copy(File sourceLocation , File targetLocation) throws IOException { 
113
                if (sourceLocation.isDirectory()) {
114
                        if (!targetLocation.exists()) {
115
                                targetLocation.mkdir();
116
                        }
117

    
118
                        String[] children = sourceLocation.list();
119
                        for (int i=0; i<children.length; i++) {
120
                                copy(new File(sourceLocation, children[i]),
121
                                                new File(targetLocation, children[i]));
122
                        }
123
                } else {
124
                        targetLocation.getParentFile().mkdirs();
125

    
126
                        InputStream in = new FileInputStream(sourceLocation);
127
                        OutputStream out = new FileOutputStream(targetLocation);
128

    
129
                        // Copy the bits from instream to outstream
130
                        byte[] buf = new byte[1024];
131
                        int len;
132
                        while ((len = in.read(buf)) > 0) {
133
                                out.write(buf, 0, len);
134
                        }
135
                        in.close();
136
                        out.close();
137
                }                 
138
        }
139

    
140
        public void setPluginDirectory(File pluginDirectory) throws InstallerCreationServiceException {
141
                if (!pluginDirectory.isDirectory()){
142
                        throw new InstallerCreationServiceException("The plugin directory has to be a directory");
143
                }
144
                this.pluginDirectory = pluginDirectory;                
145

    
146
                //If the install.info file exists
147
                File file = new File(getInstallerInfoFileName());
148
                if (file.exists()){
149
                        readInstallInfo();
150
                }else{
151
                        setCode(pluginDirectory.getName());
152
                        setName(pluginDirectory.getName());
153
                }
154
        }
155

    
156
        private void readInstallInfo() throws InstallerInfoFileException{
157
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
158
                installerInfoFileReader.read(this, getInstallerInfoFileName());
159
        }        
160

    
161
        private void writeInstallInfo() throws InstallerInfoFileException{
162
                InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter();
163
                installerInfoFileWriter.write(this, new File(getInstallerInfoFileName()));
164
        }
165

    
166
        private String getCopiedFilesDirectoryName(){
167
                return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
168
        }
169

    
170
        private String getInstallerInfoFileName(){
171
                return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME;
172
        }
173

    
174
        public InstallerInfo getInstallerInfo() {
175
                return this;
176
        }
177
}
178