You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2004/09/27 18:21:32 UTC

svn commit: rev 47303 - in cocoon/trunk/src/java/org/apache/cocoon: components/treeprocessor/sitemap generation serialization transformation

Author: vgritsenko
Date: Mon Sep 27 09:21:30 2004
New Revision: 47303

Added:
   cocoon/trunk/src/java/org/apache/cocoon/generation/GeneratorFactory.java
      - copied, changed from rev 47256, cocoon/trunk/src/java/org/apache/cocoon/generation/Generator.java
   cocoon/trunk/src/java/org/apache/cocoon/serialization/SerializerFactory.java
      - copied, changed from rev 47256, cocoon/trunk/src/java/org/apache/cocoon/serialization/Serializer.java
   cocoon/trunk/src/java/org/apache/cocoon/transformation/TransformerFactory.java   (contents, props changed)
Modified:
   cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/sitemap/ComponentsSelector.java
Log:
Add (experimental) support for ThreadSafe generators/transformers/serializers.


Modified: cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/sitemap/ComponentsSelector.java
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/sitemap/ComponentsSelector.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/sitemap/ComponentsSelector.java	Mon Sep 27 09:21:30 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 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.
@@ -19,7 +19,9 @@
 import java.util.Set;
 
 import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.component.ComponentException;
+import org.apache.avalon.framework.component.Component;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.configuration.DefaultConfiguration;
@@ -27,20 +29,22 @@
 import org.apache.cocoon.components.ExtendedComponentSelector;
 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
 import org.apache.cocoon.generation.Generator;
+import org.apache.cocoon.generation.GeneratorFactory;
 import org.apache.cocoon.matching.Matcher;
 import org.apache.cocoon.reading.Reader;
 import org.apache.cocoon.selection.Selector;
 import org.apache.cocoon.serialization.Serializer;
+import org.apache.cocoon.serialization.SerializerFactory;
 import org.apache.cocoon.transformation.Transformer;
+import org.apache.cocoon.transformation.TransformerFactory;
 
 /**
  * Component selector for sitemap components.
  *
  * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
  * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
- * @version CVS $Id: ComponentsSelector.java,v 1.13 2004/07/16 12:36:45 sylvain Exp $
+ * @version CVS $Id$
  */
-
 public class ComponentsSelector extends ExtendedComponentSelector {
 
     public static final int UNKNOWN     = -1;
@@ -81,6 +85,7 @@
     /** The set of known hints, used to add standard components (see ensureExists) */
     private Set knownHints = new HashSet();
 
+
     /**
      * Return the component instance name according to the selector role
      * (e.g. "action" for "org.apache.cocoon.acting.Action").
@@ -98,7 +103,6 @@
     }
 
     public void configure(Configuration config) throws ConfigurationException {
-        
         // Who are we ?
         String role = getRoleName(config);
         this.roleId = UNKNOWN; // unknown
@@ -115,14 +119,12 @@
         }
 
         super.configure(config);
-        
     }
 
     /**
      * Add a component in this selector.
      */
     public void addComponent(Object hint, Class clazz, Configuration config) throws ComponentException {
-
         super.addComponent(hint, clazz, config);
 
         // Add to known hints. This is needed as we cannot call isSelectable() if initialize()
@@ -134,39 +136,36 @@
      * Ensure system-defined components exist (e.g. &lt;aggregator&gt;) and initialize
      * the selector.
      */
-    public void initialize() /*throws Exception*/ {
-
+    public void initialize() /* throws Exception */ {
         // FIXME : need to catch exceptions since ECS doesn't propagate the throws clause of Initializable
         try {
-
             DefaultConfiguration config = null;
 
             // Ensure all system-defined hints exist.
             // NOTE : checking this here means they can be user-defined in the sitemap
             switch(this.roleId) {
                 case GENERATOR :
-
                     config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated");
                     config.setAttribute("name", "<notifier>");
                     ensureExists("<notifier>",
-                        org.apache.cocoon.sitemap.NotifyingGenerator.class, config);
+                                 org.apache.cocoon.sitemap.NotifyingGenerator.class, config);
 
                     config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated");
                     config.setAttribute("name", "<aggregator>");
                     ensureExists("<aggregator>",
-                        org.apache.cocoon.sitemap.ContentAggregator.class, config);
+                                 org.apache.cocoon.sitemap.ContentAggregator.class, config);
                 break;
 
                 case TRANSFORMER :
                     config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated");
                     config.setAttribute("name", "<translator>");
                     ensureExists("<translator>",
-                        org.apache.cocoon.sitemap.LinkTranslator.class, config);
+                                 org.apache.cocoon.sitemap.LinkTranslator.class, config);
 
                     config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated");
                     config.setAttribute("name", "<gatherer>");
                     ensureExists("<gatherer>",
-                        org.apache.cocoon.sitemap.LinkGatherer.class, config);
+                                 org.apache.cocoon.sitemap.LinkGatherer.class, config);
                 break;
             }
 
@@ -174,11 +173,9 @@
 
             // Don't keep known hints (they're no more needed)
             this.knownHints = null;
-
-        } catch(Exception e) {
+        } catch (Exception e) {
             throw new CascadingRuntimeException("Cannot setup default components", e);
         }
-
     }
 
     /**
@@ -187,11 +184,78 @@
      * be done before initialization.
      */
     private void ensureExists(Object hint, Class clazz, Configuration config) throws ComponentException {
-
-        if (! this.knownHints.contains(hint)) {
+        if (!this.knownHints.contains(hint)) {
             if (this.parentSelector == null || !this.parentSelector.hasComponent(hint)) {
-                this.addComponent(hint, clazz, config);
+                addComponent(hint, clazz, config);
             }
         }
+    }
+
+    /**
+     * Override parent to implement support for {@link GeneratorFactory},
+     * {@link TransformerFactory}, and {@link SerializerFactory}.
+     */
+    public Component select(Object hint) throws ComponentException {
+        final Component component = super.select(hint);
+
+        switch (this.roleId) {
+            case GENERATOR:
+                if (component instanceof GeneratorFactory) {
+                    return ((GeneratorFactory)component).getInstance();
+                }
+                break;
+            case TRANSFORMER:
+                if (component instanceof TransformerFactory) {
+                    return ((TransformerFactory)component).getInstance();
+                }
+                break;
+            case SERIALIZER:
+                if (component instanceof SerializerFactory) {
+                    return ((SerializerFactory)component).getInstance();
+                }
+                break;
+        }
+
+        return component;
+    }
+
+    /**
+     * Override parent to implement support for {@link GeneratorFactory},
+     * {@link TransformerFactory}, and {@link SerializerFactory}.
+     */
+    public void release(Component component) {
+
+        // If component is an Instance returned by Factory, get the Factory.
+        switch (this.roleId) {
+            case GENERATOR:
+                if (component instanceof GeneratorFactory.Instance) {
+                    // Dispose component, if needed
+                    if (component instanceof Disposable) {
+                        ((Disposable)component).dispose();
+                    }
+                    component = ((GeneratorFactory.Instance)component).getFactory();
+                }
+                break;
+            case TRANSFORMER:
+                if (component instanceof TransformerFactory.Instance) {
+                    // Dispose component, if needed
+                    if (component instanceof Disposable) {
+                        ((Disposable)component).dispose();
+                    }
+                    component = ((TransformerFactory.Instance)component).getFactory();
+                }
+                break;
+            case SERIALIZER:
+                if (component instanceof SerializerFactory.Instance) {
+                    // Dispose component, if needed
+                    if (component instanceof Disposable) {
+                        ((Disposable)component).dispose();
+                    }
+                    component = ((SerializerFactory.Instance)component).getFactory();
+                }
+                break;
+        }
+
+        super.release(component);
     }
 }

Copied: cocoon/trunk/src/java/org/apache/cocoon/generation/GeneratorFactory.java (from rev 47256, cocoon/trunk/src/java/org/apache/cocoon/generation/Generator.java)
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/generation/Generator.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/generation/GeneratorFactory.java	Mon Sep 27 09:21:30 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 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.
@@ -15,28 +15,46 @@
  */
 package org.apache.cocoon.generation;
 
-import org.apache.cocoon.ProcessingException;
-import org.apache.cocoon.sitemap.SitemapModelComponent;
-import org.apache.cocoon.xml.XMLProducer;
-import org.xml.sax.SAXException;
-
-import java.io.IOException;
+import org.apache.avalon.framework.component.Component;
 
 /**
- * A generator is the starting point of a pipeline. It "generates" XML
- * and starts streaming them into the pipeline.
- * 
- * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
- *         (Apache Software Foundation)
- * @version CVS $Id: Generator.java,v 1.3 2004/03/05 13:02:55 bdelacretaz Exp $
+ * A generator factory is the factory of {@link Generator}s.
+ *
+ * <p>Regular GeneratorFactory implementation should be
+ * {@link org.apache.avalon.framework.thread.ThreadSafe} component
+ * serving as a factory of lightweight {@link Generator} objects.</p>
+ *
+ * <p>GeneratorFactory can implement any number of Avalon lifecycle interfaces
+ * and perform any initializations necessary. Ligtweight Generator instances
+ * created by {@link #getInstance()} method will only need to parse
+ * additional parameters passed on sitemap component invocation via
+ * {@link org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, String, org.apache.avalon.framework.parameters.Parameters)}
+ * method and can access global configuration of GeneratorFactory.</p>
+ *
+ * <p><strong>NOTE:</strong> Only Disposable interface is applicable to
+ * the Generator instance returned by the {@link #getInstance()}.</p>
+ *
+ * @since 2.2
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
+ * @version CVS $Id$
  */
-public interface Generator extends XMLProducer, SitemapModelComponent {
+public interface GeneratorFactory extends Component {
+
+    String ROLE = Generator.ROLE;
+
+    /**
+     * Instance of the Generator created by the GeneratorFactory
+     */
+    interface Instance extends Generator {
 
-    String ROLE = Generator.class.getName();
+        /**
+         * @return GeneratorFactory which created this Generator instance
+         */
+        GeneratorFactory getFactory();
+    }
 
     /**
-     * Generate the XML and stream it into the pipeline
+     * Create an instance of the Generator
      */
-    void generate()
-    throws IOException, SAXException, ProcessingException;
+    Instance getInstance();
 }

Copied: cocoon/trunk/src/java/org/apache/cocoon/serialization/SerializerFactory.java (from rev 47256, cocoon/trunk/src/java/org/apache/cocoon/serialization/Serializer.java)
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/serialization/Serializer.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/serialization/SerializerFactory.java	Mon Sep 27 09:21:30 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 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.
@@ -15,16 +15,39 @@
  */
 package org.apache.cocoon.serialization;
 
-import org.apache.cocoon.sitemap.SitemapOutputComponent;
-import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.avalon.framework.component.Component;
 
 /**
+ * A serializer factory is the factory of {@link Serializer}s.
+ *
+ * <p>Regular SerializerFactory implementation should be
+ * {@link org.apache.avalon.framework.thread.ThreadSafe} component
+ * serving as a factory of lightweight {@link Serializer} objects.</p>
  *
- * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
- *         (Apache Software Foundation)
- * @version CVS $Id: Serializer.java,v 1.2 2004/03/05 13:02:58 bdelacretaz Exp $
+ * <p><strong>NOTE:</strong> Only Disposable interface is applicable to
+ * the Serializer instance returned by the {@link #getInstance()}.</p>
+ *
+ * @since 2.2
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
+ * @version CVS $Id$
  */
-public interface Serializer extends XMLConsumer, SitemapOutputComponent {
+public interface SerializerFactory extends Component {
+
+    String ROLE = Serializer.ROLE;
+
+    /**
+     * Instance of the Serializer created by the SerializerFactory
+     */
+    interface Instance extends Serializer {
+
+        /**
+         * @return SerializerFactory which created this Serializer instance
+         */
+        SerializerFactory getFactory();
+    }
 
-    String ROLE = Serializer.class.getName();
+    /**
+     * Create an instance of the Serializer
+     */
+    Instance getInstance();
 }

Added: cocoon/trunk/src/java/org/apache/cocoon/transformation/TransformerFactory.java
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/java/org/apache/cocoon/transformation/TransformerFactory.java	Mon Sep 27 09:21:30 2004
@@ -0,0 +1,53 @@
+/*
+ * Copyright 1999-2004 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.transformation;
+
+import org.apache.avalon.framework.component.Component;
+
+/**
+ * A transformer factory is the factory of {@link Transformer}s.
+ *
+ * <p>Regular TransformerFactory implementation should be
+ * {@link org.apache.avalon.framework.thread.ThreadSafe} component
+ * serving as a factory of lightweight {@link Transformer} objects.</p>
+ *
+ * <p><strong>NOTE:</strong> Only Disposable interface is applicable to
+ * the Transformer instance returned by the {@link #getInstance()}.</p>
+
+ * @since 2.2
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
+ * @version CVS $Id$
+ */
+public interface TransformerFactory extends Component {
+
+    String ROLE = Transformer.ROLE;
+
+    /**
+     * Instance of the Transformer created by the TransformerFactory
+     */
+    interface Instance extends Transformer {
+
+        /**
+         * @return TransformerFactory which created this Transformer instance
+         */
+        TransformerFactory getFactory();
+    }
+
+    /**
+     * Create an instance of the Transformer
+     */
+    Instance getInstance();
+}