Statistics
| Revision:

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

History | View | Annotate | Download (12.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
    private URL webURL = null;
79

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

    
87
    public String getCode() {
88
        return code;
89
    }
90

    
91
    public String getID() {
92
        String id =
93
            this.getCode() + "#" + this.getVersion() + "#" + this.getBuild() + "#" + this.getOperatingSystem() + "#" + this.getArchitecture(); 
94
        return id;
95
    }
96

    
97
    public String getName() {
98
        return name;
99
    }
100

    
101
    public String getDescription() {
102
        return description;
103
    }
104

    
105
    public Version getVersion() {
106
        return version;
107
    }
108

    
109
    public int getBuild() {
110
        return this.version.getBuild();
111
    }
112

    
113
    public String getState() {
114
        return state;
115
    }
116

    
117
    public boolean isOfficial() {
118
        return official;
119
    }
120

    
121
    public void setCode(String code) {
122
        this.code = code;
123
    }
124

    
125
    public void setName(String name) {
126
        this.name = name;
127
    }
128

    
129
    public void setDescription(String description) {
130
        this.description = description;
131
    }
132

    
133
    public void setVersion(String version) {
134
        if (version == null) {
135
            return;
136
        }
137
        int prev = this.version.getBuild();
138
        this.version.parse(version);
139
        int curr = this.version.getBuild();
140
        if (prev != 0 && curr == 0) {
141
            this.version.setBuild(prev);
142
        }
143
    }
144

    
145
    public void setVersion(Version version) {
146
        try {
147
            int prev = this.version.getBuild();
148
            this.version = (Version) version.clone();
149
            int curr = this.version.getBuild();
150
            if (prev != 0 && curr == 0) {
151
                this.version.setBuild(prev);
152
            }
153
        } catch (CloneNotSupportedException e) {
154
            throw new RuntimeException(e);
155
        }
156
    }
157

    
158
    public void setBuild(int build) {
159
        this.version.setBuild(build);
160
    }
161

    
162
    public void setState(String state) {
163
        this.state = state;
164
    }
165

    
166
    public void setOfficial(boolean official) {
167
        this.official = official;
168
    }
169

    
170
    public String getOperatingSystem() {
171
        return operatingSystem;
172
    }
173

    
174
    public void setOperatingSystem(String operatingSystem) {
175
        this.operatingSystem = operatingSystem;
176
    }
177

    
178
    public String getArchitecture() {
179
        return architecture;
180
    }
181

    
182
    public void setArchitecture(String architecture) {
183
        this.architecture = architecture;
184
    }
185

    
186
    public String getJavaVM() {
187
        return javaVM;
188
    }
189

    
190
    public void setJavaVM(String javaVM) {
191
        this.javaVM = javaVM;
192
    }
193

    
194
    public String getAntScript() {
195
        return antScript;
196
    }
197

    
198
    public void setAntScript(String antScript) {
199
        this.antScript = antScript;
200
    }
201

    
202
    public String getType() {
203
        return type;
204
    }
205

    
206
    public void setType(String type) {
207
        this.type = type;
208
    }
209

    
210
    public String getGvSIGVersion() {
211
        return gvSIGVersion;
212
    }
213

    
214
    public void setGvSIGVersion(String gvSIGVersion) {
215
        this.gvSIGVersion = gvSIGVersion;
216
    }
217

    
218
    public URL getDownloadURL() {
219
        return defaultDownloadURL;
220
    }
221

    
222
    public void setDownloadURL(URL defaultDownloadURL) {
223
        this.defaultDownloadURL = defaultDownloadURL;
224
    }
225

    
226
    public String getModelVersion() {
227
        return modelVersion;
228
    }
229

    
230
    public void setModelVersion(String modelVersion) {
231
        this.modelVersion = modelVersion;
232
    }
233

    
234
    public String getOwner() {
235
        return owner;
236
    }
237

    
238
    public void setOwner(String owner) {
239
        this.owner = owner;
240
    }
241

    
242
    public URL getSourcesURL() {
243
        return sources;
244
    }
245

    
246
    public void setSourcesURL(URL sources) {
247
        this.sources = sources;
248
    }
249

    
250
    @Override
251
    public String toString() {
252
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
253

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

    
273
        return buffer.append(')').toString();
274
    }
275

    
276
    public String toStringCompact() {
277
        // type code version state os arch jvm dep
278
        return String
279
            .format(
280
                "%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
281
                this.type, this.code, this.version, this.state,
282
                this.operatingSystem, this.architecture, this.javaVM,
283
                this.dependencies);
284
    }
285

    
286
    private DefaultPackageInfo append(StringBuffer buffer, String key,
287
        Object value) {
288
        buffer.append("\n\t").append(key).append(": ").append(
289
            value == null ? "" : value);
290
        return this;
291
    }
292

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

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

    
304
    public class FileDownloadException extends InstallPackageServiceException {
305

    
306
        private static final long serialVersionUID = 8640183295766490512L;
307

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

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

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

    
317
    }
318

    
319
    public File downloadFile(SimpleTaskStatus taskStatus)
320
        throws InstallPackageServiceException {
321
        Download download = new Download(taskStatus);
322
        try {
323
            return download.downloadFile(this.getDownloadURL(), null);
324
        } catch (IOException e) {
325
            throw new FileDownloadException(this.getDownloadURL(), e);
326
        }
327
    }
328

    
329
    public void addFileToCopy(File file) {
330
        auxFiles.add(file);
331
    }
332

    
333
    public File getFileToCopy(int i) {
334
        return auxFiles.get(i);
335
    }
336

    
337
    public void removeFileToCopy(File file) {
338
        auxFiles.remove(file);
339
    }
340

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

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

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

    
357
    public boolean removeFilesFolder(File folder) {
358
        DeleteFile delete = new DeleteFile();
359
        return delete.delete(folder);
360
    }
361

    
362
    public boolean matchID(String string) {
363
        String id = this.getID();
364
        String[] stringParts = string.split("#");
365

    
366
        if (stringParts.length == 1) {
367

    
368
            if (stringParts[0].equals(this.getCode())) {
369
                return true;
370
            } else {
371
                return false;
372
            }
373
        } else {
374
            if (stringParts.length == 2) {
375
                if ((stringParts[0] + stringParts[1]).equals((this.getCode()
376
                    + this.getVersion()))) {
377
                    return true;
378
                } else {
379
                    return true;
380
                }
381
            } else {
382
                if (string.equals(id)) {
383
                    return true;
384
                } else {
385
                    return false;
386
                }
387
            }
388
        }
389

    
390
    }
391

    
392
    public Dependencies getDependencies() {
393
        return this.dependencies;
394
    }
395

    
396
    public void setDependencies(Dependencies dependencies) {
397
        this.dependencies = dependencies;
398
    }
399

    
400
    public void setDependencies(String dependencies) {
401
        if (dependencies == null) {
402
            this.dependencies = null;
403
        } else {
404
            this.dependencies = new DefaultDependencies().parse(dependencies);
405
        }
406
    }
407

    
408
    public boolean equals(Object obj) {
409
        PackageInfo other;
410
        try {
411
            other = (PackageInfo) obj;
412
        } catch (Exception e) {
413
            return false;
414
        }
415
        if (!code.equalsIgnoreCase(other.getCode())) {
416
            return false;
417
        }
418
        if (!version.check("=", other.getVersion())) {
419
            return false;
420
        }
421
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
422
            return false;
423
        }
424
        if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
425
            return false;
426
        }
427
        if (!state.equalsIgnoreCase(other.getState())) {
428
            return false;
429
        }
430
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
431
            return false;
432
        }
433
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
434
            return false;
435
        }
436
        if (!type.equalsIgnoreCase(other.getType())) {
437
            return false;
438
        }
439
        if (official != other.isOfficial()) {
440
            return false;
441
        }
442
        return true;
443
    }
444

    
445
    public URL getWebURL() {
446
        return webURL;
447
    }
448

    
449
    public void setWebURL(URL webURL) {
450
        this.webURL = webURL;
451
    }
452

    
453
}