Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultPackageInfo.java @ 38304

History | View | Annotate | Download (19.3 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.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

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

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

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

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

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

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

    
85
    private String defaultDownloadURL = null;
86

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

    
91
    private URL webURL = null;
92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
376
    public class FileDownloadException extends InstallPackageServiceException {
377

    
378
        private static final long serialVersionUID = 8640183295766490512L;
379

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

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

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

    
389
    }
390

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

    
394
        Download download = new Download(taskStatus);
395

    
396
        // First download from the index base URL this package info has
397
        // been downloaded from. If not there, download from the URL
398
        // available in the downloadURL property.
399
        InstallerManager manager = InstallerLocator.getInstallerManager();
400
        URL baseURL = manager.getDownloadBaseURL();
401
        String relativePath =
402
            "../../pool/" + getCode() + "/" + getPackageFileName();
403
        try {
404
            URL downloadURL = new URL(baseURL, relativePath);
405
            return download.downloadFile(downloadURL, null);
406
        } catch (IOException e) {
407
            LOG.debug("Package " + getName()
408
                + " not found relative to the index URL: " + baseURL, e);
409
            try {
410
                return download.downloadFile(this.getDownloadURL(), null);
411
            } catch (IOException e2) {
412
                throw new FileDownloadException(this.getDownloadURL(), e);
413
            }
414
        }
415
    }
416

    
417
    private String getPackageFileName() {
418
        Object[] values =
419
            new Object[] { "gvSIG-desktop", getGvSIGVersion(), getCode(),
420
                getVersion(), getState(), getOperatingSystem(),
421
                getArchitecture(), getJavaVM() };
422
        StringBuffer buffer = new StringBuffer();
423
        for (int i = 0; i < values.length - 1; i++) {
424
            buffer.append(values[i]).append('-');
425
        }
426
        buffer.append(values[values.length - 1]).append(".gvspkg");
427
        return buffer.toString();
428
    }
429

    
430
    public void addFileToCopy(File file) {
431
        auxFiles.add(file);
432
    }
433

    
434
    public File getFileToCopy(int i) {
435
        return auxFiles.get(i);
436
    }
437

    
438
    public void removeFileToCopy(File file) {
439
        auxFiles.remove(file);
440
    }
441

    
442
    public void clearFilesToCopy() {
443
        auxFiles.clear();
444
    }
445

    
446
    public List<File> getFilesToCopy() {
447
        return auxFiles;
448
    }
449

    
450
    public boolean removeInstallFolder(File folder) {
451
        DeleteFile delete = new DeleteFile();
452
        boolean success = delete.delete(folder);
453
        setAntScript(null);
454
        clearFilesToCopy();
455
        return success;
456
    }
457

    
458
    public boolean removeFilesFolder(File folder) {
459
        DeleteFile delete = new DeleteFile();
460
        return delete.delete(folder);
461
    }
462

    
463
    public boolean matchID(String string) {
464
        String id = this.getID();
465
        String[] stringParts = string.split("#");
466

    
467
        if (stringParts.length == 1) {
468

    
469
            if (stringParts[0].equals(this.getCode())) {
470
                return true;
471
            } else {
472
                return false;
473
            }
474
        } else {
475
            if (stringParts.length == 2) {
476
                if ((stringParts[0] + stringParts[1])
477
                    .equals((this.getCode() + this.getVersion()))) {
478
                    return true;
479
                } else {
480
                    return true;
481
                }
482
            } else {
483
                if (string.equals(id)) {
484
                    return true;
485
                } else {
486
                    return false;
487
                }
488
            }
489
        }
490

    
491
    }
492

    
493
    public Dependencies getDependencies() {
494
        return this.dependencies;
495
    }
496

    
497
    public void setDependencies(Dependencies dependencies) {
498
        this.dependencies = dependencies;
499
    }
500

    
501
    public void setDependencies(String dependencies) {
502
        if (dependencies == null) {
503
            this.dependencies = null;
504
        } else {
505
            this.dependencies = new DefaultDependencies().parse(dependencies);
506
        }
507
    }
508

    
509
    @Override
510
    public boolean equals(Object obj) {
511
        PackageInfo other;
512
        try {
513
            other = (PackageInfo) obj;
514
        } catch (Exception e) {
515
            return false;
516
        }
517
        if (!code.equalsIgnoreCase(other.getCode())) {
518
            return false;
519
        }
520
        if (!version.check("=", other.getVersion())) {
521
            return false;
522
        }
523
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
524
            return false;
525
        }
526
        if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
527
            return false;
528
        }
529
        if (!state.equalsIgnoreCase(other.getState())) {
530
            return false;
531
        }
532
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
533
            return false;
534
        }
535
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
536
            return false;
537
        }
538
        if (!type.equalsIgnoreCase(other.getType())) {
539
            return false;
540
        }
541
        if (official != other.isOfficial()) {
542
            return false;
543
        }
544
        return true;
545
    }
546

    
547
    public URL getWebURL() {
548
        return webURL;
549
    }
550

    
551
    public void setWebURL(URL webURL) {
552
        this.webURL = webURL;
553
    }
554

    
555
    public List<String> getCategories() {
556
        return this.categories;
557
    }
558

    
559
    public void setCategories(List<String> categoriesList) {
560
        for (int i = 0; i < categoriesList.size(); i++) {
561
            if (!this.categories.contains(categoriesList.get(i))) {
562
                this.categories.add(categoriesList.get(i));
563
            }
564
        }
565
    }
566

    
567
    public String getCategoriesAsString() {
568
        String categoriesString = "";
569
        for (int i = 0; i < this.categories.size(); i++) {
570
            if (i + 1 < this.categories.size()) {
571
                categoriesString += this.categories.get(i) + ", ";
572
            } else {
573
                categoriesString += this.categories.get(i);
574
            }
575
        }
576
        return categoriesString;
577
    }
578

    
579
    public void addCategoriesAsString(String categoriesString) {
580
        if (categoriesString == null) {
581
            return;
582
        }
583
        categoriesString.trim();
584
        String[] cadena = categoriesString.split(",");
585

    
586
        for (int i = 0; i < cadena.length; i++) {
587
            String trimCadena = cadena[i].trim();
588
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
589
                this.categories.add(trimCadena);
590
            }
591
        }
592

    
593
    }
594

    
595
    public void checkSignature(byte[] pkgdata) {
596
        this.signed = false;
597
        SignUtil signutil = null;
598
        List<byte[]> keys =
599
            InstallerLocator.getInstallerManager().getPublicKeys();
600
        if (keys.size() < 1) {
601
            // No hay claves publicas, asi que no comprobamos ninguna firma
602
            // y decirmos a todos que no estan firmados.
603
            this.signed = false;
604
            return;
605
        }
606
        for (byte[] rawkey : keys) {
607
            signutil = new SignUtil(rawkey);
608
            if (signutil.canVerify()) {
609
                int status = signutil.verify(pkgdata);
610
                switch (status) {
611
                case SignUtil.SIGN_OK:
612
                    // El paquete tiene una firma correcta,lo marcamos
613
                    // como firmado y salimos.
614
                    this.signed = true;
615
                    return;
616
                case SignUtil.NOT_SIGNED:
617
                    // El paquete no va firmado, lo marcamos como no
618
                    // firmado y salimos.
619
                    this.signed = false;
620
                    return;
621
                case SignUtil.SIGN_FAIL:
622
                default:
623
                    // La firma del paquete no es correcta para esta clave,
624
                    // lo intentamos con el resto de claves.
625
                    break;
626
                }
627
            }
628
        }
629
        // Si habiendo claves publicas la comprobacion de la firma con todas
630
        // ellas
631
        // falla marcamos el paquete como roto.
632
        this.broken = true;
633
    }
634

    
635
    public boolean isBroken() {
636
        if (this.broken) {
637
            return true;
638
        }
639
        // if( this.isOfficial() && !this.isSigned() ) {
640
        // return true;
641
        // }
642
        return false;
643
    }
644

    
645
    public boolean isSigned() {
646
        return signed;
647
    }
648

    
649
}