cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Rosman <step...@rosman.id.au>
Subject Re: Accessing tomcat servlet context parameters in sitemap
Date Thu, 03 Jun 2010 03:37:49 GMT
> I have user-defined parameter 'redirectURL' in tomcat servlet context and
> need to use this parameter in my sitemap.
> I can modify ROOT.xml, but there is no opporunity to store this parameter
> somewhere outside of tomcat's server configuration.

I made a "ContextPropertyProvider" and stuck it in my web app which
copies all the properties from the tomcat context file into cocoon.

--
package tl.por.pub.ccpp;
/**
 * <p>
 * Allows you to set configuration settings in the servlet context and then have
 * them available to cocoon beans.
 * </p>
 * <p>
 * Specifically for allowing you to set the Sitemap Servlet's context-path in
 * a tomcat context fragment.
 * </p>
 * <code>
 * tomcat context fragment:
 * &lt;Context docBase="C:\apps\cocoon\cocoon.war"&gt;
 *   &lt;Parameter name="cocoon.site" value="c:/apps/cocoon/site"/&gt;
 * &lt;/Context&gt;
 * </code>
 * <code>
 * cocoon.war/WEB-INF/applicationContext.xml:
 * &lt;bean
 *     name="org.apache.cocoon.configuration.PropertyProvider"
 *     class="tl.por.pub.ccpp.ServletContextPropertyProvider"
 * /&gt;
 * </code>
 * <code>
 * cocoon.war/WEB-INF/classes/META-INF/cocoon/spring/block-servlet-service.xml:
 * &lt;bean
 *     name="org.domain.blockname"
 *     class="org.apache.cocoon.sitemap.SitemapServlet"&gt;
 *   &lt;servlet:context mount-path="/" context-path="${cocoon.site}"/&gt;
 * &lt;/bean&gt;
 * </code>
 */

import java.util.Properties;
import java.util.Enumeration;
import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;
import org.apache.cocoon.configuration.PropertyProvider;
import org.apache.cocoon.configuration.Settings;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServletContextPropertyProvider
implements ServletContextAware, PropertyProvider
{
    ServletContext ctx;
    private Log log = LogFactory.getLog(ServletContextPropertyProvider.class);

    public void setServletContext(ServletContext ctx) {
        this.ctx = ctx;
        log.debug("CCPP context set");
    }

    public Properties getProperties(Settings sets, String runmode,
String path) {

        Properties props = new Properties();
        Enumeration initParameters = ctx.getInitParameterNames();

        while (initParameters.hasMoreElements()) {
            String name = (String)initParameters.nextElement();
            props.setProperty(name, ctx.getInitParameter(name));
            if (log.isDebugEnabled()) {
                log.debug("CCPP initParam:
"+name+"="+ctx.getInitParameter(name));
            }
        }

        log.debug("CCPP properties gotten");
        return props;
    }
}

--

Then add it to your installation (I've added it to my webapp under
WEB-INF/lib/ccpp-<version>.jar but I think spring can pick it up from
multiple places.)

I've added a variable to my context fragment file...

--
<Context ...>
  <Parameter name="cocoon.site" value="c:\site"/>
  <Parameter name="app.root" value="http://localhost:8080/members/"/>
</Context>
--

In my sitemap I can use the values from the context fragment file.

--
<map:generate type="request">
  <map:parameter name="app.root" value="${app.root}"/>
</map:generate>
--

Then do all the transforming goodness using the <h:request> variables
which includes app.root.

It's been a year or more since I've last looked at this (I'm working
on the application rather than cocoon) so it's all a bit foggy.

If you have any trouble let me know and I'll have a look back over
what I did. It works for me though, I have an unfortunate need to
generate some absolute URLs which differ on my local development copy
of the app and the remote version and everything that changes is in
the tomcat context file (app.root gmaps api key) and I don't have to
worry about it.

It also lets me run multiple cocoon apps from the same codebase by
setting the cocoon.site and cocoon.logconfig in the context file and
use their values in spring somehow (like I said - a bit foggy).  Not
sure if that has caching consequences but it's not been a problem so
far.

Heh - I just noticed that the javadoc in the code I posted has the
instructions :-)

-- 
Stephen Rosman

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message