1 16 package org.apache.commons.chain.web; 17 18 19 import java.io.InputStream ; 20 import java.net.URL ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletContextEvent ; 26 import javax.servlet.ServletContextListener ; 27 import org.apache.commons.chain.Catalog; 28 import org.apache.commons.chain.CatalogFactory; 29 import org.apache.commons.chain.config.ConfigParser; 30 import org.apache.commons.chain.impl.CatalogBase; 31 import org.apache.commons.digester.RuleSet; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 36 97 98 public class ChainListener implements ServletContextListener { 99 100 101 103 104 109 public static final String CONFIG_ATTR = 110 "org.apache.commons.chain.CONFIG_ATTR"; 111 112 113 117 public static final String CONFIG_CLASS_RESOURCE = 118 "org.apache.commons.chain.CONFIG_CLASS_RESOURCE"; 119 120 121 125 public static final String CONFIG_WEB_RESOURCE = 126 "org.apache.commons.chain.CONFIG_WEB_RESOURCE"; 127 128 129 134 public static final String RULE_SET = 135 "org.apache.commons.chain.RULE_SET"; 136 137 138 140 141 144 private static final Log log = LogFactory.getLog(ChainListener.class); 145 146 147 149 150 156 public void contextDestroyed(ServletContextEvent event) { 157 158 ServletContext context = event.getServletContext(); 159 String attr = context.getInitParameter(CONFIG_ATTR); 160 if (attr != null) { 161 context.removeAttribute(attr); 162 } 163 CatalogFactory.clear(); 164 165 } 166 167 168 175 public void contextInitialized(ServletContextEvent event) { 176 177 if (log.isInfoEnabled()) { 178 log.info("Initializing chain listener"); 179 } 180 ServletContext context = event.getServletContext(); 181 182 String attr = context.getInitParameter(CONFIG_ATTR); 184 String classResources = 185 context.getInitParameter(CONFIG_CLASS_RESOURCE); 186 String ruleSet = context.getInitParameter(RULE_SET); 187 String webResources = context.getInitParameter(CONFIG_WEB_RESOURCE); 188 189 Catalog catalog = null; 191 if (attr != null) { 192 catalog = (Catalog) context.getAttribute(attr); 193 if (catalog == null) { 194 catalog = new CatalogBase(); 195 } 196 } 197 198 ConfigParser parser = new ConfigParser(); 200 if (ruleSet != null) { 201 try { 202 ClassLoader loader = 203 Thread.currentThread().getContextClassLoader(); 204 if (loader == null) { 205 loader = this.getClass().getClassLoader(); 206 } 207 Class clazz = loader.loadClass(ruleSet); 208 parser.setRuleSet((RuleSet) clazz.newInstance()); 209 } catch (Exception e) { 210 throw new RuntimeException ("Exception initalizing RuleSet '" 211 + ruleSet + "' instance: " 212 + e.getMessage()); 213 } 214 } 215 216 if (attr == null) { 218 parseJarResources(context, parser); 219 ChainResources.parseClassResources 220 (classResources, parser); 221 ChainResources.parseWebResources 222 (context, webResources, parser); 223 } else { 224 parseJarResources(catalog, context, parser); 225 ChainResources.parseClassResources 226 (catalog, classResources, parser); 227 ChainResources.parseWebResources 228 (catalog, context, webResources, parser); 229 } 230 231 if (attr != null) { 233 context.setAttribute(attr, catalog); 234 } 235 236 } 237 238 239 241 242 249 private void parseJarResources(ServletContext context, 250 ConfigParser parser) { 251 252 Set jars = context.getResourcePaths("/WEB-INF/lib"); 253 if (jars == null) { 254 jars = new HashSet (); 255 } 256 String path = null; 257 Iterator paths = jars.iterator(); 258 while (paths.hasNext()) { 259 260 path = (String ) paths.next(); 261 if (!path.endsWith(".jar")) { 262 continue; 263 } 264 URL resourceURL = null; 265 try { 266 URL jarURL = context.getResource(path); 267 resourceURL = new URL ("jar:" 268 + translate(jarURL.toExternalForm()) 269 + "!/META-INF/chain-config.xml"); 270 if (resourceURL == null) { 271 continue; 272 } 273 InputStream is = null; 274 try { 275 is = resourceURL.openStream(); 276 } catch (Exception e) { 277 ; } 279 if (is == null) { 280 continue; 281 } else { 282 is.close(); 283 } 284 parser.parse(resourceURL); 285 } catch (Exception e) { 286 throw new RuntimeException 287 ("Exception parsing chain config resource '" 288 + resourceURL.toExternalForm() + "': " 289 + e.getMessage()); 290 } 291 } 292 293 } 294 295 296 307 private void parseJarResources(Catalog catalog, ServletContext context, 308 ConfigParser parser) { 309 310 Set jars = context.getResourcePaths("/WEB-INF/lib"); 311 if (jars == null) { 312 jars = new HashSet (); 313 } 314 String path = null; 315 Iterator paths = jars.iterator(); 316 while (paths.hasNext()) { 317 318 path = (String ) paths.next(); 319 if (!path.endsWith(".jar")) { 320 continue; 321 } 322 URL resourceURL = null; 323 try { 324 URL jarURL = context.getResource(path); 325 resourceURL = new URL ("jar:" 326 + translate(jarURL.toExternalForm()) 327 + "!/META-INF/chain-config.xml"); 328 if (resourceURL == null) { 329 continue; 330 } 331 InputStream is = null; 332 try { 333 is = resourceURL.openStream(); 334 } catch (Exception e) { 335 ; } 337 if (is == null) { 338 continue; 339 } else { 340 is.close(); 341 } 342 parser.parse(catalog, resourceURL); 343 } catch (Exception e) { 344 throw new RuntimeException 345 ("Exception parsing chain config resource '" 346 + resourceURL.toExternalForm() + "': " 347 + e.getMessage()); 348 } 349 } 350 351 } 352 353 354 360 private String translate(String value) { 361 362 while (true) { 363 int index = value.indexOf(' '); 364 if (index < 0) { 365 break; 366 } 367 value = value.substring(0, index) + value.substring(index + 1); 368 } 369 return (value); 370 371 } 372 373 374 } 375 | Popular Tags |