Search

Dark theme | Light theme

September 15, 2008

Use properties from the Spring configurator in Cocoon 2.2 sitemap

Cocoon 2.2 has a new mechanism for working with configurations: the Cocoon Spring Configurator component. Adding the configurator to our application is easy, because we only have to add <configurator:settings/> to a Spring configuration file. Now we have support for running modes, property handling and Spring bean configurations. Just like that.

We will use a small example to see how properties are handled by the configurator. Suppose we have a simple Cocoon 2.2 block with a sitemap.xmap. In the sitemap.xmap we use the property enable.internal-only (see line 6.):

<?xml version="1.0" encoding="UTF-8"?>
<map:sitemap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://apache.org/cocoon/sitemap/1.0 http://cocoon.apache.org/schema/sitemap/cocoon-sitemap-1.0.xsd"
 xmlns:map="http://apache.org/cocoon/sitemap/1.0">
    <map:pipelines>
        <map:pipeline id="internal-resource" internal-only="${enable.internal-only}">
            <map:match pattern="resource/internal/**">
                <map:read src="resource/internal/{1}"/>
            </map:match>
        </map:pipeline>
        <map:pipeline id="external-resource">
            <map:match pattern="resource/external/**">
                <map:read src="resource/external/{1}"/>
            </map:match>
        </map:pipeline>
    </map:pipelines>
</map:sitemap>

To give the property a value we have different alternatives, not to say a lot of alternatives. For this example we create a new property file: src/main/resources/META-INF/cocoon/properties/sample.properties. In the file we define enable.internal-only=false. Now when we run the application we can access the pipeline internal-resource because the value of the property enable.internal-only is false.