diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index fb427cbe1ae5810326051259e3e4344dc1d345b9..319c825a74a3b1ac0bb6388fafb5152a827c81e4 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -80,6 +80,16 @@ public final class PluginManager { LOGGER.log(Level.SEVERE, "Failed to load a plug-in " + arc, e); } } + + for (PluginWrapper p : activePlugins.toArray(new PluginWrapper[0])) + try { + p.load(this); + } catch (IOException e) { + failedPlugins.add(new FailedPlugin(p.getShortName(),e)); + LOGGER.log(Level.SEVERE, "Failed to load a plug-in " + p.getShortName(), e); + activePlugins.remove(p); + plugins.remove(p); + } } public List getPlugins() { diff --git a/core/src/main/java/hudson/PluginWrapper.java b/core/src/main/java/hudson/PluginWrapper.java index 83d136b63d8d8e3181810d1900e827ef420fd0bd..70c720fb80b8721ee161e88741e54637a4524655 100644 --- a/core/src/main/java/hudson/PluginWrapper.java +++ b/core/src/main/java/hudson/PluginWrapper.java @@ -56,7 +56,7 @@ public final class PluginWrapper { * Loaded plugin instance. * Null if disabled. */ - public final Plugin plugin; + private Plugin plugin; /** * {@link ClassLoader} for loading classes from this plugin. @@ -82,6 +82,27 @@ public final class PluginWrapper { */ private final String shortName; + /** + * True if this plugin is activated for this session. + * The snapshot of disableFile.exists() as of the start up. + */ + private final boolean active; + + private final File archive; + + private final List dependencies = new ArrayList(); + + private static final class Dependency { + public final String shortName; + public final String version; + + public Dependency(String s) { + int idx = s.indexOf(':'); + this.shortName = s.substring(0,idx); + this.version = s.substring(idx+1); + } + } + /** * @param archive * A .hpi archive file jar file, or a .hpl linked plugin. @@ -92,6 +113,7 @@ public final class PluginWrapper { */ public PluginWrapper(PluginManager owner, File archive) throws IOException { LOGGER.info("Loading plugin: "+archive); + this.archive = archive; this.shortName = getShortName(archive); @@ -161,15 +183,33 @@ public final class PluginWrapper { disableFile = new File(archive.getPath()+".disabled"); if(disableFile.exists()) { LOGGER.info("Plugin is disabled"); - this.plugin = null; - return; + this.active = false; + } else { + this.active = true; } + // compute dependencies + String v = Util.fixNull(manifest.getMainAttributes().getValue("Plugin-Dependencies")); + for(String s : v.split(",")) + dependencies.add(new Dependency(s)); + } + + /** + * Loads the plugin and starts it. + * + *

+ * This should be done after all the classloaders are constructed for + * all the plugins, so that dependencies can be properly loaded by plugins. + */ + /*package*/ void load(PluginManager owner) throws IOException { String className = manifest.getMainAttributes().getValue("Plugin-Class"); if(className ==null) { throw new IOException("Plugin installation failed. No 'Plugin-Class' entry in the manifest of "+archive); } + if(!active) + return; + // 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(); @@ -248,6 +288,13 @@ public final class PluginWrapper { return shortName; } + /** + * Gets the instance of {@link Plugin} contributed by this plugin. + */ + public Plugin getPlugin() { + return plugin; + } + @Override public String toString() { return "Plugin:" + getShortName(); @@ -321,7 +368,7 @@ public final class PluginWrapper { * Returns true if this plugin is enabled for this session. */ public boolean isActive() { - return plugin!=null; + return active; } /** diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index f19afd80601c9b9ae0e699d69f39a4d9c498d218..2382dc0bffe4c6a6fcbedcc626a36009d53a8737 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -309,7 +309,7 @@ public final class Hudson extends View implements ItemGroup, Node public Plugin getPlugin(String shortName) { PluginWrapper p = pluginManager.getPlugin(shortName); if(p==null) return null; - return p.plugin; + return p.getPlugin(); } /**