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 2006/07/25 21:27:54 UTC
svn commit: r425490 - in /cocoon/trunk/core/cocoon-core/src:
main/java/org/apache/cocoon/core/container/spring/
test/java/org/apache/cocoon/core/container/
Author: cziegeler
Date: Tue Jul 25 12:27:52 2006
New Revision: 425490
URL: http://svn.apache.org/viewvc?rev=425490&view=rev
Log:
Improved Avalon ConfigReader which first reads in all roles before processing component definitions.
This should minimize the need for includes between blocks as dependencies should be on roles and not components.
Add support for ServletContextAware to our own spring bean factory.
Modified:
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonBeanFactory.java
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ConfigReader.java
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonBeanFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonBeanFactory.java?rev=425490&r1=425489&r2=425490&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonBeanFactory.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/CocoonBeanFactory.java Tue Jul 25 12:27:52 2006
@@ -21,6 +21,8 @@
import java.util.List;
import java.util.Map;
+import javax.servlet.ServletContext;
+
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
@@ -41,6 +43,9 @@
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.OrderComparator;
import org.springframework.core.io.Resource;
+import org.springframework.web.context.ServletContextAware;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.ServletContextAwareProcessor;
/**
* This is a Cocoon specific implementation of a Spring {@link DefaultListableBeanFactory}.
@@ -61,6 +66,7 @@
protected final Logger avalonLogger;
protected final Context avalonContext;
protected final ConfigurationInfo avalonConfiguration;
+ protected final ServletContext servletContext;
public CocoonBeanFactory(BeanFactory parent) {
this(null, parent, null, null, null, null);
@@ -73,6 +79,19 @@
Context avalonContext,
Settings settings) {
super(parent);
+ // try to get servlet context
+ if ( parent instanceof WebApplicationContext ) {
+ this.servletContext = ((WebApplicationContext)parent).getServletContext();
+ } else if ( parent instanceof CocoonBeanFactory ) {
+ this.servletContext = ((CocoonBeanFactory)parent).servletContext;
+ } else {
+ this.servletContext = null;
+ }
+ // add support for ServletContextAware
+ if ( this.servletContext != null ) {
+ this.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
+ this.ignoreDependencyInterface(ServletContextAware.class);
+ }
this.avalonResource = avalonResource;
this.avalonLogger = avalonLogger;
this.avalonConfiguration = avalonConfiguration;
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ConfigReader.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ConfigReader.java?rev=425490&r1=425489&r2=425490&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ConfigReader.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/ConfigReader.java Tue Jul 25 12:27:52 2006
@@ -17,10 +17,12 @@
package org.apache.cocoon.core.container.spring;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -56,6 +58,9 @@
/** Avalon environment. */
protected final AvalonEnvironment environment;
+ /** All component configurations. */
+ protected final List componentConfigs = new ArrayList();
+
public static ConfigurationInfo readConfiguration(String source, AvalonEnvironment env)
throws Exception {
final ConfigReader converter = new ConfigReader(env, null, null);
@@ -179,6 +184,9 @@
}
}
+ // now process all component configurations
+ this.processComponents();
+
// add roles as components
final Iterator i = this.configInfo.getClassNames().values().iterator();
while ( i.hasNext() ) {
@@ -206,88 +214,99 @@
} else if ( "include-beans".equals(componentName) ) {
this.handleBeanInclude(contextURI, componentConfig);
} else {
- // Component declaration
- // Find the role
- String role = componentConfig.getAttribute("role", null);
- String alias = null;
- if (role == null) {
- // Get the role from the role manager if not explicitely specified
- role = (String)this.configInfo.getShorthands().get( componentName );
- alias = componentName;
- if (role == null) {
- // Unknown role
- throw new ConfigurationException("Unknown component type '" + componentName +
- "' at " + componentConfig.getLocation());
- }
- }
+ // Component declaration, add it to list
+ this.componentConfigs.add(componentConfig);
+ }
+ }
+ }
- // Find the className
- String className = componentConfig.getAttribute("class", null);
- ComponentInfo info;
- if (className == null) {
- // Get the default class name for this role
- info = (ComponentInfo)this.configInfo.getClassNames().get( role );
- if (info == null) {
- throw new ConfigurationException("Cannot find a class for role " + role + " at " + componentConfig.getLocation());
- }
- this.configInfo.getClassNames().remove(info);
- className = info.getComponentClassName();
- } else {
- info = new ComponentInfo();
- }
- // If it has a "name" attribute, add it to the role (similar to the
- // declaration within a service selector)
- // Note: this has to be done *after* finding the className above as we change the role
- String name = componentConfig.getAttribute("name", null);
- if (name != null) {
- role = role + "/" + name;
+ protected void processComponents()
+ throws ConfigurationException {
+ final Iterator i = this.componentConfigs.iterator();
+ while ( i.hasNext() ) {
+ final Configuration componentConfig = (Configuration)i.next();
+ final String componentName = componentConfig.getName();
+
+ // Find the role
+ String role = componentConfig.getAttribute("role", null);
+ String alias = null;
+ if (role == null) {
+ // Get the role from the role manager if not explicitely specified
+ role = (String)this.configInfo.getShorthands().get( componentName );
+ alias = componentName;
+ if (role == null) {
+ // Unknown role
+ throw new ConfigurationException("Unknown component type '" + componentName +
+ "' at " + componentConfig.getLocation());
}
- info.fill(componentConfig);
- info.setComponentClassName(className);
- info.setRole(role);
- if ( alias != null ) {
- info.setAlias(alias);
+ }
+
+ // Find the className
+ String className = componentConfig.getAttribute("class", null);
+ ComponentInfo info;
+ if (className == null) {
+ // Get the default class name for this role
+ info = (ComponentInfo)this.configInfo.getClassNames().get( role );
+ if (info == null) {
+ throw new ConfigurationException("Cannot find a class for role " + role + " at " + componentConfig.getLocation());
}
- info.setConfiguration(componentConfig);
-
- this.configInfo.addComponent(info);
- // now if this is a selector, then we have to register the single components
- if ( info.getConfiguration() != null && className.endsWith("Selector") ) {
- String classAttribute = null;
- if ( className.equals("org.apache.cocoon.core.container.DefaultServiceSelector") ) {
- classAttribute = "class";
- } else if (className.equals("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector") ) {
- classAttribute = "src";
- }
- if ( classAttribute == null ) {
- this.getLogger().warn("Found unknown selector type (continuing anyway: " + className);
- } else {
- String componentRole = role;
- if ( componentRole.endsWith("Selector") ) {
- componentRole = componentRole.substring(0, componentRole.length() - 8);
- }
- componentRole += '/';
- Configuration[] children = info.getConfiguration().getChildren();
- final Map hintConfigs = (Map)this.configInfo.getKeyClassNames().get(role);
- for (int j=0; j<children.length; j++) {
- final Configuration current = children[j];
- final ComponentInfo childInfo = new ComponentInfo();
- childInfo.fill(current);
- childInfo.setConfiguration(current);
- final ComponentInfo hintInfo = (hintConfigs == null ? null : (ComponentInfo)hintConfigs.get(current.getName()));
- if ( current.getAttribute(classAttribute, null ) != null
- || hintInfo == null ) {
- childInfo.setComponentClassName(current.getAttribute(classAttribute));
- } else {
- childInfo.setComponentClassName(hintInfo.getComponentClassName());
- }
- childInfo.setRole(componentRole + current.getAttribute("name"));
- this.configInfo.addComponent(childInfo);
+ this.configInfo.getClassNames().remove(info);
+ className = info.getComponentClassName();
+ } else {
+ info = new ComponentInfo();
+ }
+ // If it has a "name" attribute, add it to the role (similar to the
+ // declaration within a service selector)
+ // Note: this has to be done *after* finding the className above as we change the role
+ String name = componentConfig.getAttribute("name", null);
+ if (name != null) {
+ role = role + "/" + name;
+ }
+ info.fill(componentConfig);
+ info.setComponentClassName(className);
+ info.setRole(role);
+ if ( alias != null ) {
+ info.setAlias(alias);
+ }
+ info.setConfiguration(componentConfig);
+
+ this.configInfo.addComponent(info);
+ // now if this is a selector, then we have to register the single components
+ if ( info.getConfiguration() != null && className.endsWith("Selector") ) {
+ String classAttribute = null;
+ if ( className.equals("org.apache.cocoon.core.container.DefaultServiceSelector") ) {
+ classAttribute = "class";
+ } else if (className.equals("org.apache.cocoon.components.treeprocessor.sitemap.ComponentsSelector") ) {
+ classAttribute = "src";
+ }
+ if ( classAttribute == null ) {
+ this.getLogger().warn("Found unknown selector type (continuing anyway: " + className);
+ } else {
+ String componentRole = role;
+ if ( componentRole.endsWith("Selector") ) {
+ componentRole = componentRole.substring(0, componentRole.length() - 8);
+ }
+ componentRole += '/';
+ Configuration[] children = info.getConfiguration().getChildren();
+ final Map hintConfigs = (Map)this.configInfo.getKeyClassNames().get(role);
+ for (int j=0; j<children.length; j++) {
+ final Configuration current = children[j];
+ final ComponentInfo childInfo = new ComponentInfo();
+ childInfo.fill(current);
+ childInfo.setConfiguration(current);
+ final ComponentInfo hintInfo = (hintConfigs == null ? null : (ComponentInfo)hintConfigs.get(current.getName()));
+ if ( current.getAttribute(classAttribute, null ) != null
+ || hintInfo == null ) {
+ childInfo.setComponentClassName(current.getAttribute(classAttribute));
+ } else {
+ childInfo.setComponentClassName(hintInfo.getComponentClassName());
}
+ childInfo.setRole(componentRole + current.getAttribute("name"));
+ this.configInfo.addComponent(childInfo);
}
}
}
- }
+ }
}
protected void handleInclude(final String contextURI,
@@ -367,7 +386,7 @@
loadedURIs.add(uri);
// what is it?
- String includeKind = includeConfig.getName();
+ final String includeKind = includeConfig.getName();
if (includeKind.equals("components")) {
// more components
this.parseConfiguration(includeConfig, uri, loadedURIs);
Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java?rev=425490&r1=425489&r2=425490&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java (original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java Tue Jul 25 12:27:52 2006
@@ -293,7 +293,6 @@
avalonEnv.logger = this.logger;
avalonEnv.context = this.context;
avalonEnv.settings = new MutableSettings("test");
-
// read roles and components
ConfigurationInfo rolesInfo = ConfigReader.readConfiguration(confRM, confCM, null, avalonEnv, null);
this.addComponents( rolesInfo );