Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultPackageInfo.java @ 38395

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

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

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

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

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

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

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

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

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

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

    
468
        switch (stringParts.length) {
469
        case 1:
470
            if (stringParts[0].equals(this.getCode())) {
471
                return true;
472
            } else {
473
                return false;
474
            }
475
        case 2:
476
            if (!stringParts[0].equals(this.getCode())) {
477
                return false;
478
            }
479
            Version version = new DefaultVersion();
480
            try {
481
                version.parse(stringParts[1]);
482
            } catch (InvalidParameterException ex) {
483
                return false;
484
            }
485

    
486
            if (version.equals(this.getVersion())) {
487
                return true;
488
            }
489
            return false;
490
        default:
491
            return string.equals(id);
492

    
493
        }
494

    
495
    }
496

    
497
    public Dependencies getDependencies() {
498
        return this.dependencies;
499
    }
500

    
501
    public void setDependencies(Dependencies dependencies) {
502
        this.dependencies = dependencies;
503
    }
504

    
505
    public void setDependencies(String dependencies) {
506
        if (dependencies == null) {
507
            this.dependencies = null;
508
        } else {
509
            this.dependencies = new DefaultDependencies().parse(dependencies);
510
        }
511
    }
512

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

    
551
    public URL getWebURL() {
552
        return webURL;
553
    }
554

    
555
    public void setWebURL(URL webURL) {
556
        this.webURL = webURL;
557
    }
558

    
559
    public List<String> getCategories() {
560
        return this.categories;
561
    }
562

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

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

    
583
    public void addCategoriesAsString(String categoriesString) {
584
        if (categoriesString == null) {
585
            return;
586
        }
587
        categoriesString.trim();
588
        String[] cadena = categoriesString.split(",");
589

    
590
        for (int i = 0; i < cadena.length; i++) {
591
            String trimCadena = cadena[i].trim();
592
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
593
                this.categories.add(trimCadena);
594
            }
595
        }
596

    
597
    }
598

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

    
639
    public boolean isBroken() {
640
        if (this.broken) {
641
            return true;
642
        }
643
        // if( this.isOfficial() && !this.isSigned() ) {
644
        // return true;
645
        // }
646
        return false;
647
    }
648

    
649
    public boolean isSigned() {
650
        return signed;
651
    }
652

    
653
}