KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > Plugin


1 package hudson;
2
3 import hudson.model.Hudson;
4 import hudson.scm.SCM;
5 import hudson.tasks.Builder;
6 import hudson.tasks.Publisher;
7 import hudson.triggers.Trigger;
8 import org.kohsuke.stapler.StaplerRequest;
9 import org.kohsuke.stapler.StaplerResponse;
10
11 import javax.servlet.ServletContext JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 /**
18  * Base class of Hudson plugin.
19  *
20  * <p>
21  * A plugin needs to derive from this class.
22  *
23  * <p>
24  * One instance of a plugin is created by Hudson, and used as the entry point
25  * to plugin functionality.
26  *
27  * <p>
28  * A plugin is bound to URL space of Hudson as <tt>${rootURL}/plugin/foo/</tt>,
29  * where "foo" is taken from your plugin name "foo.hpi". All your web resources
30  * in src/main/webapp are visible from this URL, and you can also define Jelly
31  * views against your Plugin class, and those are visible in this URL, too.
32  *
33  * @author Kohsuke Kawaguchi
34  * @since 1.42
35  */

36 public abstract class Plugin {
37
38     /**
39      * Set by the {@link PluginManager}.
40      */

41     /*package*/ PluginWrapper wrapper;
42
43     /**
44      * Called when a plugin is loaded to make the {@link ServletContext} object available to a plugin.
45      * This object allows plugins to talk to the surrounding environment.
46      *
47      * <p>
48      * The default implementation is no-op.
49      *
50      * @param context
51      * Always non-null.
52      *
53      * @since 1.42
54      */

55     public void setServletContext(ServletContext JavaDoc context) {
56     }
57
58     /**
59      * Called to allow plugins to initialize themselves.
60      *
61      * <p>
62      * This method is called after {@link #setServletContext(ServletContext)} is invoked.
63      * You can also use {@link Hudson#getInstance()} to access the singleton hudson instance,
64      * although the plugin start up happens relatively early in the initialization
65      * stage and not all the data are loaded in Hudson.
66      *
67      * <p>
68      * Plugins should override this method and register custom
69      * {@link Publisher}, {@link Builder}, {@link SCM}, and {@link Trigger}s to the corresponding list.
70      * See {@link ExtensionPoint} for the complete list of extension points in Hudson.
71      *
72      *
73      * @throws Exception
74      * any exception thrown by the plugin during the initialization will disable plugin.
75      *
76      * @since 1.42
77      * @see ExtensionPoint
78      */

79     public void start() throws Exception JavaDoc {
80     }
81
82     /**
83      * Called to orderly shut down Hudson.
84      *
85      * <p>
86      * This is a good opportunity to clean up resources that plugin started.
87      * This method will not be invoked if the {@link #start()} failed abnormally.
88      *
89      * @throws Exception
90      * if any exception is thrown, it is simply recorded and shut-down of other
91      * plugins continue. This is primarily just a convenience feature, so that
92      * each plugin author doesn't have to worry about catching an exception and
93      * recording it.
94      *
95      * @since 1.42
96      */

97     public void stop() throws Exception JavaDoc {
98     }
99
100     /**
101      * This method serves static resources in the plugin under <tt>hudson/plugin/SHORTNAME</tt>.
102      */

103     public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc {
104         String JavaDoc path = req.getRestOfPath();
105
106         if(path.length()==0)
107             path = "/";
108
109         if(path.indexOf("..")!=-1 || path.length()<1) {
110             // don't serve anything other than files in the sub directory.
111
rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
112             return;
113         }
114
115         rsp.serveFile(req, new URL JavaDoc(wrapper.baseResourceURL,'.'+path));
116     }
117 }
118
Popular Tags