This instruction will help you configuring Cocoon along with Tomcat for virtual hosting. We are going to serve several virtual hosts each from its own directory.

  
  Host                        Directory
  http://customerA/           /opt/virtHosts/customerA/
  http://customerB/           /opt/virtHosts/customerB/
  http://customerC/           /opt/virtHosts/customerC/
  http://www.foo.tld/         /opt/virtHosts/www.foo.tld/

Prepare the environment

You have installed and configured Cocoon and Tomcat according to CocoonEasyInstallation.

   /opt/jakarta-tomcat-5.x.xx
   /opt/cocoon/

Now create the following directories

   /opt/virtHosts/customerA/
   /opt/virtHosts/customerB/
   /opt/virtHosts/customerC/
   /opt/virtHosts/www.foo.tld/

Make sure your Nameserver knows about all the hosts. If you are on a local machine without nameserver you can add them to /etc/hosts file.
Put some sample which you know that is working into each customerX directory.

Define HostMatcher

The default sitemap already has many Matchers defined but there are some more that we could add.
Backup your /opt/cocoon/ROOT/sitemap.xmap and open it with your favourite editor. Search for the section

 <map:components>
   ...other components...
   <map:matchers default="wildcard">
     ...other matchers...
   </map:matchers>
   ...other components...
 </map:components>

and add the line

    <map:matcher logger="sitemap.matcher.host" name="host" src="org.apache.cocoon.matching.WildcardHostMatcher"/>

so that you now have

 <map:components>
   ...other components...
   <map:matchers default="wildcard">
     ...other matchers...
     <map:matcher logger="sitemap.matcher.host" name="host" src="org.apache.cocoon.matching.WildcardHostMatcher"/>
     ...other matchers...
   </map:matchers>
   ...other components...
 </map:components>

Now as our sitemap.xmap knows about WildcardHostMatcher we can use it in our pipeline.

Use HostMatcher (in the pipeline)

Search for the section <map:pipelines> and find the <map:pipeline> which is commented "main pipeline".

 <map:pipelines>
   ...others...
   <!-- main pipeline -->
   <map:pipeline>

and directly below, and still above the welcome pages, add the lines:

    <!-- Insertion for matching http://cocoon:[portnumber]/ -->
    <map:match pattern="cocoon:**" type="host">

Further down in the pipeline, just before beginning of comment about error handling <map:handle-errors> put the folllowing lines of which the first is the closing tag of our previous insertion from above.

    </map:match>
    <!--+
        | Mount everything else by calling the sitemap.xmap file located
        | in the requested folder.
        | First match, pattern="*:**" is for http://virthost:8080/uri
        | Second one, pattern="*" is for calling at default port.
        +-->
    <map:match pattern="*:**" type="host">
      <map:mount check-reload="yes" src="/opt/virtHosts/{1}/" uri-prefix=""/>
    </map:match>
    <map:match pattern="*" type="host">
      <map:mount check-reload="yes" src="/opt/virtHosts/{1}/" uri-prefix=""/>
    </map:match>

Save your changes to sitemap.xmap.

http://cocoon:8080/ or http://cocoon/

Be aware that we added one new matcher matching *http://cocoon:\[portnumber\]/* nested with all other matches from the original pipeline.
Please change it from pattern="cocoon:**" to pattern="cocoon" if you've connected Tomcat to Apache or if you configured Tomcat to listen on port 80. Restart Tomcat and start your browser with *http://customerA/* or another.

Conclusion

Setting up a new virtual host requires just only creating in /opt/virtHosts/ a directory with its name being the same as the host name.


Feel free to put any comments or corrections or to contact the author for questions under volkmar@pogatzki.net

Related stuff

Of course, Cocoon has a RegexpHostMatcher too and also a HostSelector which already got its own wikipage HostSelector.

If you prefer having Apache in front to match the (virtual) hosts, please have a look at Stavros Kounis' VirtualHost.

  • No labels