KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > User


1 package hudson.model;
2
3 import com.thoughtworks.xstream.XStream;
4 import hudson.CopyOnWrite;
5 import hudson.FeedAdapter;
6 import hudson.XmlFile;
7 import hudson.model.Descriptor.FormException;
8 import hudson.scm.ChangeLogSet;
9 import hudson.util.RunList;
10 import hudson.util.XStream2;
11 import org.kohsuke.stapler.StaplerRequest;
12 import org.kohsuke.stapler.StaplerResponse;
13
14 import javax.servlet.ServletException JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Calendar JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 /**
27  * Represents a user.
28  *
29  * @author Kohsuke Kawaguchi
30  */

31 public class User extends AbstractModelObject {
32
33     private transient final String JavaDoc id;
34
35     private volatile String JavaDoc fullName;
36
37     private volatile String JavaDoc description;
38
39     /**
40      * List of {@link UserProperty}s configured for this project.
41      */

42     @CopyOnWrite
43     private volatile List<UserProperty> properties = new ArrayList JavaDoc<UserProperty>();
44
45
46     private User(String JavaDoc id) {
47         this.id = id;
48         this.fullName = id; // fullName defaults to name
49

50         // load the other data from disk if it's available
51
XmlFile config = getConfigFile();
52         try {
53             if(config.exists())
54                 config.unmarshal(this);
55         } catch (IOException JavaDoc e) {
56             LOGGER.log(Level.SEVERE, "Failed to load "+config,e);
57         }
58
59         // allocate default instances if needed.
60
// doing so after load makes sure that newly added user properties do get reflected
61
for (UserPropertyDescriptor d : UserProperties.LIST) {
62             if(getProperty(d.clazz)==null) {
63                 UserProperty up = d.newInstance(this);
64                 if(up!=null)
65                     properties.add(up);
66             }
67         }
68
69         for (UserProperty p : properties)
70             p.setUser(this);
71     }
72
73     public String JavaDoc getId() {
74         return id;
75     }
76
77     public String JavaDoc getUrl() {
78         return "user/"+ id;
79     }
80
81     /**
82      * Gets the human readable name of this user.
83      * This is configurable by the user.
84      *
85      * @return
86      * never null.
87      */

88     public String JavaDoc getFullName() {
89         return fullName;
90     }
91
92     public String JavaDoc getDescription() {
93         return description;
94     }
95
96     /**
97      * Gets the user properties configured for this user.
98      */

99     public Map JavaDoc<Descriptor<UserProperty>,UserProperty> getProperties() {
100         return Descriptor.toMap(properties);
101     }
102
103     /**
104      * Gets the specific property, or null.
105      */

106     public <T extends UserProperty> T getProperty(Class JavaDoc<T> clazz) {
107         for (UserProperty p : properties) {
108             if(clazz.isInstance(p))
109                 return clazz.cast(p);
110         }
111         return null;
112     }
113
114     /**
115      * Accepts the new description.
116      */

117     public synchronized void doSubmitDescription( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
118         req.setCharacterEncoding("UTF-8");
119
120         description = req.getParameter("description");
121         save();
122         
123         rsp.sendRedirect("."); // go to the top page
124
}
125
126
127
128     public static User get(String JavaDoc name) {
129         if(name==null)
130             return null;
131         name = name.replace('\\', '_').replace('/', '_');
132         
133         synchronized(byName) {
134             User u = byName.get(name);
135             if(u==null) {
136                 u = new User(name);
137                 byName.put(name,u);
138             }
139             return u;
140         }
141     }
142
143     /**
144      * Returns the user name.
145      */

146     public String JavaDoc getDisplayName() {
147         return getFullName();
148     }
149
150     /**
151      * Gets the list of {@link Build}s that include changes by this user,
152      * by the timestamp order.
153      *
154      * TODO: do we need some index for this?
155      */

156     public List<AbstractBuild> getBuilds() {
157         List<AbstractBuild> r = new ArrayList JavaDoc<AbstractBuild>();
158         for (AbstractProject<?,?> p : Hudson.getInstance().getAllItems(AbstractProject.class)) {
159             for (AbstractBuild<?,?> b : p.getBuilds()) {
160                 for (ChangeLogSet.Entry e : b.getChangeSet()) {
161                     if(e.getAuthor()==this) {
162                         r.add(b);
163                         break;
164                     }
165                 }
166             }
167         }
168         Collections.sort(r,Run.ORDER_BY_DATE);
169         return r;
170     }
171
172     public String JavaDoc toString() {
173         return fullName;
174     }
175
176     /**
177      * The file we save our configuration.
178      */

179     protected final XmlFile getConfigFile() {
180         return new XmlFile(XSTREAM,new File(Hudson.getInstance().getRootDir(),"users/"+ id +"/config.xml"));
181     }
182
183     /**
184      * Save the settings to a file.
185      */

186     public synchronized void save() throws IOException JavaDoc {
187         XmlFile config = getConfigFile();
188         config.mkdirs();
189         config.write(this);
190     }
191
192     /**
193      * Accepts submission from the configuration page.
194      */

195     public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
196         if(!Hudson.adminCheck(req,rsp))
197             return;
198
199         req.setCharacterEncoding("UTF-8");
200
201         try {
202             fullName = req.getParameter("fullName");
203             description = req.getParameter("description");
204
205             List<UserProperty> props = new ArrayList JavaDoc<UserProperty>();
206             for (Descriptor<UserProperty> d : UserProperties.LIST)
207                 props.add(d.newInstance(req));
208             this.properties = props;
209
210             save();
211
212             rsp.sendRedirect(".");
213         } catch (FormException e) {
214             sendError(e,req,rsp);
215         }
216     }
217
218     public void doRssAll( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
219         rss(req, rsp, " all builds", RunList.fromRuns(getBuilds()));
220     }
221
222     public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
223         rss(req, rsp, " regression builds", RunList.fromRuns(getBuilds()).regressionOnly());
224     }
225
226     private void rss(StaplerRequest req, StaplerResponse rsp, String JavaDoc suffix, RunList runs) throws IOException JavaDoc, ServletException JavaDoc {
227         RSS.forwardToRss(getDisplayName()+ suffix, getUrl(),
228             runs.newBuilds(), FEED_ADAPTER, req, rsp );
229     }
230
231
232     /**
233      * Keyed by {@link User#id}.
234      */

235     private static final Map JavaDoc<String JavaDoc,User> byName = new HashMap JavaDoc<String JavaDoc,User>();
236
237     /**
238      * Used to load/save user configuration.
239      */

240     private static final XStream XSTREAM = new XStream2();
241
242     private static final Logger JavaDoc LOGGER = Logger.getLogger(User.class.getName());
243
244     static {
245         XSTREAM.alias("user",User.class);
246     }
247
248     /**
249      * {@link FeedAdapter} to produce build status summary in the feed.
250      */

251     public static final FeedAdapter<Run> FEED_ADAPTER = new FeedAdapter<Run>() {
252         public String JavaDoc getEntryTitle(Run entry) {
253             return entry+" : "+entry.getBuildStatusSummary().message;
254         }
255
256         public String JavaDoc getEntryUrl(Run entry) {
257             return entry.getUrl();
258         }
259
260         public String JavaDoc getEntryID(Run entry) {
261             return "tag:"+entry.getParent().getName()+':'+entry.getId();
262         }
263
264         public Calendar JavaDoc getEntryTimestamp(Run entry) {
265             return entry.getTimestamp();
266         }
267     };
268 }
269
Popular Tags