vgritsenko 2004/06/23 12:46:06
Modified: src/java/org/apache/cocoon/servlet CocoonServlet.java
Log:
Sync CocoonServlet and CocoonPortlet
Revision Changes Path
1.31 +161 -157
cocoon-2.1/src/java/org/apache/cocoon/servlet/CocoonServlet.java
Index: CocoonServlet.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/servlet/CocoonServlet.java,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- CocoonServlet.java 11 Jun 2004 20:29:29 -0000 1.30
+++ CocoonServlet.java 23 Jun 2004 19:46:06 -0000 1.31
@@ -15,43 +15,15 @@
*/
package org.apache.cocoon.servlet;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.lang.reflect.Constructor;
-import java.net.MalformedURLException;
-import java.net.SocketException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.StringTokenizer;
-import java.util.jar.Attributes;
-import java.util.jar.Manifest;
-
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
import org.apache.avalon.excalibur.logger.LogKitLoggerManager;
import org.apache.avalon.excalibur.logger.LoggerManager;
-
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
-import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.DefaultContext;
@@ -77,19 +49,44 @@
import org.apache.cocoon.util.StringUtils;
import org.apache.cocoon.util.log.CocoonLogFormatter;
import org.apache.cocoon.util.log.Log4JConfigurator;
-import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.lang.BooleanUtils;
import org.apache.excalibur.instrument.InstrumentManager;
import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
-
import org.apache.log.ContextMap;
+import org.apache.log.ErrorHandler;
import org.apache.log.Hierarchy;
import org.apache.log.Priority;
-import org.apache.log.ErrorHandler;
-import org.apache.log.util.DefaultErrorHandler;
import org.apache.log.output.ServletOutputLogTarget;
+import org.apache.log.util.DefaultErrorHandler;
import org.apache.log4j.LogManager;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.SocketException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
/**
* This is the entry point for Cocoon execution as an HTTP Servlet.
*
@@ -104,14 +101,15 @@
*/
public class CocoonServlet extends HttpServlet {
- /** Application <code>Context</code> Key for the servlet configuration
- * @since 2.1.3
+ /**
+ * Application <code>Context</code> Key for the servlet configuration
+ * @since 2.1.3
*/
public static final String CONTEXT_SERVLET_CONFIG = "servlet-config";
// Processing time message
protected static final String PROCESSED_BY = "Processed by "
- + Constants.COMPLETE_NAME + " in ";
+ + Constants.COMPLETE_NAME + " in ";
// Used by "show-time"
static final float SECOND = 1000;
@@ -252,19 +250,20 @@
super.init(conf);
- // Check the init-classloader parameter only if it's not
already true.
- // This is useful for subclasses of this servlet that override
the value
- // initially set by this class (i.e. false).
- if (!this.initClassLoader) {
- this.initClassLoader =
getInitParameterAsBoolean("init-classloader", false);
- }
+ // Check the init-classloader parameter only if it's not already
true.
+ // This is useful for subclasses of this servlet that override the
value
+ // initially set by this class (i.e. false).
+ if (!this.initClassLoader) {
+ this.initClassLoader =
getInitParameterAsBoolean("init-classloader", false);
+ }
if (this.initClassLoader) {
// Force context classloader so that JAXP can work correctly
// (see javax.xml.parsers.FactoryFinder.findClassLoader())
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
- } catch (Exception e){}
+ } catch (Exception e) {
+ }
}
String value;
@@ -297,7 +296,7 @@
this.workDir = workDirParamFile;
} else {
// No : consider it relative to context path
- this.workDir = new File(servletContextPath ,
workDirParam);
+ this.workDir = new File(servletContextPath,
workDirParam);
}
}
} else {
@@ -321,7 +320,7 @@
if (getLogger().isDebugEnabled()) {
getLogger().debug("getResource for /WEB-INF: " + path);
}
- path = path.substring(0,path.length() - "WEB-INF".length());
+ path = path.substring(0, path.length() - "WEB-INF".length());
if (getLogger().isDebugEnabled()) {
getLogger().debug("Path for Root: " + path);
}
@@ -363,7 +362,7 @@
}
this.appContext.put(Constants.CONTEXT_WORK_DIR, workDir);
this.appContext.put(ContextHelper.CONTEXT_ROOT_URL,
this.servletContextURL);
-
+
final String uploadDirParam =
conf.getInitParameter("upload-directory");
if (uploadDirParam != null) {
if (this.servletContextPath == null) {
@@ -376,7 +375,7 @@
this.uploadDir = uploadDirParamFile;
} else {
// No : consider it relative to context path
- this.uploadDir = new File(servletContextPath ,
uploadDirParam);
+ this.uploadDir = new File(servletContextPath,
uploadDirParam);
}
}
if (getLogger().isDebugEnabled()) {
@@ -401,12 +400,12 @@
this.allowOverwrite = false;
this.silentlyRename = false;
} else if ("allow".equalsIgnoreCase(overwriteParam)) {
- this.allowOverwrite = true;
- this.silentlyRename = false; // ignored in this case
+ this.allowOverwrite = true;
+ this.silentlyRename = false; // ignored in this case
} else {
- // either rename is specified or unsupported value - default to
rename.
- this.allowOverwrite = false;
- this.silentlyRename = true;
+ // either rename is specified or unsupported value - default to
rename.
+ this.allowOverwrite = false;
+ this.silentlyRename = true;
}
this.maxUploadSize = getInitParameterAsInteger("upload-max-size",
MAX_UPLOAD_SIZE);
@@ -423,7 +422,7 @@
this.cacheDir = cacheDirParamFile;
} else {
// No : consider it relative to context path
- this.cacheDir = new File(servletContextPath ,
cacheDirParam);
+ this.cacheDir = new File(servletContextPath,
cacheDirParam);
}
}
if (getLogger().isDebugEnabled()) {
@@ -447,7 +446,7 @@
}
// get allow reload parameter, default is true
- this.allowReload = getInitParameterAsBoolean("allow-reload",
ALLOW_RELOAD);
+ this.allowReload = getInitParameterAsBoolean("allow-reload",
ALLOW_RELOAD);
value = conf.getInitParameter("show-time");
this.showTime = BooleanUtils.toBoolean(value) ||
(this.hiddenShowTime = "hide".equals(value));
@@ -470,9 +469,9 @@
this.defaultFormEncoding =
getInitParameter("form-encoding","ISO-8859-1");
this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING,
this.defaultFormEncoding);
- this.manageExceptions =
getInitParameterAsBoolean("manage-exceptions", true);
+ this.manageExceptions =
getInitParameterAsBoolean("manage-exceptions", true);
- this.enableInstrumentation =
getInitParameterAsBoolean("enable-instrumentation", false);
+ this.enableInstrumentation =
getInitParameterAsBoolean("enable-instrumentation", false);
this.requestFactory = new RequestFactory(this.autoSaveUploads,
this.uploadDir,
@@ -492,7 +491,8 @@
if (this.initClassLoader) {
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
- } catch (Exception e){}
+ } catch (Exception e) {
+ }
}
if (this.cocoon != null) {
@@ -507,7 +507,7 @@
}
if (this.parentComponentManager != null &&
this.parentComponentManager instanceof Disposable) {
- ((Disposable)this.parentComponentManager).dispose();
+ ((Disposable) this.parentComponentManager).dispose();
}
}
@@ -635,7 +635,7 @@
}
List libList = new ArrayList();
- for(StringTokenizer st = new StringTokenizer(libValue, " ");
st.hasMoreTokens();) {
+ for (StringTokenizer st = new StringTokenizer(libValue, " ");
st.hasMoreTokens();) {
libList.add(st.nextToken());
}
@@ -654,7 +654,7 @@
this.getLogger().warn("Extracting libraries into " + root);
byte[] buffer = new byte[65536];
for (Iterator i = libList.iterator(); i.hasNext();) {
- String libName = (String)i.next();
+ String libName = (String) i.next();
long lastModified = -1;
try {
@@ -678,7 +678,7 @@
try {
os = new FileOutputStream(lib);
int count;
- while((count = is.read(buffer)) > 0) {
+ while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
} finally {
@@ -706,55 +706,55 @@
*
* @throws ServletException
*/
- protected String getExtraClassPath() throws ServletException {
- String extraClassPath = this.getInitParameter("extra-classpath");
- if (extraClassPath != null) {
- StringBuffer sb = new StringBuffer();
- StringTokenizer st = new StringTokenizer(extraClassPath,
System.getProperty("path.separator"), false);
- int i = 0;
- while (st.hasMoreTokens()) {
- String s = st.nextToken();
- if (i++ > 0) {
- sb.append(File.pathSeparatorChar);
- }
- if ((s.charAt(0) == File.separatorChar) ||
- (s.charAt(1) == ':')) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug ("extraClassPath is absolute: " +
s);
- }
- sb.append(s);
-
- addClassLoaderDirectory(s);
- } else {
- if (s.indexOf("${") != -1) {
- String path = StringUtils.replaceToken(s);
- sb.append(path);
- if (getLogger().isDebugEnabled()) {
- getLogger().debug ("extraClassPath is not
absolute replacing using token: [" + s + "] : " + path);
- }
- addClassLoaderDirectory(path);
- } else {
- String path = null;
- if (this.servletContextPath != null) {
- path = this.servletContextPath + s;
- if (getLogger().isDebugEnabled()) {
- getLogger().debug ("extraClassPath is not
absolute pre-pending context path: " + path);
- }
- } else {
- path = this.workDir.toString() + s;
- if (getLogger().isDebugEnabled()) {
- getLogger().debug ("extraClassPath is not
absolute pre-pending work-directory: " + path);
- }
- }
- sb.append(path);
- addClassLoaderDirectory(path);
- }
- }
- }
- return sb.toString();
- }
- return "";
- }
+ protected String getExtraClassPath() throws ServletException {
+ String extraClassPath = this.getInitParameter("extra-classpath");
+ if (extraClassPath != null) {
+ StringBuffer sb = new StringBuffer();
+ StringTokenizer st = new StringTokenizer(extraClassPath,
System.getProperty("path.separator"), false);
+ int i = 0;
+ while (st.hasMoreTokens()) {
+ String s = st.nextToken();
+ if (i++ > 0) {
+ sb.append(File.pathSeparatorChar);
+ }
+ if ((s.charAt(0) == File.separatorChar) ||
+ (s.charAt(1) == ':')) {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("extraClassPath is absolute: " +
s);
+ }
+ sb.append(s);
+
+ addClassLoaderDirectory(s);
+ } else {
+ if (s.indexOf("${") != -1) {
+ String path = StringUtils.replaceToken(s);
+ sb.append(path);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("extraClassPath is not
absolute replacing using token: [" + s + "] : " + path);
+ }
+ addClassLoaderDirectory(path);
+ } else {
+ String path = null;
+ if (this.servletContextPath != null) {
+ path = this.servletContextPath + s;
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("extraClassPath is not
absolute pre-pending context path: " + path);
+ }
+ } else {
+ path = this.workDir.toString() + s;
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("extraClassPath is not
absolute pre-pending work-directory: " + path);
+ }
+ }
+ sb.append(path);
+ addClassLoaderDirectory(path);
+ }
+ }
+ }
+ return sb.toString();
+ }
+ return "";
+ }
/**
* Set up the log level and path. The default log level is
@@ -896,7 +896,7 @@
URL result;
try {
// test if this is a qualified url
- if ( usedFileName.indexOf(':') == -1) {
+ if (usedFileName.indexOf(':') == -1) {
result = this.servletContext.getResource(usedFileName);
} else {
result = new URL(usedFileName);
@@ -909,12 +909,14 @@
if (result == null) {
File resultFile = new File(usedFileName);
- if (resultFile.isFile()) try {
- result = resultFile.getCanonicalFile().toURL();
- } catch (Exception e) {
- String msg = "Init parameter 'configurations' is invalid : "
+ usedFileName;
- getLogger().error(msg, e);
- throw new ServletException(msg, e);
+ if (resultFile.isFile()) {
+ try {
+ result = resultFile.getCanonicalFile().toURL();
+ } catch (Exception e) {
+ String msg = "Init parameter 'configurations' is invalid
: " + usedFileName;
+ getLogger().error(msg, e);
+ throw new ServletException(msg, e);
+ }
}
}
@@ -977,15 +979,15 @@
continue;
}
try {
- String key = property.substring(0,property.indexOf('='));
+ String key = property.substring(0,
property.indexOf('='));
String value = property.substring(property.indexOf('=')
+ 1);
if (value.indexOf("${") != -1) {
- value = StringUtils.replaceToken(value);
+ value = StringUtils.replaceToken(value);
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("setting " + key + "=" + value);
}
- systemProps.setProperty(key,value);
+ systemProps.setProperty(key, value);
} catch (Exception e) {
if (getLogger().isWarnEnabled()) {
getLogger().warn("Could not set property: " +
property, e);
@@ -1009,7 +1011,8 @@
if (this.initClassLoader) {
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
- } catch (Exception e){}
+ } catch (Exception e) {
+ }
}
// remember when we started (used for timing the processing)
@@ -1209,7 +1212,7 @@
}
try {
- ServletOutputStream out = res.getOutputStream();
+ OutputStream out = res.getOutputStream();
out.flush();
out.close();
} catch (SocketException se) {
@@ -1316,7 +1319,7 @@
*/
protected synchronized ComponentManager getParentComponentManager() {
if (parentComponentManager != null && parentComponentManager
instanceof Disposable) {
- ((Disposable)parentComponentManager).dispose();
+ ((Disposable) parentComponentManager).dispose();
}
parentComponentManager = null;
@@ -1355,7 +1358,8 @@
if (this.initClassLoader) {
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
- } catch (Exception e){}
+ } catch (Exception e) {
+ }
}
updateEnvironment();
@@ -1516,52 +1520,52 @@
* is empty.
*/
public String getInitParameter(String name) {
- String result = super.getInitParameter(name);
- if (result != null) {
- result = result.trim();
- if (result.length() == 0) {
- result = null;
- }
- }
+ String result = super.getInitParameter(name);
+ if (result != null) {
+ result = result.trim();
+ if (result.length() == 0) {
+ result = null;
+ }
+ }
- return result;
+ return result;
}
/** Convenience method to access servlet parameters */
protected String getInitParameter(String name, String defaultValue) {
- String result = getInitParameter(name);
- if (result == null) {
- if (getLogger() != null && getLogger().isDebugEnabled()) {
- getLogger().debug(name + " was not set - defaulting to
'" + defaultValue + "'");
- }
- return defaultValue;
- } else {
- return result;
- }
+ String result = getInitParameter(name);
+ if (result == null) {
+ if (getLogger() != null && getLogger().isDebugEnabled()) {
+ getLogger().debug(name + " was not set - defaulting to '" +
defaultValue + "'");
+ }
+ return defaultValue;
+ } else {
+ return result;
+ }
}
/** Convenience method to access boolean servlet parameters */
protected boolean getInitParameterAsBoolean(String name, boolean
defaultValue) {
- String value = getInitParameter(name);
- if (value != null) {
+ String value = getInitParameter(name);
+ if (value != null) {
return BooleanUtils.toBoolean(value);
}
- if (getLogger() != null && getLogger().isDebugEnabled()) {
- getLogger().debug(name + " was not set - defaulting to
'" + defaultValue + "'");
- }
- return defaultValue;
+ if (getLogger() != null && getLogger().isDebugEnabled()) {
+ getLogger().debug(name + " was not set - defaulting to '" +
defaultValue + "'");
+ }
+ return defaultValue;
}
protected int getInitParameterAsInteger(String name, int defaultValue) {
- String value = getInitParameter(name);
- if (value == null) {
- if (getLogger() != null &&
getLogger().isDebugEnabled()) {
- getLogger().debug(name + " was not set -
defaulting to '" + defaultValue + "'");
- }
- return defaultValue;
- } else {
- return Integer.parseInt(value);
- }
+ String value = getInitParameter(name);
+ if (value == null) {
+ if (getLogger() != null && getLogger().isDebugEnabled()) {
+ getLogger().debug(name + " was not set - defaulting to '" +
defaultValue + "'");
+ }
+ return defaultValue;
+ } else {
+ return Integer.parseInt(value);
+ }
}
protected Logger getLogger() {