cziegeler 01/12/17 06:15:12
Added: scratchpad/src/org/apache/cocoon/components
CocoonComponentManager.java
RequestLifecycleComponent.java
Log:
Extended component manager to support the request lifecycle components - as
discussed several weeks ago...
Revision Changes Path
1.1
xml-cocoon2/scratchpad/src/org/apache/cocoon/components/CocoonComponentManager.java
Index: CocoonComponentManager.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.cocoon.components;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
import org.apache.cocoon.environment.SourceResolver;
/**
* Cocoon Component Manager.
* This manager extends the <code>ExcaliburComponentManager</code>
* by a special lifecycle handling for RequestLifecycleComponent.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/12/17 14:15:12 $
*/
public final class CocoonComponentManager
extends ExcaliburComponentManager
{
/** The environment information */
private InheritableThreadLocal environmentStack = new InheritableThreadLocal();
/** Create the ComponentManager */
public CocoonComponentManager()
{
super();
}
/** Create the ComponentManager with a Classloader */
public CocoonComponentManager( final ClassLoader loader )
{
super(loader);
}
/** Create the ComponentManager with a Classloader and parent ComponentManager */
public CocoonComponentManager( final ComponentManager manager, final ClassLoader
loader )
{
super(manager, loader);
}
/** Create the ComponentManager with a parent ComponentManager */
public CocoonComponentManager(final ComponentManager manager)
{
super(manager);
}
public void enterEnvironment(SourceResolver resolver, Map objectModel) {
System.out.println("Entering environment");
if (this.environmentStack.get() == null) {
this.environmentStack.set(new Stack());
}
final Stack stack = (Stack)this.environmentStack.get();
stack.push(new Object[] {resolver, objectModel, new HashMap(5)});
}
public void leaveEnvironment() {
System.out.println("Leaving environment");
final Stack stack = (Stack)this.environmentStack.get();
if (stack != null) {
final Object[] objects = (Object[])stack.pop();
final Map components = (Map)objects[2];
final Iterator iter = components.values().iterator();
while (iter.hasNext() == true) {
final Component component = (Component)iter.next();
System.out.println("RELEASING COMPONENT: " + component);
super.release( component );
}
}
}
/**
* Return an instance of a component based on a Role. The Role is usually the
Interface's
* Fully Qualified Name(FQN)--unless there are multiple Components for the same
Role. In that
* case, the Role's FQN is appended with "Selector", and we return a
ComponentSelector.
*/
public Component lookup( final String role )
throws ComponentException {
final Stack stack = (Stack)this.environmentStack.get();
if ( stack != null ) {
final Object[] objects = (Object[])stack.pop();
final Map components = (Map)objects[2];
if ( components != null ) {
final Component component = (Component) components.get(role);
if ( component != null) {
System.out.println("Looked up RequestLifecycleComponent again: "
+ role);
return component;
}
}
}
final Component component = super.lookup( role );
if ( component != null && component instanceof RequestLifecycleComponent) {
if ( stack == null) {
throw new ComponentException("ComponentManager has no Environment
Stack");
}
final Object[] objects = (Object[]) stack.pop();
final Map components = (Map)objects[2];
if ( components == null ) {
throw new ComponentException("ComponentManager has no Environment
Stack");
}
components.put( role, component);
try {
((RequestLifecycleComponent)
component).setup((SourceResolver)objects[0], (Map)objects[1]);
} catch (Exception local) {
throw new ComponentException("Exception during setup of
RequestLifecycleComponent with role '"+role+"'", local);
}
System.out.println("New RequestModelComponent :" + component);
}
return component;
}
/**
* 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 )
{
return;
}
if ( component instanceof RequestLifecycleComponent) {
System.out.println("NOT RELEASING COMPONENT: " + component);
return;
}
super.release( component);
}
}
1.1
xml-cocoon2/scratchpad/src/org/apache/cocoon/components/RequestLifecycleComponent.java
Index: RequestLifecycleComponent.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.cocoon.components;
import org.apache.avalon.framework.component.Component;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.Map;
/**
* Objects implementing this marker interface have a lifecycle of one
* request. This means if one request is looking up several times
* an object implementing this interface, it's always the same object.
* In addition, the first time this object is looked up during a request,
* the setup() method is called
*
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/12/17 14:15:12 $
*/
public interface RequestLifecycleComponent extends Component {
/**
* Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
* used to process the request.
*/
void setup(SourceResolver resolver, Map objectModel)
throws ProcessingException, SAXException, IOException;
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]