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 32269 jpiera
/* gvSIG. Geographic Information System of the Valencian Government
2 33729 cordinyana
 *
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 32269 jpiera
23
/*
24 33729 cordinyana
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27
28 32269 jpiera
package org.gvsig.installer.lib.impl;
29
30
import java.io.File;
31 34925 nfrancisco
import java.io.IOException;
32 33982 cordinyana
import java.net.URL;
33 32290 jpiera
import java.util.ArrayList;
34
import java.util.List;
35 32269 jpiera
36 35979 jjdelcerro
import org.gvsig.installer.lib.api.Dependencies;
37 33686 cordinyana
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 35979 jjdelcerro
import org.gvsig.installer.lib.api.PackageInfo;
42
import org.gvsig.installer.lib.api.Version;
43 34925 nfrancisco
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
44 34018 cordinyana
import org.gvsig.installer.lib.impl.info.InstallerInfoTags;
45 35486 nfrancisco
import org.gvsig.installer.lib.impl.utils.DeleteFile;
46 34925 nfrancisco
import org.gvsig.installer.lib.impl.utils.Download;
47
import org.gvsig.tools.task.SimpleTaskStatus;
48 32269 jpiera
49
/**
50
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
51
 */
52 33729 cordinyana
public class DefaultPackageInfo implements PackageInfo {
53
54 33686 cordinyana
    private String code = null;
55
    private String name = null;
56
    private String description = null;
57 35979 jjdelcerro
    private Version version = null;
58
//    private int build = 0;
59 33686 cordinyana
    private boolean official;
60 34974 nfrancisco
    private List<File> auxFiles = null;
61 33686 cordinyana
    private String antScript = null;
62 35979 jjdelcerro
    private String type = "unknow";
63 33729 cordinyana
64 33686 cordinyana
    private String state = STATE.DEVEL;
65 33729 cordinyana
    private String operatingSystem = OS.ALL;
66
    private String architecture = ARCH.ALL;
67 33686 cordinyana
    private String javaVM = JVM.J1_5;
68 32333 jpiera
69 35966 nfrancisco
    private String owner = "";
70
    private URL sources = null;
71 35979 jjdelcerro
    private String gvSIGVersion="";
72
73 33982 cordinyana
    private URL defaultDownloadURL = null;
74
75 35966 nfrancisco
    private String modelVersion = "1.0.1";
76 35979 jjdelcerro
    private Dependencies dependencies = null;
77 34005 cordinyana
78 33729 cordinyana
    public DefaultPackageInfo() {
79
        super();
80 34974 nfrancisco
        auxFiles = new ArrayList<File>();
81 35979 jjdelcerro
        this.version = new DefaultVersion().parse("0.0.1");
82
        this.dependencies = new DefaultDependencies();
83 33729 cordinyana
    }
84 32269 jpiera
85 33686 cordinyana
    public String getCode() {
86 33729 cordinyana
        return code;
87
    }
88 32269 jpiera
89 35189 nfrancisco
    public String getID() {
90
        String id =
91
            this.getCode() + "#" + this.getVersion() + "#" + this.getBuild();
92
        return id;
93
    }
94
95 33729 cordinyana
    public String getName() {
96
        return name;
97
    }
98 32269 jpiera
99 33729 cordinyana
    public String getDescription() {
100
        return description;
101
    }
102 32269 jpiera
103 35979 jjdelcerro
    public Version getVersion() {
104 33729 cordinyana
        return version;
105
    }
106 32269 jpiera
107 33729 cordinyana
    public int getBuild() {
108 35979 jjdelcerro
        return this.version.getBuild();
109 33729 cordinyana
    }
110 32269 jpiera
111 33729 cordinyana
    public String getState() {
112
        return state;
113
    }
114 32269 jpiera
115 33729 cordinyana
    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 36015 fdiaz
            int prev = this.version.getBuild();
133 35979 jjdelcerro
        this.version.parse(version);
134 36015 fdiaz
                int curr = this.version.getBuild();
135
                if (prev!=0 && curr==0){
136
                        this.version.setBuild(prev);
137
                }
138 33729 cordinyana
    }
139
140 35979 jjdelcerro
    public void setVersion(Version version) {
141
        try {
142 36015 fdiaz
                int prev = this.version.getBuild();
143 35979 jjdelcerro
                        this.version = (Version) version.clone();
144 36015 fdiaz
                        int curr = this.version.getBuild();
145
                        if (prev!=0 && curr==0){
146
                                this.version.setBuild(prev);
147
                        }
148 35979 jjdelcerro
                } catch (CloneNotSupportedException e) {
149
                        throw new RuntimeException(e);
150
                }
151
    }
152
153 33729 cordinyana
    public void setBuild(int build) {
154 35979 jjdelcerro
            this.version.setBuild(build);
155 33729 cordinyana
    }
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 33686 cordinyana
    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 33729 cordinyana
    public String getAntScript() {
190
        return antScript;
191
    }
192 32296 jpiera
193 34974 nfrancisco
    public void setAntScript(String antScript) {
194 33729 cordinyana
        this.antScript = antScript;
195
    }
196 32296 jpiera
197 33729 cordinyana
    public String getType() {
198
        return type;
199
    }
200 32296 jpiera
201 33729 cordinyana
    public void setType(String type) {
202
        this.type = type;
203 33686 cordinyana
    }
204 32400 jpiera
205 33686 cordinyana
    public String getGvSIGVersion() {
206
        return gvSIGVersion;
207
    }
208
209
    public void setGvSIGVersion(String gvSIGVersion) {
210
        this.gvSIGVersion = gvSIGVersion;
211
    }
212
213 33982 cordinyana
    public URL getDownloadURL() {
214
        return defaultDownloadURL;
215
    }
216
217
    public void setDownloadURL(URL defaultDownloadURL) {
218
        this.defaultDownloadURL = defaultDownloadURL;
219
    }
220
221 34005 cordinyana
    public String getModelVersion() {
222
        return modelVersion;
223
    }
224
225
    public void setModelVersion(String modelVersion) {
226
        this.modelVersion = modelVersion;
227
    }
228 35979 jjdelcerro
229 35966 nfrancisco
    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 34018 cordinyana
    @Override
246
    public String toString() {
247 34022 cordinyana
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
248 34018 cordinyana
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 35966 nfrancisco
        append(buffer, InstallerInfoTags.OWNER, getOwner());
264
        append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
265
        append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
266 35979 jjdelcerro
267 35966 nfrancisco
268 34022 cordinyana
        return buffer.append(')').toString();
269 34018 cordinyana
    }
270
271 35979 jjdelcerro
    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 34018 cordinyana
    private DefaultPackageInfo append(StringBuffer buffer, String key,
286
        Object value) {
287 34925 nfrancisco
        buffer.append("\n\t").append(key).append(": ").append(
288
            value == null ? "" : value);
289 34018 cordinyana
        return this;
290
    }
291
292 34444 cordinyana
    @Override
293
    public Object clone() throws CloneNotSupportedException {
294
        DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
295 34974 nfrancisco
        clone.auxFiles = new ArrayList<File>(auxFiles);
296 34444 cordinyana
        return clone;
297
    }
298 34925 nfrancisco
299 34967 nfrancisco
    public File downloadFile() throws InstallPackageServiceException {
300 34925 nfrancisco
        return this.downloadFile(null);
301
    }
302 34967 nfrancisco
303 35947 nfrancisco
    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 35966 nfrancisco
318 34925 nfrancisco
    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 35979 jjdelcerro
                throw new FileDownloadException(this.getDownloadURL(), e);
325 34925 nfrancisco
        }
326
    }
327 35979 jjdelcerro
328 35486 nfrancisco
    public void addFileToCopy(File file) {
329
        auxFiles.add(file);
330
    }
331 35979 jjdelcerro
332 35486 nfrancisco
    public File getFileToCopy(int i) {
333
        return auxFiles.get(i);
334
    }
335 35979 jjdelcerro
336 35486 nfrancisco
    public void removeFileToCopy(File file) {
337 35979 jjdelcerro
        auxFiles.remove(file);
338 35486 nfrancisco
    }
339 34925 nfrancisco
340 35486 nfrancisco
    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 35979 jjdelcerro
361 35966 nfrancisco
    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 35979 jjdelcerro
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 32269 jpiera
}