1 28 29 package com.caucho.server.dispatch; 30 31 import com.caucho.config.BuilderProgram; 32 import com.caucho.config.NodeBuilderProgram; 33 import com.caucho.config.types.InitProgram; 34 import com.caucho.log.Log; 35 import com.caucho.util.L10N; 36 37 import javax.annotation.PostConstruct; 38 import javax.servlet.Filter ; 39 import javax.servlet.ServletException ; 40 import java.util.ArrayList ; 41 import java.util.Enumeration ; 42 import java.util.Hashtable ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 49 public class FilterManager { 50 static final Logger log = Log.open(FilterManager.class); 51 52 static final L10N L = new L10N(FilterManager.class); 53 54 private Hashtable <String ,FilterConfigImpl> _filters 55 = new Hashtable <String ,FilterConfigImpl>(); 56 57 private Hashtable <String ,Filter > _instances 58 = new Hashtable <String ,Filter >(); 59 60 63 public void addFilter(FilterConfigImpl config) 64 { 65 if (config.getServletContext() == null) 66 throw new NullPointerException (); 67 68 _filters.put(config.getFilterName(), config); 69 } 70 71 74 public FilterConfigImpl getFilter(String filterName) 75 { 76 return _filters.get(filterName); 77 } 78 79 82 @PostConstruct 83 public void init() 84 { 85 for (String name : _filters.keySet()) { 86 try { 87 createFilter(name); 88 } catch (Exception e) { 89 log.log(Level.WARNING, e.toString(), e); 90 } 91 } 92 } 93 94 101 public Filter createFilter(String filterName) 102 throws ServletException 103 { 104 FilterConfigImpl config = _filters.get(filterName); 105 106 if (config == null) { 107 throw new ServletException (L.l("`{0}' is not a known filter. Filters must be defined by <filter> before being used.", filterName)); 108 } 109 110 Class filterClass = config.getFilterClass(); 111 112 116 117 synchronized (config) { 118 try { 119 Filter filter = _instances.get(filterName); 120 121 if (filter != null) 122 return filter; 123 124 filter = (Filter ) filterClass.newInstance(); 125 126 128 InitProgram init = config.getInit(); 130 BuilderProgram program = NodeBuilderProgram.NULL; 131 132 if (init != null) 133 program = init.getBuilderProgram(); 134 135 program.configure(filter); 136 program.init(filter); 137 138 filter.init(config); 139 140 _instances.put(filterName, filter); 141 142 157 158 return filter; 159 } catch (ServletException e) { 160 162 164 throw e; 165 } catch (Throwable e) { 166 168 throw new ServletException (e); 169 } 170 } 171 } 172 173 public void destroy() 174 { 175 ArrayList <Filter > filterList = new ArrayList <Filter >(); 176 177 if (_instances != null) { 178 synchronized (_instances) { 179 Enumeration <Filter > en = _instances.elements(); 180 while (en.hasMoreElements()) { 181 Filter filter = en.nextElement(); 182 183 filterList.add(filter); 184 } 185 } 186 } 187 188 for (int i = 0; i < filterList.size(); i++) { 189 Filter filter = filterList.get(i); 190 191 try { 192 filter.destroy(); 193 } catch (Throwable e) { 194 } 196 } 197 } 198 } 199 | Popular Tags |