cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r659482 - /cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
Date Fri, 23 May 2008 09:03:09 GMT
Author: reinhard
Date: Fri May 23 02:03:08 2008
New Revision: 659482

URL: http://svn.apache.org/viewvc?rev=659482&view=rev
Log:
improve logging and exception handling

Modified:
    cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java

Modified: cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java?rev=659482&r1=659481&r2=659482&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-servlet/src/main/java/org/apache/cocoon/corona/servlet/SitemapServlet.java
Fri May 23 02:03:08 2008
@@ -21,7 +21,6 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.PrintWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Enumeration;
@@ -41,12 +40,15 @@
 import org.apache.cocoon.corona.sitemap.InvocationImpl;
 import org.apache.cocoon.corona.sitemap.SitemapBuilder;
 import org.apache.cocoon.corona.sitemap.node.Sitemap;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.BeanFactoryAware;
 
 public class SitemapServlet extends HttpServlet implements BeanFactoryAware {
 
+    private final Log logger = LogFactory.getLog(this.getClass());
     private static final long serialVersionUID = 1L;
 
     private BeanFactory beanFactory;
@@ -60,7 +62,8 @@
         super.init(this.servletConfig);
     }
 
-    public void invoke(String requestURI, Map<String, Object> parameters, OutputStream
outputStream) {
+    public void invoke(String requestURI, Map<String, Object> parameters, OutputStream
outputStream)
+            throws ServletException {
         InvocationImpl invocation = (InvocationImpl) this.beanFactory.getBean(Invocation.class.getName());
 
         invocation.setBaseURL(this.getBaseURL());
@@ -78,6 +81,9 @@
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
             IOException {
+        long start = System.nanoTime();
+        this.logRequest(request);
+
         this.lazyInitialize(this.servletConfig);
 
         try {
@@ -98,11 +104,22 @@
             response.getOutputStream().write(baos.toByteArray());
             response.setStatus(StatusCodeCollector.getStatusCode());
         } catch (Exception e) {
-            PrintWriter writer = new PrintWriter(response.getOutputStream());
-            e.printStackTrace(writer);
-            writer.close();
+            String msg = "An exception occurred while executing the sitemap.";
+            this.logger.error(msg, e);
+            throw new ServletException(msg, e);
         } finally {
             SitemapDelegator.removeSitemapServlet();
+            this.logger.info("Sitemap execution took " + (System.nanoTime() - start) / 1000000f
+ " ms.");
+        }
+    }
+
+    private void logRequest(HttpServletRequest request) throws ServletException {
+        if (this.logger.isInfoEnabled()) {
+            this.logger.info("Performing " + request.getMethod().toUpperCase() + " request
at "
+                    + request.getRequestURI());
+        }
+        if (this.logger.isDebugEnabled()) {
+            this.logger.debug("The base URL for this request is " + this.getBaseURL());
         }
     }
 
@@ -118,11 +135,13 @@
         return invocationParameters;
     }
 
-    private URL getBaseURL() {
+    private URL getBaseURL() throws ServletException {
         try {
             return this.servletConfig.getServletContext().getResource("/");
         } catch (MalformedURLException e) {
-            throw new RuntimeException(e);
+            String msg = "An exception occurred while retrieving the base URL from the servlet
context.";
+            this.logger.error(msg, e);
+            throw new ServletException(msg, e);
         }
     }
 
@@ -140,7 +159,9 @@
                 URL url = servletConfig.getServletContext().getResource("/sitemap.xmap");
                 this.sitemap = sitemapBuilder.build(url);
             } catch (Exception e) {
-                throw new ServletException(e);
+                String msg = "An exception occurred while building the sitemap.";
+                this.logger.error(msg, e);
+                throw new ServletException(msg, e);
             }
             this.initialized = true;
         }



Mime
View raw message