1 16 package org.apache.commons.chain.web; 17 18 19 import java.io.IOException ; 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletContext ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import org.apache.commons.chain.Catalog; 27 import org.apache.commons.chain.CatalogFactory; 28 import org.apache.commons.chain.config.ConfigParser; 29 import org.apache.commons.chain.impl.CatalogBase; 30 import org.apache.commons.digester.RuleSet; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 35 94 95 public class ChainServlet extends HttpServlet { 96 97 98 100 101 106 public static final String CONFIG_ATTR = 107 "org.apache.commons.chain.CONFIG_ATTR"; 108 109 110 114 public static final String CONFIG_CLASS_RESOURCE = 115 "org.apache.commons.chain.CONFIG_CLASS_RESOURCE"; 116 117 118 122 public static final String CONFIG_WEB_RESOURCE = 123 "org.apache.commons.chain.CONFIG_WEB_RESOURCE"; 124 125 126 131 public static final String RULE_SET = 132 "org.apache.commons.chain.RULE_SET"; 133 134 135 137 138 141 protected static final Log log = LogFactory.getLog(ChainServlet.class); 142 143 144 146 147 150 public void destroy() { 151 152 ServletConfig config = getServletConfig(); 153 ServletContext context = getServletContext(); 154 String attr = config.getInitParameter(CONFIG_ATTR); 155 if (attr != null) { 156 context.removeAttribute(attr); 157 } 158 CatalogFactory.clear(); 159 160 } 161 162 163 169 public void init() throws ServletException { 170 171 ServletConfig config = getServletConfig(); 172 ServletContext context = getServletContext(); 173 if (log.isInfoEnabled()) { 174 log.info("Initializing chain servlet '" 175 + config.getServletName() + "'"); 176 } 177 178 String attr = config.getInitParameter(CONFIG_ATTR); 180 String classResources = 181 context.getInitParameter(CONFIG_CLASS_RESOURCE); 182 String ruleSet = context.getInitParameter(RULE_SET); 183 String webResources = context.getInitParameter(CONFIG_WEB_RESOURCE); 184 185 Catalog catalog = null; 187 if (attr != null) { 188 catalog = (Catalog) context.getAttribute(attr); 189 if (catalog == null) { 190 catalog = new CatalogBase(); 191 } 192 } 193 194 ConfigParser parser = new ConfigParser(); 196 if (ruleSet != null) { 197 try { 198 ClassLoader loader = 199 Thread.currentThread().getContextClassLoader(); 200 if (loader == null) { 201 loader = this.getClass().getClassLoader(); 202 } 203 Class clazz = loader.loadClass(ruleSet); 204 parser.setRuleSet((RuleSet) clazz.newInstance()); 205 } catch (Exception e) { 206 throw new ServletException ("Exception initalizing RuleSet '" 207 + ruleSet + "' instance", e); 208 } 209 } 210 211 if (attr == null) { 213 ChainResources.parseClassResources 214 (classResources, parser); 215 ChainResources.parseWebResources 216 (context, webResources, parser); 217 } else { 218 ChainResources.parseClassResources 219 (catalog, classResources, parser); 220 ChainResources.parseWebResources 221 (catalog, context, webResources, parser); 222 } 223 224 if (attr != null) { 226 context.setAttribute(attr, catalog); 227 } 228 229 } 230 231 232 242 public void service(HttpServletRequest request, 243 HttpServletResponse response) 244 throws ServletException , IOException { 245 246 ; 248 } 249 250 251 } 252 | Popular Tags |