apache > cocoon

Request Processing



This document tries to explain Apache Cocoon technically. We do this by describing what happens if somebody types in the URL of a simple Cocoon page.

Intended public

The reader should have a knowledge of:

  • the Java 2 platform
  • the javax.servlet extensions
  • XML
  • HTTP

The configuration assumptions

The sequence of events described in this document, depends on some assumptions with regard to the configuration of Cocoon. That's what's described here.


The task of the sitemap is to define the pipelines that Cocoon will apply to URI's called in one's browser.

This is the minimal sitemap that is necessary. The lines here are included in the standard sitemap.xmap that comes with the distribution of Cocoon.

The sitemap is defined in ${cocoon}/sitemap.xmap.

<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> 
    <map:generators default="file">
    <map:generator name="file" label="content" 

    <map:transformers default="xslt">
      <map:transformer name="xslt"

    <map:serializers default="html">
      <map:serializer name="html" mime-type="text/html" 

    <map:selectors default="browser">
      <map:selector name="browser" 
        <browser name="explorer" useragent="MSIE"/>
        <browser name="netscape" useragent="Mozilla"/> 

    <map:matchers default="uri">
      <map:matcher name="uri" 

      <map:match pattern="hello.html">
        <map:generate src="docs/samples/hello-page.xml"/>
        <map:transform src="stylesheets/page/simple-page2html.xsl"/>
        <map:serialize type="html"/> 


cocoon.xconf is the file that defines the Avalon Components.

For our study, we need the standard cocoon.xconf file of Cocoon.

It can be found in ${cocoon}/WEB-INF/cocoon.xconf.

<?xml version="1.0"?>

<cocoon version="2.0">

<!-- ===================== General Components =========================== -->

  <component role="org.apache.cocoon.components.parser.Parser" 
  <component role="org.apache.cocoon.components.store.Store"  

    <component-instance name="java" 
      <parameter name="compiler" 
      <parameter name="code-formatter" 
      <parameter name="class-loader" 

  <component role="org.apache.cocoon.components.classloader.ClassLoaderManager" 

    <component-instance name="xsp" 
      <parameter name="prefix" value="xsp"/>
      <parameter name="uri" value="http://apache.org/xsp"/>

      <target-language name="java">
        <parameter name="core-logicsheet" 

          <parameter name="prefix" value="xsp-request"/>
          <parameter name="uri" value="http://apache.org/xsp/request/2.0"/>
          <parameter name="href" 

          <parameter name="prefix" value="xsp-response"/>
          <parameter name="uri" value="http://apache.org/xsp/response/2.0"/>
          <parameter name="href" 

    <component-instance name="sitemap" 
      <parameter name="prefix" value="map"/>
      <parameter name="uri" value="http://apache.org/cocoon/sitemap/1.0"/>

      <target-language name="java">
        <parameter name="core-logicsheet" 

  <component role="org.apache.cocoon.components.language.generator.ProgramGenerator" 
    <parameter name="auto-reload" value="true"/>

  <!-- these components is used as a PoolController for the sitemap component pools  -->
  <component role="org.apache.avalon.util.pool.PoolController" 

  <sitemap file="sitemap.xmap"/> 

The sequence of things

Role of Tomcat

The role of Tomcat is to initialize the CocoonServlet, and to receive the HttpRequest and pass it on to the CocoonServlet.

Initialize CocoonServlet

This is done by calling CocoonServlet.init(ServletConfig). This is the standard servlet way to initialize a servlet.

Pass HttpRequest

On reception of a HttpRequest, Tomcat calls CocoonServlet.service(HttpRequest, HttpResponse). This is also standard.



The steps that happen on initialization, are:

Find the classpath

Cocoon needs to know the classpath for compilation of the files it generates itself. This is where the classpath is stored.

Find the init file

The init file (normally cocoon.xconf, as defined in ${cocoon}/WEB-INF/web.xml) contains the necessary information for Cocoon to decide which classes to use for which roles (refer to Avalon).

This is a feature that is added for increased configurability. If you were developing a one time solution, the information in this file would normally be hard coded, but the use of this file increases potential reusability.

Read the init file

The init file is an xml file (normally cocoon.xconf) which describes the classes to use for which roles.

"Roles" are a concept of Avalon.

The handling of cocoon.xconf goes as follows:

  1. Get the parser: This is something necessary for bootstrapping: cocoon.xconf contains the parser to be used by Cocoon, but cocoon.xconf is an xml file that has to be parsed itself. That's why Cocoon gets a default parser out of the System properties (this refers to the environment variable $org.apache.cocoon.components.parser.Parser of the OS). If no parser is defined in the environment, Cocoon will use org.apache.cocoon.components.parser.JaxpParser (a hard-coded default).
  2. Get the components: Cocoon uses roles (refer to Avalon) as its working classes. Each role is implemented by one or more real classes (components, again an Avalon concept). This is where they are retrieved.
  3. Get the sitemap: Here the location of the sitemap is retrieved. The actual compilation of the sitemap occurs in the HttpRequest handling.

UML sequence diagram

You can find it here.

HttpRequest handling


When the CocoonServlet gets a HttpRequest from the servlet engine, it sets up an Environment (a HttpEnvironment in this case) and passes that to Cocoon. The Environment exists of Request, Response, and some servlet info (such as requested URI and the servlet's path).

This Cocoon object lets the Environment decide which sitemap to use, and passes the sitemap filename along with the Environment to a Manager.

This one puts a Handler to work: it checks whether there already exists a Handler with a compiled version of the sitemap. If not, it creates one. This is what happens then:

  1. The Handler creates a File object with the asked URL.
  2. The Manager sets the Composable and the Configuration of the Handler. (These are Avalon things).
  3. If necessary, the Manager asks the Handler to regenerate its sitemap class. (FIXME: As of today, 2000-11-08, I'm not sure if the "if necessary" check is working). Regeneration exists in:
    1. The Handler gets the "program-generator" Component from its Composable.
    2. The load() method of this ProgramGeneratorImpl is called.
    3. The ProgramGeneratorImpl gets the "markup-language" (in this case it will get a SitemapMarkupLanguage) and "programming-language" (being JavaLanguage) Components.
    4. The ProgramGeneratorImpl asks the SitemapMarkupLanguage to generate code.
    5. Then it asks the JavaLanguage to load the code. The JavaLanguage does this by creating a Javac object, setting its variables, and asking it to compile. Then it loads the class.
    6. Then its back to the ProgramGeneratorImpl who tells the JavaLanguage to instantiate the just loaded class.
  4. At last, the sitemapManager asks the Handler to process the Environment, and the Handler just forwards this request to the generated sitemap class.

UML sequence diagram

You can find it here.

Description of classes



This is the contact point for the servlet engine. It sets up the environment and passes all the work to a Cocoon object.



While this sounds to be the most important part of the Cocoon application, it is not. It is merely a Composable, meaning that it does some administrative work and gets other classes to work.



This one generates a Configuration out of a xml file.



An interface that takes an xml file and throws SAX events to the outside.



This is an Avalon interface. It assigns classes to roles. If an object needs a class for a specific role, it can ask a Configuration which class it has to use.



Something that manages Avalon Components.



This one manages the sitemap: it finds out if there exists a Handler for a sitemap, and if not, makes sure that one gets created.



A class that is responsible for dealing with sitemaps. It holds the sourcefile of the sitemap, and the compiled code for it. It checks whether the sitemap class that it contains is still valid, and if not, regenerates it.



Generates programs.



This one knows the markup of the sitemap, and helps writing the source file of the sitemap class.



This takes care for outputing Java code as source of the sitemap class.



Holds the various steps that have to be taken when executing a pipeline.