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