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 ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.Calendar ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 31 public class User extends AbstractModelObject { 32 33 private transient final String id; 34 35 private volatile String fullName; 36 37 private volatile String description; 38 39 42 @CopyOnWrite 43 private volatile List<UserProperty> properties = new ArrayList <UserProperty>(); 44 45 46 private User(String id) { 47 this.id = id; 48 this.fullName = id; 50 XmlFile config = getConfigFile(); 52 try { 53 if(config.exists()) 54 config.unmarshal(this); 55 } catch (IOException e) { 56 LOGGER.log(Level.SEVERE, "Failed to load "+config,e); 57 } 58 59 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 getId() { 74 return id; 75 } 76 77 public String getUrl() { 78 return "user/"+ id; 79 } 80 81 88 public String getFullName() { 89 return fullName; 90 } 91 92 public String getDescription() { 93 return description; 94 } 95 96 99 public Map <Descriptor<UserProperty>,UserProperty> getProperties() { 100 return Descriptor.toMap(properties); 101 } 102 103 106 public <T extends UserProperty> T getProperty(Class <T> clazz) { 107 for (UserProperty p : properties) { 108 if(clazz.isInstance(p)) 109 return clazz.cast(p); 110 } 111 return null; 112 } 113 114 117 public synchronized void doSubmitDescription( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 118 req.setCharacterEncoding("UTF-8"); 119 120 description = req.getParameter("description"); 121 save(); 122 123 rsp.sendRedirect("."); } 125 126 127 128 public static User get(String 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 146 public String getDisplayName() { 147 return getFullName(); 148 } 149 150 156 public List<AbstractBuild> getBuilds() { 157 List<AbstractBuild> r = new ArrayList <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 toString() { 173 return fullName; 174 } 175 176 179 protected final XmlFile getConfigFile() { 180 return new XmlFile(XSTREAM,new File(Hudson.getInstance().getRootDir(),"users/"+ id +"/config.xml")); 181 } 182 183 186 public synchronized void save() throws IOException { 187 XmlFile config = getConfigFile(); 188 config.mkdirs(); 189 config.write(this); 190 } 191 192 195 public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 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 <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 , ServletException { 219 rss(req, rsp, " all builds", RunList.fromRuns(getBuilds())); 220 } 221 222 public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 223 rss(req, rsp, " regression builds", RunList.fromRuns(getBuilds()).regressionOnly()); 224 } 225 226 private void rss(StaplerRequest req, StaplerResponse rsp, String suffix, RunList runs) throws IOException , ServletException { 227 RSS.forwardToRss(getDisplayName()+ suffix, getUrl(), 228 runs.newBuilds(), FEED_ADAPTER, req, rsp ); 229 } 230 231 232 235 private static final Map <String ,User> byName = new HashMap <String ,User>(); 236 237 240 private static final XStream XSTREAM = new XStream2(); 241 242 private static final Logger LOGGER = Logger.getLogger(User.class.getName()); 243 244 static { 245 XSTREAM.alias("user",User.class); 246 } 247 248 251 public static final FeedAdapter<Run> FEED_ADAPTER = new FeedAdapter<Run>() { 252 public String getEntryTitle(Run entry) { 253 return entry+" : "+entry.getBuildStatusSummary().message; 254 } 255 256 public String getEntryUrl(Run entry) { 257 return entry.getUrl(); 258 } 259 260 public String getEntryID(Run entry) { 261 return "tag:"+entry.getParent().getName()+':'+entry.getId(); 262 } 263 264 public Calendar getEntryTimestamp(Run entry) { 265 return entry.getTimestamp(); 266 } 267 }; 268 } 269 | Popular Tags |