cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r620002 - in /cocoon/whiteboard/micro/core: cocoon-core/src/main/java/org/apache/cocoon/components/source/ cocoon-core/src/main/java/org/apache/cocoon/sitemap/ cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/tr...
Date Fri, 08 Feb 2008 21:19:00 GMT
Author: gkossakowski
Date: Fri Feb  8 13:18:58 2008
New Revision: 620002

URL: http://svn.apache.org/viewvc?rev=620002&view=rev
Log:
Make a TreeProcessor a Spring bean:
  * get rid of SitemapPathCalculator which was introduced as an experiment and move its functionality
to TreeProcessor
  * let Spring init the TreeProcessor
  * set the scope of TreeProcessor to "servlet"
  * remove dependency on Processor in CocoonSourceResolver and use ServletContext to obtain
base URL

Removed:
    cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapPathCalculator.java
Modified:
    cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
    cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java
    cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java
    cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/resources/META-INF/cocoon/spring/cocoon-sitemap-impl.xml

Modified: cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/CocoonSourceResolver.java?rev=620002&r1=620001&r2=620002&view=diff
==============================================================================
--- cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
(original)
+++ cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
Fri Feb  8 13:18:58 2008
@@ -22,6 +22,8 @@
 import java.net.URL;
 import java.util.Map;
 
+import javax.servlet.ServletContext;
+
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
@@ -60,6 +62,8 @@
 
     /** The base URL */
     protected URL baseURL;
+    
+    protected ServletContext servletContext;
 
     /**
      * @see Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
@@ -94,10 +98,7 @@
     public Source resolveURI(String location, String baseURI, Map parameters)
     throws IOException {
         if (baseURI == null) {
-            final Processor processor = EnvironmentHelper.getCurrentProcessor();
-            if (processor != null) {
-                baseURI = processor.getContext();
-            }
+            baseURI = servletContext.getResource("/").toExternalForm();
         }
         if (this.customResolver != null) {
             return this.customResolver.resolveURI(location, baseURI, parameters);
@@ -194,6 +195,8 @@
             this.customResolver = (org.apache.excalibur.source.SourceResolver)
                     this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE +
"/Cocoon");
         }
+        //We obtain ServletContext which is properly scoped so we can always be sure that
we reference the right one
+        servletContext = (ServletContext)manager.lookup("javax.servlet.ServletContext/callstack");
     }
 
     /**

Modified: cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java?rev=620002&r1=620001&r2=620002&view=diff
==============================================================================
--- cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java
(original)
+++ cocoon/whiteboard/micro/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java
Fri Feb  8 13:18:58 2008
@@ -26,9 +26,10 @@
 
 import org.apache.cocoon.Processor;
 import org.apache.cocoon.components.treeprocessor.TreeProcessor;
-import org.apache.cocoon.components.treeprocessor.sitemap.SitemapPathCalculator;
 import org.apache.cocoon.servlet.RequestUtil;
-import org.apache.cocoon.spring.configurator.WebAppContextUtils;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
 
 /**
  * Use this servlet as an entry point to Cocoon. It wraps the {@link TreeProcessor}
@@ -36,10 +37,12 @@
  *
  * @version $Id$
  */
-public class SitemapServlet extends HttpServlet {
+public class SitemapServlet extends HttpServlet implements BeanFactoryAware {
 
     protected Processor processor;
     protected RequestProcessor requestProcessor;
+    
+    private BeanFactory beanFactory;
 
     /**
      * Initialize the servlet. The main purpose of this method is creating a
@@ -54,8 +57,8 @@
      */
     protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
             IOException {
-        if(this.requestProcessor == null) {
-            this.requestProcessor = new RequestProcessor(getServletContext());
+        if (requestProcessor == null) {
+            requestProcessor = new RequestProcessor(getServletContext());
         }
         this.requestProcessor.service(request, response);
     }
@@ -71,16 +74,7 @@
         }
 
         protected Processor getProcessor() {
-            TreeProcessor treeProcessor = (TreeProcessor) WebAppContextUtils.getCurrentWebApplicationContext().getBean(Processor.ROLE);
-            SitemapPathCalculator sitemapPathCalculator = new SitemapPathCalculator();
-            sitemapPathCalculator.setServletContext(this.servletContext);
-            try {
-                treeProcessor.setSitemapPathCalculator(sitemapPathCalculator);
-                treeProcessor.initialize();
-            } catch (Exception e) {
-                throw new RuntimeException("Could not create TreeProcessor", e);
-            }
-            return treeProcessor;
+            return (Processor) SitemapServlet.this.beanFactory.getBean(Processor.ROLE);
         }
 
         /**
@@ -91,5 +85,9 @@
         }
 
 
+    }
+
+    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+        this.beanFactory = beanFactory;
     }
 }

Modified: cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java?rev=620002&r1=620001&r2=620002&view=diff
==============================================================================
--- cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java
(original)
+++ cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java
Fri Feb  8 13:18:58 2008
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.net.URL;
 
+import javax.servlet.ServletContext;
 import javax.xml.XMLConstants;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
@@ -35,7 +36,6 @@
 import org.apache.cocoon.components.flow.Interpreter;
 import org.apache.cocoon.components.source.util.SourceUtil;
 import org.apache.cocoon.components.treeprocessor.sitemap.FlowNode;
-import org.apache.cocoon.components.treeprocessor.sitemap.SitemapPathCalculator;
 import org.apache.cocoon.configuration.Settings;
 import org.apache.cocoon.environment.Environment;
 import org.apache.cocoon.environment.internal.EnvironmentHelper;
@@ -77,9 +77,16 @@
 
     /** The sitemap schema used for validation. */
     protected Schema sitemapSchema;
+    
+    /**
+     * Name of the 'sitemap-path' servlet init parameter. Value should point
+     * to the location of sitemap file, defaults to '/sitemap.xmap'.
+     */
+    private static final String PARAM_SITEMAP_PATH = "sitemap-path";
 
-    /** The path to the sitemap */
-    private String sitemapPath;
+    private static final String DEFAULT_SITEMAP_PATH = "/sitemap.xmap";
+
+    private ServletContext servletContext;
 
     /**
      * Create a TreeProcessor.
@@ -96,12 +103,39 @@
         }
         this.sitemapExecutor = new DefaultExecutor();
     }
+    
+    public void setServletContext(ServletContext servletContext) {
+        this.servletContext = servletContext;
+    }
+    
+    /**
+     * Calculate the path to the sitemap based on the servlet init parameter
+     * {@link SitemapServlet#PARAM_SITEMAP_PATH} using the servlet context
+     * to resolve it. If there is no parameter set, the default value
+     * {@link SitemapServlet#DEFAULT_SITEMAP_PATH} is used.
+     *
+     * @param servletContext
+     * @return The calculated path to the sitemap.
+     * @throws IOException
+     *             if the URL can't be found.
+     */
+    private String calculateSitemapPath() throws IOException {
+        String sitemapPath = servletContext.getInitParameter(PARAM_SITEMAP_PATH);
+        if (sitemapPath == null) {
+            sitemapPath = DEFAULT_SITEMAP_PATH;
+        }
+        URL url = servletContext.getResource(sitemapPath);
+        if (url == null) {
+            throw new IOException("Couldn't find the sitemap " + sitemapPath + ".");
+        }
+        return url.toExternalForm();
+    }
 
     /**
      * Initialize the tree processor after all dependencies have been set.
      */
-    public void initialize() throws Exception {
-        this.source = this.resolver.resolveURI(this.sitemapPath);
+    public void init() throws Exception {
+        this.source = this.resolver.resolveURI(calculateSitemapPath());
         // setup the environment helper
         if (this.environmentHelper == null) {
             // We already have resolved our sitemap, so our context is the
@@ -135,16 +169,6 @@
      */
     public void setServiceManager(ServiceManager manager) {
         this.manager = manager;
-    }
-
-    /**
-     * Set the calculator to lookup the sitemap path.
-     *
-     * @param sitemapPathCalculator
-     * @throws IOException if the sitemap can't be found.
-     */
-    public void setSitemapPathCalculator(SitemapPathCalculator sitemapPathCalculator) throws
IOException {
-        this.sitemapPath = sitemapPathCalculator.calculateSitemapPath();
     }
 
     /**

Modified: cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/resources/META-INF/cocoon/spring/cocoon-sitemap-impl.xml
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/resources/META-INF/cocoon/spring/cocoon-sitemap-impl.xml?rev=620002&r1=620001&r2=620002&view=diff
==============================================================================
--- cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/resources/META-INF/cocoon/spring/cocoon-sitemap-impl.xml
(original)
+++ cocoon/whiteboard/micro/core/cocoon-sitemap/cocoon-sitemap-impl/src/main/resources/META-INF/cocoon/spring/cocoon-sitemap-impl.xml
Fri Feb  8 13:18:58 2008
@@ -17,14 +17,18 @@
 -->
 <!-- SVN $Id$ -->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+  xmlns:aop="http://www.springframework.org/schema/aop"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
   <!-- The Cocoon tree processor -->
   <bean name="org.apache.cocoon.Processor" class="org.apache.cocoon.components.treeprocessor.TreeProcessor"
-    destroy-method="destroy" scope="prototype">
+    destroy-method="destroy" scope="servlet" init-method="init">
     <property name="serviceManager" ref="org.apache.avalon.framework.service.ServiceManager"/>
     <property name="settings" ref="org.apache.cocoon.configuration.Settings"/>
     <property name="sourceResolver" ref="org.apache.excalibur.source.SourceResolver"/>
+    <property name="servletContext" ref="javax.servlet.ServletContext/callstack"/>
+    <aop:scoped-proxy proxy-target-class="false"/>
   </bean>
 
   <!-- ObjectModelEntry provider for 'cocoon' -->



Mime
View raw message