cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: rev 55286 - cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components
Date Fri, 22 Oct 2004 00:22:21 GMT
Author: vgritsenko
Date: Thu Oct 21 17:22:20 2004
New Revision: 55286

Modified:
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/EnvironmentStack.java
Log:
Reduce code duplication: add getCurrentEnvironmentDescriptor method.


Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
Thu Oct 21 17:22:20 2004
@@ -53,15 +53,12 @@
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  * @version CVS $Id$
  */
-public final class CocoonComponentManager
-extends ExcaliburComponentManager
-implements SourceResolver
-{
+public final class CocoonComponentManager extends ExcaliburComponentManager
+                                          implements SourceResolver {
 
     /** The key used to store the current process environment */
     private static final String PROCESS_KEY = CocoonComponentManager.class.getName();
 
-
     /** The environment information */
     private static InheritableThreadLocal environmentStack = new CloningInheritableThreadLocal();
 
@@ -217,8 +214,7 @@
      */
     public static void endProcessing(Environment env, Object key) {
 		env.finishingProcessing();
-        final EnvironmentDescription desc = (EnvironmentDescription)key;
-        desc.release();
+        ((EnvironmentDescription) key).release();
         env.getObjectModel().remove(PROCESS_KEY);
     }
 
@@ -276,14 +272,15 @@
      */
     public Component lookup( final String role )
     throws ComponentException {
-        if( null == role ) {
+        if (null == role) {
             final String message =
                 "ComponentLocator Attempted to retrieve component with null role.";
 
             throw new ComponentException( role, message );
         }
-        if ( role.equals(SourceResolver.ROLE) ) {
-            if ( null == this.sourceResolver ) {
+
+        if (role.equals(SourceResolver.ROLE)) {
+            if (null == this.sourceResolver) {
                 if(wasDisposed) {
                     // (BD) working on bug 27249: I think we could throw an Exception here,
as
                     // the following call fails anyway, but I'm not sure enough ;-)
@@ -294,12 +291,10 @@
             return this;
         }
 
-        final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
-        if ( null != stack && !stack.empty()) {
-            final Object[] objects = (Object[])stack.getCurrent();
-            final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
+        final EnvironmentStack stack = (EnvironmentStack) environmentStack.get();
+        if (stack != null && !stack.isEmpty()) {
+            final EnvironmentDescription desc = getCurrentEnvironmentDescriptor(stack);
+            if (null != desc) {
                 Component component = desc.getRequestLifecycleComponent(role);
                 if (null != component) {
                     return component;
@@ -311,18 +306,19 @@
             }
         }
 
-        final Component component = super.lookup( role );
-        if (null != component && component instanceof RequestLifecycleComponent)
{
+        final Component component = super.lookup(role);
+
+        if (component != null && component instanceof RequestLifecycleComponent)
{
             if (stack == null || stack.empty()) {
                 throw new ComponentException(role, "ComponentManager has no Environment Stack.");
             }
-            final Object[] objects = (Object[]) stack.getCurrent();
-            final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
 
+            final Object[] objects = (Object[]) stack.getCurrent();
+            final Map objectModel = ((Environment) objects[0]).getObjectModel();
+            EnvironmentDescription desc = (EnvironmentDescription) objectModel.get(PROCESS_KEY);
+            if (null != desc) {
                 // first test if the parent CM has already initialized this component
-                if ( !desc.containsRequestLifecycleComponent( role ) ) {
+                if (!desc.containsRequestLifecycleComponent(role)) {
                     try {
                         if (component instanceof Recomposable) {
                             ((Recomposable) component).recompose(this);
@@ -337,15 +333,15 @@
             }
         }
 
-        if (null != component && component instanceof GlobalRequestLifecycleComponent)
{
+        if (component != null && component instanceof GlobalRequestLifecycleComponent)
{
             if (stack == null || stack.empty()) {
                 throw new ComponentException(role, "ComponentManager has no Environment Stack.");
             }
+
             final Object[] objects = (Object[]) stack.getCurrent();
             final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
-
+            EnvironmentDescription desc = (EnvironmentDescription) objectModel.get(PROCESS_KEY);
+            if (null != desc) {
                 // first test if the parent CM has already initialized this component
                 if ( !desc.containsGlobalRequestLifecycleComponent( role ) ) {
                     try {
@@ -362,16 +358,15 @@
             }
         }
 
-        if ( null != component && component instanceof SitemapConfigurable) {
-
+        if (component != null && component instanceof SitemapConfigurable) {
             // FIXME: how can we prevent that this is called over and over again?
             SitemapConfigurationHolder holder;
 
-            holder = (SitemapConfigurationHolder)this.sitemapConfigurationHolders.get( role
);
-            if ( null == holder ) {
+            holder = (SitemapConfigurationHolder) this.sitemapConfigurationHolders.get(role);
+            if (null == holder) {
                 // create new holder
-                holder = new DefaultSitemapConfigurationHolder( role );
-                this.sitemapConfigurationHolders.put( role, holder );
+                holder = new DefaultSitemapConfigurationHolder(role);
+                this.sitemapConfigurationHolders.put(role, holder);
             }
 
             try {
@@ -380,6 +375,7 @@
                 throw new ComponentException(role, "Exception during setup of SitemapConfigurable.",
ce);
             }
         }
+
         return component;
     }
 
@@ -387,26 +383,28 @@
      * Release a Component.  This implementation makes sure it has a handle on the propper
      * ComponentHandler, and let's the ComponentHandler take care of the actual work.
      */
-    public void release( final Component component ) {
-        if( null == component ) {
+    public void release(final Component component) {
+        if (null == component) {
             return;
         }
 
-        if ( component instanceof RequestLifecycleComponent
-             || component instanceof GlobalRequestLifecycleComponent) {
+        if (component instanceof RequestLifecycleComponent
+                || component instanceof GlobalRequestLifecycleComponent) {
             return;
         }
-        if ( component == this ) {
+
+        if (component == this) {
             return;
         }
-        super.release( component);
+
+        super.release(component);
     }
 
     /**
      * Release a RequestLifecycleComponent
      */
-    protected void releaseRLComponent( final Component component ) {
-        super.release( component );
+    protected void releaseRLComponent(final Component component) {
+        super.release(component);
     }
 
     /**
@@ -416,12 +414,10 @@
                                                        final Component         component,
                                                        final ComponentManager  manager)
     throws ProcessingException {
-        final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
-        if ( null != stack && !stack.empty()) {
-            final Object[] objects = (Object[])stack.get(0);
-            final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
+        final EnvironmentStack stack = (EnvironmentStack) environmentStack.get();
+        if (null != stack && !stack.empty()) {
+            final EnvironmentDescription desc = getCurrentEnvironmentDescriptor(stack);
+            if (null != desc) {
                 desc.addToAutoRelease(selector, component, manager);
             }
         } else {
@@ -435,12 +431,10 @@
     public static void addComponentForAutomaticRelease(final ComponentManager manager,
                                                        final Component        component)
     throws ProcessingException {
-        final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
-        if ( null != stack && !stack.empty()) {
-            final Object[] objects = (Object[])stack.get(0);
-            final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
+        final EnvironmentStack stack = (EnvironmentStack) environmentStack.get();
+        if (null != stack && !stack.empty()) {
+            final EnvironmentDescription desc = getCurrentEnvironmentDescriptor(stack);
+            if (null != desc) {
                 desc.addToAutoRelease(manager, component);
             }
         } else {
@@ -453,12 +447,10 @@
      */
     public static void removeFromAutomaticRelease(final Component component)
     throws ProcessingException {
-        final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
-        if ( null != stack && !stack.empty()) {
-            final Object[] objects = (Object[])stack.get(0);
-            final Map objectModel = ((Environment)objects[0]).getObjectModel();
-            EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
-            if ( null != desc ) {
+        final EnvironmentStack stack = (EnvironmentStack) environmentStack.get();
+        if (null != stack && !stack.empty()) {
+            final EnvironmentDescription desc = getCurrentEnvironmentDescriptor(stack);
+            if (null != desc) {
                 desc.removeFromAutoRelease(component);
             }
         } else {
@@ -466,16 +458,22 @@
         }
     }
 
+    private static EnvironmentDescription getCurrentEnvironmentDescriptor(final EnvironmentStack
stack) {
+        final Object[] objects = (Object[]) stack.getCurrent();
+        final Map objectModel = ((Environment) objects[0]).getObjectModel();
+        return (EnvironmentDescription) objectModel.get(CocoonComponentManager.PROCESS_KEY);
+    }
+
     /**
      * Dispose
      */
     public void dispose() {
-        if(getLogger().isDebugEnabled()) {
+        if (getLogger().isDebugEnabled()) {
             getLogger().debug("CocoonComponentManager.dispose() called");
         }
 
-        if ( null != this.sourceResolver ) {
-            super.release( this.sourceResolver );
+        if (null != this.sourceResolver) {
+            super.release(this.sourceResolver);
             // We cannot null out sourceResolver here yet as some other not
             // disposed yet components might still have unreleased sources,
             // and they will call {@link #release(Source)} during their
@@ -520,8 +518,8 @@
     /**
      * Releases a resolved resource
      */
-    public void release( final Source source ) {
-        this.sourceResolver.release( source );
+    public void release(final Source source) {
+        this.sourceResolver.release(source);
     }
 
 
@@ -529,18 +527,16 @@
      * @see org.apache.avalon.excalibur.component.ExcaliburComponentManager#addComponent(java.lang.String,
java.lang.Class, org.apache.avalon.framework.configuration.Configuration)
      */
     public void addComponent(String role, Class clazz, Configuration conf)
-        throws ComponentException {
+    throws ComponentException {
         super.addComponent(role, clazz, conf);
         // Note that at this point, we're not initialized and cannot do
         // lookups, so defer parental introductions to initialize().
-        if ( ParentAware.class.isAssignableFrom( clazz ) ) {
+        if (ParentAware.class.isAssignableFrom(clazz)) {
             parentAwareComponents.add(role);
         }
     }
 
-    public void initialize()
-        throws Exception
-    {
+    public void initialize() throws Exception {
         super.initialize();
         if (parentAwareComponents == null) {
             throw new ComponentException(null, "CocoonComponentManager already initialized");
@@ -597,7 +593,7 @@
      * released.
      */
     void release() {
-        if ( this.requestLifecycleComponents != null ) {
+        if (this.requestLifecycleComponents != null) {
             final Iterator iter = this.requestLifecycleComponents.values().iterator();
             while (iter.hasNext()) {
                 final Object[] o = (Object[])iter.next();
@@ -607,15 +603,15 @@
             this.requestLifecycleComponents.clear();
         }
 
-        for(int i = 0; i < autoreleaseComponents.size(); i++) {
+        for (int i = 0; i < autoreleaseComponents.size(); i++) {
             final Object[] o = (Object[])autoreleaseComponents.get(i);
             final Component component = (Component)o[0];
             if (o[1] instanceof ComponentManager) {
                 ((ComponentManager)o[1]).release( component );
             } else {
-                ((ComponentSelector)o[1]).release( component );
+                ((ComponentSelector) o[1]).release(component);
                 if (o[2] != null) {
-                    ((ComponentManager)o[2]).release( (Component)o[1] );
+                    ((ComponentManager) o[2]).release((Component) o[1]);
                 }
             }
         }
@@ -624,7 +620,6 @@
         this.objectModel = null;
     }
 
-
     /**
      * Add a RequestLifecycleComponent to the environment
      */
@@ -692,8 +687,8 @@
      * Add an automatically released component
      */
     void addToAutoRelease(final ComponentSelector selector,
-                          final Component         component,
-                          final ComponentManager  manager) {
+                                       final Component         component,
+                                       final ComponentManager  manager) {
         this.autoreleaseComponents.add(new Object[] {component, selector, manager});
     }
 
@@ -701,7 +696,7 @@
      * Add an automatically released component
      */
     void addToAutoRelease(final ComponentManager manager,
-                          final Component        component) {
+                                       final Component        component) {
         this.autoreleaseComponents.add(new Object[] {component, manager});
     }
 
@@ -733,7 +728,6 @@
             throw new ProcessingException("Unable to remove component from automatic release:
component not found.");
         }
     }
-
 }
 
 final class CloningInheritableThreadLocal

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/EnvironmentStack.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/EnvironmentStack.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/EnvironmentStack.java
Thu Oct 21 17:22:20 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.
@@ -16,45 +16,47 @@
 package org.apache.cocoon.components;
 
 import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.environment.Environment;
+
 import org.apache.commons.collections.ArrayStack;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 
+import java.util.Map;
+
 /**
  * The stack for the processing environment.
  * This is a special implementation of a stack for the handling of the
  * cocoon protocol.
  *
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id: EnvironmentStack.java,v 1.3 2004/03/05 13:02:45 bdelacretaz Exp $
+ * @version CVS $Id$
  */
-final class EnvironmentStack 
-    extends ArrayStack 
-    implements Cloneable {
-    
+final class EnvironmentStack extends ArrayStack
+                             implements Cloneable {
+
     int offset;
-    
-    Object getCurrent() {
-        return this.get(offset);
-        //return this.peek(this.offset);
-    }
-    
+
     int getOffset() {
         return this.offset;
     }
-  
+
     void setOffset(int value) {
-        this.offset = value;  
+        this.offset = value;
+    }
+
+    Object getCurrent() {
+        return this.get(offset);
     }
-    
+
     public Object clone() {
         EnvironmentStack old = (EnvironmentStack) super.clone();
         old.offset = offset;
         return old;
     }
-    
-    XMLConsumer getEnvironmentAwareConsumerWrapper(XMLConsumer consumer, 
+
+    XMLConsumer getEnvironmentAwareConsumerWrapper(XMLConsumer consumer,
                                                    int oldOffset) {
         return new EnvironmentChanger(consumer, this, oldOffset, this.offset);
     }
@@ -68,14 +70,13 @@
  * received by some component of the calling pipeline, so inbetween we
  * have to change the environment forth and back.
  */
-final class EnvironmentChanger
-implements XMLConsumer {
+final class EnvironmentChanger implements XMLConsumer {
 
     final XMLConsumer consumer;
     final EnvironmentStack stack;
     final int oldOffset;
     final int newOffset;
-    
+
     EnvironmentChanger(XMLConsumer consumer, EnvironmentStack es,
                        int oldOffset, int newOffset) {
         this.consumer = consumer;
@@ -83,7 +84,7 @@
         this.oldOffset = oldOffset;
         this.newOffset = newOffset;
     }
-    
+
     public void setDocumentLocator(Locator locator) {
         this.stack.setOffset(this.oldOffset);
         this.consumer.setDocumentLocator(locator);
@@ -132,7 +133,7 @@
         this.consumer.endElement(uri, loc, raw);
         this.stack.setOffset(this.newOffset);
     }
-    
+
     public void characters(char c[], int start, int len)
     throws SAXException {
         this.stack.setOffset(this.oldOffset);

Mime
View raw message