KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > ServiceLifecycleManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.security.Principal JavaDoc;
21 import javax.servlet.Servlet JavaDoc;
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
30 import javax.xml.rpc.server.ServletEndpointContext JavaDoc;
31 import javax.xml.rpc.ServiceException JavaDoc;
32 import javax.xml.rpc.handler.MessageContext JavaDoc;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class ServiceLifecycleManager implements Servlet JavaDoc {
38
39     private final ServiceLifecycle JavaDoc managedService;
40     private final Servlet JavaDoc next;
41
42     public ServiceLifecycleManager(Servlet JavaDoc next, ServiceLifecycle JavaDoc managedService) {
43         this.next = next;
44         this.managedService = managedService;
45     }
46
47     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
48         next.init(config);
49         try {
50             managedService.init(new InstanceContext(config.getServletContext()));
51         } catch (ServiceException JavaDoc e) {
52             throw new ServletException JavaDoc("Unable to initialize ServiceEndpoint", e);
53         }
54     }
55
56     public ServletConfig JavaDoc getServletConfig() {
57         return next.getServletConfig();
58     }
59
60     public String JavaDoc getServletInfo() {
61         return next.getServletInfo();
62     }
63
64     public void destroy() {
65         managedService.destroy();
66         next.destroy();
67     }
68
69     public void service(ServletRequest JavaDoc req, ServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
70         ServletEndpointContext JavaDoc context = getContext();
71         try {
72             endpointContext.set(new InvocationContext((HttpServletRequest JavaDoc) 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 JavaDoc endpointContext = new ThreadLocal JavaDoc();
82
83
84     private static ServletEndpointContext JavaDoc getContext() {
85         ServletEndpointContext JavaDoc context = (ServletEndpointContext JavaDoc) endpointContext.get();
86         return context != null ? context : DEFAULT_CONTEXT;
87     }
88
89     static class InstanceContext implements ServletEndpointContext JavaDoc {
90         private final ServletContext JavaDoc servletContext;
91
92         public InstanceContext(ServletContext JavaDoc servletContext) {
93             this.servletContext = servletContext;
94         }
95
96         public MessageContext JavaDoc getMessageContext() {
97             return getContext().getMessageContext();
98         }
99
100         public Principal JavaDoc getUserPrincipal() {
101             return getContext().getUserPrincipal();
102         }
103
104         public HttpSession JavaDoc getHttpSession() {
105             return getContext().getHttpSession();
106         }
107
108         public ServletContext JavaDoc getServletContext() {
109             return servletContext;
110         }
111
112         public boolean isUserInRole(String JavaDoc s) {
113             return getContext().isUserInRole(s);
114         }
115     }
116
117     static class InvocationContext implements ServletEndpointContext JavaDoc {
118
119         private final HttpServletRequest JavaDoc request;
120
121         public InvocationContext(HttpServletRequest JavaDoc request) {
122             this.request = request;
123         }
124
125         public MessageContext JavaDoc getMessageContext() {
126             return (MessageContext JavaDoc) request.getAttribute(WebServiceContainer.MESSAGE_CONTEXT);
127         }
128
129         public Principal JavaDoc getUserPrincipal() {
130             return request.getUserPrincipal();
131         }
132
133         public HttpSession JavaDoc getHttpSession() {
134             return request.getSession();
135         }
136
137         public ServletContext JavaDoc getServletContext() {
138             throw new IllegalAccessError JavaDoc("InstanceContext should never delegate this method.");
139         }
140
141         public boolean isUserInRole(String JavaDoc s) {
142             return request.isUserInRole(s);
143         }
144     }
145
146     static class DefaultContext implements ServletEndpointContext JavaDoc {
147
148         public MessageContext JavaDoc getMessageContext() {
149             throw new IllegalStateException JavaDoc("Method cannot be called outside a request context");
150         }
151
152         public Principal JavaDoc getUserPrincipal() {
153             throw new IllegalStateException JavaDoc("Method cannot be called outside a request context");
154         }
155
156         public HttpSession JavaDoc getHttpSession() {
157             throw new javax.xml.rpc.JAXRPCException JavaDoc("Method cannot be called outside an http request context");
158         }
159
160         public ServletContext JavaDoc getServletContext() {
161             throw new IllegalAccessError JavaDoc("InstanceContext should never delegate this method.");
162         }
163
164         public boolean isUserInRole(String JavaDoc s) {
165             throw new IllegalStateException JavaDoc("Method cannot be called outside a request context");
166         }
167     }
168 }
169
Popular Tags