1 /** 2 * 3 * Copyright 2005 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.webservices; 18 19 import java.io.IOException; 20 import java.security.Principal; 21 import javax.servlet.Servlet; 22 import javax.servlet.ServletConfig; 23 import javax.servlet.ServletException; 24 import javax.servlet.ServletRequest; 25 import javax.servlet.ServletResponse; 26 import javax.servlet.ServletContext; 27 import javax.servlet.http.HttpServletRequest; 28 import javax.servlet.http.HttpSession; 29 import javax.xml.rpc.server.ServiceLifecycle; 30 import javax.xml.rpc.server.ServletEndpointContext; 31 import javax.xml.rpc.ServiceException; 32 import javax.xml.rpc.handler.MessageContext; 33 34 /** 35 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 36 */ 37 public class ServiceLifecycleManager implements Servlet { 38 39 private final ServiceLifecycle managedService; 40 private final Servlet next; 41 42 public ServiceLifecycleManager(Servlet next, ServiceLifecycle managedService) { 43 this.next = next; 44 this.managedService = managedService; 45 } 46 47 public void init(ServletConfig config) throws ServletException { 48 next.init(config); 49 try { 50 managedService.init(new InstanceContext(config.getServletContext())); 51 } catch (ServiceException e) { 52 throw new ServletException("Unable to initialize ServiceEndpoint", e); 53 } 54 } 55 56 public ServletConfig getServletConfig() { 57 return next.getServletConfig(); 58 } 59 60 public String getServletInfo() { 61 return next.getServletInfo(); 62 } 63 64 public void destroy() { 65 managedService.destroy(); 66 next.destroy(); 67 } 68 69 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 70 ServletEndpointContext context = getContext(); 71 try { 72 endpointContext.set(new InvocationContext((HttpServletRequest) req)); 73 next.service(req, res); 74 } finally { 75 endpointContext.set(context); 76 } 77 } 78 79 private static final DefaultContext DEFAULT_CONTEXT = new DefaultContext(); 80 81 private static final ThreadLocal endpointContext = new ThreadLocal(); 82 83 84 private static ServletEndpointContext getContext() { 85 ServletEndpointContext context = (ServletEndpointContext) endpointContext.get(); 86 return context != null ? context : DEFAULT_CONTEXT; 87 } 88 89 static class InstanceContext implements ServletEndpointContext { 90 private final ServletContext servletContext; 91 92 public InstanceContext(ServletContext servletContext) { 93 this.servletContext = servletContext; 94 } 95 96 public MessageContext getMessageContext() { 97 return getContext().getMessageContext(); 98 } 99 100 public Principal getUserPrincipal() { 101 return getContext().getUserPrincipal(); 102 } 103 104 public HttpSession getHttpSession() { 105 return getContext().getHttpSession(); 106 } 107 108 public ServletContext getServletContext() { 109 return servletContext; 110 } 111 112 public boolean isUserInRole(String s) { 113 return getContext().isUserInRole(s); 114 } 115 } 116 117 static class InvocationContext implements ServletEndpointContext { 118 119 private final HttpServletRequest request; 120 121 public InvocationContext(HttpServletRequest request) { 122 this.request = request; 123 } 124 125 public MessageContext getMessageContext() { 126 return (MessageContext) request.getAttribute(WebServiceContainer.MESSAGE_CONTEXT); 127 } 128 129 public Principal getUserPrincipal() { 130 return request.getUserPrincipal(); 131 } 132 133 public HttpSession getHttpSession() { 134 return request.getSession(); 135 } 136 137 public ServletContext getServletContext() { 138 throw new IllegalAccessError("InstanceContext should never delegate this method."); 139 } 140 141 public boolean isUserInRole(String s) { 142 return request.isUserInRole(s); 143 } 144 } 145 146 static class DefaultContext implements ServletEndpointContext { 147 148 public MessageContext getMessageContext() { 149 throw new IllegalStateException("Method cannot be called outside a request context"); 150 } 151 152 public Principal getUserPrincipal() { 153 throw new IllegalStateException("Method cannot be called outside a request context"); 154 } 155 156 public HttpSession getHttpSession() { 157 throw new javax.xml.rpc.JAXRPCException("Method cannot be called outside an http request context"); 158 } 159 160 public ServletContext getServletContext() { 161 throw new IllegalAccessError("InstanceContext should never delegate this method."); 162 } 163 164 public boolean isUserInRole(String s) { 165 throw new IllegalStateException("Method cannot be called outside a request context"); 166 } 167 } 168 }