PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-plugin-utils/src/main/java/org/hudsonci/utils/tasks/MetaProject.java

http://github.com/hudson/hudson
Java | 325 lines | 198 code | 57 blank | 70 comment | 22 complexity | fcd9a64b56fefa39bd8d729b53100196 MD5 | raw file
  1/**
  2 * The MIT License
  3 *
  4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  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 */
 24
 25package org.hudsonci.utils.tasks;
 26
 27import hudson.matrix.MatrixProject;
 28import hudson.model.AbstractProject;
 29import hudson.model.Descriptor;
 30import hudson.model.Hudson;
 31import hudson.model.Item;
 32import hudson.model.ItemGroup;
 33import hudson.model.Job;
 34import hudson.model.JobProperty;
 35import hudson.model.Project;
 36import hudson.security.Permission;
 37import hudson.tasks.BuildWrapper;
 38import hudson.tasks.Builder;
 39import hudson.tasks.Publisher;
 40import hudson.triggers.Trigger;
 41import hudson.util.DescribableList;
 42
 43import org.slf4j.Logger;
 44import org.slf4j.LoggerFactory;
 45
 46import java.io.IOException;
 47import java.util.ArrayList;
 48import java.util.Collection;
 49import java.util.List;
 50import java.util.UUID;
 51
 52import static com.google.common.base.Preconditions.checkNotNull;
 53
 54/**
 55 * Provides a unified interface for {@link AbstractProject} types.
 56 * 
 57 * Avoiding generic types where possible to avoid evil <tt>"inconvertible types" due to capture###</tt> crap.
 58 *
 59 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 60 * @since 2.1.0
 61 */
 62@SuppressWarnings( {"rawtypes", "unchecked"} )
 63public class MetaProject
 64{
 65    private static final Logger log = LoggerFactory.getLogger(MetaProject.class);
 66
 67    private final AbstractProject delegate;
 68
 69    public static enum Type
 70    {
 71        /**
 72         * Any other project type (like MavenModuleSet, etc).
 73         */
 74        UNSUPPORTED,
 75
 76        /**
 77         * Sub-class of {@link Project}.
 78         */
 79        NORMAL,
 80
 81        /**
 82         * Sub-class of {@link MatrixProject}.
 83         */
 84        MULTICONFIG
 85    }
 86    
 87    private final Type type;
 88
 89    public MetaProject(final AbstractProject project) {
 90        this.delegate = checkNotNull(project);
 91
 92        if (delegate instanceof MatrixProject) {
 93            type = Type.MULTICONFIG;
 94        }
 95        else if (delegate instanceof Project) {
 96            type = Type.NORMAL;
 97        }
 98        else {
 99            type = Type.UNSUPPORTED;
100            log.debug("Unsupported project type: {}", project.getClass().getName());
101        }
102    }
103
104    public AbstractProject getDelegate() {
105        return delegate;
106    }
107
108    public Type getType() {
109        return type;
110    }
111
112    public boolean isSupported() {
113        return getType() != Type.UNSUPPORTED;
114    }
115
116    public boolean isNormal() {
117        return getType() == Type.NORMAL;
118    }
119
120    public boolean isMultiConfig() {
121        return getType() == Type.MULTICONFIG;
122    }
123
124    @Override
125    public String toString() {
126        return String.format("%s (%s,%s)", getFullDisplayName(), getType(), getId());
127    }
128
129    /**
130     * Typed access to project instance as a normal {@link Project}.
131     */
132    public Project<?,?> asNormal() {
133        return (Project)getDelegate();
134    }
135
136    /**
137     * Typed access to project instance as a multi-config {@link MatrixProject}.
138     */
139    public MatrixProject asMultiConfig() {
140        return (MatrixProject)getDelegate();
141    }
142
143    public ItemGroup getParent() {
144        return getDelegate().getParent();
145    }
146
147    public String getName() {
148        return getDelegate().getName();
149    }
150
151    public String getFullName() {
152        return getDelegate().getFullName();
153    }
154
155    public String getDisplayName() {
156        return getDelegate().getDisplayName();
157    }
158
159    public String getFullDisplayName() {
160        return getDelegate().getFullDisplayName();
161    }
162
163    public UUID getId() {
164        return JobUuid.get(getDelegate());
165    }
166
167    public void setEnabled(final boolean enabled) throws IOException {
168        getDelegate().makeDisabled(!enabled);
169    }
170
171    public boolean isEnabled() {
172        return !getDelegate().isDisabled();
173    }
174
175    public Collection<JobProperty> getProperties() {
176        return getDelegate().getAllProperties();
177    }
178
179    public void addProperty(final JobProperty item) throws IOException {
180        checkNotNull(item);
181        getDelegate().addProperty(item);
182    }
183
184    public Collection<Trigger> getTriggers() {
185        return getDelegate().getTriggers().values();
186    }
187    
188    public void addTrigger(final Trigger item) throws IOException {
189        checkNotNull(item);
190        getDelegate().addTrigger(item);
191    }
192
193    /**
194     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
195     */
196    public DescribableList<BuildWrapper,Descriptor<BuildWrapper>> getBuildWrappersList() {
197        switch (getType()) {
198            case NORMAL:
199                return asNormal().getBuildWrappersList();
200
201            case MULTICONFIG:
202                return asMultiConfig().getBuildWrappersList();
203        }
204
205        throw new UnsupportedProjectException(getDelegate());
206    }
207
208    /**
209     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
210     */
211    public Collection<BuildWrapper> getBuildWrappers() {
212        return getBuildWrappersList().toList();
213    }
214
215    /**
216     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
217     */
218    public DescribableList<Builder,Descriptor<Builder>> getBuildersList() {
219        switch (getType()) {
220            case NORMAL:
221                return asNormal().getBuildersList();
222
223            case MULTICONFIG:
224                return asMultiConfig().getBuildersList();
225        }
226
227        throw new UnsupportedProjectException(getDelegate());
228    }
229
230    /**
231     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
232     */
233    public Collection<Builder> getBuilders() {
234        return getBuildersList().toList();
235    }
236
237    /**
238     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
239     */
240    public DescribableList<Publisher,Descriptor<Publisher>> getPublishersList() {
241        switch (getType()) {
242            case NORMAL:
243                return asNormal().getPublishersList();
244
245            case MULTICONFIG:
246                return asMultiConfig().getPublishersList();
247        }
248
249        throw new UnsupportedProjectException(getDelegate());
250    }
251
252    /**
253     * @throws UnsupportedProjectException if the Project type is {@link Type#UNSUPPORTED}.
254     */
255    public Collection<Publisher> getPublishers() {
256        return getPublishersList().toList();
257    }
258
259    public void checkPermission(final Permission perm) {
260        getDelegate().checkPermission(perm);
261    }
262
263    @Override
264    public boolean equals(final Object obj) {
265        if (this == obj) {
266            return true;
267        }
268        if (obj == null || getClass() != obj.getClass()) {
269            return false;
270        }
271
272        MetaProject that = (MetaProject) obj;
273        return getId().equals(that.getId());
274    }
275
276    @Override
277    public int hashCode() {
278        return getId().hashCode();
279    }
280
281    // FIXME: Extract out to service
282
283    public static interface Filter
284    {
285        /**
286         * Return true to include the given project in the result.
287         *
288         * @param project   Never null
289         */
290        boolean accept(MetaProject project);
291    }
292
293    public static Collection<MetaProject> list(final Filter filter) {
294        List<MetaProject> projects = new ArrayList<MetaProject>();
295
296        for (Item item : Hudson.getInstance().getItems()) {
297            if (item instanceof AbstractProject) {
298                MetaProject project = new MetaProject((AbstractProject)item);
299                if (filter == null || filter.accept(project)) {
300                    projects.add(project);
301                }
302            }
303        }
304
305        return projects;
306    }
307
308    public static Collection<MetaProject> list() {
309        return list(null);
310    }
311
312    public static MetaProject find(final UUID id) {
313        checkNotNull(id);
314        Job job = JobUuid.find(id);
315        if (job instanceof AbstractProject) {
316            return new MetaProject((AbstractProject)job);
317        }
318        return null;
319    }
320
321    public static MetaProject find(final String id) {
322        checkNotNull(id);
323        return find(UUID.fromString(id));
324    }
325}