cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r697546 - /cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java
Date Sun, 21 Sep 2008 16:48:14 GMT
Author: reinhard
Date: Sun Sep 21 09:48:14 2008
New Revision: 697546

URL: http://svn.apache.org/viewvc?rev=697546&view=rev
Log:
. refactor duplicated exception handling code
. sort members

Modified:
    cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java

Modified: cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java?rev=697546&r1=697545&r2=697546&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/XMLSitemapServlet.java
Sun Sep 21 09:48:14 2008
@@ -53,11 +53,11 @@
 
 public class XMLSitemapServlet extends HttpServlet implements BeanFactoryAware {
 
-    private final Log logger = LogFactory.getLog(this.getClass());
     private static final long serialVersionUID = 1L;
-
     private BeanFactory beanFactory;
+
     private boolean initialized;
+    private final Log logger = LogFactory.getLog(this.getClass());
     private ServletConfig servletConfig;
     private Sitemap sitemap;
     private String version;
@@ -112,7 +112,7 @@
             ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
             this.invoke(this.calcSitemapRequestURI(request), parameters, baos);
 
-            // collect meta information from the previous run of the sitemap
+            // collect meta information from the previous run of the sitemap engine
             long lastModified = LastModifiedCollector.getLastModified();
             String mimeType = MimeTypeCollector.getMimeType();
             int contentLengh = baos.size();
@@ -159,45 +159,25 @@
                 response.getOutputStream().write(baos.toByteArray());
             }
         } catch (Exception e) {
-            String msg = "An exception occurred while executing the sitemap.";
-            this.logger.error(msg, e);
-            ServletException servletException = new ServletException(msg, e);
-            servletException.initCause(e);
-            throw servletException;
+            throw this.wrapException(e, "An exception occurred while executing the sitemap.");
         } finally {
             SitemapDelegator.removeSitemapServlet();
             this.logger.info("Sitemap execution took " + (System.nanoTime() - start) / 1000000f
+ " ms.");
         }
     }
 
-    // FIXME After renaming Corona to Cocoon3, read the version from the manifest
-    // FIXME Make this header configureable
-    private void initVersionNumber() {
-        this.version = "3.0.0-SNAPSHOT";
-
-        if (this.version.endsWith("SNAPSHOT")) {
-            String buildNumber = "";
-            try {
-                String buildNumberAttr = ManifestUtils.getAttribute(this.getClass(), "Implementation-Build");
-                if (buildNumberAttr != null && !"".equals(buildNumberAttr) &&
!"na".equals(buildNumberAttr)) {
-                    buildNumber = "/rev" + buildNumberAttr;
-                }
-            } catch (IOException e) {
-                if (this.logger.isDebugEnabled()) {
-                    this.logger.debug("Error while reading an attribute from the manifest.",
e);
-                }
-            }
-            this.version += buildNumber;
-        }
+    private String calcSitemapRequestURI(HttpServletRequest request) {
+        String contextPath = request.getContextPath();
+        String mountPath = request.getServletPath();
+        return request.getRequestURI().substring(contextPath.length() + mountPath.length());
     }
 
-    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());
+    private URL getBaseURL() throws ServletException {
+        try {
+            return this.servletConfig.getServletContext().getResource("/");
+        } catch (MalformedURLException e) {
+            throw this.wrapException(e, "An exception occurred while retrieving the base
"
+                    + "URL from the servlet context.");
         }
     }
 
@@ -213,22 +193,38 @@
         return invocationParameters;
     }
 
-    private URL getBaseURL() throws ServletException {
-        try {
-            return this.servletConfig.getServletContext().getResource("/");
-        } catch (MalformedURLException e) {
-            String msg = "An exception occurred while retrieving the base URL from the servlet
context.";
-            this.logger.error(msg, e);
-            ServletException servletException = new ServletException(msg, e);
-            servletException.initCause(e);
-            throw servletException;
+    private String getSitemapPath() {
+        String sitemapPath = this.getInitParameter("sitemap-path");
+        if (sitemapPath == null) {
+            sitemapPath = "/sitemap.xmap";
         }
+
+        if (!sitemapPath.startsWith("/")) {
+            sitemapPath = "/" + sitemapPath;
+        }
+
+        return sitemapPath;
     }
 
-    private String calcSitemapRequestURI(HttpServletRequest request) {
-        String contextPath = request.getContextPath();
-        String mountPath = request.getServletPath();
-        return request.getRequestURI().substring(contextPath.length() + mountPath.length());
+    // FIXME After renaming Corona to Cocoon3, read the version from the manifest
+    // FIXME Make this header configureable
+    private void initVersionNumber() {
+        this.version = "3.0.0-SNAPSHOT";
+
+        if (this.version.endsWith("SNAPSHOT")) {
+            String buildNumber = "";
+            try {
+                String buildNumberAttr = ManifestUtils.getAttribute(this.getClass(), "Implementation-Build");
+                if (buildNumberAttr != null && !"".equals(buildNumberAttr) &&
!"na".equals(buildNumberAttr)) {
+                    buildNumber = "/rev" + buildNumberAttr;
+                }
+            } catch (IOException e) {
+                if (this.logger.isDebugEnabled()) {
+                    this.logger.debug("Error while reading an attribute from the manifest.",
e);
+                }
+            }
+            this.version += buildNumber;
+        }
     }
 
     private synchronized void lazyInitialize(ServletConfig servletConfig) throws ServletException
{
@@ -239,28 +235,30 @@
                 URL url = servletConfig.getServletContext().getResource(this.getSitemapPath());
                 this.sitemap = sitemapBuilder.build(url);
             } catch (Exception e) {
-                String msg = "An exception occurred while building the sitemap.";
-                this.logger.error(msg, e);
-                ServletException servletException = new ServletException(msg, e);
-                if (servletException.getCause() == null) {
-                    servletException.initCause(e);
-                }
-                throw servletException;
+                throw this.wrapException(e, "An exception occurred while building the sitemap.");
             }
             this.initialized = true;
         }
     }
 
-    private String getSitemapPath() {
-        String sitemapPath = this.getInitParameter("sitemap-path");
-        if (sitemapPath == null) {
-            sitemapPath = "/sitemap.xmap";
+    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());
         }
+    }
 
-        if (!sitemapPath.startsWith("/")) {
-            sitemapPath = "/" + sitemapPath;
+    private ServletException wrapException(Exception e, String msg) throws ServletException
{
+        this.logger.error(msg, e);
+
+        ServletException servletException = new ServletException(msg, e);
+        if (servletException.getCause() == null) {
+            servletException.initCause(e);
         }
 
-        return sitemapPath;
+        return servletException;
     }
 }



Mime
View raw message