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/13 18:37:36 UTC
svn commit: r421663 - in
/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing: ./
impl/ pipeline/ sitemap/ sitemap/impl/
Author: cziegeler
Date: Thu Jul 13 09:37:35 2006
New Revision: 421663
URL: http://svn.apache.org/viewvc?rev=421663&view=rev
Log:
New implementation for mountable and sitemap processor
Added:
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java (with props)
Removed:
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Consumer.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/SitemapProcessorFactory.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessorFactoryImpl.java
Modified:
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/Processor.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponse.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/impl/MountTableProcessorImpl.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Producer.java
cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessor.java
Added: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java?rev=421663&view=auto
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java (added)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java Thu Jul 13 09:37:35 2006
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.processing;
+
+
+/**
+ * @version $Id$
+ * @since 2.2
+ */
+public interface MountableProcessor extends Processor {
+
+ String getMountUri();
+
+ boolean stripPrefix();
+}
Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/MountableProcessor.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/Processor.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/Processor.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/Processor.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/Processor.java Thu Jul 13 09:37:35 2006
@@ -20,10 +20,10 @@
/**
* @version $Id$
+ * @since 2.2
*/
public interface Processor {
boolean process(HttpServletRequest request, HttpServletResponse response)
throws Exception;
-
}
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponse.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponse.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponse.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponse.java Thu Jul 13 09:37:35 2006
@@ -18,7 +18,6 @@
import javax.servlet.http.HttpServletResponse;
import org.xml.sax.ContentHandler;
-import org.xml.sax.ext.LexicalHandler;
/**
* This interface makrs a {@link HttpServletResponse} as SAX aware. If the
@@ -27,19 +26,15 @@
* of the response.
*
* @version $Id$
+ * @since 2.2
*/
public interface SAXAwareHttpServletResponse extends HttpServletResponse {
/**
* The {@link ContentHandler} receiving the events. This method must not
- * return null.
+ * return null. The processor should test if the returned handler also
+ * implements the {@link org.xml.sax.ext.LexicalHandler} interface.
* @return The content handler.
*/
ContentHandler getContentHandler();
-
- /**
- * The optional {@link LexicalHandler} receiving events.
- * @return The lexical handler or null.
- */
- LexicalHandler getLexicalHandler();
}
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java Thu Jul 13 09:37:35 2006
@@ -18,37 +18,42 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
+import org.apache.cocoon.xml.ContentHandlerWrapper;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;
/**
- *
+ * A response wrapper implementing the {@link SAXAwareHttpServletResponse}.
+ *
* @version $Id$
+ * @since 2.2
*/
public class SAXAwareHttpServletResponseWrapper
extends HttpServletResponseWrapper
implements SAXAwareHttpServletResponse {
- protected final LexicalHandler lexicalHandler;
protected final ContentHandler handler;
- public SAXAwareHttpServletResponseWrapper(HttpServletResponse response, ContentHandler handler, LexicalHandler lexicalHandler) {
+ public SAXAwareHttpServletResponseWrapper(HttpServletResponse response, ContentHandler handler) {
super(response);
- this.lexicalHandler = lexicalHandler;
this.handler = handler;
}
+ public SAXAwareHttpServletResponseWrapper(HttpServletResponse response,
+ ContentHandler handler,
+ LexicalHandler lexicalHandler) {
+ super(response);
+ if ( handler != lexicalHandler ) {
+ this.handler = new ContentHandlerWrapper(handler, lexicalHandler);
+ } else {
+ this.handler = handler;
+ }
+ }
+
/**
* @see org.apache.cocoon.processing.SAXAwareHttpServletResponse#getContentHandler()
*/
public ContentHandler getContentHandler() {
return this.handler;
- }
-
- /**
- * @see org.apache.cocoon.processing.SAXAwareHttpServletResponse#getLexicalHandler()
- */
- public LexicalHandler getLexicalHandler() {
- return this.lexicalHandler;
}
}
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/impl/MountTableProcessorImpl.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/impl/MountTableProcessorImpl.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/impl/MountTableProcessorImpl.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/impl/MountTableProcessorImpl.java Thu Jul 13 09:37:35 2006
@@ -18,66 +18,58 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.cocoon.components.source.SourceUtil;
-import org.apache.cocoon.configuration.Settings;
-import org.apache.cocoon.core.container.util.ConfigurationBuilder;
+import org.apache.cocoon.processing.MountableProcessor;
import org.apache.cocoon.processing.Processor;
-import org.apache.cocoon.processing.sitemap.SitemapProcessorFactory;
import org.apache.cocoon.servlet.RequestUtil;
-import org.apache.excalibur.source.Source;
-import org.apache.excalibur.source.SourceResolver;
-import org.apache.excalibur.source.SourceValidity;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.BeanFactoryUtils;
+import org.springframework.beans.factory.ListableBeanFactory;
/**
- * A processor that manages a "mount table", allowing to add sitemaps to a Cocoon application going
- * thorugh the main sitemap.
- *
- * <p>
- * The mount table is an xml file which has the following syntax:
- * <pre>
- * <mount-table>
- * <mount uri-prefix="foo" src="file://path/to/foo/directory/"/>
- * <mount uri-prefix="bar/baz" src="file://path/to/bar-baz/directory/"/>
- * </mount-table>
- * </pre>
- * The processor will scan the mount table for an "uri-prefix" value matching the beginning of the current
- * request URI, and if found, forward to a sitemap processor managing the sitemap defined by src.
+ * A processor that manages a "mount table", allowing to add sitemaps to a Cocoon application
+ * without going through the main sitemap.
*
* @version $Id$
+ * @since 2.2
*/
-public class MountTableProcessorImpl implements Processor {
-
- /** Source resolver to resolve the configuration file. */
- protected SourceResolver resolver;
-
- /** Location of the mount table. */
- protected String mountTableLocation;
-
- protected SourceValidity mountTableValidity;
+public class MountTableProcessorImpl
+ implements Processor, BeanFactoryAware {
- /** Mounttable */
- protected List mountEntries = new ArrayList();
-
- /** Settings. */
- protected Settings settings;
-
- protected SitemapProcessorFactory sitemapProcessorFactory;
-
- public void setSourceResolver(SourceResolver resolver) {
- this.resolver = resolver;
- }
+ /** All registered mountable processors. */
+ protected List mountableProcessors;
- public void setSettings(Settings settings) {
- this.settings = settings;
+ protected ListableBeanFactory beanFactory;
+
+ protected void lookupMountableProcessors() {
+ if ( this.mountableProcessors == null ) {
+ synchronized ( this ) {
+ if ( this.mountableProcessors == null ) {
+ final List processors = new ArrayList();
+ // the returned map contains the bean names as key and the beans as values
+ final Map mountedBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, MountableProcessor.class);
+ final Iterator i = mountedBeans.values().iterator();
+ while ( i.hasNext() ) {
+ final MountableProcessor processor = (MountableProcessor) i.next();
+ processors.add(processor);
+ }
+ this.mountableProcessors = processors;
+ }
+ }
+ }
}
- public void setSitemapProcessorFactory(SitemapProcessorFactory factory) {
- this.sitemapProcessorFactory = factory;
+ /**
+ * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
+ */
+ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+ this.beanFactory = (ListableBeanFactory)beanFactory;
}
/**
@@ -91,100 +83,30 @@
return true;
}
// and search for a matching prefix
- Iterator iter = this.mountEntries.iterator();
+ Iterator iter = this.mountableProcessors.iterator();
while (iter.hasNext()) {
- final MountTableEntry entry = (MountTableEntry) iter.next();
- if (uri.startsWith(entry.uriPrefix)) {
+ final MountableProcessor entry = (MountableProcessor) iter.next();
+
+ if (uri.startsWith(entry.getMountUri())) {
// Found it
// if they are equal, issue a redirect
- if ( uri.length() == entry.uriPrefix.length() ) {
+ if ( uri.length() == entry.getMountUri().length() ) {
response.sendRedirect(uri + '/');
return true;
}
// check for '/' following the prefix, otherwise we don't match
- if ( uri.charAt(entry.uriPrefix.length()) == '/' ) {
+ if ( uri.charAt(entry.getMountUri().length()) == '/' ) {
// ok, now we are sure
- if ( entry.stripPrefix ) {
- request = RequestUtil.createRequestForUri(request, uri.substring(entry.uriPrefix.length()+1));
+ HttpServletRequest req = request;
+ if ( entry.stripPrefix() ) {
+ req = RequestUtil.createRequestForUri(request, uri.substring(entry.getMountUri().length()+1));
}
- final Processor sitemapProcessor = this.sitemapProcessorFactory.getSitemapProcessor(entry.src);
- if ( sitemapProcessor != null ) {
- return sitemapProcessor.process(request, response);
- }
- return false;
+ return entry.process(req, response);
}
}
}
// Not found
return false;
- }
-
- protected void loadMountTable()
- throws Exception {
- Source source = null;
- try {
- source = this.resolver.resolveURI(this.mountTableLocation);
-
- if ( this.mountEntries != null ) {
- // Check validity
-
- int valid = this.mountTableValidity != null ? this.mountTableValidity.isValid() : SourceValidity.INVALID;
- if (valid == SourceValidity.VALID) {
- // Valid without needing the new validity
- return;
- }
-
- if (valid == SourceValidity.UNKNOWN &&
- this.mountTableValidity.isValid(source.getValidity()) == SourceValidity.VALID) {
- // Valid after comparing with the new validity
- // update validity
- this.mountTableValidity = source.getValidity();
- return;
- }
- this.mountEntries = null;
- // Invalid: fallback below to read the mount table
- }
-
- synchronized (this) {
- if ( this.mountEntries != null ) {
- // Read the mount table
- final List mounts = new ArrayList();
- ConfigurationBuilder builder = new ConfigurationBuilder(this.settings);
- Configuration config = builder.build(SourceUtil.getInputSource(source));
-
- Configuration[] children = config.getChildren();
- for (int i = 0; i < children.length; i++) {
- Configuration child = children[i];
- if ("mount".equals(child.getName())) {
- String prefix = children[i].getAttribute("uri-prefix");
- // remove a trailing '/'
- // Append a '/' at the end of a not-empty prefix
- if ( prefix.endsWith("/") ) {
- prefix = prefix.substring(0, prefix.length() - 1);
- }
- final MountTableEntry entry = new MountTableEntry();
- entry.src = children[i].getAttribute("src");
- entry.uriPrefix = prefix;
- entry.stripPrefix = children[i].getAttributeAsBoolean("strip-uri-prefix", true);
- mounts.add(entry);
- } else {
- throw new Exception(
- "Unexpected element '" + child.getName() + "' (awaiting 'mount'), at " + child.getLocation());
- }
- }
- this.mountEntries = mounts;
- this.mountTableValidity = source.getValidity();
- }
- }
- } finally {
- this.resolver.release(source);
- }
- }
-
- public static class MountTableEntry {
- public String uriPrefix;
- public String src;
- public boolean stripPrefix;
}
}
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Producer.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Producer.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Producer.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/pipeline/Producer.java Thu Jul 13 09:37:35 2006
@@ -15,6 +15,8 @@
*/
package org.apache.cocoon.processing.pipeline;
+import org.xml.sax.ContentHandler;
+
/**
* This interfaces identifies classes that produce XML data, sending SAX
* events to the configured <code>XMLConsumer</code>.
@@ -39,6 +41,7 @@
* </p>
*
* @version $Id$
+ * @since 2.2
*/
public interface Producer {
@@ -47,5 +50,5 @@
*
* @param consumer The Consumer target for SAX events.
*/
- void setConsumer(Consumer consumer);
+ void setContentHandler(ContentHandler handler);
}
Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessor.java?rev=421663&r1=421662&r2=421663&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessor.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/sitemap/impl/SitemapProcessor.java Thu Jul 13 09:37:35 2006
@@ -19,22 +19,140 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ProcessingUtil;
+import org.apache.cocoon.components.treeprocessor.TreeProcessor;
import org.apache.cocoon.processing.Processor;
import org.apache.cocoon.servlet.RequestProcessor;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.web.context.ServletContextAware;
-public class SitemapProcessor implements Processor {
+/**
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public class SitemapProcessor
+ implements Processor,
+ BeanFactoryAware,
+ DisposableBean,
+ ServletContextAware {
- protected final RequestProcessor requestProcessor;
+ protected String sitemapLocation;
- public SitemapProcessor(ServletContext servletContext, org.apache.cocoon.Processor treeProcessor) {
+ protected String uriPrefix;
+
+ /** The request processor. */
+ protected RequestProcessor requestProcessor;
+
+ protected SourceResolver resolver;
+
+ protected Logger logger;
+
+ protected Context context;
+
+ protected BeanFactory beanFactory;
+
+ protected ServiceManager manager;
+
+ protected TreeProcessor treeProcessor;
+
+ public void setSourceResolver(SourceResolver resolver) {
+ this.resolver = resolver;
+ }
+
+ public String getSitemapLocation() {
+ return sitemapLocation;
+ }
+
+ public void setSitemapLocation(String sitemapLocation) {
+ this.sitemapLocation = sitemapLocation;
+ }
+
+ public String getUriPrefix() {
+ return uriPrefix;
+ }
+
+ public void setUriPrefix(String uriPrefix) {
+ this.uriPrefix = uriPrefix;
+ }
+
+ /**
+ *
+ */
+ protected void createSitemapProcessor() throws Exception {
+ if ( this.treeProcessor == null ) {
+ synchronized ( this ) {
+ if ( this.treeProcessor == null ) {
+ Source source = null;
+ try {
+ source = this.resolver.resolveURI(sitemapLocation);
+ final String uri = source.getURI();
+ // create a configuration object
+ DefaultConfiguration rootConfiguration = new DefaultConfiguration("component");
+ // rootConfiguration.setAttribute("check-reload", false);
+ //DefaultConfiguration reloadConfig = new DefaultConfiguration("reload");
+ //reloadConfig.setAttribute("delay", 1000L);
+ //rootConfiguration.addChild(reloadConfig);
+ rootConfiguration.setAttribute("file", uri);
+
+ final TreeProcessor tp = new TreeProcessor();
+ tp.setBeanFactory(this.beanFactory);
+ tp.enableLogging(this.logger);
+ tp.contextualize(this.context);
+ tp.service(this.manager);
+ tp.configure(rootConfiguration);
+ tp.initialize();
+ this.requestProcessor.setProcessor(tp);
+
+ this.treeProcessor = tp;
+ } finally {
+ this.resolver.release(source);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
+ */
+ public void setBeanFactory(BeanFactory factory) throws BeansException {
+ this.beanFactory = factory;
+ this.logger = (Logger)this.beanFactory.getBean(ProcessingUtil.LOGGER_ROLE);
+ this.context = (Context)this.beanFactory.getBean(ProcessingUtil.CONTEXT_ROLE);
+ this.manager = (ServiceManager)this.beanFactory.getBean(ProcessingUtil.SERVICE_MANAGER_ROLE);
+ }
+
+ /**
+ * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
+ */
+ public void setServletContext(ServletContext servletContext) {
this.requestProcessor = new RequestProcessor(servletContext);
- this.requestProcessor.setProcessor(treeProcessor);
+ }
+
+ /**
+ * @see org.springframework.beans.factory.DisposableBean#destroy()
+ */
+ public void destroy() throws Exception {
+ if ( this.treeProcessor != null ) {
+ this.treeProcessor.dispose();
+ }
}
/**
* @see org.apache.cocoon.processing.Processor#process(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public boolean process(HttpServletRequest request, HttpServletResponse response) throws Exception {
+ this.createSitemapProcessor();
this.requestProcessor.service(request, response);
return true;
}