Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / packageutils / impl / DefaultPackageInfo.java @ 724

History | View | Annotate | Download (19.8 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.tools.packageutils.impl;
29

    
30
import java.io.File;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33
import java.security.InvalidParameterException;
34
import java.text.MessageFormat;
35
import java.util.ArrayList;
36
import java.util.HashMap;
37
import java.util.List;
38
import java.util.Map;
39

    
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.exception.BaseException;
42
import org.gvsig.installer.lib.api.Dependencies;
43
import org.gvsig.tools.packageutils.PackageInfo;
44
import org.gvsig.tools.packageutils.PackageManager;
45
import org.gvsig.tools.packageutils.PackageManager.ARCH;
46
import org.gvsig.tools.packageutils.PackageManager.JVM;
47
import org.gvsig.tools.packageutils.PackageManager.OS;
48
import org.gvsig.tools.packageutils.PackageManager.STATE;
49
import org.gvsig.installer.lib.api.Version;
50
import org.gvsig.tools.task.SimpleTaskStatus;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public class DefaultPackageInfo implements PackageInfo {
58

    
59
    private static final Logger logger = LoggerFactory
60
        .getLogger(DefaultPackageInfo.class);
61

    
62
        public static interface PACKAGE_FILE_NAME_FIELDS {
63
                static final int GVSIG_VERSION = 0;
64
                static final int NAME = 1;
65
                static final int VERSION = 2;
66
                static final int BUILD = 3;
67
                static final int STATE = 4;
68
                static final int OS = 5;
69
                static final int ARCH = 6;
70
                static final int JVM = 7;
71
        }
72
        private static final String PACKAGE_NAME_FORMAT = "gvSIG-desktop-{0}-{1}-{2}-{4}-{5}-{6}-{7}.gvspkg";
73
        
74
        private static final String DEFAULT_MODEL_VERSION = "1.0.1";
75
        private static final String DEFAULT_TYPE = "unknow";
76
        
77
    private String code = null;
78
    private String name = null;
79
    private String description = null;
80
    private Version version = null;
81
    private boolean official;
82
    private String type = DEFAULT_TYPE;
83
    private boolean broken = false;
84

    
85
    private String state = STATE.DEVEL;
86
    private String operatingSystem = OS.ALL;
87
    private String architecture = ARCH.ALL;
88
    private String javaVM = JVM.J1_5;
89

    
90
    private String owner = "";
91
    private URL ownerURL = null;
92
    private URL sources = null;
93
    private Version applicationVersion = null;
94

    
95
    private String defaultDownloadURL = null;
96

    
97
    private String modelVersion = DEFAULT_MODEL_VERSION;
98
    private Dependencies dependencies = null;
99
    private List categories = null;
100

    
101
    private URL webURL = null;
102
    
103
    private String postInstallScript = null;
104
    
105
    private Map aditionalProperties = null;
106

    
107
    public DefaultPackageInfo() {
108
        super();
109
        PackageManager manager = ToolsLocator.getPackageManager();
110
        this.version = manager.createVersion().parse("0.0.1");
111
        this.applicationVersion = manager.createVersion().parse("0.0.1");
112
        this.dependencies = manager.createDependencies();
113
        this.categories = new ArrayList();
114
        this.aditionalProperties = new HashMap();
115
    }
116

    
117
    public String getCode() {
118
        return code;
119
    }
120

    
121
    public String getID() {
122
        String id =
123
            this.getCode() + "#" + this.getVersion() 
124
                + "#" + this.getOperatingSystem() + "#"
125
                + this.getArchitecture();
126
        return id;
127
    }
128

    
129
    public String getName() {
130
        return name;
131
    }
132

    
133
    public String getDescription() {
134
        return description;
135
    }
136

    
137
    public Version getVersion() {
138
        return version;
139
    }
140
    
141
    public String getState() {
142
        return state;
143
    }
144

    
145
    public boolean isOfficial() {
146
        return official;
147
    }
148

    
149
    public void setCode(String code) {
150
        this.code = code;
151
    }
152

    
153
    public void setName(String name) {
154
        this.name = name;
155
    }
156

    
157
    public void setDescription(String description) {
158
        this.description = description;
159
    }
160

    
161
    public void setVersion(Version version) {
162
        try {
163
            this.version = (Version) version.clone();
164
        } catch (CloneNotSupportedException e) {
165
            throw new RuntimeException(e);
166
        }
167
    }
168

    
169
    public void setState(String state) {
170
            if( isEmptyString(state) ) {
171
                    this.state = STATE.DEVEL;
172
            } else {
173
                    this.state = state;
174
            }
175
    }
176

    
177
    public void setOfficial(boolean official) {
178
        this.official = official;
179
    }
180

    
181
    public String getOperatingSystem() {
182
        return operatingSystem;
183
    }
184

    
185
    public void setOperatingSystem(String operatingSystem) {
186
            if( isEmptyString(operatingSystem) ) {
187
                    this.operatingSystem = OS.ALL;
188
            } else {
189
                    this.operatingSystem = operatingSystem;
190
            }
191
    }
192

    
193
    public String getArchitecture() {
194
        return architecture;
195
    }
196

    
197
    public void setArchitecture(String architecture) {
198
            if( isEmptyString(architecture) ) {
199
                    this.architecture = ARCH.ALL;
200
            } else {
201
                    this.architecture = architecture;
202
            }
203
    }
204

    
205
    public String getJavaVM() {
206
        return javaVM;
207
    }
208

    
209
    public void setJavaVM(String javaVM) {
210
            if( isEmptyString(javaVM) ) {
211
                    this.javaVM = JVM.J1_5;
212
            } else {
213
                    this.javaVM = javaVM;
214
            }
215
    }
216

    
217
    public String getType() {
218
        return type;
219
    }
220

    
221
    public void setType(String type) {
222
            if( isEmptyString(type) ) {
223
                    this.type = DEFAULT_TYPE;
224
            } else {
225
                    this.type = type;
226
            }
227
    }
228

    
229
    public Version getApplicationVersion() {
230
        return applicationVersion;
231
    }
232

    
233
    public void setApplicationVersion(Version applicationVersion) {
234
        this.applicationVersion = applicationVersion;
235
    }
236

    
237
    public URL getDownloadURL() {
238
            if( this.defaultDownloadURL == null ) {
239
                    return null;
240
            }
241
            try {
242
            return new URL(this.defaultDownloadURL);
243
        } catch (MalformedURLException e) {
244
                return null;
245
//            throw new RuntimeException(
246
//                "Error converting to URL the package download url "+ this.defaultDownloadURL,
247
//                e);
248
        }
249
    }
250

    
251
    public String getDownloadURLAsString() {
252
        return this.defaultDownloadURL;
253
    }
254

    
255
    public void setDownloadURL(URL defaultDownloadURL) {
256
        this.defaultDownloadURL = defaultDownloadURL.toString();
257
    }
258

    
259
    public void setDownloadURL(String defaultDownloadURL) {
260
        this.defaultDownloadURL = defaultDownloadURL;
261
    }
262

    
263
    public String getModelVersion() {
264
        return modelVersion;
265
    }
266

    
267
    public void setModelVersion(String modelVersion) {
268
            if( isEmptyString(modelVersion) ) {
269
                    this.modelVersion = DEFAULT_MODEL_VERSION;
270
            } else {
271
                    this.modelVersion = modelVersion;
272
            }
273
    }
274

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

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

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

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

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

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

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

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

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

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

    
342
    public String toStringCompact() {
343
        // type code version state os arch jvm dep
344
            return         strformat(this.type,-8,-4) + " " + 
345
                        strformat(this.code,-40,0) + " " + 
346
                        strformat(this.version,-20,-20) + " " + 
347
                        strformat(this.state,-5,-5) + " " +
348
                        strformat(this.operatingSystem,-5,-5) + " " + 
349
                        strformat(this.architecture,-6,-6) + " " +
350
                        strformat(this.javaVM,-5,-5) + " " +
351
                        strformat(this.dependencies,-8,0);
352
    }
353

    
354
    private static final String SPACES256 = "                                                                                                                                                                                                                                                               ";
355
    
356
    private String strformat(Object o, int min, int max) {
357
            String s;
358
            boolean alignright = true;
359
            if( o == null ) {
360
                    s = "null";
361
            } else {
362
                    s = o.toString();
363
            }
364
            if( min < 0 ) {
365
                    min = - min;
366
                    alignright = false;
367
            }
368
            if( max == 0 ) {
369
                if( s.length() < min ) {
370
                        if( alignright ) {
371
                                s = SPACES256+s;
372
                                s = s.substring(s.length()-min);
373
                        } else {
374
                                s = (s + SPACES256).substring(0, min);
375
                        }
376
                    }
377
            } else {
378
                    if( max > 225) {
379
                            max = 256;
380
                    } else if( max < 0 ) {
381
                            max = -max;
382
                        alignright = false;
383
                    }
384
                    if( alignright ) {
385
                            if( s.length() > max ) {
386
                                    s = s.substring(0, max);
387
                            }
388
                        if( s.length() < min ) {
389
                                    s = SPACES256.substring(0,min-s.length())+s;
390
                            }
391
                    } else {
392
                            if( s.length() > max ) {
393
                                    s = s.substring(0, max);
394
                            }
395
                            if( s.length() < min ) {
396
                                    s = s + SPACES256.substring(0,min-s.length());
397
                            }
398
                    }
399
            }
400
            return s;
401
    }
402
        
403
    private DefaultPackageInfo append(StringBuffer buffer, String key,
404
        Object value) {
405
        buffer.append("\n\t").append(key).append(": ")
406
            .append(value == null ? "" : value);
407
        return this;
408
    }
409

    
410
    public Object clone() throws CloneNotSupportedException {
411
        DefaultPackageInfo other = (DefaultPackageInfo) super.clone();
412
        
413
        /*
414
         * If setDependencies is called with "" then
415
         * dependencies is set to null, so we must check this:
416
         */
417
        if (this.dependencies == null) {
418
            other.dependencies = null;
419
        } else {
420
            other.dependencies = (Dependencies) this.dependencies.clone();
421
        }
422

    
423
        other.version = (Version) this.version.clone();
424
        other.applicationVersion = (Version) this.applicationVersion.clone();
425
        other.categories = new ArrayList();
426
        other.categories.addAll(this.categories);
427
        other.aditionalProperties = new HashMap(this.aditionalProperties);
428
        return other;
429
    }
430

    
431

    
432
    public boolean matchID(String string) {
433
        String id = this.getID();
434
        String[] stringParts = string.split("#");
435

    
436
        switch (stringParts.length) {
437
        case 1:
438
            if (stringParts[0].equals(this.getCode())) {
439
                return true;
440
            } else {
441
                return false;
442
            }
443
        case 2:
444
            if (!stringParts[0].equals(this.getCode())) {
445
                return false;
446
            }
447
            Version version = new DefaultVersion();
448
            try {
449
                version.parse(stringParts[1]);
450
            } catch (InvalidParameterException ex) {
451
                return false;
452
            }
453

    
454
            if (version.equals(this.getVersion())) {
455
                return true;
456
            }
457
            return false;
458
        default:
459
            return string.equals(id);
460

    
461
        }
462

    
463
    }
464

    
465
    public Dependencies getDependencies() {
466
        return this.dependencies;
467
    }
468

    
469
    public void setDependencies(Dependencies dependencies) {
470
        this.dependencies = dependencies;
471
    }
472

    
473
    public void setDependencies(String dependencies) {
474
        if ( isEmptyString(dependencies)) {
475
            this.dependencies = null;
476
        } else {
477
            this.dependencies = new DefaultDependencies().parse(dependencies);
478
        }
479
    }
480

    
481
    public boolean equals(Object obj) {
482
        PackageInfo other;
483
        try {
484
            other = (PackageInfo) obj;
485
        } catch (Exception e) {
486
            return false;
487
        }
488
        if (!code.equalsIgnoreCase(other.getCode())) {
489
            return false;
490
        }
491
        if (!version.check("=", other.getVersion())) {
492
            return false;
493
        }
494
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
495
            return false;
496
        }
497
        if (!applicationVersion.equals(other.getApplicationVersion())) {
498
            return false;
499
        }
500
        if (!state.equalsIgnoreCase(other.getState())) {
501
            return false;
502
        }
503
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
504
            return false;
505
        }
506
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
507
            return false;
508
        }
509
        if (!type.equalsIgnoreCase(other.getType())) {
510
            return false;
511
        }
512
        if (official != other.isOfficial()) {
513
            return false;
514
        }
515
        return true;
516
    }
517

    
518
    public URL getWebURL() {
519
        return webURL;
520
    }
521

    
522
    public void setWebURL(URL webURL) {
523
        this.webURL = webURL;
524
    }
525

    
526
    public List getCategories() {
527
        return this.categories;
528
    }
529

    
530
    public void setCategories(List categoriesList) {
531
        for (int i = 0; i < categoriesList.size(); i++) {
532
            if (!this.categories.contains(categoriesList.get(i))) {
533
                this.categories.add(categoriesList.get(i));
534
            }
535
        }
536
    }
537

    
538
    public String getCategoriesAsString() {
539
        String categoriesString = "";
540
        for (int i = 0; i < this.categories.size(); i++) {
541
            if (i + 1 < this.categories.size()) {
542
                categoriesString += this.categories.get(i) + ", ";
543
            } else {
544
                categoriesString += this.categories.get(i);
545
            }
546
        }
547
        return categoriesString;
548
    }
549

    
550
    public void addCategoriesAsString(String categoriesString) {
551
        if (categoriesString == null) {
552
            return;
553
        }
554
        categoriesString.trim();
555
        String[] cadena = categoriesString.split(",");
556

    
557
        for (int i = 0; i < cadena.length; i++) {
558
            String trimCadena = cadena[i].trim();
559
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
560
                this.categories.add(trimCadena);
561
            }
562
        }
563

    
564
    }
565

    
566
    public boolean isBroken() {
567
        if (this.broken) {
568
            return true;
569
        }
570
        // if( this.isOfficial() && !this.isSigned() ) {
571
        // return true;
572
        // }
573
        return false;
574
    }
575

    
576
        private boolean isEmptyString(String s) {
577
                if( s == null ) {
578
                        return true;
579
                }
580
                return s.length()==0;
581
        }
582

    
583
        public String getPostInstallScript() {
584
                return this.postInstallScript;
585
        }
586

    
587
        public void setPostInstallScript(String script) {
588
                this.postInstallScript = script;
589
        }
590
        
591
        protected void setBroken(boolean broken) {
592
                this.broken = broken;
593
        }
594

    
595
        public void setValue(String name, Object value) {
596
                this.aditionalProperties.put(name, value);
597
        }
598

    
599
        public Object getValue(String name) {
600
                return this.aditionalProperties.get(name);
601
        }
602

    
603
        private Object[] getPackageNameFormatParameters() {
604
                Object[] parameters = new Object[8];
605
                parameters[PACKAGE_FILE_NAME_FIELDS.GVSIG_VERSION] = this.getApplicationVersion().toString();
606
                parameters[PACKAGE_FILE_NAME_FIELDS.NAME] = this.getCode();
607
                parameters[PACKAGE_FILE_NAME_FIELDS.VERSION] = this.getVersion();
608
                parameters[PACKAGE_FILE_NAME_FIELDS.BUILD] = new Integer(this.getVersion().getBuild());
609
                parameters[PACKAGE_FILE_NAME_FIELDS.STATE] = this.getState();
610
                parameters[PACKAGE_FILE_NAME_FIELDS.OS] = this.getOperatingSystem();
611
                parameters[PACKAGE_FILE_NAME_FIELDS.ARCH] = this.getArchitecture();
612
                parameters[PACKAGE_FILE_NAME_FIELDS.JVM] = this.getJavaVM();
613
                return parameters;
614
        }
615
        
616
        public String getPreferedPackageFileName() {
617
                Object[] parameters = getPackageNameFormatParameters();
618
                return MessageFormat.format(PACKAGE_NAME_FORMAT, parameters);
619
        }
620

    
621
        
622
        // 
623
        // =========================================================
624
        // 
625
        // Deprecated methods
626
        //
627
        
628
        
629
        public int getBuild() {
630
                return this.getVersion().getBuild();
631
        }
632

    
633
        public void setBuild(int build) {
634
                this.getVersion().setBuild(build);
635
        }
636

    
637
        public String getGvSIGVersion() {
638
                // return this.getApplicationVersion().toString();
639
            Version v = this.getApplicationVersion();
640
            return v.getMajor() + "." + v.getMinor() + "." + v.getRevision();
641
        }
642

    
643
        public void setGvSIGVersion(String gvSIGVersion) {
644
                this.getApplicationVersion().parse(gvSIGVersion);
645
        }
646

    
647
        public URL getDownloadURL(URL baseURL) {
648
                logger.info("Deprecated methos, ignore parameter baseURL.");
649
                return this.getOwnerURL();
650
        }
651

    
652
        public String getAntScript() {
653
                return this.getPostInstallScript(); 
654
        }
655

    
656
        public void setAntScript(String antScript) {
657
                this.setPostInstallScript(antScript);
658
        }
659

    
660
        public File downloadFile() throws BaseException {
661
                throw new UnsupportedOperationException();
662
        }
663

    
664
        public File downloadFile(SimpleTaskStatus taskStatus) throws BaseException {
665
                throw new UnsupportedOperationException();
666
        }
667

    
668
        public void addFileToCopy(File file) {
669
                throw new UnsupportedOperationException();
670
        }
671

    
672
        public File getFileToCopy(int i) {
673
                throw new UnsupportedOperationException();
674
        }
675

    
676
        public void removeFileToCopy(File file) {
677
                throw new UnsupportedOperationException();
678
        }
679

    
680
        public void clearFilesToCopy() {
681
                throw new UnsupportedOperationException();
682
        }
683

    
684
        public List getFilesToCopy() {
685
                throw new UnsupportedOperationException();
686
        }
687

    
688
        public boolean removeInstallFolder(File folder) {
689
                throw new UnsupportedOperationException();
690
        }
691

    
692
        public boolean removeFilesFolder(File folder) {
693
                throw new UnsupportedOperationException();
694
        }
695

    
696
        public boolean isSigned() {
697
                return true;
698
        }
699

    
700
        public void checkSignature(byte[] pkgdata) {
701
                // Do nothing
702
        }
703

    
704
    /* (non-Javadoc)
705
     * @see org.gvsig.installer.lib.api.PackageInfo#setVersion(java.lang.String)
706
     */
707
    public void setVersion(String version) {
708
        this.version.parse(version);
709
    }
710
}