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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
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.init;
25
26 import hudson.model.Hudson;
27 import jenkins.model.Jenkins;
28 import org.jvnet.hudson.annotation_indexer.Index;
29 import org.jvnet.hudson.reactor.Milestone;
30 import org.jvnet.hudson.reactor.Task;
31 import org.jvnet.hudson.reactor.TaskBuilder;
32 import org.jvnet.hudson.reactor.MilestoneImpl;
33 import org.jvnet.hudson.reactor.Reactor;
34 import org.jvnet.localizer.ResourceBundleHolder;
35
36 import java.io.IOException;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.lang.reflect.Modifier;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.MissingResourceException;
45 import java.util.Set;
46 import java.util.logging.Logger;
47
48 import static java.util.logging.Level.WARNING;
49
50 /**
51 * Discovers initialization tasks from {@link Initializer}.
52 *
53 * @author Kohsuke Kawaguchi
54 */
55 public class InitializerFinder extends TaskBuilder {
56 private final ClassLoader cl;
57
58 private final Set<Method> discovered = new HashSet<Method>();
59
60 public InitializerFinder(ClassLoader cl) {
61 this.cl = cl;
62 }
63
64 public InitializerFinder() {
65 this(Thread.currentThread().getContextClassLoader());
66 }
67
68 public Collection<Task> discoverTasks(Reactor session) throws IOException {
69 List<Task> result = new ArrayList<Task>();
70 for (Method e : Index.list(Initializer.class,cl,Method.class)) {
71 if (filter(e)) continue; // already reported once
72
73 if (!Modifier.isStatic(e.getModifiers()))
74 throw new IOException(e+" is not a static method");
75
76 Initializer i = e.getAnnotation(Initializer.class);
77 if (i==null) continue; // stale index
78
79 result.add(new TaskImpl(i, e));
80 }
81 return result;
82 }
83
84 /**
85 * Return true to ignore this method.
86 */
87 protected boolean filter(Method e) {
88 return !discovered.add(e);
89 }
90
91 /**
92 * Obtains the display name of the given initialization task
93 */
94 protected String getDisplayNameOf(Method e, Initializer i) {
95 Class<?> c = e.getDeclaringClass();
96 String key = i.displayName();
97 if (key.length()==0) return c.getSimpleName()+"."+e.getName();
98 try {
99 ResourceBundleHolder rb = ResourceBundleHolder.get(
100 c.getClassLoader().loadClass(c.getPackage().getName() + ".Messages"));
101 return rb.format(key);
102 } catch (ClassNotFoundException x) {
103 LOGGER.log(WARNING, "Failed to load "+x.getMessage()+" for "+e.toString(),x);
104 return key;
105 } catch (MissingResourceException x) {
106 LOGGER.log(WARNING, "Could not find key '" + key + "' in " + c.getPackage().getName() + ".Messages", x);
107 return key;
108 }
109 }
110
111 /**
112 * Invokes the given initialization method.
113 */
114 protected void invoke(Method e) {
115 try {
116 Class<?>[] pt = e.getParameterTypes();
117 Object[] args = new Object[pt.length];
118 for (int i=0; i<args.length; i++)
119 args[i] = lookUp(pt[i]);
120 e.invoke(null,args);
121 } catch (IllegalAccessException x) {
122 throw (Error)new IllegalAccessError().initCause(x);
123 } catch (InvocationTargetException x) {
124 throw new Error(x);
125 }
126 }
127
128 /**
129 * Determines the parameter injection of the initialization method.
130 */
131 private Object lookUp(Class<?> type) {
132 if (type==Jenkins.class || type==Hudson.class)
133 return Jenkins.getInstance();
134 throw new IllegalArgumentException("Unable to inject "+type);
135 }
136
137 /**
138 * Task implementation.
139 */
140 public class TaskImpl implements Task {
141 final Collection<Milestone> requires;
142 final Collection<Milestone> attains;
143 private final Initializer i;
144 private final Method e;
145
146 private TaskImpl(Initializer i, Method e) {
147 this.i = i;
148 this.e = e;
149 requires = toMilestones(i.requires(), i.after());
150 attains = toMilestones(i.attains(), i.before());
151 }
152
153 /**
154 * {@link Initializer} annotaion on the {@linkplain #getMethod() method}
155 */
156 public Initializer getAnnotation() {
157 return i;
158 }
159
160 /**
161 * Static method that runs the initialization, that this task wraps.
162 */
163 public Method getMethod() {
164 return e;
165 }
166
167 public Collection<Milestone> requires() {
168 return requires;
169 }
170
171 public Collection<Milestone> attains() {
172 return attains;
173 }
174
175 public String getDisplayName() {
176 return getDisplayNameOf(e, i);
177 }
178
179 public boolean failureIsFatal() {
180 return i.fatal();
181 }
182
183 public void run(Reactor session) {
184 invoke(e);
185 }
186
187 public String toString() {
188 return e.toString();
189 }
190
191 private Collection<Milestone> toMilestones(String[] tokens, InitMilestone m) {
192 List<Milestone> r = new ArrayList<Milestone>();
193 for (String s : tokens) {
194 try {
195 r.add(InitMilestone.valueOf(s));
196 } catch (IllegalArgumentException x) {
197 r.add(new MilestoneImpl(s));
198 }
199 }
200 r.add(m);
201 return r;
202 }
203 }
204
205 private static final Logger LOGGER = Logger.getLogger(InitializerFinder.class.getName());
206 }
207