From 4de81f1eb3a63c5e4206ce99abe761e6d5a7e279 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 16 Nov 2006 15:34:59 +0000 Subject: [PATCH] fixed a bug where XStream activity in plugins wasn't able to resolve classes. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1131 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/PluginManager.java | 10 +++ core/src/main/java/hudson/PluginWrapper.java | 64 +++++++++++--------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index 30297df3c0..5a6fc787a0 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -119,6 +119,16 @@ public final class PluginManager { @Override protected Class findClass(String name) throws ClassNotFoundException { + // first, use the context classloader so that plugins that are loading + // can use its own classloader first. + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if(cl!=null) + try { + return cl.loadClass(name); + } catch(ClassNotFoundException e) { + // not found. try next + } + for (PluginWrapper p : activePlugins) { try { return p.classLoader.loadClass(name); diff --git a/core/src/main/java/hudson/PluginWrapper.java b/core/src/main/java/hudson/PluginWrapper.java index 4b30cb4ee9..7a82565a04 100644 --- a/core/src/main/java/hudson/PluginWrapper.java +++ b/core/src/main/java/hudson/PluginWrapper.java @@ -171,37 +171,45 @@ public final class PluginWrapper { throw new IOException("Plugin installation failed. No 'Plugin-Class' entry in the manifest of "+archive); } + // override the context classloader so that XStream activity in plugin.start() + // will be able to resolve classes in this plugin + ClassLoader old = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); try { - Class clazz = classLoader.loadClass(className); - Object plugin = clazz.newInstance(); - if(!(plugin instanceof Plugin)) { - throw new IOException(className+" doesn't extend from hudson.Plugin"); + try { + Class clazz = classLoader.loadClass(className); + Object plugin = clazz.newInstance(); + if(!(plugin instanceof Plugin)) { + throw new IOException(className+" doesn't extend from hudson.Plugin"); + } + this.plugin = (Plugin)plugin; + this.plugin.wrapper = this; + } catch (ClassNotFoundException e) { + IOException ioe = new IOException("Unable to load " + className + " from " + archive); + ioe.initCause(e); + throw ioe; + } catch (IllegalAccessException e) { + IOException ioe = new IOException("Unable to create instance of " + className + " from " + archive); + ioe.initCause(e); + throw ioe; + } catch (InstantiationException e) { + IOException ioe = new IOException("Unable to create instance of " + className + " from " + archive); + ioe.initCause(e); + throw ioe; } - this.plugin = (Plugin)plugin; - this.plugin.wrapper = this; - } catch (ClassNotFoundException e) { - IOException ioe = new IOException("Unable to load " + className + " from " + archive); - ioe.initCause(e); - throw ioe; - } catch (IllegalAccessException e) { - IOException ioe = new IOException("Unable to create instance of " + className + " from " + archive); - ioe.initCause(e); - throw ioe; - } catch (InstantiationException e) { - IOException ioe = new IOException("Unable to create instance of " + className + " from " + archive); - ioe.initCause(e); - throw ioe; - } - // initialize plugin - try { - plugin.setServletContext(owner.context); - plugin.start(); - } catch(Throwable t) { - // gracefully handle any error in plugin. - IOException ioe = new IOException("Failed to initialize"); - ioe.initCause(t); - throw ioe; + // initialize plugin + try { + plugin.setServletContext(owner.context); + plugin.start(); + } catch(Throwable t) { + // gracefully handle any error in plugin. + IOException ioe = new IOException("Failed to initialize"); + ioe.initCause(t); + throw ioe; + } + } finally { + Thread.currentThread().setContextClassLoader(old); } } -- GitLab