Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2031 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultPackageInfo.java @ 36096

History | View | Annotate | Download (11.6 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;
29

    
30
import java.io.File;
31
import java.io.IOException;
32
import java.net.URL;
33
import java.util.ArrayList;
34
import java.util.List;
35

    
36
import org.gvsig.installer.lib.api.Dependencies;
37
import org.gvsig.installer.lib.api.InstallerManager.ARCH;
38
import org.gvsig.installer.lib.api.InstallerManager.JVM;
39
import org.gvsig.installer.lib.api.InstallerManager.OS;
40
import org.gvsig.installer.lib.api.InstallerManager.STATE;
41
import org.gvsig.installer.lib.api.PackageInfo;
42
import org.gvsig.installer.lib.api.Version;
43
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
44
import org.gvsig.installer.lib.impl.info.InstallerInfoTags;
45
import org.gvsig.installer.lib.impl.utils.DeleteFile;
46
import org.gvsig.installer.lib.impl.utils.Download;
47
import org.gvsig.tools.task.SimpleTaskStatus;
48

    
49
/**
50
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
51
 */
52
public class DefaultPackageInfo implements PackageInfo {
53

    
54
    private String code = null;
55
    private String name = null;
56
    private String description = null;
57
    private Version version = null;
58
//    private int build = 0;
59
    private boolean official;
60
    private List<File> auxFiles = null;
61
    private String antScript = null;
62
    private String type = "unknow";
63

    
64
    private String state = STATE.DEVEL;
65
    private String operatingSystem = OS.ALL;
66
    private String architecture = ARCH.ALL;
67
    private String javaVM = JVM.J1_5;
68

    
69
    private String owner = "";
70
    private URL sources = null;
71
    private String gvSIGVersion="";
72

    
73
    private URL defaultDownloadURL = null;
74

    
75
    private String modelVersion = "1.0.1";
76
    private Dependencies dependencies = null;
77

    
78
    public DefaultPackageInfo() {
79
        super();
80
        auxFiles = new ArrayList<File>();
81
        this.version = new DefaultVersion().parse("0.0.1");
82
        this.dependencies = new DefaultDependencies();
83
    }
84

    
85
    public String getCode() {
86
        return code;
87
    }
88

    
89
    public String getID() {
90
        String id =
91
            this.getCode() + "#" + this.getVersion() + "#" + this.getBuild();
92
        return id;
93
    }
94

    
95
    public String getName() {
96
        return name;
97
    }
98

    
99
    public String getDescription() {
100
        return description;
101
    }
102

    
103
    public Version getVersion() {
104
        return version;
105
    }
106

    
107
    public int getBuild() {
108
        return this.version.getBuild();
109
    }
110

    
111
    public String getState() {
112
        return state;
113
    }
114

    
115
    public boolean isOfficial() {
116
        return official;
117
    }
118

    
119
    public void setCode(String code) {
120
        this.code = code;
121
    }
122

    
123
    public void setName(String name) {
124
        this.name = name;
125
    }
126

    
127
    public void setDescription(String description) {
128
        this.description = description;
129
    }
130

    
131
    public void setVersion(String version) {
132
            int prev = this.version.getBuild();
133
        this.version.parse(version);
134
                int curr = this.version.getBuild();
135
                if (prev!=0 && curr==0){
136
                        this.version.setBuild(prev);
137
                }
138
    }
139

    
140
    public void setVersion(Version version) {
141
        try {
142
                int prev = this.version.getBuild();
143
                        this.version = (Version) version.clone();
144
                        int curr = this.version.getBuild();
145
                        if (prev!=0 && curr==0){
146
                                this.version.setBuild(prev);
147
                        }
148
                } catch (CloneNotSupportedException e) {
149
                        throw new RuntimeException(e);
150
                }
151
    }
152

    
153
    public void setBuild(int build) {
154
            this.version.setBuild(build);
155
    }
156

    
157
    public void setState(String state) {
158
        this.state = state;
159
    }
160

    
161
    public void setOfficial(boolean official) {
162
        this.official = official;
163
    }
164

    
165
    public String getOperatingSystem() {
166
        return operatingSystem;
167
    }
168

    
169
    public void setOperatingSystem(String operatingSystem) {
170
        this.operatingSystem = operatingSystem;
171
    }
172

    
173
    public String getArchitecture() {
174
        return architecture;
175
    }
176

    
177
    public void setArchitecture(String architecture) {
178
        this.architecture = architecture;
179
    }
180

    
181
    public String getJavaVM() {
182
        return javaVM;
183
    }
184

    
185
    public void setJavaVM(String javaVM) {
186
        this.javaVM = javaVM;
187
    }
188

    
189
    public String getAntScript() {
190
        return antScript;
191
    }
192

    
193
    public void setAntScript(String antScript) {
194
        this.antScript = antScript;
195
    }
196

    
197
    public String getType() {
198
        return type;
199
    }
200

    
201
    public void setType(String type) {
202
        this.type = type;
203
    }
204

    
205
    public String getGvSIGVersion() {
206
        return gvSIGVersion;
207
    }
208

    
209
    public void setGvSIGVersion(String gvSIGVersion) {
210
        this.gvSIGVersion = gvSIGVersion;
211
    }
212

    
213
    public URL getDownloadURL() {
214
        return defaultDownloadURL;
215
    }
216

    
217
    public void setDownloadURL(URL defaultDownloadURL) {
218
        this.defaultDownloadURL = defaultDownloadURL;
219
    }
220

    
221
    public String getModelVersion() {
222
        return modelVersion;
223
    }
224

    
225
    public void setModelVersion(String modelVersion) {
226
        this.modelVersion = modelVersion;
227
    }
228
    
229
    public String getOwner() {
230
        return owner;
231
    }
232

    
233
    public void setOwner(String owner) {
234
        this.owner = owner;
235
    }
236

    
237
    public URL getSourcesURL() {
238
        return sources;
239
    }
240

    
241
    public void setSourcesURL(URL sources) {
242
        this.sources = sources;
243
    }
244

    
245
    @Override
246
    public String toString() {
247
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
248

    
249
        append(buffer, InstallerInfoTags.CODE, getCode());
250
        append(buffer, InstallerInfoTags.NAME, getName());
251
        append(buffer, InstallerInfoTags.DESCRIPTION, getDescription());
252
        append(buffer, InstallerInfoTags.GVSIG_VERSION, getGvSIGVersion());
253
        append(buffer, InstallerInfoTags.VERSION, getVersion());
254
        append(buffer, InstallerInfoTags.BUILD, getBuild());
255
        append(buffer, InstallerInfoTags.OS, getOperatingSystem());
256
        append(buffer, InstallerInfoTags.ARCHITECTURE, getArchitecture());
257
        append(buffer, InstallerInfoTags.JVM, getJavaVM());
258
        append(buffer, InstallerInfoTags.DOWNLOAD_URL, getDownloadURL());
259
        append(buffer, InstallerInfoTags.STATE, getState());
260
        append(buffer, InstallerInfoTags.OFFICIAL, isOfficial());
261
        append(buffer, InstallerInfoTags.TYPE, getType());
262
        append(buffer, InstallerInfoTags.MODEL_VERSION, getModelVersion());
263
        append(buffer, InstallerInfoTags.OWNER, getOwner());
264
        append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
265
        append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
266
 
267
        
268
        return buffer.append(')').toString();
269
    }
270

    
271
    public String toStringCompact() {
272
            //            type      code       version    state    os       arch     jvm      dep
273
            return String.format("%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
274
                            this.type,
275
                            this.code,
276
                            this.version,
277
                            this.state,
278
                            this.operatingSystem,
279
                            this.architecture,
280
                            this.javaVM,
281
                            this.dependencies
282
            );
283
    }
284
    
285
    private DefaultPackageInfo append(StringBuffer buffer, String key,
286
        Object value) {
287
        buffer.append("\n\t").append(key).append(": ").append(
288
            value == null ? "" : value);
289
        return this;
290
    }
291

    
292
    @Override
293
    public Object clone() throws CloneNotSupportedException {
294
        DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
295
        clone.auxFiles = new ArrayList<File>(auxFiles);
296
        return clone;
297
    }
298

    
299
    public File downloadFile() throws InstallPackageServiceException {
300
        return this.downloadFile(null);
301
    }
302

    
303
    public class FileDownloadException extends InstallPackageServiceException {
304

    
305
        private static final long serialVersionUID = 8640183295766490512L;
306

    
307
        private static final String message = "File '%(url)s' download error";
308

    
309
        private static final String KEY = "_File_XurlX_download_error";
310

    
311
        public FileDownloadException(URL url, IOException e) {
312
            super(message, e, KEY, serialVersionUID);
313
            setValue("url", url.toString());
314
        }
315

    
316
    }
317

    
318
    public File downloadFile(SimpleTaskStatus taskStatus)
319
        throws InstallPackageServiceException {
320
        Download download = new Download(taskStatus);
321
        try {
322
            return download.downloadFile(this.getDownloadURL(), null);
323
        } catch (IOException e) {
324
                throw new FileDownloadException(this.getDownloadURL(), e);
325
        }
326
    }
327
    
328
    public void addFileToCopy(File file) {
329
        auxFiles.add(file);
330
    }
331
    
332
    public File getFileToCopy(int i) {
333
        return auxFiles.get(i);
334
    }
335
    
336
    public void removeFileToCopy(File file) {
337
        auxFiles.remove(file);        
338
    }
339

    
340
    public void clearFilesToCopy() {
341
        auxFiles.clear();
342
    }
343

    
344
    public List<File> getFilesToCopy() {
345
        return auxFiles;
346
    }
347

    
348
    public boolean removeInstallFolder(File folder) {
349
        DeleteFile delete = new DeleteFile();
350
        boolean success = delete.delete(folder);
351
        setAntScript(null);
352
        clearFilesToCopy();
353
        return success;
354
    }
355

    
356
    public boolean removeFilesFolder(File folder) {
357
        DeleteFile delete = new DeleteFile();
358
        return delete.delete(folder);
359
    }
360
    
361
    public boolean matchID(String string) {
362
        String code = this.getID();
363
        String[] stringParts = string.split("#");
364

    
365
        if (stringParts[0] == code) {
366
            return true;
367
        } else
368
            if (stringParts[0] + "#" + stringParts[1] == code) {
369
                return true;
370
            } else
371
                if (string == code) {
372
                    return true;
373
                } else
374
                    return false;
375

    
376
    }
377
    
378
        public Dependencies getDependencies() {
379
                return this.dependencies;
380
        }
381
        
382
        public void setDependencies(Dependencies dependencies) {
383
                this.dependencies = dependencies;
384
        }
385

    
386
        public void setDependencies(String dependencies) {
387
                if( dependencies == null ) {
388
                        this.dependencies = null;
389
                } else {
390
                        this.dependencies = new DefaultDependencies().parse(dependencies);
391
                }
392
        }
393

    
394
        public boolean equals(Object obj) {
395
                PackageInfo other; 
396
                try {
397
                        other = (PackageInfo) obj;
398
                } catch(Exception e) {
399
                        return false;
400
                }
401
                if( !code.equalsIgnoreCase(other.getCode())) {
402
                        return false;
403
                }
404
                if( !version.check("=", other.getVersion())) {
405
                        return false;
406
                }
407
                if( !operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
408
                        return false;
409
                }
410
                if( !gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) { 
411
                        return false;
412
                }
413
                if( !state.equalsIgnoreCase(other.getState())) {
414
                        return false;
415
                }
416
                if( !architecture.equalsIgnoreCase(other.getArchitecture())) {
417
                        return false;
418
                }
419
                if( !javaVM.equalsIgnoreCase(other.getJavaVM())) {
420
                        return false;
421
                }
422
                if( !type.equalsIgnoreCase(other.getType())) {
423
                        return false;
424
                }
425
                if( official!=other.isOfficial()) {
426
                        return false;
427
                }
428
                return true;
429
        }
430

    
431
        
432
        
433
}