KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > DefaultInstallHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.core;
12
13
14 import java.io.*;
15 import java.util.jar.*;
16
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.update.core.*;
20 import org.eclipse.update.core.JarContentReference.*;
21 import org.eclipse.update.core.model.*;
22
23 /**
24  * Default Implementation of InstallHandler
25  */

26 public class DefaultInstallHandler extends BaseInstallHandler {
27
28     /*
29      * @see IInstallHandler#nonPluginDataDownloaded(INonPluginEntry[], IVerificationListener)
30      */

31     public void nonPluginDataDownloaded(
32         INonPluginEntry[] nonPluginData,
33         IVerificationListener listener)
34         throws CoreException {
35
36         // verify non-plugin archives. The DefaultInstallHandler assumes
37
// the verifier associated with the feature is able to verify the
38
// data archives.
39
if (nonPluginData == null || nonPluginData.length == 0)
40             return;
41
42         this.nonPluginEntries = nonPluginData;
43         IFeatureContentProvider provider = this.feature.getFeatureContentProvider();
44         IVerifier verifier = provider.getVerifier();
45         if (verifier == null)
46             return;
47
48         for (int i = 0; i < this.nonPluginEntries.length; i++) {
49             ContentReference[] archives =
50                 provider.getNonPluginEntryArchiveReferences(nonPluginEntries[i], this.monitor);
51             IVerificationResult result;
52             for (int j = 0; j < archives.length; j++) {
53
54                 // see if the data entry is a jar
55
ContentReference archive = archives[j];
56                 if (!(archives[j] instanceof JarContentReference)
57                     && archives[j].getIdentifier().endsWith(".jar")) { //$NON-NLS-1$
58
try {
59                         archive =
60                             new JarContentReference(archives[j].getIdentifier(), archives[j].asFile());
61                     } catch (IOException e) {
62                     }
63                 }
64
65                 result = verifier.verify(this.feature, archive, false, this.monitor);
66                 if (result != null)
67                     promptForVerification(result, listener);
68             }
69         }
70     }
71
72     /*
73      * @see IInstallHandler#completeInstall(IFeatureContentConsumer)
74      */

75     public void completeInstall(IFeatureContentConsumer consumer)
76         throws CoreException {
77
78         // plugins have been installed. Check to see if we have any
79
// non-plugin entries that need to be handled.
80
if (this.nonPluginEntries == null || this.nonPluginEntries.length == 0)
81             return;
82
83         // install non-plugin archives
84
IFeatureContentProvider provider = this.feature.getFeatureContentProvider();
85         for (int i = 0; i < this.nonPluginEntries.length; i++) {
86             ContentReference[] archive =
87                 provider.getNonPluginEntryArchiveReferences(nonPluginEntries[i], this.monitor);
88             IContentConsumer nonPluginConsumer = consumer.open(nonPluginEntries[i]);
89             for (int j = 0; j < archive.length; j++) {
90                 String JavaDoc id = archive[j].getIdentifier();
91                 if (id.endsWith(".jar")) { //$NON-NLS-1$
92
// the non-plugin archive is a jar. Unpack it into
93
// a directory constructed using the archive id
94
try {
95                         final String JavaDoc prefix = id.substring(0, id.length() - 4) + "/"; //$NON-NLS-1$
96
JarContentReference jarRef = new JarContentReference("", archive[j].asFile()); //$NON-NLS-1$
97
ContentSelector selector = new ContentSelector() {
98                             public String JavaDoc defineIdentifier(JarEntry entry) {
99                                 if (entry == null)
100                                     return null;
101                                 else
102                                     return prefix + entry.getName();
103                             }
104                         };
105                         ContentReference[] entries = jarRef.peek(selector, this.monitor);
106                         for (int k = 0; k < entries.length; k++) {
107                             nonPluginConsumer.store(entries[k], this.monitor);
108                         }
109                     } catch (IOException e) {
110                         throw Utilities
111                             .newCoreException(NLS.bind(Messages.JarVerificationService_CancelInstall, (new String JavaDoc[] { id })),
112                         e);
113                     }
114
115                 } else {
116                     // the non-plugin archive is not a jar. Install it asis.
117
nonPluginConsumer.store(archive[j], this.monitor);
118                 }
119             }
120             nonPluginConsumer.close();
121         }
122     }
123
124     /*
125      *
126      */

127     private void promptForVerification(
128         IVerificationResult verificationResult,
129         IVerificationListener listener)
130         throws CoreException {
131
132         if (listener == null)
133             return;
134         int result = listener.prompt(verificationResult);
135
136         if (result == IVerificationListener.CHOICE_ABORT) {
137             Exception JavaDoc e = verificationResult.getVerificationException();
138             throw new InstallAbortedException(Messages.JarVerificationService_CancelInstall,e);
139         }
140         if (result == IVerificationListener.CHOICE_ERROR) {
141             throw Utilities
142                 .newCoreException(Messages.JarVerificationService_UnsucessfulVerification,
143             verificationResult.getVerificationException());
144         }
145
146         return;
147     }
148 }
149
Popular Tags