PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/tasks/Publisher.java

http://github.com/hudson/hudson
Java | 188 lines | 66 code | 15 blank | 107 comment | 5 complexity | 8861d47172088b8bfa6f1d10c2ffad25 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2011, Oracle Corporation, Kohsuke Kawaguchi, Anton Kozak
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.tasks;
 25
 26import hudson.DescriptorExtensionList;
 27import hudson.Extension;
 28import hudson.ExtensionComponent;
 29import hudson.model.Action;
 30import hudson.model.Build;
 31import hudson.model.BuildListener;
 32import hudson.model.Describable;
 33import hudson.model.Project;
 34import hudson.model.Descriptor;
 35import hudson.model.Hudson;
 36
 37import hudson.model.Result;
 38import java.util.List;
 39import java.util.ArrayList;
 40import java.util.Collections;
 41import java.util.Comparator;
 42
 43/**
 44 * {@link BuildStep}s that run after the build is completed.
 45 *
 46 * <p>
 47 * To register a custom {@link Publisher} from a plugin,
 48 * put {@link Extension} on your descriptor implementation.
 49 *
 50 * <p>
 51 * Starting 1.178, publishers are exposed to all kinds of different
 52 * project type, not just the freestyle project type (in particular,
 53 * the native maven2 job type.) This is convenient default for
 54 * {@link Publisher}s in particular initially, but we encourage advanced
 55 * plugins to consider writing MavenReporter, as it offers the
 56 * potential of reducing the amount of configuration needed to run the plugin.
 57 *
 58 * For those plugins that don't want {@link Publisher} to show up in
 59 * different job type, use {@link BuildStepDescriptor} for the base type
 60 * of your descriptor to control which job type it supports.
 61 *
 62 * @author Kohsuke Kawaguchi, Anton Kozak
 63 */
 64public abstract class Publisher extends BuildStepCompatibilityLayer implements BuildStep, Describable<Publisher> {
 65    /**
 66     * @deprecated
 67     *      Don't extend from {@link Publisher} directly. Instead, choose {@link Recorder} or {@link Notifier}
 68     *      as your base class.
 69     */
 70    @Deprecated
 71    protected Publisher() {
 72    }
 73
 74    //
 75// these two methods need to remain to keep binary compatibility with plugins built with Hudson < 1.150
 76//
 77    /**
 78     * Default implementation that does nothing.
 79     * @deprecated since 1.150
 80     */
 81    @Deprecated @Override
 82    public boolean prebuild(Build build, BuildListener listener) {
 83        return true;
 84    }
 85
 86    /**
 87     * Default implementation that does nothing.
 88     * @deprecated since 1.150
 89     */
 90    @Deprecated @Override
 91    public Action getProjectAction(Project project) {
 92        return null;
 93    }
 94
 95    /**
 96     * Return true if this {@link Publisher} needs to run after the build result is
 97     * fully finalized.
 98     *
 99     * <p>
100     * The execution of normal {@link Publisher}s are considered within a part
101     * of the build. This allows publishers to mark the build as a failure, or
102     * to include their execution time in the total build time.
103     *
104     * <p>
105     * So normally, that is the preferrable behavior, but in a few cases
106     * this is problematic. One of such cases is when a publisher needs to
107     * trigger other builds, which in turn need to see this build as a
108     * completed build. Those plugins that need to do this can return true
109     * from this method, so that the {@link #perform(AbstractBuild, Launcher, BuildListener)} 
110     * method is called after the build is marked as completed.
111     *
112     * <p>
113     * When {@link Publisher} behaves this way, note that they can no longer
114     * change the build status anymore.
115     *
116     * @author Kohsuke Kawaguchi
117     * @since 1.153
118     */
119    public boolean needsToRunAfterFinalized() {
120        return false;
121    }
122
123    /**
124     * Returns true if this {@link Publisher} needs to run depends on Build {@link Result}.
125     * <p/>
126     * Can be used if execution of {@link Publisher} is not required for some Build {@link Result},
127     * i.e. ABORTED, FAILED, etc.
128     * <p/>
129     *
130     * @since 2.1.1
131     */
132    public boolean needsToRun(Result buildResult) {
133        return true;
134    }
135
136    public Descriptor<Publisher> getDescriptor() {
137        return Hudson.getInstance().getDescriptorOrDie(getClass());
138    }
139
140    /**
141     * {@link Publisher} has a special sort semantics that requires a subtype.
142     *
143     * @see DescriptorExtensionList#createDescriptorList(Hudson, Class) 
144     */
145    public static final class DescriptorExtensionListImpl extends DescriptorExtensionList<Publisher,Descriptor<Publisher>>
146            implements Comparator<ExtensionComponent<Descriptor<Publisher>>> {
147        public DescriptorExtensionListImpl(Hudson hudson) {
148            super(hudson,Publisher.class);
149        }
150
151        @Override
152        protected List<ExtensionComponent<Descriptor<Publisher>>> sort(List<ExtensionComponent<Descriptor<Publisher>>> r) {
153            List<ExtensionComponent<Descriptor<Publisher>>> copy = new ArrayList<ExtensionComponent<Descriptor<Publisher>>>(r);
154            Collections.sort(copy,this);
155            return copy;
156        }
157
158        public int compare(ExtensionComponent<Descriptor<Publisher>> lhs, ExtensionComponent<Descriptor<Publisher>> rhs) {
159            int r = classify(lhs.getInstance())-classify(rhs.getInstance());
160            if (r!=0)   return r;
161            return lhs.compareTo(rhs);
162        }
163
164        /**
165         * If recorder, return 0, if unknown return 1, if notifier returns 2.
166         * This is used as a sort key.
167         */
168        private int classify(Descriptor<Publisher> d) {
169            if(d.isSubTypeOf(Recorder.class))    return 0;
170            if(d.isSubTypeOf(Notifier.class))    return 2;
171
172            // for compatibility, if the descriptor is manually registered in a specific way, detect that.
173            Class<? extends Publisher> kind = PublisherList.KIND.get(d);
174            if(kind==Recorder.class)    return 0;
175            if(kind==Notifier.class)    return 2;
176
177            return 1;
178        }
179    }
180
181    /**
182     * Returns all the registered {@link Publisher} descriptors.
183     */
184    // for backward compatibility, the signature is not BuildStepDescriptor
185    public static DescriptorExtensionList<Publisher,Descriptor<Publisher>> all() {
186        return Hudson.getInstance().<Publisher,Descriptor<Publisher>>getDescriptorList(Publisher.class);
187    }
188}