You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/05/15 22:06:36 UTC

svn commit: r406729 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor: NodeBuilderSelector.java ProcessingNodeBuilder.java sitemap/SitemapLanguage.java

Author: cziegeler
Date: Mon May 15 13:06:34 2006
New Revision: 406729

URL: http://svn.apache.org/viewcvs?rev=406729&view=rev
Log:
Simplify selector

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/NodeBuilderSelector.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/ProcessingNodeBuilder.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/NodeBuilderSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/NodeBuilderSelector.java?rev=406729&r1=406728&r2=406729&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/NodeBuilderSelector.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/NodeBuilderSelector.java Mon May 15 13:06:34 2006
@@ -21,7 +21,6 @@
 import java.util.Iterator;
 import java.util.Map;
 
-import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.activity.Initializable;
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
@@ -34,11 +33,8 @@
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.cocoon.core.container.spring.ComponentInfo;
-import org.apache.cocoon.util.ClassUtils;
 
 /**
  * This is the selector used to select/create node builders.
@@ -48,10 +44,8 @@
  */
 public class NodeBuilderSelector
     extends AbstractLogEnabled
-    implements ServiceSelector,
-               Serviceable,
+    implements Serviceable,
                Configurable,
-               Disposable,
                Initializable,
                Contextualizable {
 
@@ -59,12 +53,6 @@
      */
     protected ServiceManager serviceManager;
 
-    /** The role of this selector. Set in <code>configure()</code>. */
-    protected String roleName;
-
-    /** The default key */
-    protected String defaultKey;
-
     /** The application context for components */
     protected Context context;
 
@@ -74,12 +62,6 @@
     /** All singletons. */
     protected final Map singletons = Collections.synchronizedMap(new HashMap());
 
-    /** Is the Manager disposed or not? */
-    protected boolean disposed;
-
-    /** Is the Manager initialized? */
-    protected boolean initialized;
-
     /**
      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
      */
@@ -88,105 +70,27 @@
     }
 
     /**
-     * Obtain a new ComponentHandler for the specified component. 
-     * 
-     * @param role the component's role.
-     * @param componentClass Class of the component for which the handle is
-     *                       being requested.
-     * @param configuration The configuration for this component.
-     *
-     * @throws Exception If there were any problems obtaining a ComponentHandler
-     */
-    protected ComponentInfo getComponentInfo( final Class componentClass,
-                                              final Configuration configuration)
-    throws Exception {
-        final ComponentInfo info = new ComponentInfo();
-        info.fill(configuration);
-        info.setConfiguration(configuration);
-        info.setComponentClassName(componentClass.getName());
-        if ( ThreadSafe.class.isAssignableFrom( componentClass ) ) {
-            info.setModel(ComponentInfo.MODEL_SINGLETON);
-        } else {
-            info.setModel(ComponentInfo.MODEL_PRIMITIVE);
-        }
-        return info;
-    }
-
-    protected void addComponent(String        role,
-                                String        className,
-                                Configuration configuration) 
-    throws ConfigurationException {
-        if( this.initialized ) {
-            throw new ConfigurationException( 
-                "Cannot add components to an initialized service selector: " + role );
-        }
-        try {
-            if( this.getLogger().isDebugEnabled() ) {
-                this.getLogger().debug( "Adding component (" + role + " = " + className + ")" );
-            }
-
-            final Class clazz = this.getClass().getClassLoader().loadClass( className );
-
-            final ComponentInfo info = getComponentInfo( clazz,
-                                                         configuration);
-
-            this.componentInfos.put( role, info );
-
-        } catch( final ClassNotFoundException cnfe ) {
-            final String message = "Could not get class (" + className + ") for role "
-                                 + role + " at " + configuration.getLocation();
-
-            throw new ConfigurationException( message, cnfe );
-        } catch( final ServiceException ce ) {
-            final String message = "Cannot setup class "+ className + " for role " + role
-                                 + " at " + configuration.getLocation();
-            throw new ConfigurationException( message, ce );
-        } catch( final Exception e ) {
-            final String message = "Unexpected exception when setting up role " + role + " at " + configuration.getLocation();
-            throw new ConfigurationException( message, e );
-        }        
-    }
-
-    /**
-     * @see org.apache.avalon.framework.service.ServiceSelector#select(java.lang.Object)
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
      */
-    public Object select( Object hint )
+    public void service( final ServiceManager componentManager )
     throws ServiceException {
-        final String key;
-        if (hint == null) {
-            key = this.defaultKey;
-        } else {
-            key = hint.toString();
-        }
-
-        if( !this.initialized ) {
-            if( this.getLogger().isWarnEnabled() ) {
-                this.getLogger().warn( "Selecting a component on an uninitialized service selector "
-                    + "with key [" + key + "]" );
-            }
-        }
-
-        if( this.disposed ) {
-            throw new IllegalStateException(
-                "You cannot select a component from a disposed service selector." );
-        }
+        this.serviceManager = componentManager;
+    }
 
-        Object component = this.singletons.get(key);
+    public Object getBuilder( String name )
+    throws Exception {
+        Object component = this.singletons.get(name);
         if ( component == null ) {
-            final ComponentInfo info = (ComponentInfo)this.componentInfos.get( key );
+            final BuilderInfo info = (BuilderInfo)this.componentInfos.get( name );
 
             // Retrieve the instance of the requested component
             if( null == info ) {
-                final String message = this.roleName
-                   + ": service selector could not find the component for key [" + key + "]";
-                throw new ServiceException( key, message );
+                throw new Exception( "Node builder selector could not find builder for key [" + name + "]" );
             }
             try {
                 component = this.createComponent(info);
-            } catch (ServiceException se) {
-                throw se;
             } catch (Exception e) {
-                throw new ServiceException(key, "Unable to create new component.", e);
+                throw new Exception("Unable to create new builder: " + name, e);
             }
         }
 
@@ -194,98 +98,52 @@
     }
 
     /**
-     * @see org.apache.avalon.framework.service.ServiceSelector#isSelectable(java.lang.Object)
-     */
-    public boolean isSelectable( Object hint ) {
-        final String key;
-        if (hint == null) {
-            key = this.defaultKey;
-        } else {
-            key = hint.toString();
-        }
-
-        if( !this.initialized ) return false;
-        if( this.disposed ) return false;
-
-        boolean exists = false;
-
-        try {
-            Object component = this.componentInfos.get( key );
-            exists = (component != null);
-        } catch( Throwable t ) {
-            // We can safely ignore all exceptions
-        }
-
-        return exists;
-    }
-
-    /**
-     * @see org.apache.avalon.framework.service.ServiceSelector#release(java.lang.Object)
-     */
-    public void release( final Object component ) {
-        if ( !(component instanceof ThreadSafe) ) {
-            ContainerUtil.dispose(component);
-        }
-    }
-
-    /**
-     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
-     */
-    public void service( final ServiceManager componentManager )
-    throws ServiceException {
-        this.serviceManager = componentManager;
-    }
-
-    /**
      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
      */
     public void configure( final Configuration config )
     throws ConfigurationException {
-        this.roleName = config.getAttribute("role", null);
-
-        // Get default key
-        this.defaultKey = config.getAttribute("default", null);
-
         final Configuration[] instances = config.getChildren();
-
         for (int i = 0; i < instances.length; i++) {
-
             final Configuration instance = instances[i];
-            String key = instance.getAttribute("name").trim();
-
-            String classAttr = instance.getAttribute("builder", null);
-            String className;
-
-            // component-instances names explicitly defined
-            if ("node".equals(instance.getName())) {
-                className = (classAttr == null) ? null : classAttr.trim();
-            } else {
-                className = null;
-            }
-
-            if (className == null) {
-                String message = "Unable to determine class name for component named '" + key +
-                    "' at " + instance.getLocation();
-
-                throw new ConfigurationException(message);
-            }
-
-            this.addComponent( key, className, instance );
+            final String name = instance.getAttribute("name").trim();
+            final String className = instance.getAttribute("builder").trim();
+            try {
+                if( this.getLogger().isDebugEnabled() ) {
+                    this.getLogger().debug( "Adding builder (" + name + " = " + className + ")" );
+                }
+
+                final Class clazz = this.getClass().getClassLoader().loadClass( className );
+
+                final BuilderInfo info = new BuilderInfo();
+                info.builderClass = clazz;
+                info.configuration = instance;
+
+                this.componentInfos.put( name, info );
+
+            } catch( final ClassNotFoundException cnfe ) {
+                final String message = "Could not get class (" + className + ") for builder "
+                                     + name + " at " + instance.getLocation();
+
+                throw new ConfigurationException( message, cnfe );
+            } catch( final Exception e ) {
+                final String message = "Unexpected exception when setting up builder " + name + " at " + instance.getLocation();
+                throw new ConfigurationException( message, e );
+            }        
         }
     }
 
     /**
      * Create a new component.
      */
-    protected Object createComponent(ComponentInfo info)
+    protected Object createComponent(BuilderInfo info)
     throws Exception {
-        final Object component = ClassUtils.newInstance(info.getComponentClassName());
+        final Object component = info.builderClass.newInstance();
         ContainerUtil.enableLogging(component, this.getLogger());
         ContainerUtil.contextualize(component, this.context);
         ContainerUtil.service(component, this.serviceManager);
-        ContainerUtil.configure(component, info.getConfiguration());
+        ContainerUtil.configure(component, info.configuration);
         if ( component instanceof Parameterizable ) {
-            ContainerUtil.parameterize(component, Parameters.fromConfiguration(info.getConfiguration()));
+            ContainerUtil.parameterize(component, Parameters.fromConfiguration(info.configuration));
         }
         ContainerUtil.initialize(component);
         return component;
@@ -296,29 +154,18 @@
      */
     public void initialize() 
     throws Exception {
-        this.initialized = true;
-
         final Iterator i = this.componentInfos.entrySet().iterator();
         while ( i.hasNext() ) {
             final Map.Entry entry = (Map.Entry)i.next();
-            final ComponentInfo info = (ComponentInfo)entry.getValue();
-            if ( info.getModel() == ComponentInfo.MODEL_SINGLETON ) {
+            final BuilderInfo info = (BuilderInfo)entry.getValue();
+            if ( ThreadSafe.class.isAssignableFrom( info.builderClass ) ) {
                 this.singletons.put(entry.getKey(), this.createComponent(info));
             }
         }
     }
 
-    /**
-     * @see org.apache.avalon.framework.activity.Disposable#dispose()
-     */
-    public void dispose() {
-        final Iterator iter = this.singletons.values().iterator();
-        while( iter.hasNext() ) {
-            final Object current = iter.next();
-            ContainerUtil.dispose(current);
-        }
-        this.singletons.clear();
-        this.componentInfos.clear();
-        this.disposed = true;
+    protected static class BuilderInfo {
+        public Configuration configuration;
+        public Class         builderClass;
     }
 }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/ProcessingNodeBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/ProcessingNodeBuilder.java?rev=406729&r1=406728&r2=406729&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/ProcessingNodeBuilder.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/ProcessingNodeBuilder.java Mon May 15 13:06:34 2006
@@ -20,9 +20,18 @@
 /**
  * A <code>ProcessingNode</code> builder.
  * <p>
- * Lifecycle information : a <code>TreeBuilder</code> can be recycled
- * and used to build several <code>Processor</code>s, each one defining
- * a different <code>ServiceManager</code>.
+ * A processing node builder is used to create the processing statements
+ * for a sitemap.
+ * A node builder can either be implemented as a singleton, using
+ * the ThreadSafe marker interface, or each time a builder is required
+ * a new instance is created.
+ * All builders are managed by the {@link NodeBuilderSelector}.
+ * A node builder can implement the following marker interfaces from
+ * Avalon: LogEnabled, Contextualizable, Initializable
+ * and Configurable. Other marker interfaces, like Recyclable, Poolable
+ * or Disposable are not supported!
+ * If the builder needs a service manager it can fetch this one from
+ * the tree builder.
  * <p>
  *
  * @version $Id$

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java?rev=406729&r1=406728&r2=406729&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/treeprocessor/sitemap/SitemapLanguage.java Mon May 15 13:06:34 2006
@@ -44,7 +44,6 @@
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.ProcessingUtil;
@@ -153,7 +152,7 @@
      * Selector for ProcessingNodeBuilders which is set up in the context of the
      * processor that we are building.
      */
-    private ServiceSelector itsBuilders;
+    private NodeBuilderSelector itsBuilders;
 
     /**
      * The sitemap component information grabbed while building itsMaanger
@@ -294,18 +293,7 @@
         }
 
         ProcessingNodeBuilder builder;
-        try {
-            builder = (ProcessingNodeBuilder) this.itsBuilders.select(nodeName);
-        } catch (ServiceException ce) {
-            // Is it because this element is unknown ?
-            if (this.itsBuilders.isSelectable(nodeName)) {
-                // No : rethrow
-                throw ce;
-            }
-            // Throw a more meaningful exception
-            String msg = "Unknown element '" + nodeName + "' at " + config.getLocation();
-            throw new ConfigurationException(msg);
-        }
+        builder = (ProcessingNodeBuilder) this.itsBuilders.getBuilder(nodeName);
 
         builder.setBuilder(this);