Author: cziegeler
Date: Tue Oct 19 12:52:11 2004
New Revision: 55090
Modified:
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/AbstractServiceManager.java
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceManager.java
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceSelector.java
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/RoleManager.java
Log:
More refactoring
Modified:
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/AbstractServiceManager.java
==============================================================================
---
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/AbstractServiceManager.java
(original)
+++
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/AbstractServiceManager.java
Tue Oct 19 12:52:11 2004
@@ -22,9 +22,11 @@
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.Contextualizable;
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.thread.ThreadSafe;
@@ -33,7 +35,7 @@
*
* @version CVS $Revision: 1.6 $ $Date: 2004/02/28 11:47:14 $
*/
-public class AbstractServiceManager
+public abstract class AbstractServiceManager
extends AbstractLogEnabled
implements Contextualizable, ThreadSafe {
@@ -117,5 +119,52 @@
this.roleManager);
}
+ protected void addComponent(String className,
+ Object role,
+ Configuration configuration)
+ throws ConfigurationException {
+ // check for old class names
+ // TODO We can remove this as soon as we changed cocoon.roles and
cocoon.xconf
+ if (
"org.apache.cocoon.components.ExtendedComponentSelector".equals(className)) {
+ className = CocoonServiceSelector.class.getName();
+ } else if
("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector".equals(className)
) {
+ className = ComponentsSelector.class.getName();
+ }
+
+ try {
+ if( this.getLogger().isDebugEnabled() ) {
+ this.getLogger().debug( "Adding component (" + role + " = " +
className + ")" );
+ }
+
+ final Class clazz = this.loader.loadClass( className );
+ this.addComponent( role, clazz, configuration );
+ } catch( final ClassNotFoundException cnfe ) {
+ final String message = "Could not get class (" + className + ")
for role "
+ + role + " on configuration element " +
configuration.getName();
+
+ if( this.getLogger().isErrorEnabled() ) {
+ this.getLogger().error( message, cnfe );
+ }
+
+ throw new ConfigurationException( message, cnfe );
+ } catch( final ServiceException ce ) {
+ final String message = "Bad component "+ className + " for role "
+ role
+ + " on configuration element " +
configuration.getName();
+
+ if( this.getLogger().isErrorEnabled() ) {
+ this.getLogger().error( message, ce );
+ }
+
+ throw new ConfigurationException( message, ce );
+ } catch( final Exception e ) {
+ final String message = "Unexpected exception for role: " + role;
+ if( this.getLogger().isErrorEnabled() ) {
+ this.getLogger().error( "Unexpected exception for role: " +
role, e );
+ }
+ throw new ConfigurationException( message, e );
+ }
+ }
+ protected abstract void addComponent(Object role, Class clazz,
Configuration config)
+ throws ServiceException;
}
Modified:
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceManager.java
==============================================================================
---
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceManager.java
(original)
+++
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceManager.java
Tue Oct 19 12:52:11 2004
@@ -308,46 +308,7 @@
className = roleManager.getDefaultClassNameForRole(
role );
}
-
- if (
"org.apache.cocoon.components.ExtendedComponentSelector".equals(className)) {
- className = CocoonServiceSelector.class.getName();
- } else if
("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector".equals(className)
) {
- className = ComponentsSelector.class.getName();
- }
- try {
- if( this.getLogger().isDebugEnabled() ) {
- this.getLogger().debug( "Adding component (" +
role + " = "
- + className + ")" );
- }
-
- final Class clazz = this.loader.loadClass( className );
- addComponent( role, clazz, configurations[ i ] );
- } catch( final ClassNotFoundException cnfe ) {
- final String message = "Could not get class ";
-
- if( this.getLogger().isErrorEnabled() ) {
- this.getLogger().error( message + className + "
for role " + role
- + " on configuration element " +
configurations[ i ].getName(),
- cnfe );
- }
-
- throw new ConfigurationException( message, cnfe );
- } catch( final ServiceException ce ) {
- final String message = "Bad component ";
-
- if( this.getLogger().isErrorEnabled() ) {
- this.getLogger().error( message + className + "
for role " + role
- + " on configuration element " +
configurations[ i ].getName(),
- ce );
- }
-
- throw new ConfigurationException( message, ce );
- } catch( final Exception e ) {
- if( this.getLogger().isErrorEnabled() ) {
- this.getLogger().error( "Unexpected exception for
hint: " + role, e );
- }
- throw new ConfigurationException( "Unexpected
exception", e );
- }
+ this.addComponent(className, role, configurations[i]);
}
}
}
@@ -365,119 +326,114 @@
*/
public void initialize()
throws Exception {
- synchronized( this ) {
- this.initialized = true;
+ this.initialized = true;
- for( int i = 0; i < this.newComponentHandlers.size(); i++ )
- {
- final AbstractComponentHandler handler =
- (AbstractComponentHandler)this.newComponentHandlers.get( i
);
+ for( int i = 0; i < this.newComponentHandlers.size(); i++ )
+ {
+ final AbstractComponentHandler handler =
+ (AbstractComponentHandler)this.newComponentHandlers.get( i );
+ 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;
+ }
+ }
+
+ List keys = new ArrayList( this.componentHandlers.keySet() );
+
+ for( int i = 0; i < keys.size(); i++ ) {
+ final Object key = keys.get( i );
+ final AbstractComponentHandler handler =
+ (AbstractComponentHandler)this.componentHandlers.get( key );
+
+ if( !this.newComponentHandlers.contains( handler ) ) {
try {
handler.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;
}
}
+ }
+ this.newComponentHandlers.clear();
- List keys = new ArrayList( this.componentHandlers.keySet() );
-
- for( int i = 0; i < keys.size(); i++ ) {
- final Object key = keys.get( i );
- final AbstractComponentHandler handler =
- (AbstractComponentHandler)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();
-
- if (parentAwareComponents == null) {
- throw new ServiceException(null, "CocoonServiceManager already
initialized");
- }
- // Set parents for parentAware components
- Iterator iter = parentAwareComponents.iterator();
- while (iter.hasNext()) {
- String role = (String)iter.next();
- getLogger().debug(".. "+role);
- if ( parentManager != null && parentManager.hasService( role )
) {
- // lookup new component
- Object component = null;
- try {
- component = this.lookup( role );
- ((CocoonServiceSelector)component).setParentLocator(
this.parentManager, role );
- } catch (ServiceException ignore) {
- // we don't set the parent then
- } finally {
- this.release( component );
- }
+ if (parentAwareComponents == null) {
+ throw new ServiceException(null, "CocoonServiceManager already
initialized");
+ }
+ // Set parents for parentAware components
+ Iterator iter = parentAwareComponents.iterator();
+ while (iter.hasNext()) {
+ String role = (String)iter.next();
+ if ( parentManager != null && parentManager.hasService( role ) ) {
+ // lookup new component
+ Object component = null;
+ try {
+ component = this.lookup( role );
+ ((CocoonServiceSelector)component).setParentLocator(
this.parentManager, role );
+ } catch (ServiceException ignore) {
+ // we don't set the parent then
+ } finally {
+ this.release( component );
}
}
- parentAwareComponents = null; // null to save memory, and catch
logic bugs.
}
+ parentAwareComponents = null; // null to save memory, and catch logic
bugs.
}
/* (non-Javadoc)
* @see org.apache.avalon.framework.activity.Disposable#dispose()
*/
public void dispose() {
- synchronized( this ) {
- boolean forceDisposal = false;
+ boolean forceDisposal = false;
- final List disposed = new ArrayList();
+ final List disposed = new ArrayList();
- while( componentHandlers.size() > 0 ) {
- for( Iterator iterator = componentHandlers.keySet().iterator();
- iterator.hasNext(); ) {
- final Object role = iterator.next();
-
- final AbstractComponentHandler handler =
- (AbstractComponentHandler)componentHandlers.get( role
);
-
- if( forceDisposal || handler.canBeDisposed() ) {
- if( forceDisposal && getLogger().isWarnEnabled() ) {
- this.getLogger().warn
- ( "disposing of handler for unreleased
component."
- + " role [" + role + "]" );
- }
+ while( componentHandlers.size() > 0 ) {
+ for( Iterator iterator = componentHandlers.keySet().iterator();
+ iterator.hasNext(); ) {
+ final Object role = iterator.next();
- handler.dispose();
- disposed.add( role );
- }
- }
+ final AbstractComponentHandler handler =
+ (AbstractComponentHandler)componentHandlers.get( role );
- if( disposed.size() > 0 ) {
- final Iterator i = disposed.iterator();
- while ( i.hasNext() ) {
- this.componentHandlers.remove( i.next() );
+ if( forceDisposal || handler.canBeDisposed() ) {
+ if( forceDisposal && getLogger().isWarnEnabled() ) {
+ this.getLogger().warn
+ ( "disposing of handler for unreleased component."
+ + " role [" + role + "]" );
}
- disposed.clear();
- } else {
- // no more disposable handlers!
- forceDisposal = true;
+
+ handler.dispose();
+ disposed.add( role );
}
}
- this.disposed = true;
+ if( disposed.size() > 0 ) {
+ final Iterator i = disposed.iterator();
+ while ( i.hasNext() ) {
+ this.componentHandlers.remove( i.next() );
+ }
+ disposed.clear();
+ } else {
+ // no more disposable handlers!
+ forceDisposal = true;
+ }
}
+
+ this.disposed = true;
}
/**
@@ -487,21 +443,21 @@
* @param component the class of this component.
* @param configuration the configuration for this component.
*/
- public void addComponent( final String role,
+ public void addComponent( final Object role,
final Class component,
final Configuration configuration )
throws ServiceException {
if( this.initialized ) {
- throw new ServiceException( role,
+ throw new ServiceException( role.toString(),
"Cannot add components to an initialized
CocoonServiceManager." );
}
try {
if( this.getLogger().isDebugEnabled() ) {
- this.getLogger().debug( "Attempting to get handler for role ["
+ role + "]" );
+ this.getLogger().debug( "Attempting to get handler for role ["
+ role.toString() + "]" );
}
- final AbstractComponentHandler handler = this.getComponentHandler(
role,
+ final AbstractComponentHandler handler = this.getComponentHandler(
role.toString(),
component,
configuration,
this);
@@ -510,12 +466,12 @@
this.getLogger().debug( "Handler type = " +
handler.getClass().getName() );
}
- this.componentHandlers.put( role, handler );
+ this.componentHandlers.put( role.toString(), handler );
this.newComponentHandlers.add( handler );
} catch ( final ServiceException se ) {
throw se;
} catch( final Exception e ) {
- throw new ServiceException( role, "Could not set up component
handler.", e );
+ throw new ServiceException( role.toString(), "Could not set up
component handler.", e );
}
// Note that at this point, we're not initialized and cannot do
// lookups, so defer parental introductions to initialize().
Modified:
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceSelector.java
==============================================================================
---
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceSelector.java
(original)
+++
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/CocoonServiceSelector.java
Tue Oct 19 12:52:11 2004
@@ -57,10 +57,6 @@
/** The default hint */
protected String defaultHint;
- /** This selector's location (used for debugging purposes) */
- private String location;
-
-
/** Create the ComponentSelector */
public CocoonServiceSelector() {
super(null);
@@ -92,7 +88,7 @@
// Retrieve the instance of the requested component
if( null == handler ) {
// Doesn't exist here : try in parent selector
- if ( this.parentSelector != null ) {
+ if ( this.parentSelector != null ) {
return this.parentSelector.select(hint);
}
final String message = this.roleName
@@ -223,8 +219,6 @@
*/
public void configure( final Configuration config )
throws ConfigurationException {
- // Store location
- this.location = config.getLocation();
this.roleName = getRoleName(config);
// Get default hint
@@ -268,25 +262,8 @@
getLogger().error(message);
throw new ConfigurationException(message);
}
-
- if (
"org.apache.cocoon.components.ExtendedComponentSelector".equals(className)) {
- className = CocoonServiceSelector.class.getName();
- } else if
("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector".equals(className)
) {
- className = ComponentsSelector.class.getName();
- }
-
- try {
- Class clazz = this.loader.loadClass(className);
- addComponent(hint, clazz, instance);
-
- } catch(Exception e) {
-
- String message = "Could not load class " + className + " for
component named '" +
- hint + "' at " + instance.getLocation();
-
- getLogger().error(message, e);
- throw new ConfigurationException(message, e);
- }
+
+ this.addComponent( className, hint, instance );
}
}
@@ -389,6 +366,8 @@
this.getLogger().debug(
"Adding " + component.getName() + " for hint [" +
hint.toString() + "]" );
}
+ } catch (ServiceException se) {
+ throw se;
} catch( final Exception e ) {
final String message =
"Could not set up Component for hint [ " + hint + "]";
@@ -444,12 +423,12 @@
*/
protected String getRoleName(Configuration config) {
// Get the role for this selector
- String roleName = config.getAttribute("role", null);
- if (roleName == null && this.roleManager != null) {
- roleName = this.roleManager.getRoleForName(config.getName());
+ String name = config.getAttribute("role", null);
+ if (name == null && this.roleManager != null) {
+ name = this.roleManager.getRoleForName(config.getName());
}
- return roleName;
+ return name;
}
/**
Modified:
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/RoleManager.java
==============================================================================
---
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/RoleManager.java
(original)
+++
cocoon/whiteboard/ecmplus/src/java/org/apache/cocoon/components/container/RoleManager.java
Tue Oct 19 12:52:11 2004
@@ -186,11 +186,6 @@
final String shortHand = hints[ j ].getAttribute(
"shorthand" ).trim();
String className = hints[ j ].getAttribute( "class"
).trim();
- if (
"org.apache.cocoon.components.ExtendedComponentSelector".equals(className)) {
- className = CocoonServiceSelector.class.getName();
- } else if
("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector".equals(className)
) {
- className = ComponentsSelector.class.getName();
- }
hintMap.put( shortHand, className );
if( getLogger().isDebugEnabled() ) {
getLogger().debug( "Adding hint type " + shortHand +