1 9 10 package com.opensymphony.module.sitemesh.factory; 11 12 import com.opensymphony.module.sitemesh.Config; 13 import com.opensymphony.module.sitemesh.DecoratorMapper; 14 import com.opensymphony.module.sitemesh.Factory; 15 import com.opensymphony.module.sitemesh.PageParser; 16 import com.opensymphony.module.sitemesh.mapper.PathMapper; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Properties ; 21 22 28 public abstract class BaseFactory extends Factory { 29 30 protected Config config = null; 31 32 38 protected DecoratorMapper decoratorMapper = null; 39 40 41 protected Map pageParsers = null; 42 43 44 protected PathMapper excludeUrls = null; 45 46 53 protected BaseFactory(Config config) { 54 this.config = config; 55 clearDecoratorMappers(); 56 clearParserMappings(); 57 clearExcludeUrls(); 58 } 59 60 61 public DecoratorMapper getDecoratorMapper() { 62 return decoratorMapper; 63 } 64 65 76 public PageParser getPageParser(String contentType) { 77 return (PageParser) pageParsers.get(contentType); 78 } 79 80 83 public boolean shouldParsePage(String contentType) { 84 return pageParsers.containsKey(contentType); 85 } 86 87 93 public boolean isPathExcluded(String path) { 94 return excludeUrls.get(path) != null; 95 } 96 97 100 protected void clearDecoratorMappers() { 101 decoratorMapper = null; 102 } 103 104 105 protected void pushDecoratorMapper(String className, Properties properties) { 106 try { 107 Class decoratorMapperClass = null; 108 try { 109 decoratorMapperClass = loadClass(className, getClass()); 110 } 111 catch (NoClassDefFoundError e) { 112 decoratorMapperClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); 113 } 114 DecoratorMapper newMapper = (DecoratorMapper) decoratorMapperClass.newInstance(); 115 newMapper.init(config, properties, decoratorMapper); 116 decoratorMapper = newMapper; 117 } 118 catch (ClassNotFoundException e) { 119 report("Could not load DecoratorMapper class : " + className, e); 120 } 121 catch (Exception e) { 122 report("Could not initialize DecoratorMapper : " + className, e); 123 } 124 } 125 126 public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { 127 try { 128 return Thread.currentThread().getContextClassLoader().loadClass(className); 129 } catch (ClassNotFoundException e) { 130 try { 131 return Class.forName(className); 132 } catch (ClassNotFoundException ex) { 133 try { 134 return BaseFactory.class.getClassLoader().loadClass(className); 135 } catch (ClassNotFoundException exc) { 136 return callingClass.getClassLoader().loadClass(className); 137 } 138 } 139 } 140 } 141 142 143 protected void clearParserMappings() { 144 pageParsers = new HashMap (); 145 } 146 147 151 protected void mapParser(String contentType, String className) { 152 if (className.endsWith(".DefaultPageParser")) { 153 return; } 155 try { 156 PageParser pp = (PageParser) Class.forName(className).newInstance(); 157 pageParsers.put(contentType, pp); 161 } 162 catch (ClassNotFoundException e) { 163 report("Could not load PageParser class : " + className, e); 164 } 165 catch (Exception e) { 166 report("Could not instantiate PageParser : " + className, e); 167 } 168 } 169 170 protected void addExcludeUrl(String path) { 171 excludeUrls.put("", path); 172 } 173 174 177 protected void clearExcludeUrls() { 178 excludeUrls = new PathMapper(); 179 } 180 181 } | Popular Tags |