cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sylv...@apache.org
Subject svn commit: r124105 - cocoon/trunk/src/core/java/org/apache/cocoon/core/container
Date Tue, 04 Jan 2005 14:36:42 GMT
Author: sylvain
Date: Tue Jan  4 06:36:40 2005
New Revision: 124105

URL: http://svn.apache.org/viewcvs?view=rev&rev=124105
Log:
simplify initialization sequence by having component handlers automatically initialize themselves
if needed
Modified:
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java
   cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AbstractComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -22,6 +22,7 @@
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.thread.SingleThreaded;
 import org.apache.avalon.framework.thread.ThreadSafe;
@@ -45,7 +46,7 @@
     protected boolean disposed = false;
 
     /** State management boolean stating whether the Handler is initialized or not */
-    protected boolean initialized = false;
+    private boolean initialized = false;
     
     private ServiceInfo info;
 
@@ -170,14 +171,11 @@
      * @exception Exception if an error occurs
      */
     public final Object get() throws Exception {
-        if( !this.initialized ) {
-            throw new IllegalStateException(
-                "You cannot get a component from an uninitialized handler." );
-        }
+        initialize();
         if( this.disposed ) {
             throw new IllegalStateException( "You cannot get a component from a disposed
handler." );
         }
-
+        
         final Object component = this.doGet();
 
         synchronized( this.referenceSemaphore ) {
@@ -266,10 +264,14 @@
     /* (non-Javadoc)
      * @see org.apache.cocoon.core.container.ComponentHandler#initialize()
      */
-    public void initialize() throws Exception {
+    public final void initialize() throws Exception {
         if( this.initialized ) {
             return;
         }
+
+        doInitialize();
         this.initialized = true;
     }
+    
+    protected abstract void doInitialize() throws Exception;
 }

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/AliasComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -43,6 +43,8 @@
         this.aliasedHandler.put(component);
     }
     
+    protected void doInitialize() {}
+    
     public boolean isSingleton() {
         return this.aliasedHandler.isSingleton();
     }

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java	(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/CoreServiceManager.java	Tue
Jan  4 06:36:40 2005
@@ -60,11 +60,6 @@
     /** The parent ServiceManager */
     protected ServiceManager parentManager;
     
-    /** added component handlers before initialization to maintain
-     *  the order of initialization
-     */
-    private final List newComponentHandlers = new ArrayList();
-
     /** The resolver used to resolve includes. It is lazily loaded in {@link #getSourceResolver()}.
*/
     private SourceResolver cachedSourceResolver;
 
@@ -144,46 +139,27 @@
     throws Exception {
         super.initialize();
 
-        for( int i = 0; i < this.newComponentHandlers.size(); i++ ) {
-            final ComponentHandler handler =
-                (ComponentHandler)this.newComponentHandlers.get( i );
+        // Initialize component handlers. This is done in no particular order, but initializing
a
+        // handler may indirectly initialize another handler through a call to lookup().
+        // This isn't a problem as a handler's initialize() method can be called several
times.
+
+        // We copy the list of handlers as the componentHandler Map may change if implicitely
declared
+        // components are looked up.
+        ComponentHandler[] handlers = (ComponentHandler[])this.componentHandlers.values().toArray(
+                new ComponentHandler[this.componentHandlers.size()]);
+
+        for( int i = 0; i < handlers.length; i++ ) {
             try {
-                handler.initialize();
+                handlers[i].initialize();
             } catch( Exception e ) {
-                if( this.getLogger().isErrorEnabled() )
-                {
+                if( this.getLogger().isErrorEnabled() ) {
                     this.getLogger().error( "Caught an exception trying to initialize "
                                        + "the component handler.", e );
                 }
-
                 // Rethrow the exception
                 throw e;
             }
         }
-
-        List keys = new ArrayList( this.componentHandlers.keySet() );
-
-        for( int i = 0; i < keys.size(); i++ ) {
-            final Object key = keys.get( i );
-            final ComponentHandler handler =
-                (ComponentHandler)this.componentHandlers.get( key );
-
-            if( !this.newComponentHandlers.contains( handler ) ) {
-                try {
-                    handler.initialize();
-
-                } catch( Exception e ) {
-                    if( this.getLogger().isErrorEnabled() ) {
-                        this.getLogger().error( "Caught an exception trying to initialize
"
-                                           + "the component handler.", e );
-
-                    }
-                    // Rethrow the exception
-                    throw e;
-                }
-            }
-        }
-        this.newComponentHandlers.clear();
         
 //        Object[] keyArray = this.componentHandlers.keySet().toArray();
 //        java.util.Arrays.sort(keyArray);
@@ -349,22 +325,6 @@
 
         try {
             component = handler.get();
-        } catch( final IllegalStateException ise ) {
-            try {
-                handler.initialize();
-                component = handler.get();
-                
-            } catch( final ServiceException ce ) {
-                // Rethrow instead of wrapping a ServiceException with another one
-                throw ce;
-            } catch( final Exception e ) {
-                final String message = "Could not access the component for role [" + role
+ "]";
-                if( this.getLogger().isDebugEnabled() ) {
-                    this.getLogger().debug( message, e );
-                }
-
-                throw new ServiceException( role, message, e );
-            }
         } catch ( ServiceException se) {
             // Rethrow insteand of wrapping it again
             throw se;
@@ -470,7 +430,6 @@
             }
 
             this.componentHandlers.put( role, handler );
-            this.newComponentHandlers.add( handler );
         } catch ( final ServiceException se ) {
             throw se;
         } catch( final Exception e ) {

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/InstanceComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -57,6 +57,8 @@
         // nothing
     }
     
+    protected void doInitialize() {}
+
     /* (non-Javadoc)
      * @see org.apache.cocoon.core.container.ComponentHandler#dispose()
      */

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/PoolableComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -223,4 +223,6 @@
             }
         }
     }
+    
+    protected void doInitialize() {}
 }

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/SingleThreadedComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -63,5 +63,7 @@
     protected void doPut( final Object component ) {
         this.decommission( component );
     }
+    
+    protected void doInitialize() {}
 
 }

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java?view=diff&rev=124105&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java&r1=124104&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java&r2=124105
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/ThreadSafeComponentHandler.java
Tue Jan  4 06:36:40 2005
@@ -47,15 +47,10 @@
         return true;
     }
 
-    public void initialize() 
-    throws Exception {
-        if( this.initialized ) {
-            return;
-        }
+    public void doInitialize() throws Exception {
         if( this.instance == null ) {
             this.instance = this.factory.newInstance();
         }
-        super.initialize();
     }
 
     /**

Mime
View raw message