File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\CauseAction.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Michael B. Donohue
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 hudson.model;
25
26 import hudson.diagnosis.OldDataMonitor;
27 import hudson.model.Queue.Task;
28 import hudson.model.queue.FoldableAction;
29 import hudson.util.XStream2;
30 import org.kohsuke.stapler.export.Exported;
31 import org.kohsuke.stapler.export.ExportedBean;
32 import com.thoughtworks.xstream.converters.UnmarshallingContext;
33
34 import java.util.ArrayList;
35 import java.util.LinkedHashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 @ExportedBean
40 public class CauseAction implements FoldableAction, RunAction {
41 /**
42 * @deprecated since 2009-02-28
43 */
44 @Deprecated
45 // there can be multiple causes, so this is deprecated
46 private transient Cause cause;
47
48 private List<Cause> causes = new ArrayList<Cause>();
49
50 public CauseAction(Cause c) {
51 this.causes.add(c);
52 }
53
54 public CauseAction(CauseAction ca) {
55 this.causes.addAll(ca.causes);
56 }
57
58 @Exported(visibility=2)
59 public List<Cause> getCauses() {
60 return causes;
61 }
62
63 /**
64 * Finds the cause of the specific type.
65 */
66 public <T extends Cause> T findCause(Class<T> type) {
67 for (Cause c : causes)
68 if (type.isInstance(c))
69 return type.cast(c);
70 return null;
71 }
72
73 public String getDisplayName() {
74 return "Cause";
75 }
76
77 public String getIconFileName() {
78 // no icon
79 return null;
80 }
81
82 public String getUrlName() {
83 return "cause";
84 }
85
86 /**
87 * Get list of causes with duplicates combined into counters.
88 * @return Map of Cause to number of occurrences of that Cause
89 */
90 public Map<Cause,Integer> getCauseCounts() {
91 Map<Cause,Integer> result = new LinkedHashMap<Cause,Integer>();
92 for (Cause c : causes) {
93 Integer i = result.get(c);
94 result.put(c, i == null ? 1 : i.intValue() + 1);
95 }
96 return result;
97 }
98
99 /**
100 * @deprecated as of 1.288
101 * but left here for backward compatibility.
102 */
103 public String getShortDescription() {
104 if(causes.isEmpty()) return "N/A";
105 return causes.get(0).getShortDescription();
106 }
107
108 public void onLoad() {
109 // noop
110 }
111
112 public void onBuildComplete() {
113 // noop
114 }
115
116 /**
117 * When hooked up to build, notify {@link Cause}s.
118 */
119 public void onAttached(Run owner) {
120 if (owner instanceof AbstractBuild) {// this should be always true but being defensive here
121 AbstractBuild b = (AbstractBuild) owner;
122 for (Cause c : causes) {
123 c.onAddedTo(b);
124 }
125 }
126 }
127
128 public void foldIntoExisting(hudson.model.Queue.Item item, Task owner, List<Action> otherActions) {
129 CauseAction existing = item.getAction(CauseAction.class);
130 if (existing!=null) {
131 existing.causes.addAll(this.causes);
132 return;
133 }
134 // no CauseAction found, so add a copy of this one
135 item.getActions().add(new CauseAction(this));
136 }
137
138 public static class ConverterImpl extends XStream2.PassthruConverter<CauseAction> {
139 public ConverterImpl(XStream2 xstream) { super(xstream); }
140 @Override protected void callback(CauseAction ca, UnmarshallingContext context) {
141 // if we are being read in from an older version
142 if (ca.cause != null) {
143 if (ca.causes == null) ca.causes = new ArrayList<Cause>();
144 ca.causes.add(ca.cause);
145 OldDataMonitor.report(context, "1.288");
146 }
147 }
148 }
149 }
150