1 15 package org.apache.hivemind.servlet; 16 17 import java.io.IOException ; 18 import java.util.Locale ; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletResponse ; 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.hivemind.ClassResolver; 32 import org.apache.hivemind.Registry; 33 import org.apache.hivemind.impl.DefaultClassResolver; 34 import org.apache.hivemind.impl.RegistryBuilder; 35 36 48 public class AutoloadingHiveMindFilter implements Filter 49 { 50 private static final Log LOG = LogFactory.getLog(AutoloadingHiveMindFilter.class); 51 52 55 56 static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry"; 57 58 static final String REBUILD_REQUEST_KEY = "org.apache.hivemind.RebuildRegistry"; 59 60 private FilterConfig _filterConfig; 61 62 private Registry _registry; 63 64 68 public void init(FilterConfig config) throws ServletException 69 { 70 _filterConfig = config; 71 72 initializeRegistry(); 73 74 } 75 76 private void initializeRegistry() 77 { 78 long startTime = System.currentTimeMillis(); 79 80 LOG.info(ServletMessages.filterInit()); 81 82 try 83 { 84 _registry = constructRegistry(_filterConfig); 85 86 LOG.info(ServletMessages.constructedRegistry(_registry, System.currentTimeMillis() 87 - startTime)); 88 } 89 catch (Exception ex) 90 { 91 LOG.error(ex.getMessage(), ex); 92 } 93 } 94 95 100 protected Registry constructRegistry(FilterConfig config) 101 { 102 RegistryBuilder builder = new RegistryBuilder(); 103 104 ClassResolver resolver = new DefaultClassResolver(); 105 106 addDefaultModules(builder, resolver); 107 108 addWebInfDescriptor(config.getServletContext(), resolver, builder); 109 110 return builder.constructRegistry(getRegistryLocale()); 111 } 112 113 118 protected void addWebInfDescriptor(ServletContext context, ClassResolver resolver, 119 RegistryBuilder builder) 120 { 121 } 122 123 127 protected Locale getRegistryLocale() 128 { 129 return Locale.getDefault(); 130 } 131 132 protected void addDefaultModules(RegistryBuilder builder, ClassResolver resolver) 133 { 134 builder.autoDetectModules(); 135 } 136 137 141 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 142 throws IOException , ServletException 143 { 144 try 145 { 146 148 if (_registry != null) 149 _registry.setupThread(); 150 151 request.setAttribute(REQUEST_KEY, _registry); 152 153 chain.doFilter(request, response); 154 } 155 finally 156 { 157 cleanupThread(); 158 159 checkRegistryRebuild(request); 160 } 161 } 162 163 private synchronized void checkRegistryRebuild(ServletRequest request) 164 { 165 if (request.getAttribute(REBUILD_REQUEST_KEY) == null) 166 return; 167 168 Registry oldRegistry = _registry; 169 170 174 initializeRegistry(); 175 176 184 oldRegistry.shutdown(); 185 } 186 187 190 private void cleanupThread() 191 { 192 try 193 { 194 _registry.cleanupThread(); 195 } 196 catch (Exception ex) 197 { 198 LOG.error(ServletMessages.filterCleanupError(ex), ex); 199 } 200 } 201 202 205 public void destroy() 206 { 207 if (_registry != null) 208 _registry.shutdown(); 209 210 _filterConfig = null; 211 } 212 213 217 public static Registry getRegistry(HttpServletRequest request) 218 { 219 return (Registry) request.getAttribute(REQUEST_KEY); 220 } 221 222 226 public static void rebuildRegistry(HttpServletRequest request) 227 { 228 request.setAttribute(REBUILD_REQUEST_KEY, Boolean.TRUE); 229 } 230 231 } | Popular Tags |