1 package hudson; 2 3 import hudson.model.Hudson; 4 import hudson.model.ModelObject; 5 import hudson.model.Node; 6 import hudson.model.Project; 7 import hudson.model.Run; 8 import hudson.model.Items; 9 import org.kohsuke.stapler.Ancestor; 10 import org.kohsuke.stapler.StaplerRequest; 11 12 import javax.servlet.http.Cookie ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.net.URI ; 18 import java.net.URISyntaxException ; 19 import java.util.Calendar ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.SortedMap ; 23 import java.util.TreeMap ; 24 import java.util.logging.LogRecord ; 25 import java.util.logging.SimpleFormatter ; 26 27 35 public class Functions { 36 private static volatile int globalIota = 0; 37 38 private int iota; 39 40 public Functions() { 41 iota = globalIota; 42 globalIota+=1000; 45 } 46 47 50 public String generateId() { 51 return "id"+iota++; 52 } 53 54 public static boolean isModel(Object o) { 55 return o instanceof ModelObject; 56 } 57 58 public static String xsDate(Calendar cal) { 59 return Util.XS_DATETIME_FORMATTER.format(cal.getTime()); 60 } 61 62 public static String rfc822Date(Calendar cal) { 63 return Util.RFC822_DATETIME_FORMATTER.format(cal.getTime()); 64 } 65 66 70 public static String getDiffString(int i) { 71 if(i==0) return "\u00B10"; String s = Integer.toString(i); 73 if(i>0) return "+"+s; 74 else return s; 75 } 76 77 80 public static String getDiffString2(int i) { 81 if(i==0) return ""; 82 String s = Integer.toString(i); 83 if(i>0) return "+"+s; 84 else return s; 85 } 86 87 90 public static String addSuffix(int n, String singular, String plural) { 91 StringBuffer buf = new StringBuffer (); 92 buf.append(n).append(' '); 93 if(n==1) 94 buf.append(singular); 95 else 96 buf.append(plural); 97 return buf.toString(); 98 } 99 100 public static RunUrl decompose(StaplerRequest req) { 101 @SuppressWarnings ("unchecked") List <Ancestor> ancestors = req.getAncestors(); 103 for (Ancestor anc : ancestors) { 104 if(anc.getObject() instanceof Run) { 105 String ancUrl = anc.getUrl(); 107 108 String reqUri = req.getOriginalRequestURI(); 109 if(reqUri.indexOf(' ')>=0) { 114 try { 115 reqUri = new URI (null,reqUri,null).toASCIIString(); 117 } catch (URISyntaxException e) { 118 } 120 } 121 122 return new RunUrl( 123 (Run) anc.getObject(), ancUrl, 124 reqUri.substring(ancUrl.length()), 125 req.getContextPath() ); 126 } 127 } 128 return null; 129 } 130 131 public static final class RunUrl { 132 private final String contextPath; 133 private final String basePortion; 134 private final String rest; 135 private final Run run; 136 137 public RunUrl(Run run, String basePortion, String rest, String contextPath) { 138 this.run = run; 139 this.basePortion = basePortion; 140 this.rest = rest; 141 this.contextPath = contextPath; 142 } 143 144 public String getBaseUrl() { 145 return basePortion; 146 } 147 148 151 public String getNextBuildUrl() { 152 return getUrl(run.getNextBuild()); 153 } 154 155 158 public String getPreviousBuildUrl() { 159 return getUrl(run.getPreviousBuild()); 160 } 161 162 private String getUrl(Run n) { 163 if(n ==null) 164 return null; 165 else { 166 return basePortion+"/../"+n.getNumber()+rest; 167 } 168 } 169 } 170 171 public static Node.Mode[] getNodeModes() { 172 return Node.Mode.values(); 173 } 174 175 public static String getProjectListString(List <Project> projects) { 176 return Items.toNameList(projects); 177 } 178 179 public static Object ifThenElse(boolean cond, Object thenValue, Object elseValue) { 180 return cond ? thenValue : elseValue; 181 } 182 183 public static String appendIfNotNull(String text, String suffix, String nullText) { 184 return text == null ? nullText : text + suffix; 185 } 186 187 public static Map getSystemProperties() { 188 return new TreeMap <Object ,Object >(System.getProperties()); 189 } 190 191 public static Map getEnvVars() { 192 return new TreeMap <String ,String >(EnvVars.masterEnvVars); 193 } 194 195 public static boolean isWindows() { 196 return File.pathSeparatorChar==';'; 197 } 198 199 public static List <LogRecord > getLogRecords() { 200 return Hudson.logRecords; 201 } 202 203 public static String printLogRecord(LogRecord r) { 204 return formatter.format(r); 205 } 206 207 public static Cookie getCookie(HttpServletRequest req,String name) { 208 Cookie [] cookies = req.getCookies(); 209 if(cookies!=null) { 210 for (Cookie cookie : cookies) { 211 if(cookie.getName().equals(name)) { 212 return cookie; 213 } 214 } 215 } 216 return null; 217 } 218 219 public static String getCookie(HttpServletRequest req,String name, String defaultValue) { 220 Cookie c = getCookie(req, name); 221 if(c==null || c.getValue()==null) return defaultValue; 222 return c.getValue(); 223 } 224 225 228 public static String getYuiSuffix() { 229 return DEBUG_YUI ? "debug" : "min"; 230 } 231 232 235 public static boolean DEBUG_YUI = false; 236 237 240 public static <V> SortedMap <Integer ,V> filter(SortedMap <Integer ,V> map, String from, String to) { 241 if(from==null && to==null) return map; 242 if(to==null) 243 return map.headMap(Integer.parseInt(from)-1); 244 if(from==null) 245 return map.tailMap(Integer.parseInt(to)); 246 247 return map.subMap(Integer.parseInt(to),Integer.parseInt(from)-1); 248 } 249 250 private static final SimpleFormatter formatter = new SimpleFormatter (); 251 252 259 public static void configureAutoRefresh(HttpServletRequest request, HttpServletResponse response, boolean noAutoRefresh) { 260 if(noAutoRefresh) 261 return; 262 263 String param = request.getParameter("auto_refresh"); 264 boolean refresh = isAutoRefresh(request); 265 if (param != null) { 266 refresh = Boolean.parseBoolean(param); 267 Cookie c = new Cookie ("hudson_auto_refresh", Boolean.toString(refresh)); 268 c.setPath("/"); 272 response.addCookie(c); 273 } 274 if (refresh) { 275 response.addHeader("Refresh", "10"); 276 } 277 } 278 279 public static boolean isAutoRefresh(HttpServletRequest request) { 280 String param = request.getParameter("auto_refresh"); 281 if (param != null) { 282 return Boolean.parseBoolean(param); 283 } 284 Cookie [] cookies = request.getCookies(); 285 if(cookies==null) 286 return false; 288 for (Cookie c : cookies) { 289 if (c.getName().equals("hudson_auto_refresh")) { 290 return Boolean.parseBoolean(c.getValue()); 291 } 292 } 293 return false; 294 } 295 296 301 public static String getNearestAncestorUrl(StaplerRequest req,Object it) { 302 List list = req.getAncestors(); 303 for( int i=list.size()-1; i>=0; i-- ) { 304 Ancestor anc = (Ancestor) list.get(i); 305 if(anc.getObject()==it) 306 return anc.getUrl(); 307 } 308 return null; 309 } 310 311 public static String appendSpaceIfNotNull(String n) { 312 if(n==null) return null; 313 else return n+' '; 314 } 315 316 public static String getWin32ErrorMessage(IOException e) { 317 return Util.getWin32ErrorMessage(e); 318 } 319 320 public static boolean isMultiline(String s) { 321 if(s==null) return false; 322 return s.indexOf('\r')>=0 || s.indexOf('\n')>=0; 323 } 324 325 public static String encode(String s) { 326 return Util.encode(s); 327 } 328 } 329 | Popular Tags |