giacomo 01/11/21 14:15:16
Modified: src/org/apache/cocoon/servlet CocoonServlet.java
Log:
Added configurable upload max size.
Added isFooEnabled() around loggin statements.
Cosmetic changes to coding standards
Revision Changes Path
1.53 +202 -94 xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java
Index: CocoonServlet.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -r1.52 -r1.53
--- CocoonServlet.java 2001/11/13 16:50:38 1.52
+++ CocoonServlet.java 2001/11/21 22:15:16 1.53
@@ -15,7 +15,12 @@
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.Loggable;
-import org.apache.cocoon.*;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.Notification;
+import org.apache.cocoon.Notifier;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.cocoon.ConnectionResetException;
+import org.apache.cocoon.Cocoon;
import org.apache.cocoon.components.classloader.RepositoryClassLoader;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.http.HttpContext;
@@ -39,14 +44,12 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.Arrays;
-import java.util.Date;
import java.util.StringTokenizer;
/**
@@ -59,7 +62,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
- * @version CVS $Revision: 1.52 $ $Date: 2001/11/13 16:50:38 $
+ * @version CVS $Revision: 1.53 $ $Date: 2001/11/21 22:15:16 $
*/
public class CocoonServlet extends HttpServlet {
@@ -67,9 +70,9 @@
protected Logger log;
protected LogKitManager logKitManager;
- static final float second = 1000;
- static final float minute = 60 * second;
- static final float hour = 60 * minute;
+ static final float SECOND = 1000;
+ static final float MINUTE = 60 * SECOND;
+ static final float HOUR = 60 * MINUTE;
/** The time the cocoon instance was created */
protected long creationTime = 0;
@@ -92,6 +95,7 @@
private static final boolean SILENTLY_RENAME = true;
private static final boolean SAVE_UPLOADED_FILES_TO_DISK = true;
private static final int MAX_UPLOAD_SIZE = 10000000; // 10Mb
+ private int maxUploadSize = MAX_UPLOAD_SIZE; // 10Mb
private File uploadDir;
private File workDir;
private File cacheDir;
@@ -157,47 +161,64 @@
this.initLogger();
String path = this.servletContextPath;
- log.debug("getRealPath for /: " + path);
- if(path == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("getRealPath for /: " + path);
+ }
+ if (path == null) {
// Try to figure out the path of the root from that of WEB-INF
try {
path = this.servletContext.getResource("/WEB-INF").toString();
} catch (java.net.MalformedURLException me) {
throw new ServletException("Unable to get resource 'WEB-INF'.", me);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("getResource for /WEB-INF: " + path);
}
- log.debug("getResource for /WEB-INF: " + path);
- path = path.substring(0,path.length()-"WEB-INF".length());
- log.debug("Path for Root: " + path);
+ path = path.substring(0,path.length() - "WEB-INF".length());
+ if (log.isDebugEnabled()) {
+ log.debug("Path for Root: " + path);
+ }
}
try {
- if(path.indexOf(':')>1)
+ if (path.indexOf(':') > 1) {
this.servletContextURL = new URL(path);
- else
+ } else {
this.servletContextURL = (new File(path)).toURL();
+ }
} catch (java.net.MalformedURLException me) {
throw new ServletException("Unable to determine servlet context URL.",
me);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("URL for Root: " + this.servletContextURL);
}
- log.debug("URL for Root: " + this.servletContextURL);
this.forceLoadParameter = conf.getInitParameter("load-class");
- if(conf.getInitParameter("load-class") == null) {
- log.debug("load-class was not set - defaulting to false?");
+ if (conf.getInitParameter("load-class") == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("load-class was not set - defaulting to false?");
+ }
}
this.forceSystemProperty = conf.getInitParameter("force-property");
value = conf.getInitParameter("init-classloader");
this.addClassDirs = "true".equalsIgnoreCase(value) ||
"yes".equalsIgnoreCase(value);
- if(value == null) {
- log.debug("init-classloader was not set - defaulting to false");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("init-classloader was not set - defaulting to false");
+ }
}
// add work directory
if ((workDirParam != null) && (!workDirParam.trim().equals(""))) {
- log.debug("using work-directory " + this.workDir);
+ if (log.isDebugEnabled()) {
+ log.debug("using work-directory " + this.workDir);
+ }
} else {
- log.debug("work-directory was not set - defaulting to " + this.workDir);
+ if (log.isDebugEnabled()) {
+ log.debug("work-directory was not set - defaulting to " +
this.workDir);
+ }
}
this.appContext.put(Constants.CONTEXT_WORK_DIR, workDir);
@@ -209,15 +230,24 @@
this.uploadDir = IOUtils.createFile( new File(servletContextPath) ,
uploadDirParam);
}
this.uploadDir.mkdirs();
- log.debug("using upload-directory " + this.uploadDir);
+ if (log.isDebugEnabled()) {
+ log.debug("using upload-directory " + this.uploadDir);
+ }
} else {
this.uploadDir = IOUtils.createFile(workDir, "image-dir" +
File.separator);
- log.debug("upload-directory was not set - defaulting to " +
this.uploadDir);
+ if (log.isDebugEnabled()) {
+ log.debug("upload-directory was not set - defaulting to " +
this.uploadDir);
+ }
}
this.appContext.put(Constants.CONTEXT_UPLOAD_DIR, this.uploadDir);
this.uploadDir.mkdirs();
+ String maxSizeParam = conf.getInitParameter("upload-max-size");
+ if ((maxSizeParam != null) && (!maxSizeParam.trim().equals(""))) {
+ this.maxUploadSize = Integer.parseInt(maxSizeParam);
+ }
+
String cacheDirParam = conf.getInitParameter("cache-directory");
if ((cacheDirParam != null) && (!cacheDirParam.trim().equals(""))) {
if (this.servletContextPath == null) {
@@ -226,10 +256,14 @@
this.cacheDir = IOUtils.createFile( new File(servletContextPath) ,
cacheDirParam);
}
this.cacheDir.mkdirs();
- log.debug("using cache-directory " + this.cacheDir);
+ if (log.isDebugEnabled()) {
+ log.debug("using cache-directory " + this.cacheDir);
+ }
} else {
this.cacheDir = IOUtils.createFile(workDir, "cache-dir" +
File.separator);
- log.debug("cache-directory was not set - defaulting to " +
this.cacheDir);
+ if (log.isDebugEnabled()) {
+ log.debug("cache-directory was not set - defaulting to " +
this.cacheDir);
+ }
}
this.appContext.put(Constants.CONTEXT_CACHE_DIR, this.cacheDir);
@@ -237,27 +271,35 @@
this.appContext.put(Constants.CONTEXT_CONFIG_URL,
this.getConfigFile(conf.getInitParameter("configurations")));
- if(conf.getInitParameter("configurations") == null) {
- log.debug("configurations was not set - defaulting to... ?");
+ if (conf.getInitParameter("configurations") == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("configurations was not set - defaulting to... ?");
+ }
}
// get allow reload parameter, default is true
value = conf.getInitParameter("allow-reload");
this.allowReload = (value == null || value.equalsIgnoreCase("yes") ||
value.equalsIgnoreCase("true"));
- if(value == null) {
- log.debug("allow-reload was not set - defaulting to true");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("allow-reload was not set - defaulting to true");
+ }
}
value = conf.getInitParameter("show-time");
this.showTime = "yes".equalsIgnoreCase(value) ||
"true".equalsIgnoreCase(value)
|| (this.hiddenShowTime = "hide".equals(value));
- if(value == null) {
- log.debug("show-time was not set - defaulting to false");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("show-time was not set - defaulting to false");
+ }
}
parentComponentManagerClass =
conf.getInitParameter("parent-component-manager");
- if(parentComponentManagerClass == null) {
- log.debug("parent-component-manager was not set - defaulting to null.");
+ if (parentComponentManagerClass == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("parent-component-manager was not set - defaulting to
null.");
+ }
}
this.createCocoon();
@@ -270,7 +312,9 @@
{
if (this.cocoon != null)
{
- log.debug("Servlet destroyed - disposing Cocoon");
+ if (log.isDebugEnabled()) {
+ log.debug("Servlet destroyed - disposing Cocoon");
+ }
this.cocoon.dispose();
this.cocoon = null;
}
@@ -297,8 +341,6 @@
*
* We need to get this to work properly when Cocoon is in a war.
*
- * @param context The ServletContext to perform the lookup.
- *
* @throws ServletException
*/
protected String getClassPath()
@@ -307,23 +349,26 @@
File root = null;
- if(servletContextPath != null) {
+ if (servletContextPath != null) {
// Old method. There *MUST* be a better method than this...
String classDir = this.servletContext.getRealPath("/WEB-INF/classes");
String libDir = this.servletContext.getRealPath("/WEB-INF/lib");
- if(libDir != null)
+ if (libDir != null) {
root = new File(libDir);
+ }
- if(classDir != null) {
+ if (classDir != null) {
buildClassPath.append(classDir);
if (this.addClassDirs) {
try {
classLoader.addDirectory(new File(classDir));
} catch (Exception e) {
- log.debug("Could not add directory" + classDir, e);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add directory" + classDir, e);
+ }
}
}
}
@@ -335,27 +380,33 @@
try {
classDirURL = this.servletContext.getResource("/WEB-INF/classes");
} catch (java.net.MalformedURLException me) {
- this.log.warn("Unable to add WEB-INF/classes to the classpath", me);
+ if (log.isWarnEnabled()) {
+ this.log.warn("Unable to add WEB-INF/classes to the classpath",
me);
+ }
}
try {
libDirURL = this.servletContext.getResource("/WEB-INF/lib");
} catch (java.net.MalformedURLException me) {
- this.log.warn("Unable to add WEB-INF/lib to the classpath", me);
+ if (log.isWarnEnabled()) {
+ this.log.warn("Unable to add WEB-INF/lib to the classpath", me);
+ }
}
if (libDirURL != null &&
libDirURL.toExternalForm().startsWith("file:")) {
root = new File(libDirURL.toExternalForm().substring(5));
}
- if(classDirURL != null) {
+ if (classDirURL != null) {
buildClassPath.append(classDirURL.toExternalForm());
if (this.addClassDirs) {
try {
classLoader.addURL(classDirURL);
} catch (Exception e) {
- log.debug("Could not add directory " + classDirURL, e);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add directory " + classDirURL, e);
+ }
}
}
}
@@ -372,7 +423,9 @@
try {
classLoader.addDirectory(libraries[i]);
} catch (Exception e) {
- log.debug("Could not add file" +
IOUtils.getFullFilename(libraries[i]));
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add file" +
IOUtils.getFullFilename(libraries[i]));
+ }
}
}
}
@@ -391,8 +444,6 @@
* Retreives the "extra-classpath" attribute, that needs to be
* added to the class path.
*
- * @param context The ServletContext to perform the lookup.
- *
* @throws ServletException
*/
protected String getExtraClassPath()
@@ -404,47 +455,62 @@
int i = 0;
while (st.hasMoreTokens()) {
String s = st.nextToken();
- if (i++ > 0)
+ if (i++ > 0) {
sb.append(java.io.File.pathSeparatorChar);
+ }
if ((s.charAt(0) == java.io.File.separatorChar) ||
(s.charAt(1) == ':')) {
- log.debug ("extraClassPath is absolute: " + s);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is absolute: " + s);
+ }
sb.append(s);
if (this.addClassDirs) {
try {
classLoader.addDirectory(s.toString());
} catch (Exception e) {
- log.debug("Could not add " + s.toString());
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " + s.toString());
+ }
}
}
} else {
- if(s.indexOf("${")!=-1) {
+ if (s.indexOf("${") != -1) {
String path = StringUtils.replaceToken(s);
sb.append(path);
- log.debug ("extraClassPath is not absolute replacing using
token: [" + s + "] : " + path);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is not absolute replacing
using token: [" + s + "] : " + path);
+ }
if (this.addClassDirs) {
try {
classLoader.addDirectory(path);
} catch (Exception e) {
- log.debug("Could not add " + path);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " + path);
+ }
}
}
} else {
String path = null;
if (this.servletContextPath != null) {
path = this.servletContextPath + s;
- log.debug ("extraClassPath is not absolute pre-pending
context path: " + path);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is not absolute
pre-pending context path: " + path);
+ }
} else {
path = this.workDir.toString() + s;
- log.debug ("extraClassPath is not absolute pre-pending
work-directory: " + path);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is not absolute
pre-pending work-directory: " + path);
+ }
}
sb.append(path);
if (this.addClassDirs) {
try {
classLoader.addDirectory(path);
} catch (Exception e) {
- log.debug("Could not add " + path);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " + path);
+ }
}
}
}
@@ -465,8 +531,6 @@
* (Priority.DEBUG and above) as you want that get routed to the
* file.
*
- * @param context The ServletContext for the real path.
- *
* @throws ServletException
*/
private void initLogger()
@@ -495,7 +559,9 @@
if (this.servletContextPath == null) {
File logSCDir = new File(this.workDir, "log");
logSCDir.mkdirs();
- this.log.warn("Setting servlet-context for LogKit to " + logSCDir);
+ if (log.isWarnEnabled()) {
+ this.log.warn("Setting servlet-context for LogKit to " + logSCDir);
+ }
subcontext.put("context-root", logSCDir.toString());
} else {
subcontext.put("context-root", this.servletContextPath);
@@ -530,7 +596,6 @@
* Set the ConfigFile for the Cocoon object.
*
* @param configFileName The file location for the cocoon.xconf
- * @param context The servlet context to get the file handle
*
* @throws ServletException
*/
@@ -539,18 +604,24 @@
final String usedFileName;
if (configFileName == null) {
- log.warn("Servlet initialization argument 'configurations' not
specified, attempting to use '/cocoon.xconf'");
+ if (log.isWarnEnabled()) {
+ log.warn("Servlet initialization argument 'configurations' not
specified, attempting to use '/cocoon.xconf'");
+ }
usedFileName = "/cocoon.xconf";
} else {
usedFileName = configFileName;
}
- log.debug("Using configuration file: " + usedFileName);
+ if (log.isDebugEnabled()) {
+ log.debug("Using configuration file: " + usedFileName);
+ }
try {
return this.servletContext.getResource(usedFileName);
} catch (Exception mue) {
- log.error("Servlet initialization argument 'configurations' not found
at " + usedFileName, mue);
+ if (log.isErrorEnabled()) {
+ log.error("Servlet initialization argument 'configurations' not
found at " + usedFileName, mue);
+ }
throw new ServletException("Servlet initialization argument
'configurations' not found at " + usedFileName);
}
}
@@ -566,8 +637,6 @@
* separate each entry with whitespace, a comma, or a semi-colon.
* Cocoon will strip any whitespace from the entry.
*
- * @param forceLoading The array of fully qualified classes to force loading.
- *
* @throws ServletException
*/
private void forceLoad() {
@@ -578,9 +647,14 @@
final String fqcn = fqcnTokenizer.nextToken().trim();
try {
- log.debug("Trying to load class: " + fqcn);
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to load class: " + fqcn);
+ }
ClassUtils.loadClass(fqcn).newInstance();
- } catch (Exception e) { log.warn("Could not
force-load class: " + fqcn, e);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ log.warn("Could not force-load class: " + fqcn, e);
+ }
// Do not throw an exception, because it is not a fatal error.
}
}
@@ -603,18 +677,24 @@
java.util.Properties systemProps = System.getProperties();
while (tokenizer.hasMoreTokens()) {
final String property = tokenizer.nextToken().trim();
- if(property.indexOf('=')==-1)
+ if (property.indexOf('=') == -1) {
continue;
+ }
try {
String key = property.substring(0,property.indexOf('='));
- String value = property.substring(property.indexOf('=')+1);
- if(value.indexOf("${")!=-1)
+ String value = property.substring(property.indexOf('=') + 1);
+ if (value.indexOf("${") != -1) {
value = StringUtils.replaceToken(value);
- log.debug("setting " + key + "=" + value);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("setting " + key + "=" + value);
+ }
systemProps.setProperty(key,value);
} catch (Exception e) {
- log.warn("Could not set property: " + property, e);
+ if (log.isWarnEnabled()) {
+ log.warn("Could not set property: " + property, e);
+ }
// Do not throw an exception, because it is not a fatal error.
}
}
@@ -642,7 +722,7 @@
this.uploadDir,
CocoonServlet.ALLOW_OVERWRITE,
CocoonServlet.SILENTLY_RENAME,
- CocoonServlet.MAX_UPLOAD_SIZE);
+ this.maxUploadSize);
this.cocoon = getCocoon(request.getPathInfo(),
request.getParameter(Constants.RELOAD_PARAM));
@@ -664,9 +744,13 @@
// We got it... Process the request
String uri = request.getServletPath();
- if (uri == null) uri = "";
+ if (uri == null) {
+ uri = "";
+ }
String pathInfo = request.getPathInfo();
- if (pathInfo != null) uri += pathInfo;
+ if (pathInfo != null) {
+ uri += pathInfo;
+ }
if (uri.length() == 0) {
/* empty relative URI
@@ -677,7 +761,9 @@
*/
String prefix = request.getRequestURI();
- if (prefix == null) prefix = "";
+ if (prefix == null) {
+ prefix = "";
+ }
res.sendRedirect(res.encodeRedirectURL(prefix + "/"));
return;
@@ -724,7 +810,9 @@
res.setContentType(Notifier.notify(n, (OutputStream)null));
}
} catch (ResourceNotFoundException rse) {
- log.warn("The resource was not found", rse);
+ if (log.isWarnEnabled()) {
+ log.warn("The resource was not found", rse);
+ }
res.setStatus(res.SC_NOT_FOUND);
Notification n = new Notification(this);
@@ -741,7 +829,9 @@
// as the status SC_NOT_FOUND is enough
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (ConnectionResetException cre) {
- log.warn("The connection was reset", cre);
+ if (log.isWarnEnabled()) {
+ log.warn("The connection was reset", cre);
+ }
Notification n = new Notification(this);
n.setType("resource-not-found");
@@ -757,7 +847,9 @@
// as the connection was reset anyway
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (Exception e) {
- log.error("Problem with servlet", e);
+ if (log.isErrorEnabled()) {
+ log.error("Problem with servlet", e);
+ }
//res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
Notification n = new Notification(this, e);
n.setType("internal-server-error");
@@ -770,17 +862,21 @@
long end = System.currentTimeMillis();
String timeString = processTime(end - start);
- log.info("'" + uri + "' " + timeString);
+ if (log.isInfoEnabled()) {
+ log.info("'" + uri + "' " + timeString);
+ }
if (contentType != null && contentType.equals("text/html")) {
String showTime = request.getParameter(Constants.SHOWTIME_PARAM);
boolean show = this.showTime;
- if (showTime != null)
+ if (showTime != null) {
show = !showTime.equalsIgnoreCase("no");
+ }
if (show) {
boolean hide = this.hiddenShowTime;
- if(showTime != null)
+ if (showTime != null) {
hide = showTime.equalsIgnoreCase("hide");
+ }
ServletOutputStream out = res.getOutputStream();
out.print((hide) ? "<!-- " : "<p>");
out.print(timeString);
@@ -840,7 +936,9 @@
((Initializable) parentComponentManager).initialize();
}
} catch (Exception e) {
- log.error("Could not initialize parent component manager.", e);
+ if (log.isErrorEnabled()) {
+ log.error("Could not initialize parent component manager.", e);
+ }
}
}
return parentComponentManager;
@@ -869,7 +967,9 @@
try {
URL configFile = (URL)
this.appContext.get(Constants.CONTEXT_CONFIG_URL);
- log.info("Reloading from: " + configFile.toExternalForm());
+ if (log.isInfoEnabled()) {
+ log.info("Reloading from: " + configFile.toExternalForm());
+ }
Cocoon c = (Cocoon) ClassUtils.newInstance("org.apache.cocoon.Cocoon");
final String rootlogger = getInitParameter("cocoon-logger");
if (rootlogger != null) {
@@ -890,7 +990,9 @@
this.cocoon = c;
} catch (Exception e) {
- log.error("Exception reloading", e);
+ if (log.isErrorEnabled()) {
+ log.error("Exception reloading", e);
+ }
this.exception = e;
if (cocoon != null) {
@@ -907,14 +1009,14 @@
.append(Constants.COMPLETE_NAME)
.append(" in ");
- if (time > hour) {
- out.append(time / hour);
+ if (time > HOUR) {
+ out.append(time / HOUR);
out.append(" hours.");
- } else if (time > minute) {
- out.append(time / minute);
+ } else if (time > MINUTE) {
+ out.append(time / MINUTE);
out.append(" minutes.");
- } else if (time > second) {
- out.append(time / second);
+ } else if (time > SECOND) {
+ out.append(time / SECOND);
out.append(" seconds.");
} else {
out.append(time);
@@ -934,18 +1036,24 @@
throws ServletException {
if (this.cocoon != null && this.allowReload == true) {
if (this.cocoon.modifiedSince(this.creationTime)) {
- log.info("Configuration changed reload attempt");
+ if (log.isInfoEnabled()) {
+ log.info("Configuration changed reload attempt");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
} else if ((pathInfo == null) && (reloadParam != null)) {
- log.info("Forced reload attempt");
+ if (log.isInfoEnabled()) {
+ log.info("Forced reload attempt");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
}
} else if ((pathInfo == null) && (this.allowReload == true) && (reloadParam
!= null)) {
- log.info("Invalid configurations reload");
+ if (log.isInfoEnabled()) {
+ log.info("Invalid configurations reload");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]