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 @ 38191

History | View | Annotate | Download (19.2 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
        return getDownloadURL(manager.getDownloadBaseURL());
250
    }
251

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

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

    
273
    public String getDownloadURLAsString() {
274
        return this.defaultDownloadURL;
275
    }
276

    
277
    public void setDownloadURL(URL defaultDownloadURL) {
278
        this.defaultDownloadURL = defaultDownloadURL.toString();
279
    }
280

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

    
285
    public String getModelVersion() {
286
        return modelVersion;
287
    }
288

    
289
    public void setModelVersion(String modelVersion) {
290
        this.modelVersion = modelVersion;
291
    }
292

    
293
    public String getOwner() {
294
        return owner;
295
    }
296

    
297
    public void setOwner(String owner) {
298
        this.owner = owner;
299
    }
300

    
301
    public URL getOwnerURL() {
302
        return ownerURL;
303
    }
304

    
305
    public void setOwnerURL(URL sources) {
306
        this.ownerURL = sources;
307
    }
308

    
309
    public URL getSourcesURL() {
310
        return sources;
311
    }
312

    
313
    public void setSourcesURL(URL sources) {
314
        this.sources = sources;
315
    }
316

    
317
    @Override
318
    public String toString() {
319
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
320

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

    
342
        return buffer.append(')').toString();
343
    }
344

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

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

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

    
369
    public File downloadFile() throws InstallPackageServiceException {
370
        return this.downloadFile(null);
371
    }
372

    
373
    public class FileDownloadException extends InstallPackageServiceException {
374

    
375
        private static final long serialVersionUID = 8640183295766490512L;
376

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

    
379
        private static final String KEY = "_File_XurlX_download_error";
380

    
381
        public FileDownloadException(URL url, IOException e) {
382
            super(message, e, KEY, serialVersionUID);
383
            setValue("url", url.toString());
384
        }
385

    
386
    }
387

    
388
    public File downloadFile(SimpleTaskStatus taskStatus)
389
        throws InstallPackageServiceException {
390

    
391
        Download download = new Download(taskStatus);
392

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

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

    
427
    public void addFileToCopy(File file) {
428
        auxFiles.add(file);
429
    }
430

    
431
    public File getFileToCopy(int i) {
432
        return auxFiles.get(i);
433
    }
434

    
435
    public void removeFileToCopy(File file) {
436
        auxFiles.remove(file);
437
    }
438

    
439
    public void clearFilesToCopy() {
440
        auxFiles.clear();
441
    }
442

    
443
    public List<File> getFilesToCopy() {
444
        return auxFiles;
445
    }
446

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

    
455
    public boolean removeFilesFolder(File folder) {
456
        DeleteFile delete = new DeleteFile();
457
        return delete.delete(folder);
458
    }
459

    
460
    public boolean matchID(String string) {
461
        String id = this.getID();
462
        String[] stringParts = string.split("#");
463

    
464
        if (stringParts.length == 1) {
465

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

    
488
    }
489

    
490
    public Dependencies getDependencies() {
491
        return this.dependencies;
492
    }
493

    
494
    public void setDependencies(Dependencies dependencies) {
495
        this.dependencies = dependencies;
496
    }
497

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

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

    
544
    public URL getWebURL() {
545
        return webURL;
546
    }
547

    
548
    public void setWebURL(URL webURL) {
549
        this.webURL = webURL;
550
    }
551

    
552
    public List<String> getCategories() {
553
        return this.categories;
554
    }
555

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

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

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

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

    
590
    }
591

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

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

    
642
    public boolean isSigned() {
643
        return signed;
644
    }
645

    
646
}