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

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

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

    
47
/**
48
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
49
 */
50
public class DefaultInstallerCreationService extends DefaultInstallerInfo implements InstallerCreationService {
51
        public static final String INSTALLINFO_FILE_NAME = "install.info";
52
        public static final String ANT_FILE_NAME = "install.xml";
53
        public static final String COPIED_FILES_DIRECTORY_NAME = "files";
54
        private File pluginDirectory;
55
        private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultInstallerCreationService.class);
56

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

    
63
                //Write the install.info file
64
                writeInstallInfo();
65

    
66
                //if (isAdvancedMode){
67
                        //Create the ant file
68
                        if (antScript != null){
69
                                writeAntFile();
70
                        }else{
71
                                File antFileModel = new File(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile());
72
                                try{
73
                                        copy(antFileModel, new File(getAntFileName()));
74
                                }catch(IOException e){
75
                                        throw new InstallerCreationServiceException("Exception copying the ant file");
76
                                }                        
77
                        //}
78

    
79
                        //Copy the selected files
80
                        writeSelectedFiles();
81
                }
82

    
83
                Compress compress = new Compress();
84
                compress.compressPlugin(pluginDirectory, installerStream);                        
85
        }
86

    
87
        private void writeAntFile() throws InstallerCreationServiceException {
88
                try{
89
                        ByteArrayInputStream in = new ByteArrayInputStream(antScript.getBytes());
90
                        OutputStream out = new FileOutputStream(getAntFileName());
91

    
92
                        // Copy the bits from instream to outstream
93
                        byte[] buf = new byte[1024];
94
                        int len;
95
                        while ((len = in.read(buf)) > 0) {
96
                                out.write(buf, 0, len);
97
                        }
98
                        in.close();
99
                        out.close();
100
                }catch(IOException e){
101
                        throw new InstallerCreationServiceException("Exception writing the ant file");
102
                }
103
        }
104

    
105
        private void writeSelectedFiles() throws InstallerCreationServiceException {
106
                try{
107
                        String copiedFilesDirectory = getCopiedFilesDirectoryName();
108

    
109
                        //Deleting the previous files folder
110
                        File copiedFilesDirectoryfile = new File(copiedFilesDirectory);
111
                        deleteDirectory(copiedFilesDirectoryfile);
112

    
113
                        String parentDirectory = pluginDirectory.getParent();
114
                        for (int i=0 ; i<selectedFiles.size() ; i++){
115
                                String destFile = selectedFiles.get(i).getAbsolutePath();
116

    
117
                                //Deleting the root where the gvSIG plugins are located
118
                                destFile = destFile.substring(parentDirectory.length(), destFile.length());
119

    
120
                                //Create the final path
121
                                destFile = copiedFilesDirectory + destFile;
122

    
123
                                //Copy the files
124
                                copy(selectedFiles.get(i), new File(destFile));
125
                        }
126
                }catch(IOException e){
127
                        throw new InstallerCreationServiceException("Exception copying the files");
128
                }
129
        }
130

    
131
        static public boolean deleteDirectory(File path) {
132
                if( path.exists() ) {
133
                        File[] files = path.listFiles();
134
                        for(int i=0; i<files.length; i++) {
135
                                if(files[i].isDirectory()) {
136
                                        deleteDirectory(files[i]);
137
                                }
138
                                else {
139
                                        files[i].delete();
140
                                }
141
                        }
142
                }
143
                return( path.delete() );
144
        }
145

    
146

    
147
        void copy(File sourceLocation , File targetLocation) throws IOException { 
148
                if (sourceLocation.isDirectory()) {
149
                        if (!targetLocation.exists()) {
150
                                targetLocation.mkdir();
151
                        }
152

    
153
                        String[] children = sourceLocation.list();
154
                        for (int i=0; i<children.length; i++) {
155
                                copy(new File(sourceLocation, children[i]),
156
                                                new File(targetLocation, children[i]));
157
                        }
158
                } else {
159
                        targetLocation.getParentFile().mkdirs();
160

    
161
                        InputStream in = new FileInputStream(sourceLocation);
162
                        OutputStream out = new FileOutputStream(targetLocation);
163

    
164
                        // Copy the bits from instream to outstream
165
                        byte[] buf = new byte[1024];
166
                        int len;
167
                        while ((len = in.read(buf)) > 0) {
168
                                out.write(buf, 0, len);
169
                        }
170
                        in.close();
171
                        out.close();
172
                }                 
173
        }
174

    
175
        public void setPluginDirectory(File pluginDirectory) throws InstallerCreationServiceException {
176
                if (!pluginDirectory.isDirectory()){
177
                        throw new InstallerCreationServiceException("The plugin directory has to be a directory");
178
                }
179
                this.pluginDirectory = pluginDirectory;                
180

    
181
                //If the install.info file exists
182
                File file = new File(getInstallerInfoFileName());
183
                if (file.exists()){
184
                        readInstallInfo();
185
                }else{
186
                        setCode(pluginDirectory.getName());
187
                        setName(pluginDirectory.getName());
188
                }
189
        }
190

    
191
        private void readInstallInfo() throws InstallerInfoFileException{
192
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
193
                installerInfoFileReader.read(this, getInstallerInfoFileName());
194
        }        
195

    
196
        private void writeInstallInfo() throws InstallerInfoFileException{
197
                InstallerInfoFileWriter installerInfoFileWriter = new InstallerInfoFileWriter();
198
                installerInfoFileWriter.write(this, new File(getInstallerInfoFileName()));
199
        }
200

    
201
        private String getCopiedFilesDirectoryName(){
202
                return pluginDirectory + File.separator + COPIED_FILES_DIRECTORY_NAME;
203
        }
204

    
205
        private String getInstallerInfoFileName(){
206
                return pluginDirectory + File.separator + INSTALLINFO_FILE_NAME;
207
        }
208

    
209
        private String getAntFileName(){
210
                return pluginDirectory + File.separator + ANT_FILE_NAME;
211
        }
212

    
213
        public InstallerInfo getInstallerInfo() {
214
                return this;
215
        }
216

    
217
        public void loadInstallInfoFromDirectory() throws InstallerCreationServiceException {
218
                File file = new File(getInstallerInfoFileName());
219
                try{
220
                        if (file.exists()){
221
                                //Read the install.info
222
                                FileInputStream fis = new FileInputStream(file);
223

    
224
                                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
225
                                installerInfoFileReader.read(this, fis);
226

    
227
                                //Load the ant file                                
228
                                File antFile = new File(getAntFileName());
229
                                if (antFile.exists()){
230
                                        loadAntFile(antFile);
231
                                }                
232

    
233
                                //Load the files to copy                        
234
                                File copiedFilesDirectoryfile = new File(getCopiedFilesDirectoryName());
235
                                if (copiedFilesDirectoryfile.exists()){
236
                                        loadCopiedFiles(copiedFilesDirectoryfile);
237
                                }                
238
                        }
239
                }catch(IOException e){
240
                        throw new InstallerCreationServiceException("Exception copying the files");
241
                }
242
        }
243
        
244
        /* (non-Javadoc)
245
         * @see org.gvsig.installer.lib.impl.DefaultInstallerInfo#getAntScript()
246
         */
247
        @Override
248
        public String getAntScript() {
249
                if (super.getAntScript() != null){
250
                        return super.getAntScript();
251
                }
252
                //if (isAdvancedMode){
253
                        try {
254
                                loadAntFile(new File(getClass().getClassLoader().getResource(ANT_FILE_NAME).getFile()));
255
                                return antScript;
256
                        } catch (IOException e) {
257
                                logger.error("reading the ant script model", e);
258
                        }
259
                //}
260
                return null;
261
        }
262

    
263
        private void loadAntFile(File antFile) throws IOException {
264
                StringBuffer fileData = new StringBuffer();
265
                FileInputStream fis = new FileInputStream(antFile);
266
                byte[] buf = new byte[1024];
267
                int numRead = 0;
268
                while ((numRead = fis.read(buf)) != -1) {
269
                        String readData = new String(buf, 0, numRead);
270
                        fileData.append(readData);
271
                        buf = new byte[1024];
272
                }
273
                fis.close();
274
                setAnScript(fileData.toString());
275
        }
276

    
277
        private void loadCopiedFiles(File file){
278
                if (file.isDirectory()){
279
                        File[] files = file.listFiles();
280
                        for (int i=0 ; i<files.length ; i++){
281
                                loadCopiedFiles(files[i]);
282
                        }
283
                }else{
284
                        //Removing the plugin prefix
285
                        String pluginFile = file.getAbsolutePath().substring(getCopiedFilesDirectoryName().length(), file.getAbsolutePath().length());
286

    
287
                        //Ading the root directory
288
                        pluginFile = pluginDirectory.getParentFile().getAbsolutePath() + pluginFile;
289

    
290
                        addFileToCopy(new File(pluginFile));
291
                }
292
        }
293

    
294
}
295