KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > taglib > CruiseControlTagSupport


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.taglib;
38
39 import java.io.File JavaDoc;
40 import java.io.FilenameFilter JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Locale JavaDoc;
44
45 import javax.servlet.ServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.jsp.JspException JavaDoc;
48 import javax.servlet.jsp.PageContext JavaDoc;
49 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
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 /**
58  * A helper class to consolidate tags that deal with log files.
59  * @author <a HREF="mailto:robertdw@sourceforge.net">Robert Watkins</a>
60  * @author <a HREF="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
61  */

62 public class CruiseControlTagSupport extends TagSupport JavaDoc {
63     protected static final String JavaDoc LOG_PARAMETER = "log";
64
65     public static Log getLog(Class JavaDoc clazz) {
66         return (LogFactory.getLog(clazz));
67     }
68
69     private static final FilenameFilter JavaDoc DIR_FILTER = new FilenameFilter JavaDoc() {
70         public boolean accept(File JavaDoc dir, String JavaDoc name) {
71             return (new File JavaDoc(dir, name).isDirectory());
72         }
73     };
74
75     private String JavaDoc projectName = null;
76
77     protected void info(String JavaDoc message) {
78         getLog(this.getClass()).info(message);
79     }
80
81     protected void err(String JavaDoc message) {
82         getLog(this.getClass()).error(message);
83     }
84
85     protected void err(Throwable JavaDoc exception) {
86         getLog(this.getClass()).error(exception);
87     }
88
89     protected String JavaDoc getBaseLogDir() throws JspException JavaDoc {
90         String JavaDoc logDirName = getContextParam("logDir");
91         if (logDirName == null) {
92             throw new JspException JavaDoc("You need to specify a log directory as a context param");
93         }
94         return logDirName;
95     }
96         
97     protected File JavaDoc findLogDir() throws JspException JavaDoc {
98         String JavaDoc logDirName = getBaseLogDir() + getProject();
99         File JavaDoc logDir = new File JavaDoc(logDirName);
100         if (!logDir.isDirectory()) {
101             throw new JspException JavaDoc(logDirName + " either does not exist, or is not a directory");
102         }
103         return logDir;
104     }
105
106     /**
107      * Gets the correct log file, based on the project and log file requested
108      * in the query string.
109      *
110      * @return The specifed log file or the latest log, if no log file is
111      * specified in the request.
112      */

113     protected LogFile findLogFile() throws JspException JavaDoc {
114         String JavaDoc logFile = getPageContext().getRequest().getParameter(LOG_PARAMETER);
115         return getXMLFile(findLogDir(), logFile);
116     }
117
118     LogFile getXMLFile(File JavaDoc logDir, String JavaDoc 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 JavaDoc[] findProjects() throws JspException JavaDoc {
131         String JavaDoc logDirName = getBaseLogDir();
132         File JavaDoc logDir = new File JavaDoc(logDirName);
133         if (!logDir.isDirectory()) {
134             throw new JspException JavaDoc(logDirName + " either does not exist, or is not a directory");
135         }
136         String JavaDoc[] projects = logDir.list(DIR_FILTER);
137         Arrays.sort(projects);
138         return projects;
139     }
140
141     protected String JavaDoc getContextParam(final String JavaDoc name) {
142         String JavaDoc 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 JavaDoc projectName) {
150         this.projectName = projectName;
151     }
152
153     protected String JavaDoc getProject() {
154
155         if (projectName != null) {
156             return "/" + projectName;
157         }
158
159         String JavaDoc singleProjectMode = getContextParam("singleProject");
160         if (Boolean.valueOf(singleProjectMode).booleanValue()) {
161             info("in singleProjectMode");
162             return "";
163         }
164         String JavaDoc pathInfo = getRequest().getPathInfo();
165         if (pathInfo == null) {
166             info("pathInfo is null");
167             return "";
168         }
169         return pathInfo;
170     }
171
172     /**
173      * Determine if this is a single project config or not.
174      *
175      * @return true if this is a single project config.
176      */

177     protected boolean isSingleProject() {
178         String JavaDoc singleProjectMode = getContextParam("singleProject");
179         boolean isSingleProject = Boolean.getBoolean(singleProjectMode);
180
181         return isSingleProject;
182     }
183
184     public void setPageContext(PageContext JavaDoc pageContext) {
185         this.pageContext = pageContext;
186     }
187
188     protected PageContext JavaDoc getPageContext() {
189         return pageContext;
190     }
191
192     protected String JavaDoc getServletPath() {
193         final HttpServletRequest JavaDoc request = getRequest();
194         return request.getContextPath() + request.getServletPath();
195     }
196
197     protected HttpServletRequest JavaDoc getRequest() {
198         return (HttpServletRequest JavaDoc) getPageContext().getRequest();
199     }
200
201     /**
202      * Create a link to the app, including the supplied parameter, but preserving all other parameters.
203      * @param paramName the name of the parameter.
204      * @param paramValue the value of the parameter
205      */

206     protected String JavaDoc createUrl(String JavaDoc paramName, String JavaDoc paramValue) {
207         StringBuffer JavaDoc url = new StringBuffer JavaDoc(getServletPath());
208         url.append(getProject());
209         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc("?");
210         final ServletRequest JavaDoc request = getPageContext().getRequest();
211         Enumeration JavaDoc requestParams = request.getParameterNames();
212         while (requestParams.hasMoreElements()) {
213             String JavaDoc requestParamName = (String JavaDoc) requestParams.nextElement();
214             if (!requestParamName.equals(paramName)) {
215                 String JavaDoc[] requestParamValues = request.getParameterValues(requestParamName);
216                 for (int i = 0; i < requestParamValues.length; i++) {
217                     final String JavaDoc 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 JavaDoc queryString, String JavaDoc name, final String JavaDoc value) {
231         queryString.append(name);
232         queryString.append("=");
233         queryString.append(value);
234         queryString.append("&");
235     }
236
237     protected String JavaDoc createUrl(String JavaDoc paramToExclude) {
238         return createUrl(paramToExclude, null);
239     }
240
241     protected Locale JavaDoc getLocale() {
242         return pageContext.getRequest().getLocale();
243     }
244 }
245
Popular Tags