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

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

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

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

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

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

    
78
        private String defaultDownloadURL = null;
79

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

    
84
        private URL webURL = null;
85

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

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

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

    
105
        public String getName() {
106
                return name;
107
        }
108

    
109
        public String getDescription() {
110
                return description;
111
        }
112

    
113
        public Version getVersion() {
114
                return version;
115
        }
116

    
117
        public int getBuild() {
118
                return this.version.getBuild();
119
        }
120

    
121
        public String getState() {
122
                return state;
123
        }
124

    
125
        public boolean isOfficial() {
126
                return official;
127
        }
128

    
129
        public void setCode(String code) {
130
                this.code = code;
131
        }
132

    
133
        public void setName(String name) {
134
                this.name = name;
135
        }
136

    
137
        public void setDescription(String description) {
138
                this.description = description;
139
        }
140

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

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

    
166
        public void setBuild(int build) {
167
                this.version.setBuild(build);
168
        }
169

    
170
        public void setState(String state) {
171
                this.state = state;
172
        }
173

    
174
        public void setOfficial(boolean official) {
175
                this.official = official;
176
        }
177

    
178
        public String getOperatingSystem() {
179
                return operatingSystem;
180
        }
181

    
182
        public void setOperatingSystem(String operatingSystem) {
183
                this.operatingSystem = operatingSystem;
184
        }
185

    
186
        public String getArchitecture() {
187
                return architecture;
188
        }
189

    
190
        public void setArchitecture(String architecture) {
191
                this.architecture = architecture;
192
        }
193

    
194
        public String getJavaVM() {
195
                return javaVM;
196
        }
197

    
198
        public void setJavaVM(String javaVM) {
199
                this.javaVM = javaVM;
200
        }
201

    
202
        public String getAntScript() {
203
                return antScript;
204
        }
205

    
206
        public void setAntScript(String antScript) {
207
                this.antScript = antScript;
208
        }
209

    
210
        public String getType() {
211
                return type;
212
        }
213

    
214
        public void setType(String type) {
215
                this.type = type;
216
        }
217

    
218
        public String getGvSIGVersion() {
219
                return gvSIGVersion;
220
        }
221

    
222
        public void setGvSIGVersion(String gvSIGVersion) {
223
                this.gvSIGVersion = gvSIGVersion;
224
        }
225

    
226
        public URL getDownloadURL() {
227
                URL url = null;
228
                if (defaultDownloadURL != null) {
229
                        try {
230
                                url = new URL(defaultDownloadURL);
231
                        } catch (MalformedURLException e) {
232
                                // TODO Auto-generated catch block
233
                                e.printStackTrace();
234
                        }
235
                }
236
                return url;
237
        }
238

    
239
        public String getDownloadURLAsString() {
240
                return this.defaultDownloadURL;
241
        }
242

    
243
        public URL getDownloadURL(URL baseURL) {
244
                String URLString = baseURL.toString();
245

    
246
                if (!URLString.endsWith("/")) {
247
                        URLString += "/";
248
                }
249
                URLString += ("dists/" + getGvSIGVersion() + "/" + "packages.gvspki");
250
                try {
251
                        return new URL(URLString);
252
                } catch (MalformedURLException e) {
253
                        // TODO Auto-generated catch block
254
                        e.printStackTrace();
255
                        return null;
256
                }
257
        }
258

    
259
        public void setDownloadURL(URL defaultDownloadURL) {
260
                this.defaultDownloadURL = defaultDownloadURL.toString();
261
        }
262

    
263
        public void setDownloadURL(String defaultDownloadURL) {
264
                this.defaultDownloadURL = defaultDownloadURL;
265
        }
266

    
267
        public String getModelVersion() {
268
                return modelVersion;
269
        }
270

    
271
        public void setModelVersion(String modelVersion) {
272
                this.modelVersion = modelVersion;
273
        }
274

    
275
        public String getOwner() {
276
                return owner;
277
        }
278

    
279
        public void setOwner(String owner) {
280
                this.owner = owner;
281
        }
282

    
283
        public URL getOwnerURL() {
284
                return ownerURL;
285
        }
286

    
287
        public void setOwnerURL(URL sources) {
288
                this.ownerURL = sources;
289
        }
290

    
291
        public URL getSourcesURL() {
292
                return sources;
293
        }
294

    
295
        public void setSourcesURL(URL sources) {
296
                this.sources = sources;
297
        }
298

    
299
        @Override
300
        public String toString() {
301
                StringBuffer buffer = new StringBuffer(super.toString()).append(" (");
302

    
303
                append(buffer, InstallerInfoTags.CODE, getCode());
304
                append(buffer, InstallerInfoTags.NAME, getName());
305
                append(buffer, InstallerInfoTags.DESCRIPTION, getDescription());
306
                append(buffer, InstallerInfoTags.GVSIG_VERSION, getGvSIGVersion());
307
                append(buffer, InstallerInfoTags.VERSION, getVersion());
308
                append(buffer, InstallerInfoTags.BUILD, getBuild());
309
                append(buffer, InstallerInfoTags.OS, getOperatingSystem());
310
                append(buffer, InstallerInfoTags.ARCHITECTURE, getArchitecture());
311
                append(buffer, InstallerInfoTags.JVM, getJavaVM());
312
                append(buffer, InstallerInfoTags.DOWNLOAD_URL, getDownloadURL());
313
                append(buffer, InstallerInfoTags.STATE, getState());
314
                append(buffer, InstallerInfoTags.OFFICIAL, isOfficial());
315
                append(buffer, InstallerInfoTags.TYPE, getType());
316
                append(buffer, InstallerInfoTags.MODEL_VERSION, getModelVersion());
317
                append(buffer, InstallerInfoTags.OWNER, getOwner());
318
                append(buffer, InstallerInfoTags.OWNER_URL, getOwnerURL());
319
                append(buffer, InstallerInfoTags.SOURCES_URL, getSourcesURL());
320
                append(buffer, InstallerInfoTags.DEPENDENCIES, getDependencies());
321
                append(buffer, InstallerInfoTags.WEB_URL, getWebURL());
322
                append(buffer, InstallerInfoTags.CATEGORIES, getCategories());
323

    
324
                return buffer.append(')').toString();
325
        }
326

    
327
        public String toStringCompact() {
328
                // type code version state os arch jvm dep
329
                return String
330
                                .format(
331
                                                "%1$-8.8s %2$-40s %3$-20.20s %4$-5.5s %5$-5.5s %6$-6.6s %7$-5.5s %8$s",
332
                                                this.type, this.code, this.version, this.state,
333
                                                this.operatingSystem, this.architecture, this.javaVM,
334
                                                this.dependencies);
335
        }
336

    
337
        private DefaultPackageInfo append(StringBuffer buffer, String key,
338
                        Object value) {
339
                buffer.append("\n\t").append(key).append(": ").append(
340
                                value == null ? "" : value);
341
                return this;
342
        }
343

    
344
        @Override
345
        public Object clone() throws CloneNotSupportedException {
346
                DefaultPackageInfo clone = (DefaultPackageInfo) super.clone();
347
                clone.auxFiles = new ArrayList<File>(auxFiles);
348
                return clone;
349
        }
350

    
351
        public File downloadFile() throws InstallPackageServiceException {
352
                return this.downloadFile(null);
353
        }
354

    
355
        public class FileDownloadException extends InstallPackageServiceException {
356

    
357
                private static final long serialVersionUID = 8640183295766490512L;
358

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

    
361
                private static final String KEY = "_File_XurlX_download_error";
362

    
363
                public FileDownloadException(URL url, IOException e) {
364
                        super(message, e, KEY, serialVersionUID);
365
                        setValue("url", url.toString());
366
                }
367

    
368
        }
369

    
370
        public File downloadFile(SimpleTaskStatus taskStatus)
371
                        throws InstallPackageServiceException {
372
                Download download = new Download(taskStatus);
373
                try {
374
                        return download.downloadFile(this.getDownloadURL(), null);
375
                } catch (IOException e) {
376
                        throw new FileDownloadException(this.getDownloadURL(), e);
377
                }
378
        }
379

    
380
        public void addFileToCopy(File file) {
381
                auxFiles.add(file);
382
        }
383

    
384
        public File getFileToCopy(int i) {
385
                return auxFiles.get(i);
386
        }
387

    
388
        public void removeFileToCopy(File file) {
389
                auxFiles.remove(file);
390
        }
391

    
392
        public void clearFilesToCopy() {
393
                auxFiles.clear();
394
        }
395

    
396
        public List<File> getFilesToCopy() {
397
                return auxFiles;
398
        }
399

    
400
        public boolean removeInstallFolder(File folder) {
401
                DeleteFile delete = new DeleteFile();
402
                boolean success = delete.delete(folder);
403
                setAntScript(null);
404
                clearFilesToCopy();
405
                return success;
406
        }
407

    
408
        public boolean removeFilesFolder(File folder) {
409
                DeleteFile delete = new DeleteFile();
410
                return delete.delete(folder);
411
        }
412

    
413
        public boolean matchID(String string) {
414
                String id = this.getID();
415
                String[] stringParts = string.split("#");
416

    
417
                if (stringParts.length == 1) {
418

    
419
                        if (stringParts[0].equals(this.getCode())) {
420
                                return true;
421
                        } else {
422
                                return false;
423
                        }
424
                } else {
425
                        if (stringParts.length == 2) {
426
                                if ((stringParts[0] + stringParts[1])
427
                                                .equals((this.getCode() + this.getVersion()))) {
428
                                        return true;
429
                                } else {
430
                                        return true;
431
                                }
432
                        } else {
433
                                if (string.equals(id)) {
434
                                        return true;
435
                                } else {
436
                                        return false;
437
                                }
438
                        }
439
                }
440

    
441
        }
442

    
443
        public Dependencies getDependencies() {
444
                return this.dependencies;
445
        }
446

    
447
        public void setDependencies(Dependencies dependencies) {
448
                this.dependencies = dependencies;
449
        }
450

    
451
        public void setDependencies(String dependencies) {
452
                if (dependencies == null) {
453
                        this.dependencies = null;
454
                } else {
455
                        this.dependencies = new DefaultDependencies().parse(dependencies);
456
                }
457
        }
458

    
459
        @Override
460
        public boolean equals(Object obj) {
461
                PackageInfo other;
462
                try {
463
                        other = (PackageInfo) obj;
464
                } catch (Exception e) {
465
                        return false;
466
                }
467
                if (!code.equalsIgnoreCase(other.getCode())) {
468
                        return false;
469
                }
470
                if (!version.check("=", other.getVersion())) {
471
                        return false;
472
                }
473
                if (!operatingSystem.equalsIgnoreCase(other.getOperatingSystem())) {
474
                        return false;
475
                }
476
                if (!gvSIGVersion.equalsIgnoreCase(other.getGvSIGVersion())) {
477
                        return false;
478
                }
479
                if (!state.equalsIgnoreCase(other.getState())) {
480
                        return false;
481
                }
482
                if (!architecture.equalsIgnoreCase(other.getArchitecture())) {
483
                        return false;
484
                }
485
                if (!javaVM.equalsIgnoreCase(other.getJavaVM())) {
486
                        return false;
487
                }
488
                if (!type.equalsIgnoreCase(other.getType())) {
489
                        return false;
490
                }
491
                if (official != other.isOfficial()) {
492
                        return false;
493
                }
494
                return true;
495
        }
496

    
497
        public URL getWebURL() {
498
                return webURL;
499
        }
500

    
501
        public void setWebURL(URL webURL) {
502
                this.webURL = webURL;
503
        }
504

    
505
        public List<String> getCategories() {
506
                return this.categories;
507
        }
508

    
509
        public void setCategories(List<String> categoriesList) {
510
                for (int i = 0; i < categoriesList.size(); i++) {
511
                        if (!this.categories.contains(categoriesList.get(i))) {
512
                                this.categories.add(categoriesList.get(i));
513
                        }
514
                }
515
        }
516

    
517
        public String getCategoriesAsString() {
518
                String categoriesString = "";
519
                for (int i = 0; i < this.categories.size(); i++) {
520
                        if (i + 1 < this.categories.size()) {
521
                                categoriesString += this.categories.get(i) + ", ";
522
                        } else {
523
                                categoriesString += this.categories.get(i);
524
                        }
525
                }
526
                return categoriesString;
527
        }
528

    
529
        public void addCategoriesAsString(String categoriesString) {
530
                if( categoriesString == null ) {
531
                        return;
532
                }
533
                categoriesString.trim();
534
                String[] cadena = categoriesString.split(",");
535

    
536
                for (int i = 0; i < cadena.length; i++) {
537
                        String trimCadena = cadena[i].trim();
538
                        if ((!this.categories.contains(trimCadena)) && (trimCadena != "")) {
539
                                this.categories.add(trimCadena);
540
                        }
541
                }
542

    
543
        }
544

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

    
584
        public boolean isBroken() {
585
                if( this.broken ) {
586
                        return true;
587
                }
588
//                if( this.isOfficial() && !this.isSigned() ) {
589
//                        return true;
590
//                }
591
                return false;
592
        }
593

    
594
        public boolean isSigned() {
595
                return signed;
596
        }
597

    
598
}