diff --git a/core/pom.xml b/core/pom.xml index 657481f739..f4c031f71b 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -708,6 +708,11 @@ THE SOFTWARE. embedded_su4j 1.1 + + net.java.sezpoz + sezpoz + 1.0 + org.jvnet.hudson jinterop-wmi diff --git a/core/src/main/java/hudson/Extension.java b/core/src/main/java/hudson/Extension.java new file mode 100644 index 0000000000..987d31f3dc --- /dev/null +++ b/core/src/main/java/hudson/Extension.java @@ -0,0 +1,73 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson; + +import net.java.sezpoz.Indexable; + +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.*; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import hudson.ExtensionFinder.Sezpoz; + +/** + * Marks a field, a method, or a class for automatic discovery, so that Hudson can locate + * implementations of {@link ExtensionPoint}s automatically. + * + *

+ * (In contrast, in earlier Hudson, the registration was manual.) + * + *

+ * In a simplest case, put this on your class, and Hudson will create an instance of it + * and register it to the appropriate {@link ExtensionList}. + * + *

+ * If you'd like Hudson to call + * a factory method instead of a constructor, put this annotation on your static factory + * method. Hudson will invoke it and if the method returns a non-null instance, it'll be + * registered. The return type of the method is used to determine which {@link ExtensionList} + * will get the instance. + * + * Finally, you can put this annotation on a static field if the field contains a reference + * to an instance that you'd like to register. + * + *

+ * This is the default way of having your implementations auto-registered to Hudson, + * but Hudson also supports arbitrary DI containers for hosting your implementations. + * See {@link ExtensionFinder} for more details. + * + * @author Kohsuke Kawaguchi + * @since 1.286 + * @see Sezpoz + * @see ExtensionFinder + * @see ExtensionList + */ +@Indexable +@Retention(RetentionPolicy.RUNTIME) +@Target({TYPE, FIELD, METHOD}) +@Documented +public @interface Extension { +} diff --git a/core/src/main/java/hudson/ExtensionFinder.java b/core/src/main/java/hudson/ExtensionFinder.java new file mode 100644 index 0000000000..7040ce45ca --- /dev/null +++ b/core/src/main/java/hudson/ExtensionFinder.java @@ -0,0 +1,115 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson; + +import net.java.sezpoz.Index; +import net.java.sezpoz.IndexItem; +import hudson.model.Hudson; +import hudson.model.Descriptor; + +import java.util.logging.Logger; +import java.util.logging.Level; +import java.util.List; +import java.util.ArrayList; +import java.util.Collection; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * Discovers the implementations of an extension point. + * + *

+ * This extension point allows you to write your implementations of {@link ExtensionPoint}s + * in arbitrary DI containers, and have Hudson discover them. + * + *

+ * {@link ExtensionFinder} itself is an extension point, but to avoid infinite recursion, + * Hudson discovers {@link ExtensionFinder}s through {@link Sezpoz} and that alone. + * + * @author Kohsuke Kawaguchi + * @since 1.286 + */ +public abstract class ExtensionFinder implements ExtensionPoint { + /** + * Discover extensions of the given type. + * + *

+ * This method is called only once per the given type after all the plugins are loaded, + * so implementations need not worry about caching. + * + * @param + * The type of the extension points. This is not bound to {@link ExtensionPoint} because + * of {@link Descriptor}, which by itself doesn't implement {@link ExtensionPoint} for + * a historical reason. + * @param hudson + * Hudson whose behalf this extension finder is performing lookup. + * @return + * Can be empty but never null. + */ + public abstract Collection findExtensions(Class type, Hudson hudson); + + /** + * The default implementation that looks for the {@link Extension} marker. + * + *

+ * Uses Sezpoz as the underlying mechanism. + */ + @Extension + public static final class Sezpoz extends ExtensionFinder { + public Collection findExtensions(Class type, Hudson hudson) { + List result = new ArrayList(); + + ClassLoader cl = hudson.getPluginManager().uberClassLoader; + for (IndexItem item : Index.load(Extension.class, Object.class, cl)) { + try { + AnnotatedElement e = item.element(); + Class extType; + if (e instanceof Class) { + extType = (Class) e; + } else + if (e instanceof Field) { + extType = ((Field)e).getType(); + } else + if (e instanceof Method) { + extType = ((Method)e).getReturnType(); + } else + throw new AssertionError(); + + if(type.isAssignableFrom(extType)) { + Object instance = item.instance(); + if(instance!=null) + result.add(type.cast(instance)); + } + } catch (InstantiationException e) { + LOGGER.log(Level.WARNING, "Failed to load "+item.className(),e); + } + } + + return result; + } + } + + private static final Logger LOGGER = Logger.getLogger(ExtensionFinder.class.getName()); +} diff --git a/core/src/main/java/hudson/ExtensionList.java b/core/src/main/java/hudson/ExtensionList.java new file mode 100644 index 0000000000..d6f2a827f3 --- /dev/null +++ b/core/src/main/java/hudson/ExtensionList.java @@ -0,0 +1,258 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson; + +import hudson.model.Hudson; +import hudson.model.Descriptor; +import hudson.model.Describable; +import hudson.util.DescriptorList; + +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; +import java.util.logging.Logger; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ConcurrentHashMap; +import java.lang.reflect.Type; +import java.lang.reflect.ParameterizedType; + +import org.jvnet.tiger_types.Types; + +/** + * Retains the known extension instances for the given type 'T'. + * + *

+ * Extensions are loaded lazily on demand and automatically by using {@link ExtensionFinder}, but this + * class also provides a mechanism to provide compatibility with the older {@link DescriptorList}-based + * manual registration, + * + *

+ * All {@link ExtensionList} instances should be owned by {@link Hudson}, even though + * extension points can be defined by anyone on any type. Use {@link Hudson#getExtensionList(Class)} + * and {@link Hudson#getDescriptorList(Class)} to obtain the instances. + * + * @param + * Type of the extension point. This class holds instances of the subtypes of 'T'. + * + * @author Kohsuke Kawaguchi + * @since 1.286 + * @see Hudson#getExtensionList(Class) + * @see Hudson#getDescriptorList(Class) + */ +public class ExtensionList implements Iterable { + public final Hudson hudson; + public final Class extensionType; + + /** + * Once discovered, extensions are retained here. + */ + private volatile CopyOnWriteArrayList extensions; + + /** + * Place to store manually registered instances with the per-Hudson scope. + */ + private final List legacyInstances; + + /** + * View of the {@link ExtensionList} as a mutable list. + * + *

+ * Read access on this instance will see the full list that {@link ExtensionList} has, + * and the write access will allow the legacy manual registrations. + */ + private final List listView = new AbstractList() { + @Override + public T get(int index) { + ensureLoaded(); + return extensions.get(index); + } + + @Override + public int size() { + ensureLoaded(); + return extensions.size(); + } + + @Override + public Iterator iterator() { + return ExtensionList.this.iterator(); // performance optimization as this is a common path + } + + @Override + public void add(int index, T element) { + legacyInstances.add(element); + // if we've already filled extensions, add it + if(extensions!=null) + extensions.add(element); + } + }; + + protected ExtensionList(Hudson hudson, Class extensionType) { + this(hudson,extensionType,new Vector()); + } + + /** + * + * @param legacyStore + * Place to store manually registered instances. The version of the constructor that + * omits this uses a new {@link Vector}, making the storage lifespan tied to the life of {@link ExtensionList}. + * If the manually registerd instances are scoped to VM level, the caller should pass in a static list. + */ + protected ExtensionList(Hudson hudson, Class extensionType, List legacyStore) { + this.hudson = hudson; + this.extensionType = extensionType; + this.legacyInstances = legacyStore; + } + + /** + * Looks for the extension instance of the given type (subclasses excluded), + * or return null. + */ + public U of(Class type) { + for (T ext : this) + if(ext.getClass()==type) + return type.cast(ext); + return null; + } + + public Iterator iterator() { + ensureLoaded(); + return extensions.iterator(); + + } + + /** + * Returns {@link ExtensionFinder}s used to search for the extension instances. + */ + protected Iterable finders() { + return hudson.getExtensionList(ExtensionFinder.class); + } + + private void ensureLoaded() { + if(extensions!=null) + return; // already loaded + synchronized (this) { + if(extensions==null) { + List r = load(); + r.addAll(legacyInstances); + extensions = new CopyOnWriteArrayList(r); + } + } + } + + /** + * Loads all the extensions. + */ + protected List load() { + List r = new ArrayList(); + for (ExtensionFinder finder : finders()) + r.addAll(finder.findExtensions(extensionType, hudson)); + return r; + } + + /** + * Provides the {@link List} adapter for the extension list, in case + * the compatibility with older Hudson requires that there be a mutable list. + * + *

+ * Read access to this list will see the same thing as {@link ExtensionList#iterator()}, + * and write acecss will keep the objects in a separate list, so that they can be merged + * to the list of auto-disovered instances. + */ + public List asList() { + return listView; + } + + /** + * {@link ExtensionList} for holding a subset of {@link Descriptor}s, which is a group of descriptors for + * the same extension point. + */ + public static final class DescriptorSubList> extends ExtensionList> { + /** + * Type of the {@link Describable} that this extension list retains. + */ + private final Class describableType; + + public DescriptorSubList(Hudson hudson, Class describableType) { + super(hudson, (Class)Descriptor.class,allocateLegacyInstanceStore(describableType)); + this.describableType = describableType; + } + + /** + * Loading the descriptors in this case means filtering the descriptor from the master {@link ExtensionList}. + */ + @Override + protected List> load() { + List r = new ArrayList(); + for( Descriptor d : hudson.getExtensionList(Descriptor.class) ) { + Type subTyping = Types.getBaseClass(d.getClass(), Descriptor.class); + if (!(subTyping instanceof ParameterizedType)) { + LOGGER.severe(d.getClass()+" doesn't extend Descriptor with a type parameter"); + continue; // skip this one + } + if(Types.getTypeArgument(subTyping,0)==describableType) + r.add(d); + } + return r; + } + + /** + * Obtain the statically-scoped storage to store manulaly registered {@link Descriptor}s. + */ + private static List allocateLegacyInstanceStore(Class describableType) { + List r = legacyDescriptors.get(describableType); + if(r!=null) return r; + + r = new CopyOnWriteArrayList(); + List x = legacyDescriptors.putIfAbsent(describableType, r); + if(x==null) return r; + return x; + } + + /** + * Stores manually registered Descriptor instances. + */ + private static final ConcurrentHashMap legacyDescriptors = new ConcurrentHashMap(); + } + + public static ExtensionList create(Hudson hudson, Class type) { + if(type==ExtensionFinder.class) + return new ExtensionList(hudson,type) { + /** + * If this ExtensionList is searching for ExtensionFinders, calling hudosn.getExtensionList + * results in infinite recursion. + */ + @Override + protected Iterable finders() { + return Collections.singleton(new ExtensionFinder.Sezpoz()); + } + }; + return new ExtensionList(hudson,type); + } + + private static final Logger LOGGER = Logger.getLogger(ExtensionList.class.getName()); +} diff --git a/core/src/main/java/hudson/ExtensionPoint.java b/core/src/main/java/hudson/ExtensionPoint.java index 86f1f7f30b..bacc75a4d2 100644 --- a/core/src/main/java/hudson/ExtensionPoint.java +++ b/core/src/main/java/hudson/ExtensionPoint.java @@ -29,10 +29,16 @@ package hudson; * *

* See respective interfaces/classes for more about how to register custom - * implementations to Hudson. + * implementations to Hudson. See {@link Extension} for how to have + * Hudson auto-discover your implementations. + * + *

+ * This interface is used for auto-generating + * documentation. * * @author Kohsuke Kawaguchi * @see Plugin + * @see Extension */ public interface ExtensionPoint { } diff --git a/core/src/main/java/hudson/FileSystemProvisioner.java b/core/src/main/java/hudson/FileSystemProvisioner.java index 856f4a3595..592153548a 100644 --- a/core/src/main/java/hudson/FileSystemProvisioner.java +++ b/core/src/main/java/hudson/FileSystemProvisioner.java @@ -186,6 +186,7 @@ public abstract class FileSystemProvisioner implements ExtensionPoint, Describab * Default implementation that doesn't rely on any file system specific capability, * and thus can be used anywhere that Hudson runs. */ + @Extension public static final class Default extends FileSystemProvisioner { private Default() {} diff --git a/core/src/main/java/hudson/model/AdministrativeMonitor.java b/core/src/main/java/hudson/model/AdministrativeMonitor.java index c30c034a4a..aed8ce8a21 100644 --- a/core/src/main/java/hudson/model/AdministrativeMonitor.java +++ b/core/src/main/java/hudson/model/AdministrativeMonitor.java @@ -24,6 +24,8 @@ package hudson.model; import hudson.ExtensionPoint; +import hudson.ExtensionList; +import hudson.Extension; import hudson.triggers.SCMTrigger; import hudson.triggers.TimerTrigger; @@ -40,8 +42,7 @@ import org.kohsuke.stapler.StaplerResponse; *

How to implement?

*

* Plugins who wish to contribute such notifications can implement this - * class and add to {@link Hudson#administrativeMonitors} to register - * the object to Hudson. + * class and put to {@link Extension} to register it to Hudson. * *

* Once installed, it's the implementor's responsibility to perform @@ -144,4 +145,11 @@ public abstract class AdministrativeMonitor extends AbstractModelObject implemen disable(true); rsp.sendRedirect2(req.getContextPath()+"/manage"); } + + /** + * All registered {@link AdministrativeMonitor} instances. + */ + public static ExtensionList all() { + return Hudson.getInstance().getExtensionList(AdministrativeMonitor.class); + } } diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index 0ba900ec1e..f4f2bb2bca 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.Collection; import hudson.model.Descriptor.FormException; +import hudson.Extension; /** * {@link View} that contains everything. @@ -81,21 +82,8 @@ public class AllView extends View { // noop } - public ViewDescriptor getDescriptor() { - return DESCRIPTOR; - } - - public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); - - static { - LIST.add(DESCRIPTOR); - } - + @Extension public static final class DescriptorImpl extends ViewDescriptor { - private DescriptorImpl() { - super(AllView.class); - } - @Override public boolean isInstantiable() { for (View v : Hudson.getInstance().getViews()) diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 9bbfb7552f..1b4f2aa2ec 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -40,10 +40,12 @@ import static hudson.Util.fixEmpty; import hudson.WebAppMain; import hudson.XmlFile; import hudson.UDPBroadcastThread; +import hudson.ExtensionList; +import hudson.ExtensionPoint; +import hudson.ExtensionList.DescriptorSubList; import hudson.logging.LogRecorderManager; import hudson.lifecycle.WindowsInstallerLink; import hudson.lifecycle.Lifecycle; -import hudson.os.solaris.ZFSInstaller; import hudson.model.Descriptor.FormException; import hudson.model.listeners.ItemListener; import hudson.model.listeners.JobListener; @@ -116,7 +118,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.commons.jelly.Script; import org.apache.commons.jelly.JellyException; import org.kohsuke.stapler.MetaClass; -import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.Stapler; import org.kohsuke.stapler.StaplerProxy; import org.kohsuke.stapler.StaplerRequest; @@ -129,6 +130,7 @@ import org.kohsuke.stapler.framework.adjunct.AdjunctManager; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; import org.xml.sax.InputSource; +import org.jvnet.tiger_types.Types; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -183,6 +185,8 @@ import java.util.logging.LogRecord; import java.util.logging.Logger; import java.util.regex.Pattern; import java.nio.charset.Charset; +import java.lang.reflect.Type; +import java.lang.reflect.ParameterizedType; import javax.servlet.RequestDispatcher; /** @@ -278,6 +282,16 @@ public final class Hudson extends Node implements ItemGroup, Stapl private transient volatile DependencyGraph dependencyGraph; + /** + * All {@link ExtensionList} keyed by their {@link ExtensionList#extensionType}. + */ + private transient final ConcurrentHashMap extensionLists = new ConcurrentHashMap(); + + /** + * All {@link DescriptorSubList} keyed by their {@link DescriptorSubList#describableType}. + */ + private transient final ConcurrentHashMap descriptorLists = new ConcurrentHashMap(); + /** * Active {@link Cloud}s. */ @@ -400,7 +414,7 @@ public final class Hudson extends Node implements ItemGroup, Stapl * * @see AdministrativeMonitor */ - public transient final List administrativeMonitors = new CopyOnWriteArrayList(); + public transient final List administrativeMonitors = getExtensionList(AdministrativeMonitor.class).asList(); /*package*/ final CopyOnWriteArraySet disabledAdministrativeMonitors = new CopyOnWriteArraySet(); @@ -479,7 +493,6 @@ public final class Hudson extends Node implements ItemGroup, Stapl // run the init code of SubversionSCM before we load plugins so that plugins can change SubversionWorkspaceSelector. SubversionSCM.DescriptorImpl.DESCRIPTOR.getDisplayName(); - ZFSInstaller.init(); // load plugins. pluginManager = new PluginManager(context); @@ -682,6 +695,13 @@ public final class Hudson extends Node implements ItemGroup, Stapl return null; } + public Descriptor getDescriptor(Class type) { + for( Descriptor d : Descriptor.ALL ) + if(d.clazz==type) + return d; + return null; + } + /** * Gets the {@link SecurityRealm} descriptors by name. Primarily used for making them web-visible. */ @@ -1194,11 +1214,11 @@ public final class Hudson extends Node implements ItemGroup, Stapl trimLabels(); save(); } - + public DescribableList, NodePropertyDescriptor> getNodeProperties() { return nodeProperties; } - + public void setNodeProperties(Collection> nodeProperties) throws IOException { this.nodeProperties.replaceBy(nodeProperties); } @@ -1441,6 +1461,48 @@ public final class Hudson extends Node implements ItemGroup, Stapl return Lifecycle.get(); } + /** + * Returns {@link ExtensionList} that retains the discovered instances for the given extension type. + * + * @param extensionType + * The base type that represents the extension point. Normally {@link ExtensionPoint} subtype + * but that's not a hard requirement. + * @return + * Can be an empty list but never null. + */ + @SuppressWarnings({"unchecked"}) + public ExtensionList getExtensionList(Class extensionType) { + // by far the common case is where we find an instance already created. + ExtensionList r = extensionLists.get(extensionType); + if(r!=null) return r; + + // if we have to create a new one, make sure we don't create two at the same time. + r = ExtensionList.create(this,extensionType); + ExtensionList x = extensionLists.putIfAbsent(extensionType, r); + if(x==null) return r; + else return x; + } + + /** + * Returns {@link ExtensionList} that retains the discovered {@link Descriptor} instances for the given + * kind of {@link Describable}. + * + * @return + * Can be an empty list but never null. + */ + @SuppressWarnings({"unchecked"}) + public > ExtensionList> getDescriptorList(final Class type) { + // by far the common case is where we find an instance already created. + DescriptorSubList r = descriptorLists.get(type); + if(r!=null) return r; + + // if we have to create a new one, make sure we don't create two at the same time. + r = new DescriptorSubList(this,type); + ExtensionList x = descriptorLists.putIfAbsent(type, r); + if(x==null) return r; + else return x; + } + /** * Returns the root {@link ACL}. * diff --git a/core/src/main/java/hudson/model/ListView.java b/core/src/main/java/hudson/model/ListView.java index 9734d83fa1..ad7694bcf7 100644 --- a/core/src/main/java/hudson/model/ListView.java +++ b/core/src/main/java/hudson/model/ListView.java @@ -24,6 +24,7 @@ package hudson.model; import hudson.Util; +import hudson.Extension; import hudson.views.ListViewColumn; import hudson.model.Descriptor.FormException; import hudson.util.CaseInsensitiveComparator; @@ -177,21 +178,8 @@ public class ListView extends View { } } - public ViewDescriptor getDescriptor() { - return DESCRIPTOR; - } - - public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); - - static { - LIST.add(DESCRIPTOR); - } - + @Extension public static final class DescriptorImpl extends ViewDescriptor { - private DescriptorImpl() { - super(ListView.class); - } - public String getDisplayName() { return Messages.ListView_DisplayName(); } diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 8e4d835c6a..c875fef10b 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -29,6 +29,7 @@ import hudson.PluginManager; import hudson.PluginWrapper; import hudson.Util; import hudson.ProxyConfiguration; +import hudson.Extension; import hudson.lifecycle.Lifecycle; import hudson.util.DaemonThreadFactory; import hudson.util.TextFile; @@ -126,8 +127,6 @@ public class UpdateCenter extends AbstractModelObject { */ public UpdateCenter(Hudson parent) { configure(new UpdateCenterConfiguration()); - if(parent!=null) // parent==null just for test - parent.administrativeMonitors.add(new CoreUpdateMonitor()); } /** @@ -327,14 +326,15 @@ public class UpdateCenter extends AbstractModelObject { /** * {@link AdministrativeMonitor} that checks if there's Hudson update. */ - public final class CoreUpdateMonitor extends AdministrativeMonitor { + @Extension + public static final class CoreUpdateMonitor extends AdministrativeMonitor { public boolean isActivated() { Data data = getData(); return data!=null && data.hasCoreUpdates(); } public Data getData() { - return UpdateCenter.this.getData(); + return Hudson.getInstance().getUpdateCenter().getData(); } } diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 362ad0afec..8ed3013d79 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -158,7 +158,9 @@ public abstract class View extends AbstractModelObject implements AccessControll return description; } - public abstract ViewDescriptor getDescriptor(); + public ViewDescriptor getDescriptor() { + return (ViewDescriptor)Hudson.getInstance().getDescriptor(getClass()); + } public String getDisplayName() { return getViewName(); @@ -523,11 +525,9 @@ public abstract class View extends AbstractModelObject implements AccessControll /** * A list of available view types. */ - public static final DescriptorList LIST = new DescriptorList(); + public static final DescriptorList LIST = new DescriptorList(View.class); static { - LIST.load(ListView.class); - LIST.load(AllView.class); LIST.load(MyView.class); if(Boolean.getBoolean("hudson.TreeView")) LIST.load(TreeView.class); diff --git a/core/src/main/java/hudson/os/solaris/ZFSInstaller.java b/core/src/main/java/hudson/os/solaris/ZFSInstaller.java index 19c444800d..334299b08c 100644 --- a/core/src/main/java/hudson/os/solaris/ZFSInstaller.java +++ b/core/src/main/java/hudson/os/solaris/ZFSInstaller.java @@ -29,6 +29,7 @@ import com.sun.solaris.EmbeddedSu; import hudson.FilePath; import hudson.Launcher.LocalLauncher; import hudson.Util; +import hudson.Extension; import hudson.model.AdministrativeMonitor; import hudson.model.Computer; import hudson.model.Hudson; @@ -312,10 +313,8 @@ public class ZFSInstaller extends AdministrativeMonitor implements Serializable }.start(); } - - public static void init() { - List monitors = Hudson.getInstance().administrativeMonitors; - + @Extension + public static AdministrativeMonitor init() { String migrationTarget = System.getProperty(ZFSInstaller.class.getName() + ".migrate"); if(migrationTarget!=null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -323,22 +322,22 @@ public class ZFSInstaller extends AdministrativeMonitor implements Serializable try { if(migrate(listener,migrationTarget)) { // completed successfully - monitors.add(new MigrationCompleteNotice()); - return; + return new MigrationCompleteNotice(); } } catch (Exception e) { // if we let any exception from here, it will prevent Hudson from starting. e.printStackTrace(listener.error("Migration failed")); } // migration failed - monitors.add(new MigrationFailedNotice(out)); - return; + return new MigrationFailedNotice(out); } // install the monitor if applicable ZFSInstaller zi = new ZFSInstaller(); if(zi.isActivated()) - monitors.add(zi); + return zi; + + return null; } /** diff --git a/core/src/main/java/hudson/triggers/SCMTrigger.java b/core/src/main/java/hudson/triggers/SCMTrigger.java index 2810cedc52..093bcbad55 100644 --- a/core/src/main/java/hudson/triggers/SCMTrigger.java +++ b/core/src/main/java/hudson/triggers/SCMTrigger.java @@ -25,6 +25,7 @@ package hudson.triggers; import antlr.ANTLRException; import hudson.Util; +import hudson.Extension; import hudson.model.AbstractProject; import hudson.model.Action; import hudson.model.Cause; @@ -154,8 +155,6 @@ public class SCMTrigger extends Trigger { */ private int maximumThreads; - /*package*/ final AdministrativeMonitorImpl monitor = new AdministrativeMonitorImpl(); - DescriptorImpl() { load(); /* @@ -165,16 +164,6 @@ public class SCMTrigger extends Trigger { resizeThreadPool(); } - @Override - public void load() { - super.load(); - - // install the monitor - List monitorList = Hudson.getInstance().administrativeMonitors; - if(!monitorList.contains(monitor)) - monitorList.add(monitor); - } - public boolean isApplicable(Item item) { return item instanceof SCMedItem; } @@ -200,10 +189,10 @@ public class SCMTrigger extends Trigger { /** * Checks if the queue is clogged, and if so, - * activate {@link #monitor}. + * activate {@link AdministrativeMonitorImpl}. */ public void clogCheck() { - monitor.on = isClogged(); + AdministrativeMonitor.all().of(AdministrativeMonitorImpl.class).on = isClogged(); } /** @@ -286,6 +275,7 @@ public class SCMTrigger extends Trigger { } } + @Extension public static final class AdministrativeMonitorImpl extends AdministrativeMonitor { private boolean on; diff --git a/core/src/main/java/hudson/util/DescriptorList.java b/core/src/main/java/hudson/util/DescriptorList.java index 914b9d6cf3..a58070d619 100644 --- a/core/src/main/java/hudson/util/DescriptorList.java +++ b/core/src/main/java/hudson/util/DescriptorList.java @@ -23,28 +23,121 @@ */ package hudson.util; +import hudson.Extension; +import hudson.ExtensionList; import hudson.model.Describable; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; +import hudson.model.Hudson; import net.sf.json.JSONObject; import org.kohsuke.stapler.Stapler; +import java.util.AbstractList; +import java.util.Iterator; +import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; /** * List of {@link Descriptor}s. * *

- * This class is really just a list but also defines - * some Hudson specific methods that operate on - * {@link Descriptor} list. - * + * Before Hudson 1.286, this class stored {@link Descriptor}s directly, but since 1.286, + * this class works in two modes that are rather different. + * + *

+ * One is the compatibility mode, where it works just like pre 1.286 and store everything locally, + * disconnected from any of the additions of 1.286. This is necessary for situations where + * {@link DescriptorList} is owned by pre-1.286 plugins where this class doesn't know 'T'. + * In this mode, {@link #legacy} is non-null but {@link #type} is null. + * + *

+ * The other mode is the new mode, where the {@link Descriptor}s are actually stored in {@link ExtensionList} + * (see {@link Hudson#getDescriptorList(Class)}) and this class acts as a view to it. This enables + * bi-directional interoperability — both descriptors registred automatically and descriptors registered + * manually are visible from both {@link DescriptorList} and {@link ExtensionList}. In this mode, + * {@link #legacy} is null but {@link #type} is non-null. + * + *

+ * The number of plugins that define extension points are limited, so we expect to be able to remove + * this dual behavior first, then when everyone stops using {@link DescriptorList}, we can remove this class + * altogether. + * * @author Kohsuke Kawaguchi * @since 1.161 */ -public final class DescriptorList> extends CopyOnWriteArrayList> { +public final class DescriptorList> extends AbstractList> { + + private final Class type; + + private final CopyOnWriteArrayList> legacy; + + /** + * This will create a legacy {@link DescriptorList} that is disconnected from + * {@link ExtensionList}. + * + * @deprecated + * As of 1.286. Use {@link #DescriptorList(Class)} instead. + */ public DescriptorList(Descriptor... descriptors) { - super(descriptors); + this.type = null; + this.legacy = new CopyOnWriteArrayList>(descriptors); + } + + /** + * Creates a {@link DescriptorList} backed by {@link ExtensionList}. + */ + public DescriptorList(Class type) { + this.type = type; + this.legacy = null; + } + + @Override + public Descriptor get(int index) { + return store().get(index); + } + + @Override + public int size() { + return store().size(); + } + + @Override + public Iterator> iterator() { + return store().iterator(); + } + + /** + * {@inheritDoc} + * + * @deprecated + * As of 1.286. Put {@link Extension} on your descriptor to have it auto-registered, + * instead of registering a descriptor manually. + */ + @Override + public boolean add(Descriptor d) { + return super.add(d); + } + + /** + * {@inheritDoc} + * + * @deprecated + * As of 1.286. Put {@link Extension} on your descriptor to have it auto-registered, + * instead of registering a descriptor manually. + */ + @Override + public void add(int index, Descriptor element) { + store().add(element); + } + + /** + * Gets the actual data store. This is the key to control the dual-mode nature of {@link DescriptorList} + */ + private List> store() { + if(type==null) + return legacy; + else + return Hudson.getInstance().getDescriptorList(type).asList(); } /**