1 7 package winstone; 8 9 import java.io.IOException ; 10 import java.util.Collections ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.Map ; 14 15 import javax.servlet.Filter ; 16 import javax.servlet.FilterChain ; 17 import javax.servlet.ServletContext ; 18 import javax.servlet.ServletException ; 19 import javax.servlet.ServletRequest ; 20 import javax.servlet.ServletResponse ; 21 import javax.servlet.UnavailableException ; 22 23 import org.w3c.dom.Node ; 24 25 30 public class FilterConfiguration implements javax.servlet.FilterConfig { 31 final String ELEM_NAME = "filter-name"; 32 final String ELEM_DISPLAY_NAME = "display-name"; 33 final String ELEM_CLASS = "filter-class"; 34 final String ELEM_DESCRIPTION = "description"; 35 final String ELEM_INIT_PARAM = "init-param"; 36 final String ELEM_INIT_PARAM_NAME = "param-name"; 37 final String ELEM_INIT_PARAM_VALUE = "param-value"; 38 39 private String filterName; 40 private String classFile; 41 private Filter instance; 42 private Map initParameters; 43 private ServletContext context; 44 private ClassLoader loader; 45 private boolean unavailableException; 46 private Object filterSemaphore = new Boolean (true); 47 48 protected FilterConfiguration(ServletContext context, ClassLoader loader) { 49 this.context = context; 50 this.loader = loader; 51 this.initParameters = new Hashtable (); 52 } 53 54 57 public FilterConfiguration(ServletContext context, ClassLoader loader, Node elm) { 58 this(context, loader); 59 60 for (int n = 0; n < elm.getChildNodes().getLength(); n++) { 62 Node child = elm.getChildNodes().item(n); 63 if (child.getNodeType() != Node.ELEMENT_NODE) 64 continue; 65 String nodeName = child.getNodeName(); 66 67 if (nodeName.equals(ELEM_NAME)) 69 this.filterName = WebAppConfiguration.getTextFromNode(child); 70 else if (nodeName.equals(ELEM_CLASS)) 71 this.classFile = WebAppConfiguration.getTextFromNode(child); 72 else if (nodeName.equals(ELEM_INIT_PARAM)) { 73 String paramName = null; 74 String paramValue = null; 75 for (int k = 0; k < child.getChildNodes().getLength(); k++) { 76 Node paramNode = child.getChildNodes().item(k); 77 if (paramNode.getNodeType() != Node.ELEMENT_NODE) 78 continue; 79 else if (paramNode.getNodeName().equals( 80 ELEM_INIT_PARAM_NAME)) 81 paramName = WebAppConfiguration.getTextFromNode(paramNode); 82 else if (paramNode.getNodeName().equals( 83 ELEM_INIT_PARAM_VALUE)) 84 paramValue = WebAppConfiguration.getTextFromNode(paramNode); 85 } 86 if ((paramName != null) && (paramValue != null)) 87 this.initParameters.put(paramName, paramValue); 88 } 89 } 90 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, 91 "FilterConfiguration.DeployedInstance", new String [] { 92 this.filterName, this.classFile }); 93 } 94 95 public String getFilterName() { 96 return this.filterName; 97 } 98 99 public String getInitParameter(String paramName) { 100 return (String ) this.initParameters.get(paramName); 101 } 102 103 public Enumeration getInitParameterNames() { 104 return Collections.enumeration(this.initParameters.keySet()); 105 } 106 107 public ServletContext getServletContext() { 108 return this.context; 109 } 110 111 115 public Filter getFilter() throws ServletException { 116 synchronized (this.filterSemaphore) { 117 if (isUnavailable()) 118 throw new WinstoneException(Launcher.RESOURCES 119 .getString("FilterConfiguration.FilterUnavailable")); 120 else if (this.instance == null) 121 try { 122 ClassLoader cl = Thread.currentThread() 123 .getContextClassLoader(); 124 Thread.currentThread().setContextClassLoader(this.loader); 125 126 Class filterClass = Class.forName(classFile, true, 127 this.loader); 128 this.instance = (Filter ) filterClass.newInstance(); 129 Logger.log(Logger.DEBUG, Launcher.RESOURCES, 130 "FilterConfiguration.init", this.filterName); 131 132 this.instance.init(this); 134 Thread.currentThread().setContextClassLoader(cl); 135 } catch (ClassNotFoundException err) { 136 Logger.log(Logger.ERROR, Launcher.RESOURCES, 137 "FilterConfiguration.ClassLoadError", 138 this.classFile, err); 139 } catch (IllegalAccessException err) { 140 Logger.log(Logger.ERROR, Launcher.RESOURCES, 141 "FilterConfiguration.ClassLoadError", 142 this.classFile, err); 143 } catch (InstantiationException err) { 144 Logger.log(Logger.ERROR, Launcher.RESOURCES, 145 "FilterConfiguration.ClassLoadError", 146 this.classFile, err); 147 } catch (ServletException err) { 148 this.instance = null; 149 if (err instanceof UnavailableException ) 150 setUnavailable(); 151 throw err; 152 } 155 } 156 return this.instance; 157 } 158 159 162 public void destroy() { 163 synchronized (this.filterSemaphore) { 164 setUnavailable(); 165 } 166 } 167 168 public String toString() { 169 return Launcher.RESOURCES.getString("FilterConfiguration.Description", 170 new String [] { this.filterName, this.classFile }); 171 } 172 173 public boolean isUnavailable() { 174 return this.unavailableException; 175 } 176 177 protected void setUnavailable() { 178 this.unavailableException = true; 179 if (this.instance != null) { 180 Logger.log(Logger.DEBUG, Launcher.RESOURCES, 181 "FilterConfiguration.destroy", this.filterName); 182 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 183 Thread.currentThread().setContextClassLoader(this.loader); 184 this.instance.destroy(); 185 Thread.currentThread().setContextClassLoader(cl); 186 this.instance = null; 187 } 188 } 189 190 public void execute(ServletRequest request, ServletResponse response, FilterChain chain) 191 throws ServletException , IOException { 192 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 193 Thread.currentThread().setContextClassLoader(this.loader); 194 try { 195 getFilter().doFilter(request, response, chain); 196 } catch (UnavailableException err) { 197 setUnavailable(); 198 throw new ServletException (Launcher.RESOURCES 199 .getString("RequestDispatcher.FilterError"), err); 200 } finally { 201 Thread.currentThread().setContextClassLoader(cl); 202 } 203 } 204 } 205 | Popular Tags |