1 17 package org.apache.geronimo.tomcat; 18 19 import java.beans.PropertyChangeListener ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.naming.NamingException ; 26 import javax.security.auth.Subject ; 27 import javax.security.jacc.PolicyContext ; 28 import javax.servlet.Servlet ; 29 import javax.servlet.ServletException ; 30 31 import org.apache.catalina.Container; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.Loader; 34 import org.apache.catalina.Manager; 35 import org.apache.catalina.Valve; 36 import org.apache.catalina.Wrapper; 37 import org.apache.catalina.connector.Request; 38 import org.apache.catalina.connector.Response; 39 import org.apache.catalina.core.StandardContext; 40 import org.apache.catalina.valves.ValveBase; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.apache.geronimo.common.DeploymentException; 44 import org.apache.geronimo.naming.enc.EnterpriseNamingContext; 45 import org.apache.geronimo.security.ContextManager; 46 import org.apache.geronimo.security.IdentificationPrincipal; 47 import org.apache.geronimo.security.SubjectId; 48 import org.apache.geronimo.security.deploy.DefaultPrincipal; 49 import org.apache.geronimo.security.util.ConfigurationUtil; 50 import org.apache.geronimo.tomcat.interceptor.BeforeAfter; 51 import org.apache.geronimo.tomcat.interceptor.ComponentContextBeforeAfter; 52 import org.apache.geronimo.tomcat.interceptor.InstanceContextBeforeAfter; 53 import org.apache.geronimo.tomcat.interceptor.PolicyContextBeforeAfter; 54 import org.apache.geronimo.tomcat.util.SecurityHolder; 55 import org.apache.geronimo.tomcat.valve.DefaultSubjectValve; 56 import org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve; 57 import org.apache.geronimo.webservices.POJOWebServiceServlet; 58 import org.apache.geronimo.webservices.WebServiceContainer; 59 import org.apache.geronimo.webservices.WebServiceContainerInvoker; 60 61 62 public class GeronimoStandardContext extends StandardContext { 63 64 private static final Log log = LogFactory.getLog(GeronimoStandardContext.class); 65 66 private static final long serialVersionUID = 3834587716552831032L; 67 68 private Subject defaultSubject = null; 69 70 private Map webServiceMap = null; 71 72 private boolean pipelineInitialized; 73 74 private BeforeAfter beforeAfter = null; 75 private int contextCount = 0; 76 77 public void setContextProperties(TomcatContext ctx) throws DeploymentException { 78 if (ctx instanceof TomcatWebAppContext) { 80 TomcatWebAppContext tctx = (TomcatWebAppContext) ctx; 81 setJavaVMs(tctx.getJavaVMs()); 82 setServer(tctx.getServer()); 83 setJ2EEApplication(tctx.getJ2EEApplication()); 84 setJ2EEServer(tctx.getJ2EEServer()); 85 } 86 javax.naming.Context enc = null; 88 Map componentContext = ctx.getComponentContext(); 89 try { 90 if (componentContext != null) { 91 enc = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext, ctx.getUserTransaction(), ctx.getKernel(), ctx.getClassLoader()); 92 } 93 } catch (NamingException ne) { 94 log.error(ne); 95 } 96 97 int index = 0; 98 BeforeAfter interceptor = new InstanceContextBeforeAfter(null, 99 index++, 100 index++, ctx.getUnshareableResources(), 101 ctx.getApplicationManagedSecurityResources(), 102 ctx.getTrackedConnectionAssociator()); 103 104 if (enc != null) { 106 interceptor = new ComponentContextBeforeAfter(interceptor, index++, enc); 107 } 108 109 SecurityHolder securityHolder = ctx.getSecurityHolder(); 111 if (securityHolder != null) { 112 if (securityHolder.getPolicyContextID() != null) { 113 114 PolicyContext.setContextID(securityHolder.getPolicyContextID()); 115 116 119 DefaultPrincipal defaultPrincipal = securityHolder.getDefaultPrincipal(); 120 if (defaultPrincipal != null) { 121 defaultSubject = ConfigurationUtil.generateDefaultSubject(defaultPrincipal, ctx.getClassLoader()); 122 ContextManager.registerSubject(defaultSubject); 123 SubjectId id = ContextManager.getSubjectId(defaultSubject); 124 defaultSubject.getPrincipals().add(new IdentificationPrincipal(id)); 125 } 126 127 interceptor = new PolicyContextBeforeAfter(interceptor, index++, index++, securityHolder.getPolicyContextID()); 128 } 129 } 130 131 GeronimoBeforeAfterValve geronimoBAValve = new GeronimoBeforeAfterValve(interceptor, index); 133 addValve(geronimoBAValve); 134 beforeAfter = interceptor; 135 contextCount = index; 136 137 140 addValve(new SystemMethodValve()); 141 142 List valveChain = ctx.getValveChain(); 144 if (valveChain != null) { 145 Iterator iterator = valveChain.iterator(); 146 while (iterator.hasNext()) { 147 Valve valve = (Valve) iterator.next(); 148 addValve(valve); 149 } 150 } 151 152 156 Manager manager = ctx.getManager(); 157 if (manager != null) 158 this.setManager(manager); 159 160 pipelineInitialized = true; 161 this.webServiceMap = ctx.getWebServices(); 162 163 this.setCrossContext(ctx.isCrossContext()); 164 165 this.setCookies(!ctx.isDisableCookies()); 166 167 this.addInstanceListener("org.apache.geronimo.tomcat.listener.DispatchListener"); 169 } 170 171 public synchronized void start() throws LifecycleException { 172 if (pipelineInitialized) { 173 try { 174 Valve valve = getFirst(); 175 valve.invoke(null, null); 176 179 Valve defaultSubjectValve = new DefaultSubjectValve(defaultSubject); 180 addValve(defaultSubjectValve); 181 } catch (IOException e) { 182 if (e.getCause() instanceof LifecycleException) { 183 throw (LifecycleException) e.getCause(); 184 } 185 throw new LifecycleException(e); 186 } catch (ServletException e) { 187 throw new LifecycleException(e); 188 } 189 } else 190 super.start(); 191 } 192 193 public synchronized void stop() throws LifecycleException { 194 if (defaultSubject != null) { 196 ContextManager.unregisterSubject(defaultSubject); 197 } 198 199 super.stop(); 200 } 201 202 public void addChild(Container child) { 203 Wrapper wrapper = (Wrapper) child; 204 205 String servletClassName = wrapper.getServletClass(); 206 if (servletClassName == null) { 207 super.addChild(child); 208 return; 209 } 210 211 ClassLoader cl = this.getParentClassLoader(); 212 213 Class baseServletClass; 214 Class servletClass; 215 try { 216 baseServletClass = cl.loadClass(Servlet .class.getName()); 217 servletClass = cl.loadClass(servletClassName); 218 if (!baseServletClass.isAssignableFrom(servletClass)) { 220 if (webServiceMap != null) { 222 WebServiceContainer webServiceContainer = (WebServiceContainer) webServiceMap.get(wrapper.getName()); 223 224 if (webServiceContainer != null) { 225 wrapper.setServletClass("org.apache.geronimo.webservices.POJOWebServiceServlet"); 228 229 String webServicecontainerID = wrapper.getName() + WebServiceContainerInvoker.WEBSERVICE_CONTAINER + webServiceContainer.hashCode(); 231 getServletContext().setAttribute(webServicecontainerID, webServiceContainer); 232 wrapper.addInitParameter(WebServiceContainerInvoker.WEBSERVICE_CONTAINER, webServicecontainerID); 233 234 String pojoClassID = wrapper.getName() + POJOWebServiceServlet.POJO_CLASS + servletClass.hashCode(); 236 getServletContext().setAttribute(pojoClassID, servletClass); 237 wrapper.addInitParameter(POJOWebServiceServlet.POJO_CLASS, pojoClassID); 238 } 239 } 240 } 241 } catch (ClassNotFoundException e) { 242 throw new RuntimeException (e.getMessage(), e); 243 } 244 245 super.addChild(child); 246 } 247 248 public synchronized void setLoader(final Loader delegate) { 249 Loader loader = new Loader() { 250 251 public void backgroundProcess() { 252 delegate.backgroundProcess(); 253 } 254 255 public ClassLoader getClassLoader() { 256 return parentClassLoader; 259 } 260 261 public Container getContainer() { 262 return delegate.getContainer(); 263 } 264 265 public void setContainer(Container container) { 266 delegate.setContainer(container); 267 } 268 269 public boolean getDelegate() { 270 return delegate.getDelegate(); 271 } 272 273 public void setDelegate(boolean delegateBoolean) { 274 delegate.setDelegate(delegateBoolean); 275 } 276 277 public String getInfo() { 278 return delegate.getInfo(); 279 } 280 281 public boolean getReloadable() { 282 return false; 283 } 284 285 public void setReloadable(boolean reloadable) { 286 if (reloadable) { 287 throw new UnsupportedOperationException ("Reloadable context is not supported."); 288 } 289 } 290 291 public void addPropertyChangeListener(PropertyChangeListener listener) { 292 delegate.addPropertyChangeListener(listener); 293 } 294 295 public void addRepository(String repository) { 296 delegate.addRepository(repository); 297 } 298 299 public String [] findRepositories() { 300 return delegate.findRepositories(); 301 } 302 303 public boolean modified() { 304 return delegate.modified(); 305 } 306 307 public void removePropertyChangeListener(PropertyChangeListener listener) { 308 delegate.removePropertyChangeListener(listener); 309 } 310 }; 311 312 super.setLoader(loader); 313 } 314 315 private class SystemMethodValve extends ValveBase { 316 317 public void invoke(Request request, Response response) throws IOException , ServletException { 318 if (request == null && response == null) { 319 try { 320 GeronimoStandardContext.super.start(); 321 } catch (LifecycleException e) { 322 throw (IOException ) new IOException ("wrapping lifecycle exception").initCause(e); 323 } 324 } else { 325 getNext().invoke(request, response); 326 } 327 328 } 329 } 330 331 332 public BeforeAfter getBeforeAfter() { 333 return beforeAfter; 334 } 335 336 public int getContextCount() { 337 return contextCount; 338 } 339 340 } 341 | Popular Tags |