From ee711d4e63a01fa031a32ee9e1c50259c96c42b1 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 22 Apr 2010 21:42:41 +0000 Subject: [PATCH] added a convenient base class. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@30333 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/model/AbstractDescribableImpl.java | 35 +++++++++++++++++++ .../java/hudson/scm/RepositoryBrowser.java | 8 ++--- .../security/AuthorizationStrategy.java | 17 ++------- .../java/hudson/security/SecurityRealm.java | 6 ++-- .../java/hudson/slaves/ComputerLauncher.java | 17 ++++----- .../java/hudson/slaves/RetentionStrategy.java | 11 ++---- .../main/java/hudson/tasks/BuildWrapper.java | 18 ++-------- .../hudson/tasks/junit/TestDataPublisher.java | 7 +--- .../java/hudson/tools/ToolInstallation.java | 13 ++----- 9 files changed, 55 insertions(+), 77 deletions(-) create mode 100644 core/src/main/java/hudson/model/AbstractDescribableImpl.java diff --git a/core/src/main/java/hudson/model/AbstractDescribableImpl.java b/core/src/main/java/hudson/model/AbstractDescribableImpl.java new file mode 100644 index 0000000000..1f10177495 --- /dev/null +++ b/core/src/main/java/hudson/model/AbstractDescribableImpl.java @@ -0,0 +1,35 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Kohsuke Kawaguchi + * + * 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.model; + +/** + * Partial default implementation of {@link Describable}. + * + * @author Kohsuke Kawaguchi + */ +public abstract class AbstractDescribableImpl> implements Describable { + public Descriptor getDescriptor() { + return Hudson.getInstance().getDescriptorOrDie(getClass()); + } +} diff --git a/core/src/main/java/hudson/scm/RepositoryBrowser.java b/core/src/main/java/hudson/scm/RepositoryBrowser.java index 4a70ae1058..1ce8767b37 100644 --- a/core/src/main/java/hudson/scm/RepositoryBrowser.java +++ b/core/src/main/java/hudson/scm/RepositoryBrowser.java @@ -26,7 +26,7 @@ package hudson.scm; import hudson.ExtensionPoint; import hudson.DescriptorExtensionList; import hudson.Extension; -import hudson.model.Describable; +import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.Hudson; @@ -56,7 +56,7 @@ import org.kohsuke.stapler.export.ExportedBean; * @see RepositoryBrowsers */ @ExportedBean -public abstract class RepositoryBrowser implements ExtensionPoint, Describable>, Serializable { +public abstract class RepositoryBrowser extends AbstractDescribableImpl> implements ExtensionPoint, Serializable { /** * Determines the link to the given change set. * @@ -95,10 +95,6 @@ public abstract class RepositoryBrowser implements } } - public Descriptor> getDescriptor() { - return Hudson.getInstance().getDescriptorOrDie(getClass()); - } - /** * Returns all the registered {@link RepositoryBrowser} descriptors. */ diff --git a/core/src/main/java/hudson/security/AuthorizationStrategy.java b/core/src/main/java/hudson/security/AuthorizationStrategy.java index bdd05ea9fb..ea9cab0cce 100644 --- a/core/src/main/java/hudson/security/AuthorizationStrategy.java +++ b/core/src/main/java/hudson/security/AuthorizationStrategy.java @@ -26,16 +26,7 @@ package hudson.security; import hudson.DescriptorExtensionList; import hudson.Extension; import hudson.ExtensionPoint; -import hudson.model.AbstractItem; -import hudson.model.AbstractProject; -import hudson.model.Computer; -import hudson.model.Describable; -import hudson.model.Descriptor; -import hudson.model.Hudson; -import hudson.model.Job; -import hudson.model.Node; -import hudson.model.User; -import hudson.model.View; +import hudson.model.*; import hudson.slaves.Cloud; import hudson.util.DescriptorList; @@ -68,7 +59,7 @@ import org.kohsuke.stapler.StaplerRequest; * @author Kohsuke Kawaguchi * @see SecurityRealm */ -public abstract class AuthorizationStrategy implements Describable, ExtensionPoint { +public abstract class AuthorizationStrategy extends AbstractDescribableImpl implements ExtensionPoint { /** * Returns the instance of {@link ACL} where all the other {@link ACL} instances * for all the other model objects eventually delegate. @@ -175,10 +166,6 @@ public abstract class AuthorizationStrategy implements Describable getGroups(); - public Descriptor getDescriptor() { - return Hudson.getInstance().getDescriptorOrDie(getClass()); - } - /** * Returns all the registered {@link AuthorizationStrategy} descriptors. */ diff --git a/core/src/main/java/hudson/security/SecurityRealm.java b/core/src/main/java/hudson/security/SecurityRealm.java index 24c7e1bf94..3a56a64a8a 100644 --- a/core/src/main/java/hudson/security/SecurityRealm.java +++ b/core/src/main/java/hudson/security/SecurityRealm.java @@ -30,7 +30,7 @@ import hudson.ExtensionPoint; import hudson.DescriptorExtensionList; import hudson.Extension; import hudson.cli.CLICommand; -import hudson.model.Describable; +import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.Hudson; import hudson.util.DescriptorList; @@ -123,7 +123,7 @@ import java.util.logging.Logger; * @since 1.160 * @see PluginServletFilter */ -public abstract class SecurityRealm implements Describable, ExtensionPoint { +public abstract class SecurityRealm extends AbstractDescribableImpl implements ExtensionPoint { /** * Creates fully-configured {@link AuthenticationManager} that performs authentication * against the user realm. The implementation hides how such authentication manager @@ -169,7 +169,7 @@ public abstract class SecurityRealm implements Describable, Exten * global.jelly. */ public Descriptor getDescriptor() { - return Hudson.getInstance().getDescriptorOrDie(getClass()); + return super.getDescriptor(); } /** diff --git a/core/src/main/java/hudson/slaves/ComputerLauncher.java b/core/src/main/java/hudson/slaves/ComputerLauncher.java index 6e9cd24981..6c69199891 100644 --- a/core/src/main/java/hudson/slaves/ComputerLauncher.java +++ b/core/src/main/java/hudson/slaves/ComputerLauncher.java @@ -25,15 +25,14 @@ package hudson.slaves; import hudson.ExtensionPoint; import hudson.Extension; -import hudson.model.Computer; -import hudson.model.Describable; -import hudson.model.Descriptor; -import hudson.model.Hudson; -import hudson.model.TaskListener; +import hudson.model.*; +import hudson.remoting.Channel; import hudson.util.DescriptorList; import hudson.util.StreamTaskListener; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; /** * Extension point to allow control over how {@link Computer}s are "launched", @@ -50,7 +49,7 @@ import java.io.IOException; * @author Stephen Connolly * @since 24-Apr-2008 22:12:35 */ -public abstract class ComputerLauncher implements Describable, ExtensionPoint { +public abstract class ComputerLauncher extends AbstractDescribableImpl implements ExtensionPoint { /** * Returns true if this {@link ComputerLauncher} supports * programatic launch of the slave agent in the target {@link Computer}. @@ -63,7 +62,7 @@ public abstract class ComputerLauncher implements Describable, * Launches the slave agent for the given {@link Computer}. * *

- * If the slave agent is launched successfully, {@link SlaveComputer#setChannel(InputStream, OutputStream, OutputStream, Listener)} + * If the slave agent is launched successfully, {@link SlaveComputer#setChannel(InputStream, OutputStream, OutputStream, Channel.Listener)} * should be invoked in the end to notify Hudson of the established connection. * The operation could also fail, in which case there's no need to make any callback notification, * (except to notify the user of the failure through {@link StreamTaskListener}.) @@ -129,10 +128,6 @@ public abstract class ComputerLauncher implements Describable, return new StreamTaskListener(listener.getLogger()); } - public Descriptor getDescriptor() { - return (Descriptor)Hudson.getInstance().getDescriptorOrDie(getClass()); - } - /** * All registered {@link ComputerLauncher} implementations. * diff --git a/core/src/main/java/hudson/slaves/RetentionStrategy.java b/core/src/main/java/hudson/slaves/RetentionStrategy.java index 159799e0ca..1aaff50672 100644 --- a/core/src/main/java/hudson/slaves/RetentionStrategy.java +++ b/core/src/main/java/hudson/slaves/RetentionStrategy.java @@ -27,10 +27,7 @@ import hudson.ExtensionPoint; import hudson.Util; import hudson.DescriptorExtensionList; import hudson.Extension; -import hudson.model.Computer; -import hudson.model.Describable; -import hudson.model.Descriptor; -import hudson.model.Hudson; +import hudson.model.*; import hudson.util.DescriptorList; import org.kohsuke.stapler.DataBoundConstructor; @@ -43,7 +40,7 @@ import java.util.logging.Logger; * @author Stephen Connolly * @author Kohsuke Kawaguchi */ -public abstract class RetentionStrategy implements Describable>, ExtensionPoint { +public abstract class RetentionStrategy extends AbstractDescribableImpl> implements ExtensionPoint { /** * This method will be called periodically to allow this strategy to decide what to do with it's owning slave. @@ -80,10 +77,6 @@ public abstract class RetentionStrategy implements Describab check(c); } - public Descriptor> getDescriptor() { - return Hudson.getInstance().getDescriptorOrDie(getClass()); - } - /** * Returns all the registered {@link RetentionStrategy} descriptors. */ diff --git a/core/src/main/java/hudson/tasks/BuildWrapper.java b/core/src/main/java/hudson/tasks/BuildWrapper.java index 42dadd7a5b..2220491ecf 100644 --- a/core/src/main/java/hudson/tasks/BuildWrapper.java +++ b/core/src/main/java/hudson/tasks/BuildWrapper.java @@ -27,15 +27,7 @@ import hudson.ExtensionPoint; import hudson.Launcher; import hudson.DescriptorExtensionList; import hudson.LauncherDecorator; -import hudson.model.AbstractBuild; -import hudson.model.Build; -import hudson.model.BuildListener; -import hudson.model.Describable; -import hudson.model.Project; -import hudson.model.Action; -import hudson.model.AbstractProject; -import hudson.model.Hudson; -import hudson.model.Descriptor; +import hudson.model.*; import hudson.model.Run.RunnerAbortedException; import java.io.IOException; @@ -60,7 +52,7 @@ import java.util.Collections; * * @author Kohsuke Kawaguchi */ -public abstract class BuildWrapper implements ExtensionPoint, Describable { +public abstract class BuildWrapper extends AbstractDescribableImpl implements ExtensionPoint { /** * Represents the environment set up by {@link BuildWrapper#setUp(Build,Launcher,BuildListener)}. * @@ -211,12 +203,6 @@ public abstract class BuildWrapper implements ExtensionPoint, Describable getDescriptor() { - return (Descriptor) Hudson.getInstance().getDescriptorOrDie(getClass()); - - } - /** * Returns all the registered {@link BuildWrapper} descriptors. */ diff --git a/core/src/main/java/hudson/tasks/junit/TestDataPublisher.java b/core/src/main/java/hudson/tasks/junit/TestDataPublisher.java index eb1de140ce..117d1ca363 100644 --- a/core/src/main/java/hudson/tasks/junit/TestDataPublisher.java +++ b/core/src/main/java/hudson/tasks/junit/TestDataPublisher.java @@ -42,7 +42,7 @@ import java.io.IOException; * * @since 1.320 */ -public abstract class TestDataPublisher implements Describable, ExtensionPoint { +public abstract class TestDataPublisher extends AbstractDescribableImpl implements ExtensionPoint { /** * Called after test results are collected by Hudson, to create a resolver for {@link TestAction}s. @@ -54,11 +54,6 @@ public abstract class TestDataPublisher implements Describable build, Launcher launcher, BuildListener listener, TestResult testResult) throws IOException, InterruptedException; - @SuppressWarnings("unchecked") - public Descriptor getDescriptor() { - return Hudson.getInstance().getDescriptorOrDie(getClass()); - } - public static DescriptorExtensionList> all() { return Hudson.getInstance().getDescriptorList(TestDataPublisher.class); } diff --git a/core/src/main/java/hudson/tools/ToolInstallation.java b/core/src/main/java/hudson/tools/ToolInstallation.java index dd5fb84931..dbfccd9e6d 100644 --- a/core/src/main/java/hudson/tools/ToolInstallation.java +++ b/core/src/main/java/hudson/tools/ToolInstallation.java @@ -29,12 +29,7 @@ import hudson.EnvVars; import hudson.Extension; import hudson.ExtensionPoint; import hudson.diagnosis.OldDataMonitor; -import hudson.model.Describable; -import hudson.model.EnvironmentSpecific; -import hudson.model.Hudson; -import hudson.model.Node; -import hudson.model.Saveable; -import hudson.model.TaskListener; +import hudson.model.*; import hudson.slaves.NodeSpecific; import hudson.util.DescribableList; import hudson.util.XStream2; @@ -74,7 +69,7 @@ import com.thoughtworks.xstream.converters.UnmarshallingContext; * @author huybrechts * @since 1.286 */ -public abstract class ToolInstallation implements Serializable, Describable, ExtensionPoint { +public abstract class ToolInstallation extends AbstractDescribableImpl implements Serializable, ExtensionPoint { private final String name; private /*almost final*/ String home; @@ -135,10 +130,6 @@ public abstract class ToolInstallation implements Serializable, Describable getDescriptor() { - return (ToolDescriptor) Hudson.getInstance().getDescriptorOrDie(getClass()); - } - /** * Finds a tool on a node. * Checks if the location of the tool is overridden for the given node, and if so,