KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionRegistry;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.update.core.ContentReference;
31 import org.eclipse.update.core.IFeature;
32 import org.eclipse.update.core.IFeatureContentConsumer;
33 import org.eclipse.update.core.IInstallHandler;
34 import org.eclipse.update.core.IInstallHandlerEntry;
35 import org.eclipse.update.core.IInstallHandlerWithFilter;
36 import org.eclipse.update.core.INonPluginEntry;
37 import org.eclipse.update.core.IPluginEntry;
38 import org.eclipse.update.core.IVerificationListener;
39 import org.eclipse.update.core.InstallMonitor;
40 import org.eclipse.update.core.Utilities;
41 import org.osgi.framework.Bundle;
42
43 public class InstallHandlerProxy implements IInstallHandlerWithFilter {
44
45     private IFeature feature = null;
46     private int type;
47     private IInstallHandler handler = null;
48     private IStatus savedStatus = null;
49     private boolean DEBUG = false;
50
51     private static final String JavaDoc EXT_PLUGIN = "org.eclipse.update.core"; //$NON-NLS-1$
52
private static final String JavaDoc UI_PLUGIN = "org.eclipse.ui"; //$NON-NLS-1$
53
private static final String JavaDoc EXT_POINT = "installHandlers"; //$NON-NLS-1$
54
private Method JavaDoc nonPluginDataAcceptor = null;
55
56     /**
57      * A class loader that combines a the org.eclipse.update.core plugin class loader with the
58      * org.eclipse.ui class loader (only when UI is active).
59      */

60     private static class InstallHandlerClassLoader extends URLClassLoader JavaDoc {
61         private Bundle updateCore;
62         private Bundle eclipseUI;
63
64         public InstallHandlerClassLoader(URL JavaDoc[] classpath) {
65             super(classpath);
66             updateCore = Platform.getBundle(EXT_PLUGIN);
67             eclipseUI = Platform.getBundle(UI_PLUGIN);
68             if (eclipseUI != null && eclipseUI.getState() != Bundle.ACTIVE)
69                 eclipseUI = null;
70         }
71
72         public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
73             // First check update core plugin loader, then the eclipse ui plugin loader
74
Class JavaDoc c = null;
75             try {
76                 c = updateCore.loadClass(className);
77             } catch (ClassNotFoundException JavaDoc e) {
78                 try {
79                     if(eclipseUI != null)
80                         c = eclipseUI.loadClass(className);
81                 } catch (ClassNotFoundException JavaDoc e2) {
82                 } finally {
83                 }
84             } finally {
85             }
86             if (c != null)
87                 return c;
88             else
89                 return super.loadClass(className);
90         }
91
92         public URL JavaDoc getResource(String JavaDoc resName) {
93             // First check update core plugin loader, then the eclipse ui plugin loader
94
URL JavaDoc u = updateCore.getResource(resName);
95             if(u == null && eclipseUI != null)
96                 u = eclipseUI.getResource(resName);
97                 
98             if (u != null)
99                 return u;
100             else
101                 return super.getResource(resName);
102         }
103     }
104     
105     public InstallHandlerProxy(
106         int type,
107         IFeature feature,
108         IInstallHandlerEntry entry,
109         InstallMonitor monitor)
110         throws CoreException {
111
112         initialize(type, feature, entry, monitor);
113     }
114
115     /*
116      * @see IInstallHandler#initialize
117      */

118     public void initialize(
119         int type,
120         IFeature feature,
121         IInstallHandlerEntry entry,
122         InstallMonitor monitor)
123         throws CoreException {
124
125         DEBUG = UpdateCore.DEBUG_SHOW_IHANDLER;
126         // validate arguments
127
if (feature == null)
128             throw new IllegalArgumentException JavaDoc();
129         this.feature = feature;
130         this.type = type;
131
132         // check if we have a handler entry specified in the feature.xml
133
if (entry == null) {
134             if (DEBUG)
135                 debug("not specified"); //$NON-NLS-1$
136
return; // no handler entry
137
}
138
139         String JavaDoc library = entry.getLibrary();
140         String JavaDoc handlerName = entry.getHandlerName();
141         if (handlerName == null || handlerName.trim().equals("")) { //$NON-NLS-1$
142
if (DEBUG)
143                 debug("not specified"); //$NON-NLS-1$
144
return; // no handler class spacified in entry
145
}
146         if (DEBUG) {
147             debug("handler=" + handlerName); //$NON-NLS-1$
148
debug("path= " + library); //$NON-NLS-1$
149
}
150
151         // get handler instance
152
try {
153             if (library == null || library.trim().equals("")) //$NON-NLS-1$
154
this.handler = getGlobalHandler(handlerName);
155             else
156                 this.handler = getLocalHandler(library, handlerName);
157             if (this.handler == null)
158                 return;
159             handler.initialize(type, feature, entry, monitor);
160         } catch (ClassNotFoundException JavaDoc e) {
161             handleExceptionInInit(
162                 NLS.bind(Messages.InstallHandler_notFound, (new String JavaDoc[] { feature.getLabel() })),
163                 e);
164
165         } catch (ClassCastException JavaDoc e) {
166             handleExceptionInInit(
167                 NLS.bind(Messages.InstallHandler_invalidHandler, (new String JavaDoc[] { feature.getLabel() })),
168                 e);
169         } catch (CoreException e) {
170             handleExceptionInInit(null, e);
171         } catch (Exception JavaDoc e) {
172             handleExceptionInInit(
173                 NLS.bind(Messages.InstallHandler_unableToCreateHandler, (new String JavaDoc[] { feature.getLabel() })),
174                 e);
175         }
176
177     }
178
179     /*
180      * @see IInstallHandler#installInitiated
181      */

182     public void installInitiated() throws CoreException {
183         if (handler == null)
184             return;
185         else {
186             try {
187                 if (DEBUG)
188                     debug("calling installInitiated()"); //$NON-NLS-1$
189
handler.installInitiated();
190             } catch (Throwable JavaDoc e) {
191                 handleExceptionInCall(e, feature);
192             }
193         }
194     }
195
196     /*
197      * @see IInstallHandler#allPluginsDownloaded
198      */

199     public void pluginsDownloaded(IPluginEntry[] plugins) throws CoreException {
200         if (handler == null)
201             return;
202         else {
203             try {
204                 if (DEBUG)
205                     debug("calling pluginsDownloaded()"); //$NON-NLS-1$
206
handler.pluginsDownloaded(plugins);
207             } catch (Throwable JavaDoc e) {
208                 handleExceptionInCall(e, feature);
209             }
210         }
211     }
212
213     /*
214      * @see IInstallHandler#allPluginsInstalled
215      */

216     public void completeInstall(IFeatureContentConsumer consumer)
217         throws CoreException {
218         if (handler == null)
219             return;
220         else {
221             try {
222                 if (DEBUG)
223                     debug("calling completeInstall()"); //$NON-NLS-1$
224
handler.completeInstall(consumer);
225             } catch (Throwable JavaDoc e) {
226                 handleExceptionInCall(e, feature);
227             }
228         }
229     }
230
231     /*
232      * @see IInstallHandler#allDataDownloaded
233      */

234     public void nonPluginDataDownloaded(
235         INonPluginEntry[] nonPluginData,
236         IVerificationListener listener)
237         throws CoreException {
238         if (handler == null)
239             return;
240         else {
241             try {
242                 if (DEBUG)
243                     debug("calling nonPluginDataDownloaded()"); //$NON-NLS-1$
244
handler.nonPluginDataDownloaded(nonPluginData, listener);
245             } catch (Throwable JavaDoc e) {
246                 handleExceptionInCall(e, feature);
247             }
248         }
249     }
250
251     /*
252      * @see IInstallHandler#installCompleted
253      */

254     public void installCompleted(boolean success) throws CoreException {
255         if (handler == null)
256             return;
257         else {
258             try {
259                 if (DEBUG)
260                     debug("calling installCompleted()"); //$NON-NLS-1$
261
handler.installCompleted(success);
262             } catch (Throwable JavaDoc e) {
263                 handleExceptionInCall(e, feature);
264             }
265         }
266     }
267
268     /*
269      * @see IInstallHandler#configureInitiated
270      */

271     public void configureInitiated() throws CoreException {
272         if (handler == null)
273             return;
274         else {
275             try {
276                 if (DEBUG)
277                     debug("calling configureInitiated()"); //$NON-NLS-1$
278
handler.configureInitiated();
279             } catch (Throwable JavaDoc e) {
280                 handleExceptionInCall(e, feature);
281             }
282         }
283     }
284
285     /*
286      * @see IInstallHandler#completeConfigure
287      */

288     public void completeConfigure() throws CoreException {
289         if (handler == null)
290             return;
291         else {
292             try {
293                 if (DEBUG)
294                     debug("calling completeConfigure()"); //$NON-NLS-1$
295
handler.completeConfigure();
296             } catch (Throwable JavaDoc e) {
297                 handleExceptionInCall(e, feature);
298             }
299         }
300     }
301
302     /*
303      * @see IInstallHandler#configureCompleted
304      */

305     public void configureCompleted(boolean success) throws CoreException {
306         if (handler == null)
307             return;
308         else {
309             try {
310                 if (DEBUG)
311                     debug("calling configureCompleted()"); //$NON-NLS-1$
312
handler.configureCompleted(success);
313             } catch (Throwable JavaDoc e) {
314                 handleExceptionInCall(e, feature);
315             }
316         }
317     }
318
319     /*
320      * @see IInstallHandler#unconfigureInitiated
321      */

322     public void unconfigureInitiated() throws CoreException {
323         if (handler == null)
324             return;
325         else {
326             try {
327                 if (DEBUG)
328                     debug("calling unconfigureInitiated()"); //$NON-NLS-1$
329
handler.unconfigureInitiated();
330             } catch (Throwable JavaDoc e) {
331                 handleExceptionInCall(e, feature);
332             }
333         }
334     }
335
336     /*
337      * @see IInstallHandler#completeUnconfigure
338      */

339     public void completeUnconfigure() throws CoreException {
340         if (handler == null)
341             return;
342         else {
343             try {
344                 if (DEBUG)
345                     debug("calling completeUnconfigure()"); //$NON-NLS-1$
346
handler.completeUnconfigure();
347             } catch (Throwable JavaDoc e) {
348                 handleExceptionInCall(e, feature);
349             }
350         }
351     }
352
353     /*
354      * @see IInstallHandler#unconfigureCompleted
355      */

356     public void unconfigureCompleted(boolean success) throws CoreException {
357         if (handler == null) {
358             if (savedStatus == null)
359                 return;
360             else
361                 throw new CoreException(savedStatus); // delayed exception
362
} else {
363             try {
364                 if (DEBUG)
365                     debug("calling unconfigureCompleted()"); //$NON-NLS-1$
366
handler.unconfigureCompleted(success);
367             } catch (Throwable JavaDoc e) {
368                 handleExceptionInCall(e, feature);
369             }
370             if (savedStatus != null)
371                 throw new CoreException(savedStatus); // delayed exception
372
}
373     }
374
375     /*
376      * @see IInstallHandler#uninstallInitiated
377      */

378     public void uninstallInitiated() throws CoreException {
379         if (handler == null)
380             return;
381         else {
382             try {
383                 if (DEBUG)
384                     debug("calling uninstallInitiated()"); //$NON-NLS-1$
385
handler.uninstallInitiated();
386             } catch (Throwable JavaDoc e) {
387                 handleExceptionInCall(e, feature);
388             }
389         }
390     }
391
392     /*
393      * @see IInstallHandler#completeUninstall
394      */

395     public void completeUninstall() throws CoreException {
396         if (handler == null)
397             return;
398         else {
399             try {
400                 if (DEBUG)
401                     debug("calling completeUninstall()"); //$NON-NLS-1$
402
handler.completeUninstall();
403             } catch (Throwable JavaDoc e) {
404                 handleExceptionInCall(e, feature);
405             }
406         }
407     }
408
409     /*
410      * @see IInstallHandler#uninstallCompleted
411      */

412     public void uninstallCompleted(boolean success) throws CoreException {
413         if (handler == null) {
414             if (savedStatus == null)
415                 return;
416             else
417                 throw new CoreException(savedStatus); // delayed exception
418
} else {
419             try {
420                 if (DEBUG)
421                     debug("calling uninstallCompleted()"); //$NON-NLS-1$
422
handler.uninstallCompleted(success);
423             } catch (Throwable JavaDoc e) {
424                 handleExceptionInCall(e, feature);
425             }
426             if (savedStatus != null)
427                 throw new CoreException(savedStatus); // delayed exception
428
}
429     }
430
431     /*
432      * common exception handling for initialization
433      */

434     private void handleExceptionInInit(String JavaDoc s, Exception JavaDoc e)
435         throws CoreException {
436
437         CoreException ce;
438         if (e instanceof CoreException)
439             ce = (CoreException) e;
440         else
441             ce = Utilities.newCoreException(s, e);
442
443         if (isUndoAction()) {
444             // for "undo" operations, deactivate handler and log error
445
String JavaDoc id =
446                 UpdateCore.getPlugin().getBundle().getSymbolicName();
447             IStatus status =
448                 new Status(IStatus.ERROR, id, 0, "InstallHandler.deactivated", ce); //$NON-NLS-1$
449
UpdateCore.getPlugin().getLog().log(status);
450             handler = null; // disable subsequent handler calls
451
savedStatus = status;
452         } else
453             // for "do" operations, hurl ...
454
throw ce;
455     }
456
457     /*
458      * common exception handling for calls to install handler
459      */

460     private void handleExceptionInCall(Throwable JavaDoc e, IFeature feature)
461         throws CoreException {
462
463         CoreException ce;
464         if (e instanceof CoreException)
465             ce = (CoreException) e;
466         else
467             ce =
468                 Utilities.newCoreException(
469                     NLS.bind(Messages.InstallHandler_callException, (new String JavaDoc[] { feature.getLabel() })),
470                     e);
471         
472         if (isUndoAction()) {
473             // for "undo" operations, deactivate handler and log error
474
String JavaDoc id =
475                 UpdateCore.getPlugin().getBundle().getSymbolicName();
476             IStatus status =
477                 new Status(IStatus.ERROR, id, 0, "InstallHandler.deactivated", ce); //$NON-NLS-1$
478
UpdateCore.getPlugin().getLog().log(status);
479             handler = null; // disable subsequent handler calls
480
savedStatus = status;
481         } else
482             // for "do" operations, hurl ...
483
throw ce;
484     }
485
486     /*
487      * Indicates whether we are doing (install, configure) or
488      * undoing (uninstall, unconfigure)
489      */

490     private boolean isUndoAction() {
491         if (this.type == IInstallHandler.HANDLER_ACTION_INSTALL
492             || this.type == IInstallHandler.HANDLER_ACTION_CONFIGURE)
493             return false; // causes exception to be thrown and action aborted
494
else
495             return true; // causes exception to be logged and action continues
496
}
497
498     /*
499      * get an instance of handler downloaded as part of the feature
500      */

501     private IInstallHandler getLocalHandler(String JavaDoc libs, String JavaDoc name)
502         throws IOException JavaDoc, CoreException, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
503
504         // Get baseline URL for handler (relative to feature.xml). For
505
// features being installed from a server (eg. http protocol)
506
// the URL will most likely be to a local file copy containing the
507
// unpacked feature jar.
508
ContentReference baseRef =
509             feature.getFeatureContentProvider().getFeatureManifestReference(null);
510         URL JavaDoc base = null;
511         if (baseRef != null)
512             base = baseRef.asURL();
513         if (base == null)
514             throw Utilities.newCoreException(
515                 NLS.bind(Messages.InstallHandler_unableToCreateHandler, (new String JavaDoc[] { this.feature.getLabel() })),
516                 null);
517
518
519         // determine loader class path
520
StringTokenizer JavaDoc libraries = new StringTokenizer JavaDoc(libs, ","); //$NON-NLS-1$
521
URL JavaDoc[] cp = new URL JavaDoc[libraries.countTokens()];
522         for( int token = 0; token < cp.length; token++) {
523             cp[token] = new URL JavaDoc(base, libraries.nextToken());
524         }
525         if (this.type == IInstallHandler.HANDLER_ACTION_UNINSTALL) {
526             // check if we are doing uninstall
527
// ... need to make temp copy of library (being removed)
528
URL JavaDoc[] jars = new URL JavaDoc[cp.length];
529             for( int jar = 0; jar < cp.length; jar++) {
530                 File JavaDoc tempLib = File.createTempFile("tmp" + jar, ".jar"); //$NON-NLS-1$ //$NON-NLS-2$
531
tempLib.deleteOnExit();
532                 FileOutputStream JavaDoc fos = null;
533                 InputStream JavaDoc is = null;
534                 try {
535                     fos = new FileOutputStream JavaDoc(tempLib);
536                     is = new FileInputStream JavaDoc(cp[jar].getPath());
537                     Utilities.copy(is, fos, null);
538                 } finally {
539                     if (fos != null)
540                         try {
541                             fos.close();
542                         } catch (Exception JavaDoc e) {
543                         }
544                     if (is != null)
545                         try {
546                             is.close();
547                         } catch (Exception JavaDoc e) {
548                         }
549                 }
550                 jars[jar] = tempLib.toURL();
551             }
552             cp = jars;
553         }
554
555         // create class loader, load and instantiate handler
556
ClassLoader JavaDoc loader = new InstallHandlerClassLoader(cp);
557         Class JavaDoc clazz = loader.loadClass(name);
558         IInstallHandler handler = (IInstallHandler) clazz.newInstance();
559         return handler;
560     }
561
562     /*
563      * get instance of global handler registered via extension point
564      */

565     private IInstallHandler getGlobalHandler(String JavaDoc name) throws Exception JavaDoc {
566
567         IExtensionRegistry reg = Platform.getExtensionRegistry();
568         IConfigurationElement[] handlerExtension =
569             reg.getConfigurationElementsFor(EXT_PLUGIN, EXT_POINT, name);
570         if (handlerExtension == null || handlerExtension.length <= 0)
571             throw Utilities.newCoreException(
572                 NLS.bind(Messages.InstallHandler_unableToCreateHandler, (new String JavaDoc[] { this.feature.getLabel() })),
573                 null);
574
575         return (IInstallHandler) handlerExtension[0].createExecutableExtension("class"); //$NON-NLS-1$
576
}
577     
578     private void debug(String JavaDoc s) {
579         String JavaDoc pfx = (feature==null) ? "" : feature.getVersionedIdentifier().toString(); //$NON-NLS-1$
580
System.out.println("InstallHandler["+pfx+"]: " + s); //$NON-NLS-1$ //$NON-NLS-2$
581
}
582
583     public boolean acceptNonPluginData(INonPluginEntry data) {
584         Boolean JavaDoc result = new Boolean JavaDoc(true);
585         if (handler != null){
586             if (DEBUG)
587                 debug("calling acceptNonPluginData()"); //$NON-NLS-1$
588
if(handler instanceof IInstallHandlerWithFilter)
589                 return ((IInstallHandlerWithFilter)handler).acceptNonPluginData(data);
590             else{ //support upgrade from legacy versions
591
if(getNonPluginDataAcceptor() != null){
592                     try{
593                         Object JavaDoc[] param = {data};
594                         result = (Boolean JavaDoc)getNonPluginDataAcceptor().invoke(handler,param);
595                     }catch(Exception JavaDoc e){
596                         //todo
597
}
598                 }
599             }
600         }
601         return result.booleanValue();
602     }
603     private Method JavaDoc getNonPluginDataAcceptor(){
604         if(nonPluginDataAcceptor == null){
605             try{
606                 Class JavaDoc[] types = {INonPluginEntry.class};
607                 nonPluginDataAcceptor = handler.getClass().getMethod("acceptNonPluginData",types); //$NON-NLS-1$
608
}catch(NoSuchMethodException JavaDoc nsme){
609             }
610         }
611         return nonPluginDataAcceptor;
612     }
613 }
614
Popular Tags