1 37 package net.sourceforge.cruisecontrol.taglib; 38 39 import java.io.File ; 40 import java.io.FilenameFilter ; 41 import java.util.Arrays ; 42 import java.util.Enumeration ; 43 import java.util.Locale ; 44 45 import javax.servlet.ServletRequest ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.jsp.JspException ; 48 import javax.servlet.jsp.PageContext ; 49 import javax.servlet.jsp.tagext.TagSupport ; 50 51 import net.sourceforge.cruisecontrol.LogFile; 52 53 import org.apache.commons.logging.Log; 54 import org.apache.commons.logging.LogFactory; 55 56 57 62 public class CruiseControlTagSupport extends TagSupport { 63 protected static final String LOG_PARAMETER = "log"; 64 65 public static Log getLog(Class clazz) { 66 return (LogFactory.getLog(clazz)); 67 } 68 69 private static final FilenameFilter DIR_FILTER = new FilenameFilter () { 70 public boolean accept(File dir, String name) { 71 return (new File (dir, name).isDirectory()); 72 } 73 }; 74 75 private String projectName = null; 76 77 protected void info(String message) { 78 getLog(this.getClass()).info(message); 79 } 80 81 protected void err(String message) { 82 getLog(this.getClass()).error(message); 83 } 84 85 protected void err(Throwable exception) { 86 getLog(this.getClass()).error(exception); 87 } 88 89 protected String getBaseLogDir() throws JspException { 90 String logDirName = getContextParam("logDir"); 91 if (logDirName == null) { 92 throw new JspException ("You need to specify a log directory as a context param"); 93 } 94 return logDirName; 95 } 96 97 protected File findLogDir() throws JspException { 98 String logDirName = getBaseLogDir() + getProject(); 99 File logDir = new File (logDirName); 100 if (!logDir.isDirectory()) { 101 throw new JspException (logDirName + " either does not exist, or is not a directory"); 102 } 103 return logDir; 104 } 105 106 113 protected LogFile findLogFile() throws JspException { 114 String logFile = getPageContext().getRequest().getParameter(LOG_PARAMETER); 115 return getXMLFile(findLogDir(), logFile); 116 } 117 118 LogFile getXMLFile(File logDir, String logName) { 119 LogFile logFile; 120 if (logName == null || logName.trim().equals("")) { 121 logFile = LogFile.getLatestLogFile(logDir); 122 info("Using latest log file: " + logFile.getFile().getAbsolutePath()); 123 } else { 124 logFile = new LogFile(logDir, logName); 125 info("Using specified log file: " + logFile.getFile().getAbsolutePath()); 126 } 127 return logFile; 128 } 129 130 protected String [] findProjects() throws JspException { 131 String logDirName = getBaseLogDir(); 132 File logDir = new File (logDirName); 133 if (!logDir.isDirectory()) { 134 throw new JspException (logDirName + " either does not exist, or is not a directory"); 135 } 136 String [] projects = logDir.list(DIR_FILTER); 137 Arrays.sort(projects); 138 return projects; 139 } 140 141 protected String getContextParam(final String name) { 142 String value = pageContext.getServletConfig().getInitParameter(name); 143 if (value == null) { 144 value = pageContext.getServletContext().getInitParameter(name); 145 } 146 return value; 147 } 148 149 public void setProject(String projectName) { 150 this.projectName = projectName; 151 } 152 153 protected String getProject() { 154 155 if (projectName != null) { 156 return "/" + projectName; 157 } 158 159 String singleProjectMode = getContextParam("singleProject"); 160 if (Boolean.valueOf(singleProjectMode).booleanValue()) { 161 info("in singleProjectMode"); 162 return ""; 163 } 164 String pathInfo = getRequest().getPathInfo(); 165 if (pathInfo == null) { 166 info("pathInfo is null"); 167 return ""; 168 } 169 return pathInfo; 170 } 171 172 177 protected boolean isSingleProject() { 178 String singleProjectMode = getContextParam("singleProject"); 179 boolean isSingleProject = Boolean.getBoolean(singleProjectMode); 180 181 return isSingleProject; 182 } 183 184 public void setPageContext(PageContext pageContext) { 185 this.pageContext = pageContext; 186 } 187 188 protected PageContext getPageContext() { 189 return pageContext; 190 } 191 192 protected String getServletPath() { 193 final HttpServletRequest request = getRequest(); 194 return request.getContextPath() + request.getServletPath(); 195 } 196 197 protected HttpServletRequest getRequest() { 198 return (HttpServletRequest ) getPageContext().getRequest(); 199 } 200 201 206 protected String createUrl(String paramName, String paramValue) { 207 StringBuffer url = new StringBuffer (getServletPath()); 208 url.append(getProject()); 209 StringBuffer queryString = new StringBuffer ("?"); 210 final ServletRequest request = getPageContext().getRequest(); 211 Enumeration requestParams = request.getParameterNames(); 212 while (requestParams.hasMoreElements()) { 213 String requestParamName = (String ) requestParams.nextElement(); 214 if (!requestParamName.equals(paramName)) { 215 String [] requestParamValues = request.getParameterValues(requestParamName); 216 for (int i = 0; i < requestParamValues.length; i++) { 217 final String requestParamValue = requestParamValues[i]; 218 appendParam(queryString, requestParamName, requestParamValue); 219 } 220 } 221 } 222 url.append(queryString.toString()); 223 if (paramName != null && paramValue != null) { 224 appendParam(url, paramName, paramValue); 225 } 226 url.setLength(url.length() - 1); 227 return url.toString(); 228 } 229 230 private void appendParam(StringBuffer queryString, String name, final String value) { 231 queryString.append(name); 232 queryString.append("="); 233 queryString.append(value); 234 queryString.append("&"); 235 } 236 237 protected String createUrl(String paramToExclude) { 238 return createUrl(paramToExclude, null); 239 } 240 241 protected Locale getLocale() { 242 return pageContext.getRequest().getLocale(); 243 } 244 } 245 | Popular Tags |