001: /*
002: * Title: Factory
003: * Description:
004: *
005: * This software is published under the terms of the OpenSymphony Software
006: * License version 1.1, of which a copy has been included with this
007: * distribution in the LICENSE.txt file.
008: */
009:
010: package com.opensymphony.module.sitemesh;
011:
012: import com.opensymphony.module.sitemesh.factory.FactoryException;
013: import com.opensymphony.module.sitemesh.util.ClassLoaderUtil;
014: import com.opensymphony.module.sitemesh.util.Container;
015:
016: import javax.naming.InitialContext;
017: import javax.rmi.PortableRemoteObject;
018: import java.lang.reflect.Constructor;
019:
020: /**
021: * Factory responsible for creating appropriate instances of implementations.
022: * This is specific to a web context and is obtained through {@link #getInstance(com.opensymphony.module.sitemesh.Config)}.
023: *
024: * <p>The actual Factory method used is determined by the enviroment entry <code>sitemesh.factory</code>.
025: * If this doesn't exist, it defaults to {@link com.opensymphony.module.sitemesh.factory.DefaultFactory} .</p>
026: *
027: * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
028: * @version $Revision: 1.7 $
029: */
030: public abstract class Factory implements PageParserSelector {
031: /** Web context lookup key */
032: private static final String SITEMESH_FACTORY = "sitemesh.factory";
033:
034: /**
035: * Entry-point for obtaining singleton instance of Factory. The default factory class
036: * that will be instantiated can be overridden with the environment
037: * entry <code>sitemesh.factory</code>.
038: */
039: public static Factory getInstance(Config config) {
040: Factory instance = (Factory) config.getServletContext()
041: .getAttribute(SITEMESH_FACTORY);
042: if (instance == null) {
043: String factoryClass = getEnvEntry("sitemesh.factory",
044: "com.opensymphony.module.sitemesh.factory.DefaultFactory");
045: try {
046: Class cls = ClassLoaderUtil.loadClass(factoryClass,
047: config.getClass());
048: Constructor con = cls
049: .getConstructor(new Class[] { Config.class });
050: instance = (Factory) con
051: .newInstance(new Config[] { config });
052: config.getServletContext().setAttribute(
053: SITEMESH_FACTORY, instance);
054: } catch (Exception e) {
055: throw new FactoryException(
056: "Cannot construct Factory : " + factoryClass, e);
057: }
058: }
059: instance.refresh();
060: return instance;
061: }
062:
063: public abstract void refresh();
064:
065: /** Return instance of DecoratorMapper. */
066: public abstract DecoratorMapper getDecoratorMapper();
067:
068: /**
069: * Create a PageParser suitable for the given content-type.
070: *
071: * <p>For example, if the supplied parameter is <code>text/html</code>
072: * a parser shall be returned that can parse HTML accordingly.</p> Never returns null.
073: *
074: * @param contentType The MIME content-type of the data to be parsed
075: * @return Appropriate <code>PageParser</code> for reading data
076: *
077: */
078: public abstract PageParser getPageParser(String contentType);
079:
080: /** Determine whether a Page of given content-type should be parsed or not. */
081: public abstract boolean shouldParsePage(String contentType);
082:
083: /**
084: * Determine whether the given path should be excluded from decoration or not.
085: */
086: public abstract boolean isPathExcluded(String path);
087:
088: /** Find String environment entry, or return default if not found. */
089: private static String getEnvEntry(String envEntry,
090: String defaultValue) {
091: String result = null;
092: try {
093: if (Container.get() != Container.JRUN) {
094: // TODO: JRun really isn't happy with this
095: InitialContext ctx = new InitialContext();
096: Object o = ctx.lookup("java:comp/env/" + envEntry);
097: ctx.close();
098: result = (String) PortableRemoteObject.narrow(o,
099: String.class); // rmi-iiop friendly.
100: }
101: } catch (Exception e) {
102: } // failed - don't moan, just return default.
103: return result == null || result.trim().length() == 0 ? defaultValue
104: : result;
105: }
106: }
|