You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2003/07/11 16:17:44 UTC
cvs commit: cocoon-2.1/src/blocks/taglib/java/org/apache/cocoon/jxpath JXPathCocoonContexts.java
cziegeler 2003/07/11 07:17:44
Modified: src/java/org/apache/cocoon/components
RequestLifecycleComponent.java
CocoonComponentManager.java
src/blocks/taglib/java/org/apache/cocoon/jxpath
JXPathCocoonContexts.java
Added: src/java/org/apache/cocoon/components
GlobalRequestLifecycleComponent.java
Log:
Adding global request lifecycle component
Revision Changes Path
1.2 +16 -12 cocoon-2.1/src/java/org/apache/cocoon/components/RequestLifecycleComponent.java
Index: RequestLifecycleComponent.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/RequestLifecycleComponent.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestLifecycleComponent.java 9 Mar 2003 00:08:46 -0000 1.1
+++ RequestLifecycleComponent.java 11 Jul 2003 14:17:44 -0000 1.2
@@ -50,26 +50,30 @@
*/
package org.apache.cocoon.components;
+import java.io.IOException;
+import java.util.Map;
+
import org.apache.avalon.excalibur.pool.Poolable;
-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
- *
+ * Components implementing this marker interface have a lifecycle of one
+ * request. This means if during one request a component accepting this
+ * interface is looked up several times, it's always the same instance.
+ * Each internal subrequest, e.g. using the cocoon protocol, is considered
+ * as a new request. So an instance looked up in the "main" request is
+ * not available to a subrequest.
+ * In addition, the first time this component is looked up during a request,
+ * the {@link setup(SourceResolver, Map)} method is called.
+ *
+ * @see org.apache.cocoon.components.GlobalRequestLifecycleComponent
+ *
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
* @version CVS $Id$
*/
-public interface RequestLifecycleComponent extends Component, Poolable {
+public interface RequestLifecycleComponent extends Poolable {
/**
* Set the {@link SourceResolver} and the objectModel
1.15 +108 -15 cocoon-2.1/src/java/org/apache/cocoon/components/CocoonComponentManager.java
Index: CocoonComponentManager.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/CocoonComponentManager.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- CocoonComponentManager.java 10 Jul 2003 13:17:04 -0000 1.14
+++ CocoonComponentManager.java 11 Jul 2003 14:17:44 -0000 1.15
@@ -93,8 +93,7 @@
{
/** The key used to store the current process environment */
- private static final String PROCESS_KEY =
- "org.apache.cocoon.components.CocoonComponentManager";
+ private static final String PROCESS_KEY = CocoonComponentManager.class.getName();
/** The environment information */
@@ -164,7 +163,21 @@
*/
public static void leaveEnvironment() {
final EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
- stack.pop();
+ final Object[] objs = (Object[])stack.pop();
+ if ( stack.isEmpty() ) {
+ final Environment env = (Environment)objs[0];
+ final Map globalComponents = (Map)env.getAttribute(GlobalRequestLifecycleComponent.class.getName());
+ if ( globalComponents != null) {
+
+ final Iterator iter = globalComponents.values().iterator();
+ while ( iter.hasNext() ) {
+ final Object[] o = (Object[])iter.next();
+ final Component c = (Component)o[0];
+ ((CocoonComponentManager)o[1]).releaseRLComponent( c );
+ }
+ }
+ env.removeAttribute(GlobalRequestLifecycleComponent.class.getName());
+ }
}
/**
@@ -288,7 +301,11 @@
final Map objectModel = ((Environment)objects[0]).getObjectModel();
EnvironmentDescription desc = (EnvironmentDescription)objectModel.get(PROCESS_KEY);
if ( null != desc ) {
- final Component component = desc.getRequestLifecycleComponent(role);
+ Component component = desc.getRequestLifecycleComponent(role);
+ if (null != component) {
+ return component;
+ }
+ component = desc.getGlobalRequestLifecycleComponent(role);
if (null != component) {
return component;
}
@@ -321,6 +338,31 @@
}
}
+ if (null != component && 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 ) {
+
+ // first test if the parent CM has already initialized this component
+ if ( !desc.containsGlobalRequestLifecycleComponent( role ) ) {
+ try {
+ if (component instanceof Recomposable) {
+ ((Recomposable) component).recompose(this);
+ }
+ ((GlobalRequestLifecycleComponent) component).setup((org.apache.cocoon.environment.SourceResolver)objects[0],
+ objectModel);
+ } catch (Exception local) {
+ throw new ComponentException(role, "Exception during setup of RequestLifecycleComponent.", local);
+ }
+ desc.addGlobalRequestLifecycleComponent(role, component, this);
+ }
+ }
+ }
+
if ( null != component && component instanceof SitemapConfigurable) {
// FIXME: how can we prevent that this is called over and over again?
@@ -351,7 +393,8 @@
return;
}
- if ( component instanceof RequestLifecycleComponent) {
+ if ( component instanceof RequestLifecycleComponent
+ || component instanceof GlobalRequestLifecycleComponent) {
return;
}
if ( component == this ) {
@@ -514,9 +557,9 @@
Environment environment;
Map objectModel;
- Map requestLifecycleComponents = new HashMap(5);
- List autoreleaseComponents = new ArrayList(2);
-
+ Map requestLifecycleComponents;
+ List autoreleaseComponents = new ArrayList(4);
+
/**
* Constructor
*/
@@ -524,6 +567,15 @@
this.environment = env;
this.objectModel = env.getObjectModel();
}
+
+ Map getGlobalRequestLifcecycleComponents() {
+ Map m = (Map)environment.getAttribute(GlobalRequestLifecycleComponent.class.getName());
+ if ( m == null ) {
+ m = new HashMap();
+ environment.setAttribute(GlobalRequestLifecycleComponent.class.getName(), m);
+ }
+ return m;
+ }
/**
* Release all components of this environment
@@ -531,11 +583,14 @@
* released.
*/
void release() {
- final Iterator iter = this.requestLifecycleComponents.values().iterator();
- while (iter.hasNext()) {
- final Object[] o = (Object[])iter.next();
- final Component component = (Component)o[0];
- ((CocoonComponentManager)o[1]).releaseRLComponent( component );
+ if ( this.requestLifecycleComponents != null ) {
+ final Iterator iter = this.requestLifecycleComponents.values().iterator();
+ while (iter.hasNext()) {
+ final Object[] o = (Object[])iter.next();
+ final Component component = (Component)o[0];
+ ((CocoonComponentManager)o[1]).releaseRLComponent( component );
+ }
+ this.requestLifecycleComponents.clear();
}
for(int i = 0; i < autoreleaseComponents.size(); i++) {
@@ -550,7 +605,6 @@
}
}
}
- this.requestLifecycleComponents.clear();
this.autoreleaseComponents.clear();
this.environment = null;
this.objectModel = null;
@@ -563,18 +617,57 @@
void addRequestLifecycleComponent(final String role,
final Component co,
final ComponentManager manager) {
+ if ( this.requestLifecycleComponents == null ) {
+ this.requestLifecycleComponents = new HashMap();
+ }
this.requestLifecycleComponents.put(role, new Object[] {co, manager});
}
+ /**
+ * Add a GlobalRequestLifecycleComponent to the environment
+ */
+ void addGlobalRequestLifecycleComponent(final String role,
+ final Component co,
+ final ComponentManager manager) {
+ this.getGlobalRequestLifcecycleComponents().put(role, new Object[] {co, manager});
+ }
+
+ /**
+ * Do we already have a request lifecycle component
+ */
boolean containsRequestLifecycleComponent(final String role) {
+ if ( this.requestLifecycleComponents == null ) {
+ return false;
+ }
return this.requestLifecycleComponents.containsKey( role );
}
/**
+ * Do we already have a global request lifecycle component
+ */
+ boolean containsGlobalRequestLifecycleComponent(final String role) {
+ return this.getGlobalRequestLifcecycleComponents().containsKey( role );
+ }
+
+ /**
* Search a RequestLifecycleComponent
*/
Component getRequestLifecycleComponent(final String role) {
+ if ( this.requestLifecycleComponents == null ) {
+ return null;
+ }
final Object[] o = (Object[])this.requestLifecycleComponents.get(role);
+ if ( null != o ) {
+ return (Component)o[0];
+ }
+ return null;
+ }
+
+ /**
+ * Search a GlobalRequestLifecycleComponent
+ */
+ Component getGlobalRequestLifecycleComponent(final String role) {
+ final Object[] o = (Object[])this.getGlobalRequestLifcecycleComponents().get(role);
if ( null != o ) {
return (Component)o[0];
}
1.1 cocoon-2.1/src/java/org/apache/cocoon/components/GlobalRequestLifecycleComponent.java
Index: GlobalRequestLifecycleComponent.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components;
import java.io.IOException;
import java.util.Map;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.xml.sax.SAXException;
/**
* Components implementing this marker interface have a lifecycle of one
* request-response cycle. This means if during one cycle a component accepting this
* interface is looked up several times, it's always the same instance.
* Each internal subrequest happens in the same cycle, so an instance looked up in
* either the "main" request or in any of the subrequests is available to all
* other requests in this cycle.
* In addition, the first time this component is looked up during a request,
* the {@link setup(SourceResolver, Map)} method is called.
*
* @see org.apache.cocoon.components.RequestLifecycleComponent
*
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
* @version CVS $Id: GlobalRequestLifecycleComponent.java,v 1.1 2003/07/11 14:17:44 cziegeler Exp $
*/
public interface GlobalRequestLifecycleComponent {
/**
* Set the {@link SourceResolver} and the objectModel
* used to process the current request.
*/
void setup(SourceResolver resolver, Map objectModel)
throws ProcessingException, SAXException, IOException;
}
1.3 +3 -2 cocoon-2.1/src/blocks/taglib/java/org/apache/cocoon/jxpath/JXPathCocoonContexts.java
Index: JXPathCocoonContexts.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/taglib/java/org/apache/cocoon/jxpath/JXPathCocoonContexts.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JXPathCocoonContexts.java 16 Mar 2003 17:49:08 -0000 1.2
+++ JXPathCocoonContexts.java 11 Jul 2003 14:17:44 -0000 1.3
@@ -54,6 +54,7 @@
import java.util.Map;
import org.apache.avalon.excalibur.pool.Recyclable;
+import org.apache.avalon.framework.component.Component;
import org.apache.cocoon.components.RequestLifecycleComponent;
import org.apache.cocoon.environment.Context;
import org.apache.cocoon.environment.ObjectModelHelper;
@@ -95,7 +96,7 @@
* @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
* @version CVS $Id$
*/
-public final class JXPathCocoonContexts implements RequestLifecycleComponent, Recyclable {
+public final class JXPathCocoonContexts implements RequestLifecycleComponent, Component, Recyclable {
public static final String ROLE = JXPathCocoonContexts.class.getName();
private static JXPathContextFactory factory;
private Map objectModel;