Author: cziegeler
Date: Tue May  3 22:30:01 2005
New Revision: 168073

URL: http://svn.apache.org/viewcvs?rev=168073&view=rev
Log:
Add access to sitemap locator

Added:
    
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
   (with props)
Modified:
    
cocoon/trunk/src/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
    cocoon/trunk/src/java/org/apache/cocoon/core/Core.java
    cocoon/trunk/src/java/org/apache/cocoon/sitemap/ComponentLocator.java
    cocoon/trunk/src/java/org/apache/cocoon/sitemap/impl/ComponentManager.java

Modified: 
cocoon/trunk/src/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/source/CocoonSourceResolver.java?rev=168073&r1=168072&r2=168073&view=diff
==============================================================================
--- 
cocoon/trunk/src/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
 (original)
+++ 
cocoon/trunk/src/java/org/apache/cocoon/components/source/CocoonSourceResolver.java
 Tue May  3 22:30:01 2005
@@ -32,9 +32,13 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.Processor;
+import org.apache.cocoon.core.Core;
+import org.apache.cocoon.core.container.ComponentLocatorWrapper;
 import org.apache.cocoon.environment.SourceResolver;
 import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.sitemap.ComponentLocator;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceException;
 import org.apache.excalibur.source.SourceFactory;
@@ -62,6 +66,9 @@
     /** The base URL */
     protected URL baseURL;
 
+    /** The core */
+    protected Core core;
+
     /**
      * @see 
org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
      */
@@ -138,7 +145,7 @@
             }
         }
 
-        final ServiceManager m = this.getServiceManager();
+        final ComponentLocator m = this.getComponentLocator();
 
         Source source = null;
         // search for a SourceFactory implementing the protocol
@@ -150,7 +157,7 @@
                 getLogger().debug( "Resolved to systemID : " + systemID );
             }
             source = factory.getSource( systemID, parameters );
-        } catch( final ServiceException ce ) {
+        } catch( final ProcessingException ce ) {
             // no selector available, use fallback
         } finally {
             m.release( factory );
@@ -164,7 +171,7 @@
                     getLogger().debug( "Resolved to systemID : " + systemID );
                 }
                 source = factory.getSource( systemID, parameters );
-            } catch (ServiceException se ) {
+            } catch (ProcessingException se ) {
                 throw new SourceException( "Unable to select source factory 
for " + systemID, se );
             } finally {
                 m.release(factory);
@@ -193,6 +200,7 @@
             this.customResolver = (org.apache.excalibur.source.SourceResolver)
                      
this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE+"/Cocoon");
         }
+        this.core = (Core)this.manager.lookup(Core.ROLE);
     }
 
     /**
@@ -202,27 +210,29 @@
         if ( this.manager != null ) {
             this.manager.release( this.customResolver );
             this.customResolver = null;
+            this.manager.release(this.core);
+            this.core = null;
             this.manager = null;
         }
     }
 
     /**
-     * Get the service manager.
+     * Get the component locator.
      */
-    protected ServiceManager getServiceManager() {
-        ServiceManager m = EnvironmentHelper.getSitemapServiceManager();
-        if ( m == null ) {
-            m = this.manager;
+    protected ComponentLocator getComponentLocator() {
+        ComponentLocator l = this.core.getSitemapComponentLocator();
+        if ( l == null ) {
+            l = new ComponentLocatorWrapper(this.manager);
         }
-        return m;
+        return l;
     }
 
     /**
      * Get the SourceFactory
      */
-    protected SourceFactory getSourceFactory(ServiceManager m, String scheme) 
-    throws ServiceException {
-        return (SourceFactory)m.lookup(SourceFactory.ROLE + '/' + scheme);
+    protected SourceFactory getSourceFactory(ComponentLocator m, String 
scheme) 
+    throws ProcessingException {
+        return (SourceFactory)m.getComponent(SourceFactory.ROLE + '/' + 
scheme);
     }
 
     /**
@@ -234,7 +244,7 @@
         if ( this.customResolver != null ) {
             this.customResolver.release( source );
         } else {
-            final ServiceManager m = this.getServiceManager();
+            final ComponentLocator m = this.getComponentLocator();
             
             // search for a SourceFactory implementing the protocol
             final String scheme = source.getScheme();
@@ -243,11 +253,11 @@
             try {
                 factory = this.getSourceFactory(m, scheme);
                 factory.release(source);
-            } catch (ServiceException se ) {
+            } catch (ProcessingException se ) {
                 try {
                     factory = this.getSourceFactory(m, "*");
                     factory.release(source);
-                } catch (ServiceException sse ) {
+                } catch (ProcessingException sse ) {
                     throw new CascadingRuntimeException( "Unable to select 
source factory for " + source.getURI(), se );
                 }
             } finally {

Modified: cocoon/trunk/src/java/org/apache/cocoon/core/Core.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/core/Core.java?rev=168073&r1=168072&r2=168073&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/core/Core.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/core/Core.java Tue May  3 22:30:01 
2005
@@ -25,8 +25,12 @@
 import org.apache.avalon.framework.CascadingRuntimeException;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.core.container.ComponentLocatorWrapper;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.sitemap.ComponentLocator;
 
 /**
  * This is the core Cocoon component.
@@ -143,5 +147,22 @@
         } catch (ContextException ce) {
             throw new CascadingRuntimeException("Unable to get the cache 
directory from the context.", ce);
         }        
+    }
+    
+    /**
+     * Return the locator of the current sitemap.
+     * @return The current locator or null if no request is currently processed
+     */
+    public ComponentLocator getSitemapComponentLocator() {
+        final ServiceManager m = EnvironmentHelper.getSitemapServiceManager();
+        ComponentLocator l = null;
+        if ( m != null ) {
+            if ( !(m instanceof ComponentLocator) ) {
+                l = new ComponentLocatorWrapper(m);
+            } else {
+                l = (ComponentLocator)m;
+            }
+        }
+        return l;
     }
 }

Added: 
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java?rev=168073&view=auto
==============================================================================
--- 
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
 (added)
+++ 
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
 Tue May  3 22:30:01 2005
@@ -0,0 +1,55 @@
+/* 
+ * Copyright 2005 The Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.container;
+
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.sitemap.ComponentLocator;
+
+/**
+ * Wrapper for a service manager.
+ *
+ * @version CVS $Id$
+ */
+final public class ComponentLocatorWrapper
+implements ComponentLocator {
+
+    protected final ServiceManager manager;
+
+    public ComponentLocatorWrapper(ServiceManager m) {
+        this.manager = m;
+    }
+
+    public boolean hasComponent(String key) {
+        return this.manager.hasService(key);
+    }
+
+    public Object getComponent(String key) 
+    throws ProcessingException {
+        try {
+            return this.manager.lookup(key);
+        } catch (ServiceException se) {
+            throw new ProcessingException("Unable to lookup component for key: 
" + key, se);
+        }
+    }
+
+    public void release(Object component) {
+        this.manager.release(component);
+    }
+
+}
\ No newline at end of file

Propchange: 
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
cocoon/trunk/src/java/org/apache/cocoon/core/container/ComponentLocatorWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/src/java/org/apache/cocoon/sitemap/ComponentLocator.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/sitemap/ComponentLocator.java?rev=168073&r1=168072&r2=168073&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/sitemap/ComponentLocator.java 
(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/sitemap/ComponentLocator.java Tue 
May  3 22:30:01 2005
@@ -15,6 +15,8 @@
  */
 package org.apache.cocoon.sitemap;
 
+import org.apache.cocoon.ProcessingException;
+
 
 /**
  * TODO WORK IN PROGRESS!!
@@ -23,11 +25,11 @@
  * and an optional application/sitemap container.
  *
  * @since 2.2
- * @version $Id:$
+ * @version $Id$
  */
 public interface ComponentLocator {
 
-    Object lookup(String key);
+    Object getComponent(String key) throws ProcessingException;
 
     void release(Object component);
 

Modified: 
cocoon/trunk/src/java/org/apache/cocoon/sitemap/impl/ComponentManager.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/sitemap/impl/ComponentManager.java?rev=168073&r1=168072&r2=168073&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/sitemap/impl/ComponentManager.java 
(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/sitemap/impl/ComponentManager.java 
Tue May  3 22:30:01 2005
@@ -18,6 +18,7 @@
 import org.apache.avalon.framework.CascadingRuntimeException;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.sitemap.ComponentLocator;
 
 
@@ -32,7 +33,7 @@
  * the component in question, the service manager is asked.
  *
  * @since 2.2
- * @version $Id:$
+ * @version $Id$
  */
 public class ComponentManager implements ServiceManager, ComponentLocator {
 
@@ -76,23 +77,41 @@
     public boolean hasComponent(String key) {
         return this.hasService(key);
     }
+
     /**
-     * @see org.apache.cocoon.sitemap.ComponentLocator#lookup(java.lang.String)
      * @see 
org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
      */
-    public Object lookup(String key) {
+    public Object lookup(String key) 
+    throws ServiceException {
+        try {
+            return this.doLookup(key);
+        } catch (ProcessingException se) {
+            throw new ServiceException("ComponentLocator", 
+                                       "Unable to lookup component for key: " 
+ key, se);
+        }
+    }
+
+    /**
+     * @see 
org.apache.cocoon.sitemap.ComponentLocator#getComponent(java.lang.String)
+     */
+    public Object getComponent(String key) throws ProcessingException {
+        try {
+            return this.doLookup(key);
+        } catch (ServiceException se) {
+            throw new ProcessingException("Unable to lookup component for key: 
" + key, se);
+        }
+    }
+
+    protected Object doLookup(String key)
+    throws ProcessingException, ServiceException {
         Object component = null;
         if ( this.componentLocator != null ) {
             if ( this.componentLocator.hasComponent(key) ) {
-                component = this.componentLocator.lookup(key);
+                component = this.componentLocator.getComponent(key);
             }
         }
         if ( component == null && this.serviceManager.hasService(key) ) {
-            try {
-                component = this.serviceManager.lookup(key);
-            } catch (ServiceException se) {
-                throw new CascadingRuntimeException("Unable to lookup 
component: " + key, se);
-            }
+            component = this.serviceManager.lookup(key);
         }
         return component;
     }


Reply via email to