KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > JavadocArchiver


1 package hudson.tasks;
2
3 import hudson.FilePath;
4 import hudson.Launcher;
5 import hudson.Util;
6 import hudson.model.Action;
7 import hudson.model.Build;
8 import hudson.model.BuildListener;
9 import hudson.model.Descriptor;
10 import hudson.model.DirectoryBrowserSupport;
11 import hudson.model.Project;
12 import hudson.model.ProminentProjectAction;
13 import hudson.model.Result;
14 import hudson.model.Actionable;
15 import hudson.model.AbstractItem;
16 import org.kohsuke.stapler.StaplerRequest;
17 import org.kohsuke.stapler.StaplerResponse;
18
19 import javax.servlet.ServletException JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * Saves Javadoc for the project and publish them.
25  *
26  * @author Kohsuke Kawaguchi
27  */

28 public class JavadocArchiver extends Publisher {
29     /**
30      * Path to the Javadoc directory in the workspace.
31      */

32     private final String JavaDoc javadocDir;
33
34     public JavadocArchiver(String JavaDoc javadocDir) {
35         this.javadocDir = javadocDir;
36     }
37
38     public String JavaDoc getJavadocDir() {
39         return javadocDir;
40     }
41
42     /**
43      * Gets the directory where the Javadoc is stored for the given project.
44      */

45     private static File JavaDoc getJavadocDir(AbstractItem project) {
46         return new File JavaDoc(project.getRootDir(),"javadoc");
47     }
48
49     public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException JavaDoc {
50         listener.getLogger().println("Publishing Javadoc");
51
52         FilePath javadoc = build.getParent().getWorkspace().child(javadocDir);
53         FilePath target = new FilePath(getJavadocDir(build.getParent()));
54
55         try {
56             javadoc.copyRecursiveTo("**/*",target);
57         } catch (IOException JavaDoc e) {
58             Util.displayIOException(e,listener);
59             e.printStackTrace(listener.fatalError("Unable to copy Javadoc from "+javadoc+" to "+target));
60             build.setResult(Result.FAILURE);
61         }
62
63         return true;
64     }
65
66     public Action getProjectAction(Project project) {
67         return new JavadocAction(project);
68     }
69
70     public Descriptor<Publisher> getDescriptor() {
71         return DESCRIPTOR;
72     }
73
74
75     public static final Descriptor<Publisher> DESCRIPTOR = new Descriptor<Publisher>(JavadocArchiver.class) {
76         public String JavaDoc getDisplayName() {
77             return "Publish Javadoc";
78         }
79
80         public Publisher newInstance(StaplerRequest req) {
81             return new JavadocArchiver(req.getParameter("javadoc_dir"));
82         }
83     };
84
85     public static final class JavadocAction extends Actionable implements ProminentProjectAction {
86         private final AbstractItem project;
87
88         public JavadocAction(AbstractItem project) {
89             this.project = project;
90         }
91
92         public String JavaDoc getUrlName() {
93             return "javadoc";
94         }
95
96         public String JavaDoc getDisplayName() {
97             return "Javadoc";
98         }
99
100         public String JavaDoc getIconFileName() {
101             if(getJavadocDir(project).exists())
102                 return "help.gif";
103             else
104                 // hide it since we don't have javadoc yet.
105
return null;
106         }
107
108         public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc, InterruptedException JavaDoc {
109             new DirectoryBrowserSupport(this).serveFile(req, rsp, new FilePath(getJavadocDir(project)), "help.gif", false);
110         }
111     }
112 }
113
Popular Tags