Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultPackageInfo.java @ 40560

History | View | Annotate | Download (22.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {Prodevelop}   {Task}
27
 */
28

    
29
package org.gvsig.installer.lib.impl;
30

    
31
import java.io.File;
32
import java.io.IOException;
33
import java.net.MalformedURLException;
34
import java.net.URL;
35
import java.security.InvalidParameterException;
36
import java.util.ArrayList;
37
import java.util.List;
38

    
39
import org.gvsig.installer.lib.api.Dependencies;
40
import org.gvsig.installer.lib.api.InstallerLocator;
41
import org.gvsig.installer.lib.api.InstallerManager;
42
import org.gvsig.installer.lib.api.InstallerManager.ARCH;
43
import org.gvsig.installer.lib.api.InstallerManager.JVM;
44
import org.gvsig.installer.lib.api.InstallerManager.OS;
45
import org.gvsig.installer.lib.api.InstallerManager.STATE;
46
import org.gvsig.installer.lib.api.PackageInfo;
47
import org.gvsig.installer.lib.api.Version;
48
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoTags;
50
import org.gvsig.installer.lib.impl.utils.DeleteFile;
51
import org.gvsig.installer.lib.impl.utils.Download;
52
import org.gvsig.installer.lib.impl.utils.SignUtil;
53
import org.gvsig.tools.packageutils.StringWithAlias;
54
import org.gvsig.tools.packageutils.impl.DefaultStringWithAlias;
55
import org.gvsig.tools.task.SimpleTaskStatus;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
/**
60
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
61
 */
62
public class DefaultPackageInfo implements PackageInfo {
63

    
64
    private static final Logger LOG = LoggerFactory
65
        .getLogger(DefaultPackageInfo.class);
66

    
67
    private StringWithAlias code = null;
68
    private String name = null;
69
    private String description = null;
70
    private Version version = null;
71
    private boolean official;
72
    private List<File> auxFiles = null;
73
    private String antScript = null;
74
    private String type = "unknow";
75
    private Boolean signed = false;
76
    private Boolean broken = false;
77

    
78
    private String state = STATE.DEVEL;
79
    private String operatingSystem = OS.ALL;
80
    private String architecture = ARCH.ALL;
81
    private String javaVM = JVM.J1_5;
82

    
83
    private String owner = "";
84
    private URL ownerURL = null;
85
    private URL sources = null;
86
    private String gvSIGVersion = "";
87

    
88
    private String defaultDownloadURL = null;
89

    
90
    private String modelVersion = "1.0.1";
91
    private Dependencies dependencies = null;
92
    private List<String> categories = null;
93

    
94
    private URL webURL = null;
95

    
96
    public DefaultPackageInfo() {
97
        super();
98
        auxFiles = new ArrayList<File>();
99
        this.version = new DefaultVersion().parse("0.0.1");
100
        this.dependencies = new DefaultDependencies();
101
        this.categories = new ArrayList<String>();
102
    }
103

    
104
    public String getCode() {
105
            if( code == null ) {
106
                    return null;
107
            }
108
        return code.toString();
109
    }
110

    
111
    public StringWithAlias getAllCodes() {
112
            return this.code;
113
    }
114
    
115
    public boolean hasThisCode(String code) {
116
            if( this.code == null ) {
117
                    if( code == null ) {
118
                            return true;
119
                    }
120
                    return false;
121
            }
122
            return this.code.equalsIgnoreCase(code);
123
    }
124
    
125
    public boolean hasThisCode(StringWithAlias code) {
126
            if( this.code == null ) {
127
                    if( code == null ) {
128
                            return true;
129
                    }
130
                    return false;
131
            }
132
            return this.code.equalsIgnoreCase(code);
133
    }
134
    
135

    
136
    public String getID() {
137
        String id =
138
            this.getCode() + "#" + this.getVersion() 
139
                + "#" + this.getOperatingSystem() + "#"
140
                + this.getArchitecture();
141
        return id;
142
    }
143

    
144
    public String getName() {
145
        return name;
146
    }
147

    
148
    public String getDescription() {
149
        return description;
150
    }
151

    
152
    public Version getVersion() {
153
        return version;
154
    }
155

    
156
    public int getBuild() {
157
        return this.version.getBuild();
158
    }
159

    
160
    public String getState() {
161
        return state;
162
    }
163

    
164
    public boolean isOfficial() {
165
        return official;
166
    }
167

    
168
    public void setCode(String code) {
169
            if( code == null ) {
170
                    this.code = null;
171
            } else {
172
                    this.code = new DefaultStringWithAlias(code);
173
            }
174
    }
175

    
176
    public void setName(String name) {
177
        this.name = name;
178
    }
179

    
180
    public void setDescription(String description) {
181
        this.description = description;
182
    }
183

    
184
    public void setVersion(String version) {
185
        if (version == null) {
186
            return;
187
        }
188
        int prev = this.version.getBuild();
189
        this.version.parse(version);
190
        int curr = this.version.getBuild();
191
        if (prev != 0 && curr == 0) {
192
            this.version.setBuild(prev);
193
        }
194
    }
195

    
196
    public void setVersion(Version version) {
197
        try {
198
            int prev = this.version.getBuild();
199
            this.version = (Version) version.clone();
200
            int curr = this.version.getBuild();
201
            if (prev != 0 && curr == 0) {
202
                this.version.setBuild(prev);
203
            }
204
        } catch (CloneNotSupportedException e) {
205
            throw new RuntimeException(e);
206
        }
207
    }
208

    
209
    public void setBuild(int build) {
210
        this.version.setBuild(build);
211
    }
212

    
213
    public void setState(String state) {
214
        this.state = state;
215
    }
216

    
217
    public void setOfficial(boolean official) {
218
        this.official = official;
219
    }
220

    
221
    public String getOperatingSystem() {
222
        return operatingSystem;
223
    }
224

    
225
    public void setOperatingSystem(String operatingSystem) {
226
        this.operatingSystem = operatingSystem;
227
    }
228

    
229
    public String getArchitecture() {
230
        return architecture;
231
    }
232

    
233
    public void setArchitecture(String architecture) {
234
        this.architecture = architecture;
235
    }
236

    
237
    public String getJavaVM() {
238
        return javaVM;
239
    }
240

    
241
    public void setJavaVM(String javaVM) {
242
        this.javaVM = javaVM;
243
    }
244

    
245
    public String getAntScript() {
246
        return antScript;
247
    }
248

    
249
    public void setAntScript(String antScript) {
250
        this.antScript = antScript;
251
    }
252

    
253
    public String getType() {
254
        return type;
255
    }
256

    
257
    public void setType(String type) {
258
        this.type = type;
259
    }
260

    
261
    public String getGvSIGVersion() {
262
        return gvSIGVersion;
263
    }
264

    
265
    public void setGvSIGVersion(String gvSIGVersion) {
266
        this.gvSIGVersion = gvSIGVersion;
267
    }
268

    
269
    private URL internalGetDownloadURL() {
270
        if (defaultDownloadURL != null) {
271
            try {
272
                return new URL(defaultDownloadURL);
273
            } catch (MalformedURLException e) {
274
                throw new RuntimeException(
275
                    "Error converting to URL the package download url: "
276
                        + defaultDownloadURL, e);
277
            }
278
        }
279
        return null;
280
    }
281

    
282
    public URL getDownloadURL() {
283
        InstallerManager manager = InstallerLocator.getInstallerManager();
284
        if (manager == null) {
285
            return null;
286
        }
287
        return getDownloadURL(manager.getDownloadBaseURL());
288
    }
289

    
290
    public URL getDownloadURL(URL baseURL) {
291
        try {
292
            return internalGetDownloadURL();
293
        } catch (RuntimeException e) {
294
            // The download URL in the package info is not a valid URL,
295
            // so it might be a relative one.
296
            // Try to create and absolute one.
297
        }
298

    
299
        // Create a full URL with the base one and the download one.
300
        try {
301
            return new URL(baseURL, this.defaultDownloadURL);
302
        } catch (MalformedURLException e) {
303
            throw new RuntimeException(
304
                "Error converting to URL the package download url, "
305
                    + "with the base URL: " + baseURL
306
                    + ", the package download URL: " + this.defaultDownloadURL,
307
                e);
308
        }
309
    }
310

    
311
    public String getDownloadURLAsString() {
312
        return this.defaultDownloadURL;
313
    }
314

    
315
    public void setDownloadURL(URL defaultDownloadURL) {
316
        this.defaultDownloadURL = defaultDownloadURL.toString();
317
    }
318

    
319
    public void setDownloadURL(String defaultDownloadURL) {
320
        this.defaultDownloadURL = defaultDownloadURL;
321
    }
322

    
323
    public String getModelVersion() {
324
        return modelVersion;
325
    }
326

    
327
    public void setModelVersion(String modelVersion) {
328
        this.modelVersion = modelVersion;
329
    }
330

    
331
    public String getOwner() {
332
        return owner;
333
    }
334

    
335
    public void setOwner(String owner) {
336
        this.owner = owner;
337
    }
338

    
339
    public URL getOwnerURL() {
340
        return ownerURL;
341
    }
342

    
343
    public void setOwnerURL(URL sources) {
344
        this.ownerURL = sources;
345
    }
346

    
347
    public URL getSourcesURL() {
348
        return sources;
349
    }
350

    
351
    public void setSourcesURL(URL sources) {
352
        this.sources = sources;
353
    }
354

    
355
    @Override
356
    public String toString() {
357
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
358

    
359
        append(buffer, InstallerInfoTags.CODE, getCode());
360
        append(buffer, InstallerInfoTags.NAME, getName());
361
        append(buffer, InstallerInfoTags.DESCRIPTION, getDescription());
362
        append(buffer, InstallerInfoTags.GVSIG_VERSION, getGvSIGVersion());
363
        append(buffer, InstallerInfoTags.VERSION, getVersion());
364
        append(buffer, InstallerInfoTags.BUILD, getBuild());
365
        append(buffer, InstallerInfoTags.OS, getOperatingSystem());
366
        append(buffer, InstallerInfoTags.ARCHITECTURE, getArchitecture());
367
        append(buffer, InstallerInfoTags.JVM, getJavaVM());
368
        append(buffer, InstallerInfoTags.DOWNLOAD_URL, getDownloadURL());
369
        append(buffer, InstallerInfoTags.STATE, getState());
370
        append(buffer, InstallerInfoTags.OFFICIAL, isOfficial());
371
        append(buffer, InstallerInfoTags.TYPE, getType());
372
        append(buffer, InstallerInfoTags.MODEL_VERSION, getModelVersion());
373
        append(buffer, InstallerInfoTags.OWNER, getOwner());
374
        append(buffer, InstallerInfoTags.OWNER_URL, getOwnerURL());
375
        append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
376
        append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
377
        append(buffer, InstallerInfoTags.WEB_URL, getWebURL());
378
        append(buffer, InstallerInfoTags.CATEGORIES, getCategories());
379

    
380
        return buffer.append(')').toString();
381
    }
382

    
383
    public String toStringCompact() {
384
        // type code version state os arch jvm dep
385
        return String
386
            .format(
387
                "%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
388
                this.type, this.code, this.version, this.state,
389
                this.operatingSystem, this.architecture, this.javaVM,
390
                this.dependencies);
391
    }
392

    
393
    private DefaultPackageInfo append(StringBuffer buffer, String key,
394
        Object value) {
395
        buffer.append("\n\t").append(key).append(": ")
396
            .append(value == null ? "" : value);
397
        return this;
398
    }
399

    
400
    @Override
401
    public Object clone() throws CloneNotSupportedException {
402
        DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
403
        clone.auxFiles = new ArrayList<File>(auxFiles);
404
        return clone;
405
    }
406

    
407
    public File downloadFile() throws InstallPackageServiceException {
408
        return this.downloadFile(null);
409
    }
410

    
411
    public class FileDownloadException extends InstallPackageServiceException {
412

    
413
        private static final long serialVersionUID = 8640183295766490512L;
414

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

    
417
        private static final String KEY = "_File_XurlX_download_error";
418

    
419
        public FileDownloadException(URL url, IOException e) {
420
            super(message, e, KEY, serialVersionUID);
421
            setValue("url", url.toString());
422
        }
423

    
424
    }
425

    
426
    public File downloadFile(SimpleTaskStatus taskStatus)
427
        throws InstallPackageServiceException {
428

    
429
        Download download = new Download(taskStatus);
430

    
431
        // First try to download from the index base URL this package info has
432
        // been downloaded from, looking first as if the index is located in the
433
        // main version folder, and if not in the build folder. 
434
        // If also not there, download from the URL available in the 
435
        // downloadURL property.
436
        // Example baseURL: http://downloads.gvsig.org/download/gvsig-desktop/dists/2.0.0/
437
                String versionRelativePath = "../../pool/" + getCode() + "/"
438
                                + getPackageFileName();
439
                File file = downloadFromRelativeURL(download, versionRelativePath);
440

    
441
                if (file == null) {
442
                // Example baseURL: http://downloads.gvsig.org/download/gvsig-desktop/dists/2.0.0/builds/2048/
443
                        String buildRelativePath = "../../../../pool/" + getCode() + "/"
444
                                        + getPackageFileName();
445
                        file = downloadFromRelativeURL(download, buildRelativePath);
446
                }
447

    
448
                if (file == null) {
449
                        file = downloadFromPackageInfoURL(download);
450
                }
451

    
452
                return file;
453
        }
454

    
455
        private File downloadFromRelativeURL(Download download,
456
                        String buildRelativePath) {
457
                InstallerManager manager = InstallerLocator.getInstallerManager();
458
                URL baseURL = manager.getDownloadBaseURL();
459
                try {
460
                        URL downloadURL = new URL(baseURL, buildRelativePath);
461
                        return download.downloadFile(downloadURL, null);
462
                } catch (IOException e) {
463
                        LOG.debug("Package " + getName()
464
                                        + " not found relative to the build index URL: " + baseURL
465
                                        + ", in the URL: " + buildRelativePath, e);
466
                }
467
                return null;
468
        }
469

    
470
        private File downloadFromPackageInfoURL(Download download) throws FileDownloadException {
471
                try {
472
                        return download.downloadFile(this.getDownloadURL(), null);
473
                } catch (IOException ex3) {
474
                        throw new FileDownloadException(this.getDownloadURL(), ex3);
475
                }
476
        }
477

    
478
    private String getPackageFileName() {
479
        Object[] values =
480
            new Object[] { "gvSIG-desktop", getGvSIGVersion(), getCode(),
481
                getVersion(), getState(), getOperatingSystem(),
482
                getArchitecture(), getJavaVM() };
483
        StringBuffer buffer = new StringBuffer();
484
        for (int i = 0; i < values.length - 1; i++) {
485
            buffer.append(values[i]).append('-');
486
        }
487
        buffer.append(values[values.length - 1]).append(".gvspkg");
488
        return buffer.toString();
489
    }
490

    
491
    public void addFileToCopy(File file) {
492
        auxFiles.add(file);
493
    }
494

    
495
    public File getFileToCopy(int i) {
496
        return auxFiles.get(i);
497
    }
498

    
499
    public void removeFileToCopy(File file) {
500
        auxFiles.remove(file);
501
    }
502

    
503
    public void clearFilesToCopy() {
504
        auxFiles.clear();
505
    }
506

    
507
    public List<File> getFilesToCopy() {
508
        return auxFiles;
509
    }
510

    
511
    public boolean removeInstallFolder(File folder) {
512
        DeleteFile delete = new DeleteFile();
513
        boolean success = delete.delete(folder);
514
        setAntScript(null);
515
        clearFilesToCopy();
516
        return success;
517
    }
518

    
519
    public boolean removeFilesFolder(File folder) {
520
        DeleteFile delete = new DeleteFile();
521
        return delete.delete(folder);
522
    }
523

    
524
    public boolean matchID(String string) {
525
        String id = this.getID();
526
        String[] stringParts = string.split("#");
527

    
528
        switch (stringParts.length) {
529
        case 1:
530
            if (stringParts[0].equals(this.getCode())) {
531
                return true;
532
            } else {
533
                return false;
534
            }
535
        case 2:
536
            if (!stringParts[0].equals(this.getCode())) {
537
                return false;
538
            }
539
            Version version = new DefaultVersion();
540
            try {
541
                version.parse(stringParts[1]);
542
            } catch (InvalidParameterException ex) {
543
                return false;
544
            }
545

    
546
            if (version.equals(this.getVersion())) {
547
                return true;
548
            }
549
            return false;
550
        default:
551
            return string.equals(id);
552

    
553
        }
554

    
555
    }
556

    
557
    public Dependencies getDependencies() {
558
        return this.dependencies;
559
    }
560

    
561
    public void setDependencies(Dependencies dependencies) {
562
        this.dependencies = dependencies;
563
    }
564

    
565
    public void setDependencies(String dependencies) {
566
        if (dependencies == null) {
567
            this.dependencies = null;
568
        } else {
569
            this.dependencies = new DefaultDependencies().parse(dependencies);
570
        }
571
    }
572

    
573
    @Override
574
    public boolean equals(Object obj) {
575
        PackageInfo other;
576
        try {
577
            other = (PackageInfo) obj;
578
        } catch (Exception e) {
579
            return false;
580
        }
581
        if (!code.equalsIgnoreCase(other.getCode())) {
582
            return false;
583
        }
584
        if (!version.check("=", other.getVersion())) {
585
            return false;
586
        }
587
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
588
            return false;
589
        }
590
        if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
591
            return false;
592
        }
593
        if (!state.equalsIgnoreCase(other.getState())) {
594
            return false;
595
        }
596
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
597
            return false;
598
        }
599
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
600
            return false;
601
        }
602
        if (!type.equalsIgnoreCase(other.getType())) {
603
            return false;
604
        }
605
        if (official != other.isOfficial()) {
606
            return false;
607
        }
608
        return true;
609
    }
610

    
611
    public URL getWebURL() {
612
        return webURL;
613
    }
614

    
615
    public void setWebURL(URL webURL) {
616
        this.webURL = webURL;
617
    }
618

    
619
    public List<String> getCategories() {
620
        return this.categories;
621
    }
622

    
623
    public void setCategories(List categoriesList) {
624
        for (int i = 0; i < categoriesList.size(); i++) {
625
            if (!this.categories.contains(categoriesList.get(i))) {
626
                this.categories.add((String) categoriesList.get(i));
627
            }
628
        }
629
    }
630

    
631
    public String getCategoriesAsString() {
632
        String categoriesString = "";
633
        for (int i = 0; i < this.categories.size(); i++) {
634
            if (i + 1 < this.categories.size()) {
635
                categoriesString += this.categories.get(i) + ", ";
636
            } else {
637
                categoriesString += this.categories.get(i);
638
            }
639
        }
640
        return categoriesString;
641
    }
642

    
643
    public void addCategoriesAsString(String categoriesString) {
644
        if (categoriesString == null) {
645
            return;
646
        }
647
        categoriesString.trim();
648
        String[] cadena = categoriesString.split(",");
649

    
650
        for (int i = 0; i < cadena.length; i++) {
651
            String trimCadena = cadena[i].trim();
652
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
653
                this.categories.add(trimCadena);
654
            }
655
        }
656

    
657
    }
658

    
659
    public void checkSignature(byte[] pkgdata) {
660
        this.signed = false;
661
        SignUtil signutil = null;
662
        List<byte[]> keys =
663
            InstallerLocator.getInstallerManager().getPublicKeys();
664
        if (keys.size() < 1) {
665
            // No hay claves publicas, asi que no comprobamos ninguna firma
666
            // y decirmos a todos que no estan firmados.
667
            this.signed = false;
668
            return;
669
        }
670
        for (byte[] rawkey : keys) {
671
            signutil = new SignUtil(rawkey);
672
            if (signutil.canVerify()) {
673
                int status = signutil.verify(pkgdata);
674
                switch (status) {
675
                case SignUtil.SIGN_OK:
676
                    // El paquete tiene una firma correcta,lo marcamos
677
                    // como firmado y salimos.
678
                    this.signed = true;
679
                    return;
680
                case SignUtil.NOT_SIGNED:
681
                    // El paquete no va firmado, lo marcamos como no
682
                    // firmado y salimos.
683
                    this.signed = false;
684
                    return;
685
                case SignUtil.SIGN_FAIL:
686
                default:
687
                    // La firma del paquete no es correcta para esta clave,
688
                    // lo intentamos con el resto de claves.
689
                    break;
690
                }
691
            }
692
        }
693
        // Si habiendo claves publicas la comprobacion de la firma con todas
694
        // ellas
695
        // falla marcamos el paquete como roto.
696
        this.broken = true;
697
    }
698

    
699
    public boolean isBroken() {
700
        if (this.broken) {
701
            return true;
702
        }
703
        // if( this.isOfficial() && !this.isSigned() ) {
704
        // return true;
705
        // }
706
        return false;
707
    }
708

    
709
    public boolean isSigned() {
710
        return signed;
711
    }
712

    
713
    /* (non-Javadoc)
714
     * @see org.gvsig.installer.lib.api.PackageInfo#getApplicationVersion()
715
     */
716
    public Version getApplicationVersion() {
717
        // TODO Auto-generated method stub
718
        return null;
719
    }
720

    
721
    /* (non-Javadoc)
722
     * @see org.gvsig.installer.lib.api.PackageInfo#setApplicationVersion(org.gvsig.installer.lib.api.Version)
723
     */
724
    public void setApplicationVersion(Version version) {
725
        // TODO Auto-generated method stub
726
        
727
    }
728

    
729
    /* (non-Javadoc)
730
     * @see org.gvsig.installer.lib.api.PackageInfo#setValue(java.lang.String, java.lang.Object)
731
     */
732
    public void setValue(String name, Object value) {
733
        // TODO Auto-generated method stub
734
        
735
    }
736

    
737
    /* (non-Javadoc)
738
     * @see org.gvsig.installer.lib.api.PackageInfo#getValue(java.lang.String)
739
     */
740
    public Object getValue(String name) {
741
        // TODO Auto-generated method stub
742
        return null;
743
    }
744

    
745
    /* (non-Javadoc)
746
     * @see org.gvsig.installer.lib.api.PackageInfo#getPreferedPackageFileName()
747
     */
748
    public String getPreferedPackageFileName() {
749
        // TODO Auto-generated method stub
750
        return null;
751
    }
752

    
753
}