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

History | View | Annotate | Download (17.8 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.gvsig.installer.lib.api.Dependencies;
38
import org.gvsig.installer.lib.api.InstallerLocator;
39
import org.gvsig.installer.lib.api.InstallerManager;
40
import org.gvsig.installer.lib.api.InstallerManager.ARCH;
41
import org.gvsig.installer.lib.api.InstallerManager.JVM;
42
import org.gvsig.installer.lib.api.InstallerManager.OS;
43
import org.gvsig.installer.lib.api.InstallerManager.STATE;
44
import org.gvsig.installer.lib.api.PackageInfo;
45
import org.gvsig.installer.lib.api.Version;
46
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
47
import org.gvsig.installer.lib.impl.info.InstallerInfoTags;
48
import org.gvsig.installer.lib.impl.utils.DeleteFile;
49
import org.gvsig.installer.lib.impl.utils.Download;
50
import org.gvsig.installer.lib.impl.utils.SignUtil;
51
import org.gvsig.tools.task.SimpleTaskStatus;
52

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

    
58
    private String code = null;
59
    private String name = null;
60
    private String description = null;
61
    private Version version = null;
62
    private boolean official;
63
    private List<File> auxFiles = null;
64
    private String antScript = null;
65
    private String type = "unknow";
66
    private Boolean signed = false;
67
    private Boolean broken = false;
68

    
69
    private String state = STATE.DEVEL;
70
    private String operatingSystem = OS.ALL;
71
    private String architecture = ARCH.ALL;
72
    private String javaVM = JVM.J1_5;
73

    
74
    private String owner = "";
75
    private URL ownerURL = null;
76
    private URL sources = null;
77
    private String gvSIGVersion = "";
78

    
79
    private String defaultDownloadURL = null;
80

    
81
    private String modelVersion = "1.0.1";
82
    private Dependencies dependencies = null;
83
    private List<String> categories = null;
84

    
85
    private URL webURL = null;
86

    
87
    public DefaultPackageInfo() {
88
        super();
89
        auxFiles = new ArrayList<File>();
90
        this.version = new DefaultVersion().parse("0.0.1");
91
        this.dependencies = new DefaultDependencies();
92
        this.categories = new ArrayList<String>();
93
    }
94

    
95
    public String getCode() {
96
        return code;
97
    }
98

    
99
    public String getID() {
100
        String id =
101
            this.getCode() + "#" + this.getVersion() + "#" + this.getBuild()
102
                + "#" + this.getOperatingSystem() + "#"
103
                + this.getArchitecture();
104
        return id;
105
    }
106

    
107
    public String getName() {
108
        return name;
109
    }
110

    
111
    public String getDescription() {
112
        return description;
113
    }
114

    
115
    public Version getVersion() {
116
        return version;
117
    }
118

    
119
    public int getBuild() {
120
        return this.version.getBuild();
121
    }
122

    
123
    public String getState() {
124
        return state;
125
    }
126

    
127
    public boolean isOfficial() {
128
        return official;
129
    }
130

    
131
    public void setCode(String code) {
132
        this.code = code;
133
    }
134

    
135
    public void setName(String name) {
136
        this.name = name;
137
    }
138

    
139
    public void setDescription(String description) {
140
        this.description = description;
141
    }
142

    
143
    public void setVersion(String version) {
144
        if (version == null) {
145
            return;
146
        }
147
        int prev = this.version.getBuild();
148
        this.version.parse(version);
149
        int curr = this.version.getBuild();
150
        if (prev != 0 && curr == 0) {
151
            this.version.setBuild(prev);
152
        }
153
    }
154

    
155
    public void setVersion(Version version) {
156
        try {
157
            int prev = this.version.getBuild();
158
            this.version = (Version) version.clone();
159
            int curr = this.version.getBuild();
160
            if (prev != 0 && curr == 0) {
161
                this.version.setBuild(prev);
162
            }
163
        } catch (CloneNotSupportedException e) {
164
            throw new RuntimeException(e);
165
        }
166
    }
167

    
168
    public void setBuild(int build) {
169
        this.version.setBuild(build);
170
    }
171

    
172
    public void setState(String state) {
173
        this.state = state;
174
    }
175

    
176
    public void setOfficial(boolean official) {
177
        this.official = official;
178
    }
179

    
180
    public String getOperatingSystem() {
181
        return operatingSystem;
182
    }
183

    
184
    public void setOperatingSystem(String operatingSystem) {
185
        this.operatingSystem = operatingSystem;
186
    }
187

    
188
    public String getArchitecture() {
189
        return architecture;
190
    }
191

    
192
    public void setArchitecture(String architecture) {
193
        this.architecture = architecture;
194
    }
195

    
196
    public String getJavaVM() {
197
        return javaVM;
198
    }
199

    
200
    public void setJavaVM(String javaVM) {
201
        this.javaVM = javaVM;
202
    }
203

    
204
    public String getAntScript() {
205
        return antScript;
206
    }
207

    
208
    public void setAntScript(String antScript) {
209
        this.antScript = antScript;
210
    }
211

    
212
    public String getType() {
213
        return type;
214
    }
215

    
216
    public void setType(String type) {
217
        this.type = type;
218
    }
219

    
220
    public String getGvSIGVersion() {
221
        return gvSIGVersion;
222
    }
223

    
224
    public void setGvSIGVersion(String gvSIGVersion) {
225
        this.gvSIGVersion = gvSIGVersion;
226
    }
227

    
228
    private URL internalGetDownloadURL() {
229
        if (defaultDownloadURL != null) {
230
            try {
231
                return new URL(defaultDownloadURL);
232
            } catch (MalformedURLException e) {
233
                throw new RuntimeException(
234
                    "Error converting to URL the package download url: "
235
                        + defaultDownloadURL, e);
236
            }
237
        }
238
        return null;
239
    }
240

    
241
    public URL getDownloadURL() {
242
        InstallerManager manager = InstallerLocator.getInstallerManager();
243
        return getDownloadURL(manager.getDownloadBaseURL());
244
    }
245

    
246
    public URL getDownloadURL(URL baseURL) {
247
        try {
248
            return internalGetDownloadURL();
249
        } catch (RuntimeException e) {
250
            // The download URL in the package info is not a valid URL,
251
            // so it might be a relative one.
252
            // Try to create and absolute one.
253
        }
254

    
255
        // Create a full URL with the base one and the download one.
256
        try {
257
            return new URL(baseURL, this.defaultDownloadURL);
258
        } catch (MalformedURLException e) {
259
            throw new RuntimeException(
260
                "Error converting to URL the package download url, "
261
                    + "with the base URL: " + baseURL
262
                    + ", the package download URL: " + this.defaultDownloadURL,
263
                e);
264
        }
265
    }
266

    
267
    public String getDownloadURLAsString() {
268
        return this.defaultDownloadURL;
269
    }
270

    
271
    public void setDownloadURL(URL defaultDownloadURL) {
272
        this.defaultDownloadURL = defaultDownloadURL.toString();
273
    }
274

    
275
    public void setDownloadURL(String defaultDownloadURL) {
276
        this.defaultDownloadURL = defaultDownloadURL;
277
    }
278

    
279
    public String getModelVersion() {
280
        return modelVersion;
281
    }
282

    
283
    public void setModelVersion(String modelVersion) {
284
        this.modelVersion = modelVersion;
285
    }
286

    
287
    public String getOwner() {
288
        return owner;
289
    }
290

    
291
    public void setOwner(String owner) {
292
        this.owner = owner;
293
    }
294

    
295
    public URL getOwnerURL() {
296
        return ownerURL;
297
    }
298

    
299
    public void setOwnerURL(URL sources) {
300
        this.ownerURL = sources;
301
    }
302

    
303
    public URL getSourcesURL() {
304
        return sources;
305
    }
306

    
307
    public void setSourcesURL(URL sources) {
308
        this.sources = sources;
309
    }
310

    
311
    @Override
312
    public String toString() {
313
        StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
314

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

    
336
        return buffer.append(')').toString();
337
    }
338

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

    
349
    private DefaultPackageInfo append(StringBuffer buffer, String key,
350
        Object value) {
351
        buffer.append("\n\t").append(key).append(": ")
352
            .append(value == null ? "" : value);
353
        return this;
354
    }
355

    
356
    @Override
357
    public Object clone() throws CloneNotSupportedException {
358
        DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
359
        clone.auxFiles = new ArrayList<File>(auxFiles);
360
        return clone;
361
    }
362

    
363
    public File downloadFile() throws InstallPackageServiceException {
364
        return this.downloadFile(null);
365
    }
366

    
367
    public class FileDownloadException extends InstallPackageServiceException {
368

    
369
        private static final long serialVersionUID = 8640183295766490512L;
370

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

    
373
        private static final String KEY = "_File_XurlX_download_error";
374

    
375
        public FileDownloadException(URL url, IOException e) {
376
            super(message, e, KEY, serialVersionUID);
377
            setValue("url", url.toString());
378
        }
379

    
380
    }
381

    
382
    public File downloadFile(SimpleTaskStatus taskStatus)
383
        throws InstallPackageServiceException {
384
        Download download = new Download(taskStatus);
385
        try {
386
            return download.downloadFile(this.getDownloadURL(), null);
387
        } catch (IOException e) {
388
            throw new FileDownloadException(this.getDownloadURL(), e);
389
        }
390
    }
391

    
392
    public void addFileToCopy(File file) {
393
        auxFiles.add(file);
394
    }
395

    
396
    public File getFileToCopy(int i) {
397
        return auxFiles.get(i);
398
    }
399

    
400
    public void removeFileToCopy(File file) {
401
        auxFiles.remove(file);
402
    }
403

    
404
    public void clearFilesToCopy() {
405
        auxFiles.clear();
406
    }
407

    
408
    public List<File> getFilesToCopy() {
409
        return auxFiles;
410
    }
411

    
412
    public boolean removeInstallFolder(File folder) {
413
        DeleteFile delete = new DeleteFile();
414
        boolean success = delete.delete(folder);
415
        setAntScript(null);
416
        clearFilesToCopy();
417
        return success;
418
    }
419

    
420
    public boolean removeFilesFolder(File folder) {
421
        DeleteFile delete = new DeleteFile();
422
        return delete.delete(folder);
423
    }
424

    
425
    public boolean matchID(String string) {
426
        String id = this.getID();
427
        String[] stringParts = string.split("#");
428

    
429
        if (stringParts.length == 1) {
430

    
431
            if (stringParts[0].equals(this.getCode())) {
432
                return true;
433
            } else {
434
                return false;
435
            }
436
        } else {
437
            if (stringParts.length == 2) {
438
                if ((stringParts[0] + stringParts[1])
439
                    .equals((this.getCode() + this.getVersion()))) {
440
                    return true;
441
                } else {
442
                    return true;
443
                }
444
            } else {
445
                if (string.equals(id)) {
446
                    return true;
447
                } else {
448
                    return false;
449
                }
450
            }
451
        }
452

    
453
    }
454

    
455
    public Dependencies getDependencies() {
456
        return this.dependencies;
457
    }
458

    
459
    public void setDependencies(Dependencies dependencies) {
460
        this.dependencies = dependencies;
461
    }
462

    
463
    public void setDependencies(String dependencies) {
464
        if (dependencies == null) {
465
            this.dependencies = null;
466
        } else {
467
            this.dependencies = new DefaultDependencies().parse(dependencies);
468
        }
469
    }
470

    
471
    @Override
472
    public boolean equals(Object obj) {
473
        PackageInfo other;
474
        try {
475
            other = (PackageInfo) obj;
476
        } catch (Exception e) {
477
            return false;
478
        }
479
        if (!code.equalsIgnoreCase(other.getCode())) {
480
            return false;
481
        }
482
        if (!version.check("=", other.getVersion())) {
483
            return false;
484
        }
485
        if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
486
            return false;
487
        }
488
        if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
489
            return false;
490
        }
491
        if (!state.equalsIgnoreCase(other.getState())) {
492
            return false;
493
        }
494
        if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
495
            return false;
496
        }
497
        if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
498
            return false;
499
        }
500
        if (!type.equalsIgnoreCase(other.getType())) {
501
            return false;
502
        }
503
        if (official != other.isOfficial()) {
504
            return false;
505
        }
506
        return true;
507
    }
508

    
509
    public URL getWebURL() {
510
        return webURL;
511
    }
512

    
513
    public void setWebURL(URL webURL) {
514
        this.webURL = webURL;
515
    }
516

    
517
    public List<String> getCategories() {
518
        return this.categories;
519
    }
520

    
521
    public void setCategories(List<String> categoriesList) {
522
        for (int i = 0; i < categoriesList.size(); i++) {
523
            if (!this.categories.contains(categoriesList.get(i))) {
524
                this.categories.add(categoriesList.get(i));
525
            }
526
        }
527
    }
528

    
529
    public String getCategoriesAsString() {
530
        String categoriesString = "";
531
        for (int i = 0; i < this.categories.size(); i++) {
532
            if (i + 1 < this.categories.size()) {
533
                categoriesString += this.categories.get(i) + ", ";
534
            } else {
535
                categoriesString += this.categories.get(i);
536
            }
537
        }
538
        return categoriesString;
539
    }
540

    
541
    public void addCategoriesAsString(String categoriesString) {
542
        if (categoriesString == null) {
543
            return;
544
        }
545
        categoriesString.trim();
546
        String[] cadena = categoriesString.split(",");
547

    
548
        for (int i = 0; i < cadena.length; i++) {
549
            String trimCadena = cadena[i].trim();
550
            if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
551
                this.categories.add(trimCadena);
552
            }
553
        }
554

    
555
    }
556

    
557
    public void checkSignature(byte[] pkgdata) {
558
        this.signed = false;
559
        SignUtil signutil = null;
560
        List<byte[]> keys =
561
            InstallerLocator.getInstallerManager().getPublicKeys();
562
        if (keys.size() < 1) {
563
            // No hay claves publicas, asi que no comprobamos ninguna firma
564
            // y decirmos a todos que no estan firmados.
565
            this.signed = false;
566
            return;
567
        }
568
        for (byte[] rawkey : keys) {
569
            signutil = new SignUtil(rawkey);
570
            if (signutil.canVerify()) {
571
                int status = signutil.verify(pkgdata);
572
                switch (status) {
573
                case SignUtil.SIGN_OK:
574
                    // El paquete tiene una firma correcta,lo marcamos
575
                    // como firmado y salimos.
576
                    this.signed = true;
577
                    return;
578
                case SignUtil.NOT_SIGNED:
579
                    // El paquete no va firmado, lo marcamos como no
580
                    // firmado y salimos.
581
                    this.signed = false;
582
                    return;
583
                case SignUtil.SIGN_FAIL:
584
                default:
585
                    // La firma del paquete no es correcta para esta clave,
586
                    // lo intentamos con el resto de claves.
587
                    break;
588
                }
589
            }
590
        }
591
        // Si habiendo claves publicas la comprobacion de la firma con todas
592
        // ellas
593
        // falla marcamos el paquete como roto.
594
        this.broken = true;
595
    }
596

    
597
    public boolean isBroken() {
598
        if (this.broken) {
599
            return true;
600
        }
601
        // if( this.isOfficial() && !this.isSigned() ) {
602
        // return true;
603
        // }
604
        return false;
605
    }
606

    
607
    public boolean isSigned() {
608
        return signed;
609
    }
610

    
611
}