Statistics
| Revision:

root / 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 @ 38421

History | View | Annotate | Download (20.1 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.security.InvalidParameterException;
35
import java.util.ArrayList;
36
import java.util.List;
37

    
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

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

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

    
62
    private static final Logger LOG = LoggerFactory
63
        .getLogger(DefaultPackageInfo.class);
64

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

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

    
81
    private String owner = "";
82
    private URL ownerURL = null;
83
    private URL sources = null;
84
    private String gvSIGVersion = "";
85

    
86
    private String defaultDownloadURL = null;
87

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

    
92
    private URL webURL = null;
93

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

    
102
    public String getCode() {
103
        return code;
104
    }
105

    
106
    public String getID() {
107
        String id =
108
            this.getCode() + "#" + this.getVersion() 
109
                + "#" + this.getOperatingSystem() + "#"
110
                + this.getArchitecture();
111
        return id;
112
    }
113

    
114
    public String getName() {
115
        return name;
116
    }
117

    
118
    public String getDescription() {
119
        return description;
120
    }
121

    
122
    public Version getVersion() {
123
        return version;
124
    }
125

    
126
    public int getBuild() {
127
        return this.version.getBuild();
128
    }
129

    
130
    public String getState() {
131
        return state;
132
    }
133

    
134
    public boolean isOfficial() {
135
        return official;
136
    }
137

    
138
    public void setCode(String code) {
139
        this.code = code;
140
    }
141

    
142
    public void setName(String name) {
143
        this.name = name;
144
    }
145

    
146
    public void setDescription(String description) {
147
        this.description = description;
148
    }
149

    
150
    public void setVersion(String version) {
151
        if (version == null) {
152
            return;
153
        }
154
        int prev = this.version.getBuild();
155
        this.version.parse(version);
156
        int curr = this.version.getBuild();
157
        if (prev != 0 && curr == 0) {
158
            this.version.setBuild(prev);
159
        }
160
    }
161

    
162
    public void setVersion(Version version) {
163
        try {
164
            int prev = this.version.getBuild();
165
            this.version = (Version) version.clone();
166
            int curr = this.version.getBuild();
167
            if (prev != 0 && curr == 0) {
168
                this.version.setBuild(prev);
169
            }
170
        } catch (CloneNotSupportedException e) {
171
            throw new RuntimeException(e);
172
        }
173
    }
174

    
175
    public void setBuild(int build) {
176
        this.version.setBuild(build);
177
    }
178

    
179
    public void setState(String state) {
180
        this.state = state;
181
    }
182

    
183
    public void setOfficial(boolean official) {
184
        this.official = official;
185
    }
186

    
187
    public String getOperatingSystem() {
188
        return operatingSystem;
189
    }
190

    
191
    public void setOperatingSystem(String operatingSystem) {
192
        this.operatingSystem = operatingSystem;
193
    }
194

    
195
    public String getArchitecture() {
196
        return architecture;
197
    }
198

    
199
    public void setArchitecture(String architecture) {
200
        this.architecture = architecture;
201
    }
202

    
203
    public String getJavaVM() {
204
        return javaVM;
205
    }
206

    
207
    public void setJavaVM(String javaVM) {
208
        this.javaVM = javaVM;
209
    }
210

    
211
    public String getAntScript() {
212
        return antScript;
213
    }
214

    
215
    public void setAntScript(String antScript) {
216
        this.antScript = antScript;
217
    }
218

    
219
    public String getType() {
220
        return type;
221
    }
222

    
223
    public void setType(String type) {
224
        this.type = type;
225
    }
226

    
227
    public String getGvSIGVersion() {
228
        return gvSIGVersion;
229
    }
230

    
231
    public void setGvSIGVersion(String gvSIGVersion) {
232
        this.gvSIGVersion = gvSIGVersion;
233
    }
234

    
235
    private URL internalGetDownloadURL() {
236
        if (defaultDownloadURL != null) {
237
            try {
238
                return new URL(defaultDownloadURL);
239
            } catch (MalformedURLException e) {
240
                throw new RuntimeException(
241
                    "Error converting to URL the package download url: "
242
                        + defaultDownloadURL, e);
243
            }
244
        }
245
        return null;
246
    }
247

    
248
    public URL getDownloadURL() {
249
        InstallerManager manager = InstallerLocator.getInstallerManager();
250
        if (manager == null) {
251
            return null;
252
        }
253
        return getDownloadURL(manager.getDownloadBaseURL());
254
    }
255

    
256
    public URL getDownloadURL(URL baseURL) {
257
        try {
258
            return internalGetDownloadURL();
259
        } catch (RuntimeException e) {
260
            // The download URL in the package info is not a valid URL,
261
            // so it might be a relative one.
262
            // Try to create and absolute one.
263
        }
264

    
265
        // Create a full URL with the base one and the download one.
266
        try {
267
            return new URL(baseURL, this.defaultDownloadURL);
268
        } catch (MalformedURLException e) {
269
            throw new RuntimeException(
270
                "Error converting to URL the package download url, "
271
                    + "with the base URL: " + baseURL
272
                    + ", the package download URL: " + this.defaultDownloadURL,
273
                e);
274
        }
275
    }
276

    
277
    public String getDownloadURLAsString() {
278
        return this.defaultDownloadURL;
279
    }
280

    
281
    public void setDownloadURL(URL defaultDownloadURL) {
282
        this.defaultDownloadURL = defaultDownloadURL.toString();
283
    }
284

    
285
    public void setDownloadURL(String defaultDownloadURL) {
286
        this.defaultDownloadURL = defaultDownloadURL;
287
    }
288

    
289
    public String getModelVersion() {
290
        return modelVersion;
291
    }
292

    
293
    public void setModelVersion(String modelVersion) {
294
        this.modelVersion = modelVersion;
295
    }
296

    
297
    public String getOwner() {
298
        return owner;
299
    }
300

    
301
    public void setOwner(String owner) {
302
        this.owner = owner;
303
    }
304

    
305
    public URL getOwnerURL() {
306
        return ownerURL;
307
    }
308

    
309
    public void setOwnerURL(URL sources) {
310
        this.ownerURL = sources;
311
    }
312

    
313
    public URL getSourcesURL() {
314
        return sources;
315
    }
316

    
317
    public void setSourcesURL(URL sources) {
318
        this.sources = sources;
319
    }
320

    
321
    @Override
322
    public String toString() {
323
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
324

    
325
        append(buffer, InstallerInfoTags.CODE, getCode());
326
        append(buffer, InstallerInfoTags.NAME, getName());
327
        append(buffer, InstallerInfoTags.DESCRIPTION, getDescription());
328
        append(buffer, InstallerInfoTags.GVSIG_VERSION, getGvSIGVersion());
329
        append(buffer, InstallerInfoTags.VERSION, getVersion());
330
        append(buffer, InstallerInfoTags.BUILD, getBuild());
331
        append(buffer, InstallerInfoTags.OS, getOperatingSystem());
332
        append(buffer, InstallerInfoTags.ARCHITECTURE, getArchitecture());
333
        append(buffer, InstallerInfoTags.JVM, getJavaVM());
334
        append(buffer, InstallerInfoTags.DOWNLOAD_URL, getDownloadURL());
335
        append(buffer, InstallerInfoTags.STATE, getState());
336
        append(buffer, InstallerInfoTags.OFFICIAL, isOfficial());
337
        append(buffer, InstallerInfoTags.TYPE, getType());
338
        append(buffer, InstallerInfoTags.MODEL_VERSION, getModelVersion());
339
        append(buffer, InstallerInfoTags.OWNER, getOwner());
340
        append(buffer, InstallerInfoTags.OWNER_URL, getOwnerURL());
341
        append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
342
        append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
343
        append(buffer, InstallerInfoTags.WEB_URL, getWebURL());
344
        append(buffer, InstallerInfoTags.CATEGORIES, getCategories());
345

    
346
        return buffer.append(')').toString();
347
    }
348

    
349
    public String toStringCompact() {
350
        // type code version state os arch jvm dep
351
        return String
352
            .format(
353
                "%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
354
                this.type, this.code, this.version, this.state,
355
                this.operatingSystem, this.architecture, this.javaVM,
356
                this.dependencies);
357
    }
358

    
359
    private DefaultPackageInfo append(StringBuffer buffer, String key,
360
        Object value) {
361
        buffer.append("\n\t").append(key).append(": ")
362
            .append(value == null ? "" : value);
363
        return this;
364
    }
365

    
366
    @Override
367
    public Object clone() throws CloneNotSupportedException {
368
        DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
369
        clone.auxFiles = new ArrayList<File>(auxFiles);
370
        return clone;
371
    }
372

    
373
    public File downloadFile() throws InstallPackageServiceException {
374
        return this.downloadFile(null);
375
    }
376

    
377
    public class FileDownloadException extends InstallPackageServiceException {
378

    
379
        private static final long serialVersionUID = 8640183295766490512L;
380

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

    
383
        private static final String KEY = "_File_XurlX_download_error";
384

    
385
        public FileDownloadException(URL url, IOException e) {
386
            super(message, e, KEY, serialVersionUID);
387
            setValue("url", url.toString());
388
        }
389

    
390
    }
391

    
392
    public File downloadFile(SimpleTaskStatus taskStatus)
393
        throws InstallPackageServiceException {
394

    
395
        Download download = new Download(taskStatus);
396

    
397
        // First try to download from the index base URL this package info has
398
        // been downloaded from, looking first as if the index is located in the
399
        // main version folder, and if not in the build folder. 
400
        // If also not there, download from the URL available in the 
401
        // downloadURL property.
402
        // Example baseURL: http://downloads.gvsig.org/download/gvsig-desktop/dists/2.0.0/
403
                String versionRelativePath = "../../pool/" + getCode() + "/"
404
                                + getPackageFileName();
405
                File file = downloadFromRelativeURL(download, versionRelativePath);
406

    
407
                if (file == null) {
408
                // Example baseURL: http://downloads.gvsig.org/download/gvsig-desktop/dists/2.0.0/builds/2048/
409
                        String buildRelativePath = "../../../../pool/" + getCode() + "/"
410
                                        + getPackageFileName();
411
                        file = downloadFromRelativeURL(download, buildRelativePath);
412
                }
413

    
414
                if (file == null) {
415
                        file = downloadFromPackageInfoURL(download);
416
                }
417

    
418
                return file;
419
        }
420

    
421
        private File downloadFromRelativeURL(Download download,
422
                        String buildRelativePath) {
423
                InstallerManager manager = InstallerLocator.getInstallerManager();
424
                URL baseURL = manager.getDownloadBaseURL();
425
                try {
426
                        URL downloadURL = new URL(baseURL, buildRelativePath);
427
                        return download.downloadFile(downloadURL, null);
428
                } catch (IOException e) {
429
                        LOG.debug("Package " + getName()
430
                                        + " not found relative to the build index URL: " + baseURL
431
                                        + ", in the URL: " + buildRelativePath, e);
432
                }
433
                return null;
434
        }
435

    
436
        private File downloadFromPackageInfoURL(Download download) throws FileDownloadException {
437
                try {
438
                        return download.downloadFile(this.getDownloadURL(), null);
439
                } catch (IOException ex3) {
440
                        throw new FileDownloadException(this.getDownloadURL(), ex3);
441
                }
442
        }
443

    
444
    private String getPackageFileName() {
445
        Object[] values =
446
            new Object[] { "gvSIG-desktop", getGvSIGVersion(), getCode(),
447
                getVersion(), getState(), getOperatingSystem(),
448
                getArchitecture(), getJavaVM() };
449
        StringBuffer buffer = new StringBuffer();
450
        for (int i = 0; i < values.length - 1; i++) {
451
            buffer.append(values[i]).append('-');
452
        }
453
        buffer.append(values[values.length - 1]).append(".gvspkg");
454
        return buffer.toString();
455
    }
456

    
457
    public void addFileToCopy(File file) {
458
        auxFiles.add(file);
459
    }
460

    
461
    public File getFileToCopy(int i) {
462
        return auxFiles.get(i);
463
    }
464

    
465
    public void removeFileToCopy(File file) {
466
        auxFiles.remove(file);
467
    }
468

    
469
    public void clearFilesToCopy() {
470
        auxFiles.clear();
471
    }
472

    
473
    public List<File> getFilesToCopy() {
474
        return auxFiles;
475
    }
476

    
477
    public boolean removeInstallFolder(File folder) {
478
        DeleteFile delete = new DeleteFile();
479
        boolean success = delete.delete(folder);
480
        setAntScript(null);
481
        clearFilesToCopy();
482
        return success;
483
    }
484

    
485
    public boolean removeFilesFolder(File folder) {
486
        DeleteFile delete = new DeleteFile();
487
        return delete.delete(folder);
488
    }
489

    
490
    public boolean matchID(String string) {
491
        String id = this.getID();
492
        String[] stringParts = string.split("#");
493

    
494
        switch (stringParts.length) {
495
        case 1:
496
            if (stringParts[0].equals(this.getCode())) {
497
                return true;
498
            } else {
499
                return false;
500
            }
501
        case 2:
502
            if (!stringParts[0].equals(this.getCode())) {
503
                return false;
504
            }
505
            Version version = new DefaultVersion();
506
            try {
507
                version.parse(stringParts[1]);
508
            } catch (InvalidParameterException ex) {
509
                return false;
510
            }
511

    
512
            if (version.equals(this.getVersion())) {
513
                return true;
514
            }
515
            return false;
516
        default:
517
            return string.equals(id);
518

    
519
        }
520

    
521
    }
522

    
523
    public Dependencies getDependencies() {
524
        return this.dependencies;
525
    }
526

    
527
    public void setDependencies(Dependencies dependencies) {
528
        this.dependencies = dependencies;
529
    }
530

    
531
    public void setDependencies(String dependencies) {
532
        if (dependencies == null) {
533
            this.dependencies = null;
534
        } else {
535
            this.dependencies = new DefaultDependencies().parse(dependencies);
536
        }
537
    }
538

    
539
    @Override
540
    public boolean equals(Object obj) {
541
        PackageInfo other;
542
        try {
543
            other = (PackageInfo) obj;
544
        } catch (Exception e) {
545
            return false;
546
        }
547
        if (!code.equalsIgnoreCase(other.getCode())) {
548
            return false;
549
        }
550
        if (!version.check("=", other.getVersion())) {
551
            return false;
552
        }
553
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
554
            return false;
555
        }
556
        if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
557
            return false;
558
        }
559
        if (!state.equalsIgnoreCase(other.getState())) {
560
            return false;
561
        }
562
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
563
            return false;
564
        }
565
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
566
            return false;
567
        }
568
        if (!type.equalsIgnoreCase(other.getType())) {
569
            return false;
570
        }
571
        if (official != other.isOfficial()) {
572
            return false;
573
        }
574
        return true;
575
    }
576

    
577
    public URL getWebURL() {
578
        return webURL;
579
    }
580

    
581
    public void setWebURL(URL webURL) {
582
        this.webURL = webURL;
583
    }
584

    
585
    public List<String> getCategories() {
586
        return this.categories;
587
    }
588

    
589
    public void setCategories(List<String> categoriesList) {
590
        for (int i = 0; i < categoriesList.size(); i++) {
591
            if (!this.categories.contains(categoriesList.get(i))) {
592
                this.categories.add(categoriesList.get(i));
593
            }
594
        }
595
    }
596

    
597
    public String getCategoriesAsString() {
598
        String categoriesString = "";
599
        for (int i = 0; i < this.categories.size(); i++) {
600
            if (i + 1 < this.categories.size()) {
601
                categoriesString += this.categories.get(i) + ", ";
602
            } else {
603
                categoriesString += this.categories.get(i);
604
            }
605
        }
606
        return categoriesString;
607
    }
608

    
609
    public void addCategoriesAsString(String categoriesString) {
610
        if (categoriesString == null) {
611
            return;
612
        }
613
        categoriesString.trim();
614
        String[] cadena = categoriesString.split(",");
615

    
616
        for (int i = 0; i < cadena.length; i++) {
617
            String trimCadena = cadena[i].trim();
618
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
619
                this.categories.add(trimCadena);
620
            }
621
        }
622

    
623
    }
624

    
625
    public void checkSignature(byte[] pkgdata) {
626
        this.signed = false;
627
        SignUtil signutil = null;
628
        List<byte[]> keys =
629
            InstallerLocator.getInstallerManager().getPublicKeys();
630
        if (keys.size() < 1) {
631
            // No hay claves publicas, asi que no comprobamos ninguna firma
632
            // y decirmos a todos que no estan firmados.
633
            this.signed = false;
634
            return;
635
        }
636
        for (byte[] rawkey : keys) {
637
            signutil = new SignUtil(rawkey);
638
            if (signutil.canVerify()) {
639
                int status = signutil.verify(pkgdata);
640
                switch (status) {
641
                case SignUtil.SIGN_OK:
642
                    // El paquete tiene una firma correcta,lo marcamos
643
                    // como firmado y salimos.
644
                    this.signed = true;
645
                    return;
646
                case SignUtil.NOT_SIGNED:
647
                    // El paquete no va firmado, lo marcamos como no
648
                    // firmado y salimos.
649
                    this.signed = false;
650
                    return;
651
                case SignUtil.SIGN_FAIL:
652
                default:
653
                    // La firma del paquete no es correcta para esta clave,
654
                    // lo intentamos con el resto de claves.
655
                    break;
656
                }
657
            }
658
        }
659
        // Si habiendo claves publicas la comprobacion de la firma con todas
660
        // ellas
661
        // falla marcamos el paquete como roto.
662
        this.broken = true;
663
    }
664

    
665
    public boolean isBroken() {
666
        if (this.broken) {
667
            return true;
668
        }
669
        // if( this.isOfficial() && !this.isSigned() ) {
670
        // return true;
671
        // }
672
        return false;
673
    }
674

    
675
    public boolean isSigned() {
676
        return signed;
677
    }
678

    
679
}