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 / DefaultPackageInfo.java @ 37601

History | View | Annotate | Download (13 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.MalformedURLException;
33
import java.net.URL;
34
import java.util.ArrayList;
35
import java.util.List;
36

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

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

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

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

    
70
        private String owner = "";
71
        private URL ownerURL = null;
72
        private URL sources = null;
73
        private String gvSIGVersion = "";
74

    
75
        // private URL defaultDownloadURL = null;
76
        private String defaultDownloadURL = null;
77

    
78
        private String modelVersion = "1.0.1";
79
        private Dependencies dependencies = null;
80
        // private Set<String> categories = null;
81
        private List<String> categories = null;
82

    
83
        private URL webURL = null;
84

    
85
        public DefaultPackageInfo() {
86
                super();
87
                auxFiles = new ArrayList<File>();
88
                this.version = new DefaultVersion().parse("0.0.1");
89
                this.dependencies = new DefaultDependencies();
90
                this.categories = new ArrayList<String>();
91
        }
92

    
93
        public String getCode() {
94
                return code;
95
        }
96

    
97
        public String getID() {
98
                String id = this.getCode() + "#" + this.getVersion() + "#"
99
                                + this.getBuild() + "#" + this.getOperatingSystem() + "#"
100
                                + this.getArchitecture();
101
                return id;
102
        }
103

    
104
        public String getName() {
105
                return name;
106
        }
107

    
108
        public String getDescription() {
109
                return description;
110
        }
111

    
112
        public Version getVersion() {
113
                return version;
114
        }
115

    
116
        public int getBuild() {
117
                return this.version.getBuild();
118
        }
119

    
120
        public String getState() {
121
                return state;
122
        }
123

    
124
        public boolean isOfficial() {
125
                return official;
126
        }
127

    
128
        public void setCode(String code) {
129
                this.code = code;
130
        }
131

    
132
        public void setName(String name) {
133
                this.name = name;
134
        }
135

    
136
        public void setDescription(String description) {
137
                this.description = description;
138
        }
139

    
140
        public void setVersion(String version) {
141
                if (version == null) {
142
                        return;
143
                }
144
                int prev = this.version.getBuild();
145
                this.version.parse(version);
146
                int curr = this.version.getBuild();
147
                if (prev != 0 && curr == 0) {
148
                        this.version.setBuild(prev);
149
                }
150
        }
151

    
152
        public void setVersion(Version version) {
153
                try {
154
                        int prev = this.version.getBuild();
155
                        this.version = (Version) version.clone();
156
                        int curr = this.version.getBuild();
157
                        if (prev != 0 && curr == 0) {
158
                                this.version.setBuild(prev);
159
                        }
160
                } catch (CloneNotSupportedException e) {
161
                        throw new RuntimeException(e);
162
                }
163
        }
164

    
165
        public void setBuild(int build) {
166
                this.version.setBuild(build);
167
        }
168

    
169
        public void setState(String state) {
170
                this.state = state;
171
        }
172

    
173
        public void setOfficial(boolean official) {
174
                this.official = official;
175
        }
176

    
177
        public String getOperatingSystem() {
178
                return operatingSystem;
179
        }
180

    
181
        public void setOperatingSystem(String operatingSystem) {
182
                this.operatingSystem = operatingSystem;
183
        }
184

    
185
        public String getArchitecture() {
186
                return architecture;
187
        }
188

    
189
        public void setArchitecture(String architecture) {
190
                this.architecture = architecture;
191
        }
192

    
193
        public String getJavaVM() {
194
                return javaVM;
195
        }
196

    
197
        public void setJavaVM(String javaVM) {
198
                this.javaVM = javaVM;
199
        }
200

    
201
        public String getAntScript() {
202
                return antScript;
203
        }
204

    
205
        public void setAntScript(String antScript) {
206
                this.antScript = antScript;
207
        }
208

    
209
        public String getType() {
210
                return type;
211
        }
212

    
213
        public void setType(String type) {
214
                this.type = type;
215
        }
216

    
217
        public String getGvSIGVersion() {
218
                return gvSIGVersion;
219
        }
220

    
221
        public void setGvSIGVersion(String gvSIGVersion) {
222
                this.gvSIGVersion = gvSIGVersion;
223
        }
224

    
225
        public URL getDownloadURL() {
226
                URL url = null;
227
                if (defaultDownloadURL != null) {
228
                        try {
229
                                url = new URL(defaultDownloadURL);
230
                        } catch (MalformedURLException e) {
231
                                // TODO Auto-generated catch block
232
                                e.printStackTrace();
233
                        }
234
                }
235
                return url;
236
        }
237

    
238
        public String getDownloadURLAsString() {
239
                return this.defaultDownloadURL;
240
        }
241

    
242
        public URL getDownloadURL(URL baseURL) {
243
                String URLString = baseURL.toString();
244

    
245
                if (!URLString.endsWith("/")) {
246
                        URLString += "/";
247
                }
248
                URLString += ("dists/" + getGvSIGVersion() + "/" + "packages.gvspki");
249
                try {
250
                        return new URL(URLString);
251
                } catch (MalformedURLException e) {
252
                        // TODO Auto-generated catch block
253
                        e.printStackTrace();
254
                        return null;
255
                }
256
        }
257

    
258
        public void setDownloadURL(URL defaultDownloadURL) {
259
                this.defaultDownloadURL = defaultDownloadURL.toString();
260
        }
261

    
262
        public void setDownloadURL(String defaultDownloadURL) {
263
                this.defaultDownloadURL = defaultDownloadURL;
264
        }
265

    
266
        public String getModelVersion() {
267
                return modelVersion;
268
        }
269

    
270
        public void setModelVersion(String modelVersion) {
271
                this.modelVersion = modelVersion;
272
        }
273

    
274
        public String getOwner() {
275
                return owner;
276
        }
277

    
278
        public void setOwner(String owner) {
279
                this.owner = owner;
280
        }
281

    
282
        public URL getOwnerURL() {
283
                return ownerURL;
284
        }
285

    
286
        public void setOwnerURL(URL sources) {
287
                this.ownerURL = sources;
288
        }
289

    
290
        public URL getSourcesURL() {
291
                return sources;
292
        }
293

    
294
        public void setSourcesURL(URL sources) {
295
                this.sources = sources;
296
        }
297

    
298
        @Override
299
        public String toString() {
300
                StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
301

    
302
                append(buffer, InstallerInfoTags.CODE, getCode());
303
                append(buffer, InstallerInfoTags.NAME, getName());
304
                append(buffer, InstallerInfoTags.DESCRIPTION, getDescription());
305
                append(buffer, InstallerInfoTags.GVSIG_VERSION, getGvSIGVersion());
306
                append(buffer, InstallerInfoTags.VERSION, getVersion());
307
                append(buffer, InstallerInfoTags.BUILD, getBuild());
308
                append(buffer, InstallerInfoTags.OS, getOperatingSystem());
309
                append(buffer, InstallerInfoTags.ARCHITECTURE, getArchitecture());
310
                append(buffer, InstallerInfoTags.JVM, getJavaVM());
311
                append(buffer, InstallerInfoTags.DOWNLOAD_URL, getDownloadURL());
312
                append(buffer, InstallerInfoTags.STATE, getState());
313
                append(buffer, InstallerInfoTags.OFFICIAL, isOfficial());
314
                append(buffer, InstallerInfoTags.TYPE, getType());
315
                append(buffer, InstallerInfoTags.MODEL_VERSION, getModelVersion());
316
                append(buffer, InstallerInfoTags.OWNER, getOwner());
317
                append(buffer, InstallerInfoTags.OWNER_URL, getOwnerURL());
318
                append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
319
                append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
320
                append(buffer, InstallerInfoTags.WEB_URL, getWebURL());
321
                append(buffer, InstallerInfoTags.CATEGORIES, getCategories());
322

    
323
                return buffer.append(')').toString();
324
        }
325

    
326
        public String toStringCompact() {
327
                // type code version state os arch jvm dep
328
                return String
329
                                .format(
330
                                                "%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
331
                                                this.type, this.code, this.version, this.state,
332
                                                this.operatingSystem, this.architecture, this.javaVM,
333
                                                this.dependencies);
334
        }
335

    
336
        private DefaultPackageInfo append(StringBuffer buffer, String key,
337
                        Object value) {
338
                buffer.append("\n\t").append(key).append(": ").append(
339
                                value == null ? "" : value);
340
                return this;
341
        }
342

    
343
        @Override
344
        public Object clone() throws CloneNotSupportedException {
345
                DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
346
                clone.auxFiles = new ArrayList<File>(auxFiles);
347
                return clone;
348
        }
349

    
350
        public File downloadFile() throws InstallPackageServiceException {
351
                return this.downloadFile(null);
352
        }
353

    
354
        public class FileDownloadException extends InstallPackageServiceException {
355

    
356
                private static final long serialVersionUID = 8640183295766490512L;
357

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

    
360
                private static final String KEY = "_File_XurlX_download_error";
361

    
362
                public FileDownloadException(URL url, IOException e) {
363
                        super(message, e, KEY, serialVersionUID);
364
                        setValue("url", url.toString());
365
                }
366

    
367
        }
368

    
369
        public File downloadFile(SimpleTaskStatus taskStatus)
370
                        throws InstallPackageServiceException {
371
                Download download = new Download(taskStatus);
372
                try {
373
                        return download.downloadFile(this.getDownloadURL(), null);
374
                } catch (IOException e) {
375
                        throw new FileDownloadException(this.getDownloadURL(), e);
376
                }
377
        }
378

    
379
        public void addFileToCopy(File file) {
380
                auxFiles.add(file);
381
        }
382

    
383
        public File getFileToCopy(int i) {
384
                return auxFiles.get(i);
385
        }
386

    
387
        public void removeFileToCopy(File file) {
388
                auxFiles.remove(file);
389
        }
390

    
391
        public void clearFilesToCopy() {
392
                auxFiles.clear();
393
        }
394

    
395
        public List<File> getFilesToCopy() {
396
                return auxFiles;
397
        }
398

    
399
        public boolean removeInstallFolder(File folder) {
400
                DeleteFile delete = new DeleteFile();
401
                boolean success = delete.delete(folder);
402
                setAntScript(null);
403
                clearFilesToCopy();
404
                return success;
405
        }
406

    
407
        public boolean removeFilesFolder(File folder) {
408
                DeleteFile delete = new DeleteFile();
409
                return delete.delete(folder);
410
        }
411

    
412
        public boolean matchID(String string) {
413
                String id = this.getID();
414
                String[] stringParts = string.split("#");
415

    
416
                if (stringParts.length == 1) {
417

    
418
                        if (stringParts[0].equals(this.getCode())) {
419
                                return true;
420
                        } else {
421
                                return false;
422
                        }
423
                } else {
424
                        if (stringParts.length == 2) {
425
                                if ((stringParts[0] + stringParts[1])
426
                                                .equals((this.getCode() + this.getVersion()))) {
427
                                        return true;
428
                                } else {
429
                                        return true;
430
                                }
431
                        } else {
432
                                if (string.equals(id)) {
433
                                        return true;
434
                                } else {
435
                                        return false;
436
                                }
437
                        }
438
                }
439

    
440
        }
441

    
442
        public Dependencies getDependencies() {
443
                return this.dependencies;
444
        }
445

    
446
        public void setDependencies(Dependencies dependencies) {
447
                this.dependencies = dependencies;
448
        }
449

    
450
        public void setDependencies(String dependencies) {
451
                if (dependencies == null) {
452
                        this.dependencies = null;
453
                } else {
454
                        this.dependencies = new DefaultDependencies().parse(dependencies);
455
                }
456
        }
457

    
458
        @Override
459
        public boolean equals(Object obj) {
460
                PackageInfo other;
461
                try {
462
                        other = (PackageInfo) obj;
463
                } catch (Exception e) {
464
                        return false;
465
                }
466
                if (!code.equalsIgnoreCase(other.getCode())) {
467
                        return false;
468
                }
469
                if (!version.check("=", other.getVersion())) {
470
                        return false;
471
                }
472
                if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
473
                        return false;
474
                }
475
                if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
476
                        return false;
477
                }
478
                if (!state.equalsIgnoreCase(other.getState())) {
479
                        return false;
480
                }
481
                if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
482
                        return false;
483
                }
484
                if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
485
                        return false;
486
                }
487
                if (!type.equalsIgnoreCase(other.getType())) {
488
                        return false;
489
                }
490
                if (official != other.isOfficial()) {
491
                        return false;
492
                }
493
                return true;
494
        }
495

    
496
        public URL getWebURL() {
497
                return webURL;
498
        }
499

    
500
        public void setWebURL(URL webURL) {
501
                this.webURL = webURL;
502
        }
503

    
504
        public List<String> getCategories() {
505
                return this.categories;
506
        }
507

    
508
        public void setCategories(List<String> categoriesList) {
509
                for (int i = 0; i < categoriesList.size(); i++) {
510
                        if (!this.categories.contains(categoriesList.get(i))) {
511
                                this.categories.add(categoriesList.get(i));
512
                        }
513
                }
514
        }
515

    
516
        public String getCategoriesAsString() {
517
                String categoriesString = "";
518
                for (int i = 0; i < this.categories.size(); i++) {
519
                        categoriesString += this.categories.get(i) + ", ";
520
                }
521
                return categoriesString;
522
        }
523

    
524
        public void addCategoriesAsString(String categoriesString) {
525
                categoriesString.trim();
526
                String[] cadena = categoriesString.split(",");
527

    
528
                for (int i = 0; i < cadena.length; i++) {
529
                        String trimCadena = cadena[i].trim();
530
                        if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
531
                                this.categories.add(trimCadena);
532
                        }
533
                }
534

    
535
        }
536

    
537
}