/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 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 <stefano@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.treeprocessor;
import org.apache.avalon.excalibur.component.RoleManageable;
import org.apache.avalon.excalibur.component.RoleManager;
import org.apache.avalon.excalibur.logger.LogKitManageable;
import org.apache.avalon.excalibur.logger.LogKitManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
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.framework.component.ComponentSelector;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.component.Recomposable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.NamespacedSAXConfigurationHandler;
import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.Processor;
import org.apache.cocoon.components.CocoonComponentManager;
import org.apache.cocoon.components.ExtendedComponentSelector;
import org.apache.cocoon.components.LifecycleHelper;
import org.apache.cocoon.components.pipeline.EventPipeline;
import org.apache.cocoon.components.pipeline.StreamPipeline;
import org.apache.cocoon.components.source.CocoonSourceFactory;
import org.apache.cocoon.components.source.DelayedRefreshSourceWrapper;
import org.apache.cocoon.components.source.SourceHandler;
import org.apache.cocoon.components.source.URLSource;
import org.apache.cocoon.components.url.URLFactory;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.Source;
import java.io.InputStream;
import java.util.*;
/**
* Interpreted tree-traversal implementation of a pipeline assembly language.
*
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @version CVS $Id: TreeProcessor.java,v 1.4.2.2 2002/06/09 00:23:26 vgritsenko Exp $
*/
public class TreeProcessor extends AbstractLoggable implements ThreadSafe, Processor,
Composable, Configurable, LogKitManageable, RoleManageable, Initializable, Contextualizable, Disposable {
private static final String XCONF_URL =
"resource://org/apache/cocoon/components/treeprocessor/treeprocessor-builtins.xml";
/** The parent TreeProcessor, if any */
protected TreeProcessor parent;
/** The context */
protected Context context;
/** The component manager */
protected ComponentManager manager;
/** The logkit manager to get Loggers */
protected LogKitManager logKit;
/** The role manager */
protected RoleManager roleManager;
/** The language used by this processor */
protected String language;
/** Selector of TreeBuilders, the hint is the language name */
protected ExtendedComponentSelector builderSelector;
/** The root node of the processing tree */
protected ProcessingNode rootNode;
/** The list of processing nodes that should be disposed when disposing this processor */
protected List disposableNodes;
/** Last modification time */
protected long lastModified = 0;
/** The source of the tree definition */
protected Source source;
/** Delay for <code>sourceLastModified</code>. */
protected long lastModifiedDelay;
/** The current language configuration */
protected Configuration currentLanguage;
protected SourceHandler sourceHandler;
/**
* Create a TreeProcessor.
*/
public TreeProcessor() {
// Language can be overriden in the configuration.
this.language = "sitemap";
}
/**
* Create a child processor for a given language
*/
protected TreeProcessor(TreeProcessor parent, ComponentManager manager, String language) {
this.parent = parent;
this.language = (language == null) ? parent.language : language;
// Copy all that can be copied from the parent
this.context = parent.context;
this.logKit = parent.logKit;
this.builderSelector = parent.builderSelector;
// We have our own CM
this.manager = manager;
// Other fields are setup in initialize()
}
/**
* Create a new child of this processor (used for mounting submaps).
*
* @param manager the component manager to be used by the child processor.
* @param language the language to be used by the child processor.
* @return a new child processor.
*/
public TreeProcessor createChildProcessor(
ComponentManager manager,
String language,
Source source)
throws Exception {
TreeProcessor child = new TreeProcessor(this, manager, language);
child.setLogger(getLogger());
child.initialize();
// FIXME : make delay configurable
child.source = new DelayedRefreshSourceWrapper(source, 1000L);
return child;
}
public void contextualize(Context context) throws ContextException {
this.context = context;
}
public void compose(ComponentManager manager) throws ComponentException {
this.manager = manager;
}
public void setLogKitManager(LogKitManager logKit) {
this.logKit = logKit;
}
public void setRoleManager(RoleManager rm) {
this.roleManager = rm;
}
/*
<processor>
<reload delay="10"/>
<root-language name="sitemap"/>
<language>...</language>
</processor>
*/
public void configure(Configuration config) throws ConfigurationException {
Configuration rootLangConfig = config.getChild("root-language", false);
if (rootLangConfig != null) {
this.language = rootLangConfig.getAttribute("name");
}
// Obtain the configuration file, or use the XCONF_URL if none
// is defined
String xconfURL = config.getAttribute("config", XCONF_URL);
// Reload check delay. Default is 1 second.
this.lastModifiedDelay = config.getChild("reload").getAttributeAsLong("delay", 1000L);
// Read the builtin languages definition file
Configuration builtin;
try {
URLFactory factory = (URLFactory)this.manager.lookup(URLFactory.ROLE);
URLSource source = new URLSource(factory.getURL(xconfURL), this.manager);
try {
SAXConfigurationHandler handler = new SAXConfigurationHandler();
source.toSAX(handler);
builtin = handler.getConfiguration();
} finally {
this.manager.release((Component)factory);
if (source != null) {
source.recycle();
}
}
} catch(Exception e) {
String msg = "Error while reading " + xconfURL + ": " + e.getMessage();
getLogger().error(msg, e);
throw new ConfigurationException(msg, e);
}
// Create a selector for tree builders of all languages
this.builderSelector = new ExtendedComponentSelector(Thread.currentThread().getContextClassLoader());
try {
LifecycleHelper.setupComponent(this.builderSelector,
getLogger(),
this.context,
this.manager,
this.roleManager,
this.logKit,
builtin
);
} catch(ConfigurationException ce) {
throw ce;
} catch(Exception e) {
throw new ConfigurationException("Could not setup builder selector", e);
}
}
public void initialize() throws Exception {
// Get a new Source handler
this.sourceHandler = (SourceHandler)this.manager.lookup(SourceHandler.ROLE);
// and add the special "cocoon:" source factory
this.sourceHandler.addFactory("cocoon", new CocoonSourceFactory(this, this.manager));
}
public boolean process(Environment environment) throws Exception {
InvokeContext context = new InvokeContext();
context.setLogger(getLogger());
try {
return process(environment, context);
} finally {
context.dispose();
}
}
public boolean process(Environment environment, StreamPipeline pipeline, EventPipeline eventPipeline)
throws Exception {
InvokeContext context = new InvokeContext(pipeline, eventPipeline);
context.setLogger(getLogger());
SourceHandler oldSourceHandler = environment.getSourceHandler();
CocoonComponentManager.enterEnvironment(environment, environment.getObjectModel());
try {
environment.setSourceHandler(this.sourceHandler);
return process(environment, context);
} finally {
CocoonComponentManager.leaveEnvironment();
environment.setSourceHandler(oldSourceHandler);
context.dispose();
}
}
protected boolean process(Environment environment, InvokeContext context)
throws Exception {
SourceHandler oldSourceHandler = environment.getSourceHandler();
CocoonComponentManager.enterEnvironment(environment, environment.getObjectModel());
try {
environment.setSourceHandler(this.sourceHandler);
if (this.rootNode == null || this.source.getLastModified() > this.lastModified) {
setupRootNode(environment);
}
return this.rootNode.invoke(environment, context);
} finally {
CocoonComponentManager.leaveEnvironment();
environment.setSourceHandler(oldSourceHandler);
}
}
protected synchronized void setupRootNode(Environment env) throws Exception {
// Now that we entered the synchronized area, recheck what's already
// been checked in process().
if (this.rootNode != null && source.getLastModified() <= this.lastModified) {
// Nothing changed
return;
}
long startTime = System.currentTimeMillis();
// Dispose the previous tree, if any
disposeTree();
// Get a builder
TreeBuilder builder = (TreeBuilder)this.builderSelector.select(this.language);
ProcessingNode root;
try {
if (builder instanceof Recomposable) {
((Recomposable)builder).recompose(this.manager);
}
builder.setProcessor(this);
if (this.source == null) {
// FIXME : make the delay configurable
this.source = new DelayedRefreshSourceWrapper(env.resolve(builder.getFileName()), 1000L);
}
root = builder.build(this.source);
this.disposableNodes = builder.getDisposableNodes();
} finally {
this.builderSelector.release(builder);
}
this.lastModified = System.currentTimeMillis();
if (getLogger().isDebugEnabled()) {
double time = (this.lastModified - startTime) / 1000.0;
getLogger().debug("TreeProcessor built in " + time + " secs from " + source.getSystemId());
//System.out.println("TreeProcessor built in " + time + " secs from " + source.getSystemId());
}
// Finished
this.rootNode = root;
}
public void dispose() {
disposeTree();
this.manager.release(this.sourceHandler);
if (this.parent == null) {
// root processor : dispose the builder selector
this.builderSelector.dispose();
}
}
/**
* Dispose all nodes in the tree that are disposable
*/
protected void disposeTree() {
if (this.disposableNodes != null) {
Iterator iter = this.disposableNodes.iterator();
while (iter.hasNext()) {
((Disposable)iter.next()).dispose();
}
this.disposableNodes = null;
}
}
}