
hudson.console.ConsoleAnnotatorFactory Maven / Gradle / Ivy
Show all versions of hudson-core Show documentation
/*******************************************************************************
*
* Copyright (c) 2004-2010, Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
*
*
*
*******************************************************************************/
package hudson.console;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Hudson;
import hudson.model.Run;
import hudson.util.TimeUnit2;
import org.jvnet.tiger_types.Types;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;
import javax.servlet.ServletException;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URL;
/**
* Entry point to the {@link ConsoleAnnotator} extension point. This class creates a new instance
* of {@link ConsoleAnnotator} that starts a new console annotation session.
*
*
* {@link ConsoleAnnotatorFactory}s are used whenever a browser requests console output (as opposed to when
* the console output is being produced — for that see {@link ConsoleNote}.)
*
*
* {@link ConsoleAnnotator}s returned by {@link ConsoleAnnotatorFactory} are asked to start from
* an arbitrary line of the output, because typically browsers do not request the entire console output.
* Because of this, {@link ConsoleAnnotatorFactory} is generally suitable for peep-hole local annotation
* that only requires a small contextual information, such as keyword coloring, URL hyperlinking, and so on.
*
*
* To register, put @{@link Extension} on your {@link ConsoleAnnotatorFactory} subtype.
*
*
Behaviour, JavaScript, and CSS
*
* {@link ConsoleNote} can have associated script.js and style.css (put them
* in the same resource directory that you normally put Jelly scripts), which will be loaded into
* the HTML page whenever the console notes are used. This allows you to use minimal markup in
* code generation, and do the styling in CSS and perform the rest of the interesting work as a CSS behaviour/JavaScript.
*
* @author Kohsuke Kawaguchi
* @since 1.349
*/
public abstract class ConsoleAnnotatorFactory implements ExtensionPoint {
/**
* Called when a console output page is requested to create a stateful {@link ConsoleAnnotator}.
*
*
* This method can be invoked concurrently by multiple threads.
*
* @param context
* The model object that owns the console output, such as {@link Run}.
* This method is only called when the context object if assignable to
* {@linkplain #type() the advertised type}.
* @return
* null if this factory is not going to participate in the annotation of this console.
*/
public abstract ConsoleAnnotator newInstance(T context);
/**
* For which context type does this annotator work?
*/
public Class type() {
Type type = Types.getBaseClass(getClass(), ConsoleAnnotator.class);
if (type instanceof ParameterizedType)
return Types.erasure(Types.getTypeArgument(type,0));
else
return Object.class;
}
/**
* Returns true if this descriptor has a JavaScript to be inserted on applicable console page.
*/
public boolean hasScript() {
return getResource("/script.js") !=null;
}
public boolean hasStylesheet() {
return getResource("/style.css") !=null;
}
private URL getResource(String fileName) {
Class c = getClass();
return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+ fileName);
}
/**
* Serves the JavaScript file associated with this console annotator factory.
*/
@WebMethod(name="script.js")
public void doScriptJs(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req, getResource("/script.js"), TimeUnit2.DAYS.toMillis(1));
}
@WebMethod(name="style.css")
public void doStyleCss(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.serveFile(req, getResource("/style.css"), TimeUnit2.DAYS.toMillis(1));
}
/**
* All the registered instances.
*/
public static ExtensionList all() {
return Hudson.getInstance().getExtensionList(ConsoleAnnotatorFactory.class);
}
}