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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, 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.console;
25
26 import hudson.Extension;
27 import hudson.ExtensionList;
28 import hudson.ExtensionPoint;
29 import jenkins.model.Jenkins;
30 import hudson.model.Run;
31 import hudson.util.TimeUnit2;
32 import org.jvnet.tiger_types.Types;
33 import org.kohsuke.stapler.StaplerRequest;
34 import org.kohsuke.stapler.StaplerResponse;
35 import org.kohsuke.stapler.WebMethod;
36
37 import javax.servlet.ServletException;
38 import java.io.IOException;
39 import java.lang.reflect.ParameterizedType;
40 import java.lang.reflect.Type;
41 import java.net.URL;
42
43 /**
44 * Entry point to the {@link ConsoleAnnotator} extension point. This class creates a new instance
45 * of {@link ConsoleAnnotator} that starts a new console annotation session.
46 *
47 * <p>
48 * {@link ConsoleAnnotatorFactory}s are used whenever a browser requests console output (as opposed to when
49 * the console output is being produced &mdash; for that see {@link ConsoleNote}.)
50 *
51 * <p>
52 * {@link ConsoleAnnotator}s returned by {@link ConsoleAnnotatorFactory} are asked to start from
53 * an arbitrary line of the output, because typically browsers do not request the entire console output.
54 * Because of this, {@link ConsoleAnnotatorFactory} is generally suitable for peep-hole local annotation
55 * that only requires a small contextual information, such as keyword coloring, URL hyperlinking, and so on.
56 *
57 * <p>
58 * To register, put @{@link Extension} on your {@link ConsoleAnnotatorFactory} subtype.
59 *
60 * <h2>Behaviour, JavaScript, and CSS</h2>
61 * <p>
62 * {@link ConsoleNote} can have associated <tt>script.js</tt> and <tt>style.css</tt> (put them
63 * in the same resource directory that you normally put Jelly scripts), which will be loaded into
64 * the HTML page whenever the console notes are used. This allows you to use minimal markup in
65 * code generation, and do the styling in CSS and perform the rest of the interesting work as a CSS behaviour/JavaScript.
66 *
67 * @author Kohsuke Kawaguchi
68 * @since 1.349
69 */
70 public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
71 /**
72 * Called when a console output page is requested to create a stateful {@link ConsoleAnnotator}.
73 *
74 * <p>
75 * This method can be invoked concurrently by multiple threads.
76 *
77 * @param context
78 * The model object that owns the console output, such as {@link Run}.
79 * This method is only called when the context object if assignable to
80 * {@linkplain #type() the advertised type}.
81 * @return
82 * null if this factory is not going to participate in the annotation of this console.
83 */
84 public abstract ConsoleAnnotator newInstance(T context);
85
86 /**
87 * For which context type does this annotator work?
88 */
89 public Class type() {
90 Type type = Types.getBaseClass(getClass(), ConsoleAnnotator.class);
91 if (type instanceof ParameterizedType)
92 return Types.erasure(Types.getTypeArgument(type,0));
93 else
94 return Object.class;
95 }
96
97 /**
98 * Returns true if this descriptor has a JavaScript to be inserted on applicable console page.
99 */
100 public boolean hasScript() {
101 return getResource("/script.js") !=null;
102 }
103
104 public boolean hasStylesheet() {
105 return getResource("/style.css") !=null;
106 }
107
108 private URL getResource(String fileName) {
109 Class c = getClass();
110 return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+ fileName);
111 }
112
113 /**
114 * Serves the JavaScript file associated with this console annotator factory.
115 */
116 @WebMethod(name="script.js")
117 public void doScriptJs(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
118 rsp.serveFile(req, getResource("/script.js"), TimeUnit2.DAYS.toMillis(1));
119 }
120
121 @WebMethod(name="style.css")
122 public void doStyleCss(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
123 rsp.serveFile(req, getResource("/style.css"), TimeUnit2.DAYS.toMillis(1));
124 }
125
126 /**
127 * All the registered instances.
128 */
129 public static ExtensionList<ConsoleAnnotatorFactory> all() {
130 return Jenkins.getInstance().getExtensionList(ConsoleAnnotatorFactory.class);
131 }
132 }
133