001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.webservices;
017:
018: import java.io.IOException;
019: import java.security.Principal;
020: import javax.servlet.Servlet;
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletException;
023: import javax.servlet.ServletRequest;
024: import javax.servlet.ServletResponse;
025: import javax.servlet.ServletContext;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028: import javax.xml.rpc.server.ServiceLifecycle;
029: import javax.xml.rpc.server.ServletEndpointContext;
030: import javax.xml.rpc.ServiceException;
031: import javax.xml.rpc.handler.MessageContext;
032:
033: /**
034: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
035: */
036: public class ServiceLifecycleManager implements Servlet {
037:
038: private final ServiceLifecycle managedService;
039: private final Servlet next;
040:
041: public ServiceLifecycleManager(Servlet next,
042: ServiceLifecycle managedService) {
043: this .next = next;
044: this .managedService = managedService;
045: }
046:
047: public void init(ServletConfig config) throws ServletException {
048: next.init(config);
049: try {
050: managedService.init(new InstanceContext(config
051: .getServletContext()));
052: } catch (ServiceException e) {
053: throw new ServletException(
054: "Unable to initialize ServiceEndpoint", e);
055: }
056: }
057:
058: public ServletConfig getServletConfig() {
059: return next.getServletConfig();
060: }
061:
062: public String getServletInfo() {
063: return next.getServletInfo();
064: }
065:
066: public void destroy() {
067: managedService.destroy();
068: next.destroy();
069: }
070:
071: public void service(ServletRequest req, ServletResponse res)
072: throws ServletException, IOException {
073: ServletEndpointContext context = getContext();
074: try {
075: endpointContext.set(new InvocationContext(
076: (HttpServletRequest) req));
077: next.service(req, res);
078: } finally {
079: endpointContext.set(context);
080: }
081: }
082:
083: private static final DefaultContext DEFAULT_CONTEXT = new DefaultContext();
084:
085: private static final ThreadLocal endpointContext = new ThreadLocal();
086:
087: private static ServletEndpointContext getContext() {
088: ServletEndpointContext context = (ServletEndpointContext) endpointContext
089: .get();
090: return context != null ? context : DEFAULT_CONTEXT;
091: }
092:
093: static class InstanceContext implements ServletEndpointContext {
094: private final ServletContext servletContext;
095:
096: public InstanceContext(ServletContext servletContext) {
097: this .servletContext = servletContext;
098: }
099:
100: public MessageContext getMessageContext() {
101: return getContext().getMessageContext();
102: }
103:
104: public Principal getUserPrincipal() {
105: return getContext().getUserPrincipal();
106: }
107:
108: public HttpSession getHttpSession() {
109: return getContext().getHttpSession();
110: }
111:
112: public ServletContext getServletContext() {
113: return servletContext;
114: }
115:
116: public boolean isUserInRole(String s) {
117: return getContext().isUserInRole(s);
118: }
119: }
120:
121: static class InvocationContext implements ServletEndpointContext {
122:
123: private final HttpServletRequest request;
124:
125: public InvocationContext(HttpServletRequest request) {
126: this .request = request;
127: }
128:
129: public MessageContext getMessageContext() {
130: return (MessageContext) request
131: .getAttribute(WebServiceContainer.MESSAGE_CONTEXT);
132: }
133:
134: public Principal getUserPrincipal() {
135: return request.getUserPrincipal();
136: }
137:
138: public HttpSession getHttpSession() {
139: return request.getSession();
140: }
141:
142: public ServletContext getServletContext() {
143: throw new IllegalAccessError(
144: "InstanceContext should never delegate this method.");
145: }
146:
147: public boolean isUserInRole(String s) {
148: return request.isUserInRole(s);
149: }
150: }
151:
152: static class DefaultContext implements ServletEndpointContext {
153:
154: public MessageContext getMessageContext() {
155: throw new IllegalStateException(
156: "Method cannot be called outside a request context");
157: }
158:
159: public Principal getUserPrincipal() {
160: throw new IllegalStateException(
161: "Method cannot be called outside a request context");
162: }
163:
164: public HttpSession getHttpSession() {
165: throw new javax.xml.rpc.JAXRPCException(
166: "Method cannot be called outside an http request context");
167: }
168:
169: public ServletContext getServletContext() {
170: throw new IllegalAccessError(
171: "InstanceContext should never delegate this method.");
172: }
173:
174: public boolean isUserInRole(String s) {
175: throw new IllegalStateException(
176: "Method cannot be called outside a request context");
177: }
178: }
179: }
|