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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
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.Functions;
27 import hudson.model.queue.Tasks;
28 import jenkins.model.ModelObjectWithContextMenu;
29 import org.apache.commons.jelly.JellyContext;
30 import org.apache.commons.jelly.JellyException;
31 import org.apache.commons.jelly.JellyTagException;
32 import org.apache.commons.jelly.Script;
33 import org.apache.commons.jelly.XMLOutput;
34 import org.kohsuke.stapler.StaplerRequest;
35 import org.kohsuke.stapler.StaplerResponse;
36 import org.kohsuke.stapler.WebApp;
37 import org.kohsuke.stapler.export.Exported;
38 import org.kohsuke.stapler.export.ExportedBean;
39 import org.kohsuke.stapler.jelly.JellyClassTearOff;
40 import org.kohsuke.stapler.jelly.JellyFacet;
41 import org.xml.sax.helpers.DefaultHandler;
42
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Vector;
46 import java.util.concurrent.CopyOnWriteArrayList;
47
48 /**
49 * {@link ModelObject} that can have additional {@link Action}s.
50 *
51 * @author Kohsuke Kawaguchi
52 */
53 @ExportedBean
54 public abstract class Actionable extends AbstractModelObject implements ModelObjectWithContextMenu {
55 /**
56 * Actions contributed to this model object.
57 *
58 * Typed more strongly than it should to improve the serialization signature.
59 */
60 private volatile CopyOnWriteArrayList<Action> actions;
61
62 /**
63 * Gets actions contributed to this build.
64 *
65 * <p>
66 * A new {@link Action} can be added by {@code getActions().add(...)}.
67 *
68 * @return
69 * may be empty but never null.
70 */
71 @Exported
72 public List<Action> getActions() {
73 if(actions == null) {
74 synchronized (this) {
75 if(actions == null) {
76 actions = new CopyOnWriteArrayList<Action>();
77 }
78 }
79 }
80 return actions;
81 }
82
83 /**
84 * Gets all actions of a specified type that contributed to this build.
85 *
86 * @param type The type of action to return.
87 * @return
88 * may be empty but never null.
89 * @see #getAction(Class)
90 */
91 public <T extends Action> List<T> getActions(Class<T> type) {
92 List<T> result = new Vector<T>();
93 for (Action a : getActions())
94 if (type.isInstance(a))
95 result.add(type.cast(a));
96 return result;
97 }
98
99 /**
100 * Adds a new action.
101 *
102 * Short for <tt>getActions().add(a)</tt>
103 */
104 public void addAction(Action a) {
105 if(a==null) throw new IllegalArgumentException();
106 getActions().add(a);
107 }
108
109 public Action getAction(int index) {
110 if(actions==null) return null;
111 return actions.get(index);
112 }
113
114 /**
115 * Gets the action (first instance to be found) of a specified type that contributed to this build.
116 *
117 * @param type
118 * @return The action or <code>null</code> if no such actions exist.
119 * @see #getActions(Class)
120 */
121 public <T extends Action> T getAction(Class<T> type) {
122 for (Action a : getActions())
123 if (type.isInstance(a))
124 return type.cast(a);
125 return null;
126 }
127
128 public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
129 for (Action a : getActions()) {
130 if(a==null)
131 continue; // be defensive
132 String urlName = a.getUrlName();
133 if(urlName==null)
134 continue;
135 if(urlName.equals(token))
136 return a;
137 }
138 return null;
139 }
140
141 public ContextMenu doContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
142 return new ContextMenu().from(this,request,response);
143 }
144 }
145