1 11 package org.eclipse.update.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.lang.reflect.Method ; 19 import java.net.URL ; 20 import java.net.URLClassLoader ; 21 import java.util.StringTokenizer ; 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 EXT_PLUGIN = "org.eclipse.update.core"; private static final String UI_PLUGIN = "org.eclipse.ui"; private static final String EXT_POINT = "installHandlers"; private Method nonPluginDataAcceptor = null; 55 56 60 private static class InstallHandlerClassLoader extends URLClassLoader { 61 private Bundle updateCore; 62 private Bundle eclipseUI; 63 64 public InstallHandlerClassLoader(URL [] 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 loadClass(String className) throws ClassNotFoundException { 73 Class c = null; 75 try { 76 c = updateCore.loadClass(className); 77 } catch (ClassNotFoundException e) { 78 try { 79 if(eclipseUI != null) 80 c = eclipseUI.loadClass(className); 81 } catch (ClassNotFoundException 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 getResource(String resName) { 93 URL 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 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 if (feature == null) 128 throw new IllegalArgumentException (); 129 this.feature = feature; 130 this.type = type; 131 132 if (entry == null) { 134 if (DEBUG) 135 debug("not specified"); return; } 138 139 String library = entry.getLibrary(); 140 String handlerName = entry.getHandlerName(); 141 if (handlerName == null || handlerName.trim().equals("")) { if (DEBUG) 143 debug("not specified"); return; } 146 if (DEBUG) { 147 debug("handler=" + handlerName); debug("path= " + library); } 150 151 try { 153 if (library == null || library.trim().equals("")) 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 e) { 161 handleExceptionInInit( 162 NLS.bind(Messages.InstallHandler_notFound, (new String [] { feature.getLabel() })), 163 e); 164 165 } catch (ClassCastException e) { 166 handleExceptionInInit( 167 NLS.bind(Messages.InstallHandler_invalidHandler, (new String [] { feature.getLabel() })), 168 e); 169 } catch (CoreException e) { 170 handleExceptionInInit(null, e); 171 } catch (Exception e) { 172 handleExceptionInInit( 173 NLS.bind(Messages.InstallHandler_unableToCreateHandler, (new String [] { feature.getLabel() })), 174 e); 175 } 176 177 } 178 179 182 public void installInitiated() throws CoreException { 183 if (handler == null) 184 return; 185 else { 186 try { 187 if (DEBUG) 188 debug("calling installInitiated()"); handler.installInitiated(); 190 } catch (Throwable e) { 191 handleExceptionInCall(e, feature); 192 } 193 } 194 } 195 196 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()"); handler.pluginsDownloaded(plugins); 207 } catch (Throwable e) { 208 handleExceptionInCall(e, feature); 209 } 210 } 211 } 212 213 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()"); handler.completeInstall(consumer); 225 } catch (Throwable e) { 226 handleExceptionInCall(e, feature); 227 } 228 } 229 } 230 231 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()"); handler.nonPluginDataDownloaded(nonPluginData, listener); 245 } catch (Throwable e) { 246 handleExceptionInCall(e, feature); 247 } 248 } 249 } 250 251 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()"); handler.installCompleted(success); 262 } catch (Throwable e) { 263 handleExceptionInCall(e, feature); 264 } 265 } 266 } 267 268 271 public void configureInitiated() throws CoreException { 272 if (handler == null) 273 return; 274 else { 275 try { 276 if (DEBUG) 277 debug("calling configureInitiated()"); handler.configureInitiated(); 279 } catch (Throwable e) { 280 handleExceptionInCall(e, feature); 281 } 282 } 283 } 284 285 288 public void completeConfigure() throws CoreException { 289 if (handler == null) 290 return; 291 else { 292 try { 293 if (DEBUG) 294 debug("calling completeConfigure()"); handler.completeConfigure(); 296 } catch (Throwable e) { 297 handleExceptionInCall(e, feature); 298 } 299 } 300 } 301 302 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()"); handler.configureCompleted(success); 313 } catch (Throwable e) { 314 handleExceptionInCall(e, feature); 315 } 316 } 317 } 318 319 322 public void unconfigureInitiated() throws CoreException { 323 if (handler == null) 324 return; 325 else { 326 try { 327 if (DEBUG) 328 debug("calling unconfigureInitiated()"); handler.unconfigureInitiated(); 330 } catch (Throwable e) { 331 handleExceptionInCall(e, feature); 332 } 333 } 334 } 335 336 339 public void completeUnconfigure() throws CoreException { 340 if (handler == null) 341 return; 342 else { 343 try { 344 if (DEBUG) 345 debug("calling completeUnconfigure()"); handler.completeUnconfigure(); 347 } catch (Throwable e) { 348 handleExceptionInCall(e, feature); 349 } 350 } 351 } 352 353 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); } else { 363 try { 364 if (DEBUG) 365 debug("calling unconfigureCompleted()"); handler.unconfigureCompleted(success); 367 } catch (Throwable e) { 368 handleExceptionInCall(e, feature); 369 } 370 if (savedStatus != null) 371 throw new CoreException(savedStatus); } 373 } 374 375 378 public void uninstallInitiated() throws CoreException { 379 if (handler == null) 380 return; 381 else { 382 try { 383 if (DEBUG) 384 debug("calling uninstallInitiated()"); handler.uninstallInitiated(); 386 } catch (Throwable e) { 387 handleExceptionInCall(e, feature); 388 } 389 } 390 } 391 392 395 public void completeUninstall() throws CoreException { 396 if (handler == null) 397 return; 398 else { 399 try { 400 if (DEBUG) 401 debug("calling completeUninstall()"); handler.completeUninstall(); 403 } catch (Throwable e) { 404 handleExceptionInCall(e, feature); 405 } 406 } 407 } 408 409 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); } else { 419 try { 420 if (DEBUG) 421 debug("calling uninstallCompleted()"); handler.uninstallCompleted(success); 423 } catch (Throwable e) { 424 handleExceptionInCall(e, feature); 425 } 426 if (savedStatus != null) 427 throw new CoreException(savedStatus); } 429 } 430 431 434 private void handleExceptionInInit(String s, Exception 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 String id = 446 UpdateCore.getPlugin().getBundle().getSymbolicName(); 447 IStatus status = 448 new Status(IStatus.ERROR, id, 0, "InstallHandler.deactivated", ce); UpdateCore.getPlugin().getLog().log(status); 450 handler = null; savedStatus = status; 452 } else 453 throw ce; 455 } 456 457 460 private void handleExceptionInCall(Throwable 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 [] { feature.getLabel() })), 470 e); 471 472 if (isUndoAction()) { 473 String id = 475 UpdateCore.getPlugin().getBundle().getSymbolicName(); 476 IStatus status = 477 new Status(IStatus.ERROR, id, 0, "InstallHandler.deactivated", ce); UpdateCore.getPlugin().getLog().log(status); 479 handler = null; savedStatus = status; 481 } else 482 throw ce; 484 } 485 486 490 private boolean isUndoAction() { 491 if (this.type == IInstallHandler.HANDLER_ACTION_INSTALL 492 || this.type == IInstallHandler.HANDLER_ACTION_CONFIGURE) 493 return false; else 495 return true; } 497 498 501 private IInstallHandler getLocalHandler(String libs, String name) 502 throws IOException , CoreException, ClassNotFoundException , InstantiationException , IllegalAccessException { 503 504 ContentReference baseRef = 509 feature.getFeatureContentProvider().getFeatureManifestReference(null); 510 URL 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 [] { this.feature.getLabel() })), 516 null); 517 518 519 StringTokenizer libraries = new StringTokenizer (libs, ","); URL [] cp = new URL [libraries.countTokens()]; 522 for( int token = 0; token < cp.length; token++) { 523 cp[token] = new URL (base, libraries.nextToken()); 524 } 525 if (this.type == IInstallHandler.HANDLER_ACTION_UNINSTALL) { 526 URL [] jars = new URL [cp.length]; 529 for( int jar = 0; jar < cp.length; jar++) { 530 File tempLib = File.createTempFile("tmp" + jar, ".jar"); tempLib.deleteOnExit(); 532 FileOutputStream fos = null; 533 InputStream is = null; 534 try { 535 fos = new FileOutputStream (tempLib); 536 is = new FileInputStream (cp[jar].getPath()); 537 Utilities.copy(is, fos, null); 538 } finally { 539 if (fos != null) 540 try { 541 fos.close(); 542 } catch (Exception e) { 543 } 544 if (is != null) 545 try { 546 is.close(); 547 } catch (Exception e) { 548 } 549 } 550 jars[jar] = tempLib.toURL(); 551 } 552 cp = jars; 553 } 554 555 ClassLoader loader = new InstallHandlerClassLoader(cp); 557 Class clazz = loader.loadClass(name); 558 IInstallHandler handler = (IInstallHandler) clazz.newInstance(); 559 return handler; 560 } 561 562 565 private IInstallHandler getGlobalHandler(String name) throws Exception { 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 [] { this.feature.getLabel() })), 573 null); 574 575 return (IInstallHandler) handlerExtension[0].createExecutableExtension("class"); } 577 578 private void debug(String s) { 579 String pfx = (feature==null) ? "" : feature.getVersionedIdentifier().toString(); System.out.println("InstallHandler["+pfx+"]: " + s); } 582 583 public boolean acceptNonPluginData(INonPluginEntry data) { 584 Boolean result = new Boolean (true); 585 if (handler != null){ 586 if (DEBUG) 587 debug("calling acceptNonPluginData()"); if(handler instanceof IInstallHandlerWithFilter) 589 return ((IInstallHandlerWithFilter)handler).acceptNonPluginData(data); 590 else{ if(getNonPluginDataAcceptor() != null){ 592 try{ 593 Object [] param = {data}; 594 result = (Boolean )getNonPluginDataAcceptor().invoke(handler,param); 595 }catch(Exception e){ 596 } 598 } 599 } 600 } 601 return result.booleanValue(); 602 } 603 private Method getNonPluginDataAcceptor(){ 604 if(nonPluginDataAcceptor == null){ 605 try{ 606 Class [] types = {INonPluginEntry.class}; 607 nonPluginDataAcceptor = handler.getClass().getMethod("acceptNonPluginData",types); }catch(NoSuchMethodException nsme){ 609 } 610 } 611 return nonPluginDataAcceptor; 612 } 613 } 614 | Popular Tags |