You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by un...@apache.org on 2003/12/30 12:31:03 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/components/cprocessor TreeProcessor.java

unico       2003/12/30 03:31:03

  Modified:    src/java/org/apache/cocoon/components/cprocessor
                        TreeProcessor.java
  Log:
  not a compiling processor after all
  code cleanup
  work on sitemap mounts
  
  Revision  Changes    Path
  1.2       +102 -107  cocoon-2.2/src/java/org/apache/cocoon/components/cprocessor/TreeProcessor.java
  
  Index: TreeProcessor.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/cprocessor/TreeProcessor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TreeProcessor.java	28 Dec 2003 21:03:17 -0000	1.1
  +++ TreeProcessor.java	30 Dec 2003 11:31:03 -0000	1.2
  @@ -54,10 +54,8 @@
   import java.net.URL;
   import java.util.Map;
   
  -import javax.xml.transform.Transformer;
   import javax.xml.transform.sax.SAXResult;
   import javax.xml.transform.sax.TransformerHandler;
  -import javax.xml.transform.stream.StreamSource;
   
   import org.apache.avalon.excalibur.logger.LoggerManager;
   import org.apache.avalon.fortress.impl.DefaultContainerManager;
  @@ -72,13 +70,14 @@
   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.context.DefaultContext;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.Logger;
   import org.apache.avalon.framework.service.ServiceException;
   import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.avalon.framework.service.Serviceable;
  -import org.apache.cocoon.CompilingProcessor;
   import org.apache.cocoon.Constants;
  +import org.apache.cocoon.Modifiable;
   import org.apache.cocoon.Processor;
   import org.apache.cocoon.components.pipeline.ProcessingPipeline;
   import org.apache.cocoon.components.source.impl.DelayedRefreshSourceWrapper;
  @@ -100,12 +99,12 @@
    * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
    * 
    * @avalon.component
  - * @avalon.service type=CompilingProcessor
  + * @avalon.service type=Processor
    * @x-avalon.lifestyle type=singleton
    * @x-avalon.info name=sitemap
    */
   public class TreeProcessor extends AbstractLogEnabled 
  -implements CompilingProcessor, Contextualizable, Serviceable, Configurable, Initializable, Disposable {
  +implements Processor, Contextualizable, Serviceable, Configurable, Initializable, Disposable {
   
       /** Environment attribute key for redirection status communication */
       public static final String COCOON_REDIRECT_ATTR = "cocoon: redirect url";
  @@ -113,11 +112,14 @@
       /** The sitemap namespace */
       public static final String SITEMAP_NS = "http://apache.org/cocoon/sitemap/1.0";
       
  -    /** The xsl transformation location for turning a sitemap into a Fortress container configuration */
  +    /* The xsl transformation location for turning a 
  +     * sitemap into a Fortress container configuration 
  +     */
       private static final String SITEMAP2XCONF_URL = 
  -//        "resource://org/apache/cocoon/components/treeprocessor/sitemap2xconf.xsl";
  -        "file://d:/apache/cocoon-2.2/src/java/org/apache/cocoon/components/cprocessor/sitemap2xconf.xsl";
  -    /** The parent TreeProcessor, if any */
  +        "resource://org/apache/cocoon/components/treeprocessor/sitemap2xconf.xsl";
  +//        "file://d:/apache/cocoon-2.2/src/java/org/apache/cocoon/components/cprocessor/sitemap2xconf.xsl";
  +    
  +    /* The parent TreeProcessor, if any */
       private TreeProcessor m_parent;
       private EnvironmentHelper m_environmentHelper;
       
  @@ -125,22 +127,23 @@
       private ServiceManager m_manager;
       private SourceResolver m_resolver;
       
  -    /** The object that manages the sitemap container */
  +    /* The object that manages the sitemap container */
       private DefaultContainerManager m_cm;
  +    private SitemapContainer m_container;
       
       /* some configuration options */
       private String m_fileName;
       private boolean m_checkReload;
       private long m_lastModifiedDelay;
       
  -    /* the tree configuration source (the sitemap) */
  -    private DelayedRefreshSourceWrapper m_source;
  -    private long m_lastModified;
  +    /* the sitemap source */
  +    private Source m_source;
  +    private long m_creationTime;
       
  -    /* the sitemap2xconf.xsl stylesheet */
  +    /* the sitemap2xconf.xsl source */
       private Source m_transform;
       
  -    /** The root node of the processing tree */
  +    /* The root node of the processing tree */
       private ProcessingNode m_rootNode;
       
       // ---------------------------------------------------- lifecycle
  @@ -148,24 +151,26 @@
       public TreeProcessor() {
       }
       
  -    private TreeProcessor(TreeProcessor parent) throws Exception {
  +    private TreeProcessor(TreeProcessor parent, Source source, boolean checkReload) throws Exception {
           m_parent = parent;
  +        m_transform = parent.m_transform;
  +        m_checkReload = checkReload;
  +        m_source = source;
  +        m_environmentHelper = new EnvironmentHelper(parent.m_environmentHelper);
           ContainerUtil.enableLogging(this,parent.getLogger());
           ContainerUtil.contextualize(this,parent.m_context);
  -        ContainerUtil.service(this,parent.m_manager);
  -        m_fileName = parent.m_fileName;
  -        m_checkReload = parent.m_checkReload;
  -        m_lastModifiedDelay = parent.m_lastModifiedDelay;
  +        ContainerUtil.service(this,parent.m_container.getServiceManager());
  +        ContainerUtil.initialize(this);
       }
       
  -    public void contextualize(Context context) throws ContextException {
  +    public void contextualize(Context context) {
           m_context = context;
       }
       
       /**
  -     * @avalon.dependency  type="SourceResolver"
  -     * @avalon.dependency  type="SAXParser"
  -     * @avalon.dependency  type="XSLTProcessor"
  +     * @avalon.dependency  type=SourceResolver
  +     * @avalon.dependency  type=SAXParser
  +     * @avalon.dependency  type=XSLTProcessor
        */
       public void service(ServiceManager manager) throws ServiceException {
           m_manager = manager;
  @@ -189,19 +194,31 @@
           }
           ContainerUtil.enableLogging(m_environmentHelper,getLogger());
           ContainerUtil.service(m_environmentHelper,m_manager);
  -        m_transform = m_resolver.resolveURI(SITEMAP2XCONF_URL);
  +        
  +        // resolve the sources
  +        // in the case of a child processor these are already set
  +        // hence the null checks
  +        if (m_transform == null) {
  +            m_transform = m_resolver.resolveURI(SITEMAP2XCONF_URL);
  +        }
  +        if (m_source == null) {
  +            Source source = m_resolver.resolveURI(m_fileName);
  +            m_source = new DelayedRefreshSourceWrapper(source,m_lastModifiedDelay);
  +        }
       }
   
       public void dispose() {
  -        ContainerUtil.dispose(m_cm);
  -        m_cm = null;
  +        disposeContainer();
           ContainerUtil.dispose(m_environmentHelper);
           m_environmentHelper = null;
           if (m_manager != null) {
               if (m_source != null ) {
  -                m_resolver.release(m_source.getSource());
  +                m_resolver.release(((DelayedRefreshSourceWrapper) m_source).getSource());
                   m_source = null;
               }
  +            if (m_transform != null) {
  +                m_resolver.release(m_transform);
  +            }
               m_manager.release(m_resolver);
               m_resolver = null;
               m_manager = null;
  @@ -221,26 +238,18 @@
       }
       
       /**
  -     * Do the actual processing, be it producing the response or just building the pipeline.
  -     * 
  -     * @param environment
  -     * @param context
  -     * @return
  -     * @throws Exception
  +     * Do the actual processing, be it producing the response 
  +     * or just building the pipeline.
        */
       private boolean process(Environment environment, InvokeContext context)
       throws Exception {
       
  -        // first, check whether we need to load the sitemap
  -        if (m_rootNode == null || (m_checkReload && m_source.getLastModified() > m_lastModified)) {
  -            setupRootNode(environment);
  -        }
  +        final ProcessingNode rootNode = getRootNode();
           
           // and now process
           EnvironmentHelper.enterProcessor(this, m_manager, environment);
           try {
  -            boolean success = m_rootNode.invoke(environment, context);
  -            if (success) {
  +            if (rootNode.invoke(environment, context)) {
                   // Do we have a cocoon: redirect ?
                   String cocoonRedirect = (String) environment.getAttribute(COCOON_REDIRECT_ATTR);
                   if (cocoonRedirect != null) {
  @@ -260,45 +269,62 @@
           }
       }
       
  -    private synchronized void setupRootNode(Environment env) throws Exception {
  +    private ProcessingNode getRootNode() throws Exception {
  +        // first, check whether we need to load the sitemap
  +        if (m_rootNode == null || shouldReload()) {
  +            setupRootNode();
  +        }
  +        return m_rootNode;
  +    }
  +    
  +    private boolean shouldReload() {
  +        return m_checkReload && m_source.getLastModified() > m_creationTime;
  +    }
  +    
  +    private synchronized void setupRootNode() throws Exception {
   
  -        // Now that we entered the synchronized area, recheck what's already
  -        // been checked in process().
  -        if (m_rootNode != null && m_source.getLastModified() <= m_lastModified) {
  -            // Nothing changed
  +        // Now that we entered the synchronized area, recheck if
  +        // we still need to setup the root node
  +        if (m_rootNode != null && !shouldReload()) {
               return;
           }
           
  +        disposeContainer();
           long startTime = System.currentTimeMillis();
  +        createContainer();
  +        m_rootNode = m_container.getRootNode();
  +        long endTime = System.currentTimeMillis();
   
  -        if (m_source == null) {
  -            Source source = m_resolver.resolveURI(m_fileName);
  -            m_source = new DelayedRefreshSourceWrapper(source,m_lastModifiedDelay);
  -        }        
  -
  -        ContainerUtil.dispose(m_cm);
  +        if (getLogger().isDebugEnabled()) {
  +            double time = (endTime - startTime) / 1000.0;
  +            getLogger().debug("TreeProcessor built in " + time + " secs from " + m_source.getURI());
  +        }
  +        
  +        m_creationTime = endTime;
  +    }
  +    
  +    private void createContainer() throws Exception {
           // create the sitemap container
  -        FortressConfig config = new FortressConfig(m_context);
  -        config.setContainerClass(Thread.currentThread().getContextClassLoader().
  -            loadClass(SitemapContainer.class.getName()));
  -        config.setContextRootURL(new URL((String) m_context.get(Constants.CONTEXT_ROOT_URL)));
  +        DefaultContext context = new DefaultContext(m_context);
  +        context.put("treeprocessor",this);
  +        context.makeReadOnly();
  +        FortressConfig config = new FortressConfig(context);
  +        config.setContainerClass(SitemapContainer.class);
           config.setContextClassLoader(Thread.currentThread().getContextClassLoader());
           config.setContainerConfiguration(buildConfiguration(m_source));
           config.setServiceManager(m_manager);
           config.setLoggerManager((LoggerManager) m_manager.lookup(LoggerManager.ROLE));
           m_cm = new DefaultContainerManager(config.getContext(),getLogger());
           m_cm.initialize();
  -        ProcessingNode root = ((SitemapContainer) m_cm.getContainer()).getRootNode();
  -        
  -        m_lastModified = System.currentTimeMillis();
  -
  -        if (getLogger().isDebugEnabled()) {
  -            double time = (m_lastModified - startTime) / 1000.0;
  -            getLogger().debug("TreeProcessor built in " + time + " secs from " + m_source.getURI());
  +        m_container = (SitemapContainer) m_cm.getContainer();
  +    }
  +    
  +    private void disposeContainer() {
  +        if (m_cm != null) {
  +            ContainerUtil.dispose(m_cm);
           }
  -
  -        // Finished
  -        m_rootNode = root;
  +        m_cm = null;
  +        m_container = null;
       }
       
       private Configuration buildConfiguration(Source source) throws Exception {
  @@ -368,18 +394,11 @@
           return processor.process(newEnv, context);
       }
       
  -    /**
  -     * Process the given <code>Environment</code> to assemble
  -     * a <code>ProcessingPipeline</code>.
  -     * @since 2.1
  -     */
       public ProcessingPipeline buildPipeline(Environment environment) throws Exception {
  -        InvokeContext context = new InvokeContext( true );
  -
  +        InvokeContext context = new InvokeContext(true);
           context.enableLogging(getLogger());
  -
           try {
  -            if ( process(environment, context) ) {
  +            if (process(environment, context)) {
                   return context.getProcessingPipeline();
               } else {
                   return null;
  @@ -389,35 +408,26 @@
           }
       }
   
  -    /**
  -     * TODO: do we still need this?
  -     */
       public Map getComponentConfigurations() {
  +        // TODO: implement
           return null;
       }
       
  -    /**
  -     * TODO: do we still need this?
  -     */
       public String getContext() {
           return getEnvironmentHelper().getContext();
       }
       
       /**
  -     * TODO: do we still need this?
  -     * 
  -     * Get the root parent of this processor
  -     * @since 2.1.1
  +     * Returns the root sitemap processor.
        */
       public Processor getRootProcessor() {
           TreeProcessor result = this;
           while(result.m_parent != null) {
               result = result.m_parent;
           }
  -        
           return result;
       }
  -
  +    
       public EnvironmentHelper getEnvironmentHelper() {
           return m_environmentHelper;
       }
  @@ -425,14 +435,13 @@
       /**
        * 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.
  +     * @param source  the location of the child sitemap.
  +     * @return  a new child processor.
        */
  -    public TreeProcessor createChildProcessor(Source source) throws Exception {
  -        TreeProcessor child = new TreeProcessor(this);
  -        child.m_source = new DelayedRefreshSourceWrapper(source, m_lastModifiedDelay);
  -        return child;
  +    public TreeProcessor createChildProcessor(String src, boolean checkReload) throws Exception {
  +        Source delayedSource = new DelayedRefreshSourceWrapper(
  +            m_resolver.resolveURI(src),m_lastModifiedDelay);
  +        return new TreeProcessor(this, delayedSource, checkReload);
       }
       
       /**
  @@ -463,18 +472,4 @@
           }
       }
   
  -    public void precompile(
  -        String fileName,
  -        Environment environment,
  -        String markupLanguage,
  -        String programmingLanguage)
  -        throws Exception {
  -        // TODO Auto-generated method stub
  -
  -    }
  -
  -    public boolean modifiedSince(long date) {
  -        // TODO Auto-generated method stub
  -        return false;
  -    }
   }