KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > AbstractItem


1 package hudson.model;
2
3 import hudson.XmlFile;
4 import hudson.Util;
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.util.Collection JavaDoc;
9
10 import org.kohsuke.stapler.StaplerRequest;
11 import org.kohsuke.stapler.StaplerResponse;
12
13 import javax.servlet.ServletException JavaDoc;
14
15 /**
16  * Partial default implementation of {@link Item}.
17  *
18  * @author Kohsuke Kawaguchi
19  */

20 // Item doesn't necessarily have to be Actionable, but
21
// Java doesn't let multiple inheritance.
22
public abstract class AbstractItem extends Actionable implements Item {
23     /**
24      * Project name.
25      */

26     protected /*final*/ transient String JavaDoc name;
27
28     /**
29      * Project description. Can be HTML.
30      */

31     protected String JavaDoc description;
32
33     private transient ItemGroup parent;
34
35     protected AbstractItem(ItemGroup parent, String JavaDoc name) {
36         this.parent = parent;
37         doSetName(name);
38     }
39
40     public String JavaDoc getName() {
41         return name;
42     }
43
44     public String JavaDoc getDisplayName() {
45         return getName();
46     }
47
48     public File getRootDir() {
49         return parent.getRootDirFor(this);
50     }
51
52     public ItemGroup getParent() {
53         assert parent!=null;
54         return parent;
55     }
56
57     /**
58      * Gets the project description HTML.
59      */

60     public String JavaDoc getDescription() {
61         return description;
62     }
63
64     /**
65      * Sets the project description HTML.
66      */

67     public void setDescription(String JavaDoc description) {
68         this.description = description;
69     }
70
71     /**
72      * Just update {@link #name} without performing the rename operation,
73      * which would involve copying files and etc.
74      */

75     protected void doSetName(String JavaDoc name) {
76         this.name = name;
77         getRootDir().mkdirs();
78     }
79
80     /**
81      * Gets all the jobs that this {@link Item} contains as descendants.
82      */

83     public abstract Collection JavaDoc<? extends Job> getAllJobs();
84
85     public final String JavaDoc getFullName() {
86         String JavaDoc n = getParent().getFullName();
87         if(n.length()==0) return getName();
88         else return n+'/'+getName();
89     }
90
91     /**
92      * Called right after when a {@link Item} is loaded from disk.
93      * This is an opporunity to do a post load processing.
94      */

95     public void onLoad(ItemGroup<? extends Item> parent, String JavaDoc name) throws IOException JavaDoc {
96         this.parent = parent;
97         doSetName(name);
98     }
99
100     /**
101      * When a {@link Item} is copied from existing one,
102      * the files are first copied on the file system,
103      * then it will be loaded, then this method will be invoked
104      * to perform any implementation-specific work.
105      */

106     public void onCopiedFrom(Item src) {
107     }
108
109     public final String JavaDoc getUrl() {
110         return getParent().getUrl()+getShortUrl();
111     }
112
113     public String JavaDoc getShortUrl() {
114         return getParent().getUrlChildPrefix()+'/'+getName()+'/';
115     }
116
117     /**
118      * Save the settings to a file.
119      */

120     public synchronized void save() throws IOException JavaDoc {
121         getConfigFile().write(this);
122     }
123
124     protected final XmlFile getConfigFile() {
125         return Items.getConfigFile(this);
126     }
127
128     /**
129      * Accepts the new description.
130      */

131     public synchronized void doSubmitDescription( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
132         if(!Hudson.adminCheck(req,rsp))
133             return;
134
135         req.setCharacterEncoding("UTF-8");
136         setDescription(req.getParameter("description"));
137         save();
138         rsp.sendRedirect("."); // go to the top page
139
}
140
141     /**
142      * Deletes this item.
143      */

144     public synchronized void doDoDelete( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc {
145         if(!Hudson.adminCheck(req,rsp))
146             return;
147         Util.deleteRecursive(getRootDir());
148
149         if(this instanceof TopLevelItem)
150             Hudson.getInstance().deleteJob((TopLevelItem)this);
151         rsp.sendRedirect2(req.getContextPath()+"/");
152     }
153 }
154
Popular Tags