1 9 10 package com.opensymphony.module.sitemesh; 11 12 import com.opensymphony.module.sitemesh.factory.FactoryException; 13 import com.opensymphony.module.sitemesh.util.Container; 14 15 import javax.naming.InitialContext ; 16 import javax.rmi.PortableRemoteObject ; 17 import java.lang.reflect.Constructor ; 18 19 29 public abstract class Factory { 30 31 private static final String SITEMESH_FACTORY = "sitemesh.factory"; 32 33 38 public static Factory getInstance(Config config) { 39 Factory instance = (Factory)config.getServletContext().getAttribute(SITEMESH_FACTORY); 40 if (instance == null) { 41 String factoryClass = getEnvEntry("sitemesh.factory", "com.opensymphony.module.sitemesh.factory.DefaultFactory"); 42 try { 43 Class cls; 44 try { 45 cls = Class.forName(factoryClass); 46 } 47 catch (NoClassDefFoundError e) { 48 cls = Class.forName(factoryClass, true, Thread.currentThread().getContextClassLoader()); 49 } 50 51 Constructor con = cls.getConstructor(new Class [] { Config.class }); 52 instance = (Factory)con.newInstance(new Config[] { config }); 53 config.getServletContext().setAttribute(SITEMESH_FACTORY, instance); 54 } 55 catch (Exception e) { 56 report("Cannot construct Factory : " + factoryClass, e); 57 } 58 } 59 return instance; 60 } 61 62 63 public abstract DecoratorMapper getDecoratorMapper(); 64 65 75 public abstract PageParser getPageParser(String contentType); 76 77 78 public abstract boolean shouldParsePage(String contentType); 79 80 83 public abstract boolean isPathExcluded(String path); 84 85 86 protected static void report(String msg, Exception e) { 87 throw new FactoryException(msg, e); 88 } 89 90 91 private static String getEnvEntry(String envEntry, String defaultValue) { 92 String result = null; 93 try { 94 if (Container.get() != Container.JRUN) { 95 InitialContext ctx = new InitialContext (); 97 Object o = ctx.lookup("java:comp/env/" + envEntry); 98 ctx.close(); 99 result = (String )PortableRemoteObject.narrow(o, String .class); } 101 } 102 catch (Exception e) { } return result == null || result.trim().length() == 0 ? defaultValue : result; 104 } 105 } | Popular Tags |