KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty > JettyPOJOWebServiceHolder


1 /**
2  *
3  * Copyright 2004 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.jetty;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.security.jacc.PolicyContext JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.ServletResponse JavaDoc;
29 import javax.servlet.UnavailableException JavaDoc;
30
31 import org.apache.geronimo.gbean.GBeanInfo;
32 import org.apache.geronimo.gbean.GBeanInfoBuilder;
33 import org.apache.geronimo.gbean.GBeanLifecycle;
34 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
35 import org.apache.geronimo.kernel.StoredObject;
36 import org.apache.geronimo.webservices.POJOWebServiceServlet;
37 import org.apache.geronimo.webservices.WebServiceContainer;
38 import org.apache.geronimo.webservices.WebServiceContainerInvoker;
39 import org.mortbay.jetty.servlet.ServletHolder;
40 import org.mortbay.jetty.servlet.ServletHttpRequest;
41
42
43 /**
44  * This is intended to hold the web service stack for an axis POJO web service.
45  * It is starting life as a copy of JettyServletHolder.
46  *
47  * @version $Rev: 154436 $ $Date: 2005-02-19 10:22:02 -0800 (Sat, 19 Feb 2005) $
48  */

49 public class JettyPOJOWebServiceHolder extends ServletHolder implements GBeanLifecycle {
50     private StoredObject storedWebServiceContainer;
51     private Set JavaDoc servletMappings;
52     private Map JavaDoc webRoleRefPermissions;
53     private JettyServletRegistration context;
54     private String JavaDoc pojoClassName;
55
56     //todo consider interface instead of this constructor for endpoint use.
57
public JettyPOJOWebServiceHolder() {
58
59     }
60
61     public JettyPOJOWebServiceHolder(String JavaDoc pojoClassName,
62                                      String JavaDoc servletName,
63                                      Map JavaDoc initParams,
64                                      Integer JavaDoc loadOnStartup,
65                                      Set JavaDoc servletMappings,
66                                      Map JavaDoc webRoleRefPermissions,
67                                      StoredObject storedWebServiceContainer,
68                                      JettyServletRegistration context) throws Exception JavaDoc {
69         super(context == null ? null : context.getServletHandler(), servletName, POJOWebServiceServlet.class.getName(), null);
70         //context will be null only for use as "default servlet info holder" in deployer.
71

72         this.pojoClassName = pojoClassName;
73         this.context = context;
74         this.storedWebServiceContainer = storedWebServiceContainer;
75         if (context != null) {
76             putAll(initParams);
77             if (loadOnStartup != null) {
78                 setInitOrder(loadOnStartup.intValue());
79             }
80             this.servletMappings = servletMappings;
81             this.webRoleRefPermissions = webRoleRefPermissions == null ? Collections.EMPTY_MAP : webRoleRefPermissions;
82         }
83     }
84
85     //todo how do we stop/destroy the servlet?
86
//todo is start called twice???
87

88     public String JavaDoc getServletName() {
89         return getName();
90     }
91
92     /**
93      * Service a request with this servlet. Set the ThreadLocal to hold the
94      * current JettyServletHolder.
95      */

96     public void handle(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
97             throws ServletException JavaDoc, UnavailableException JavaDoc, IOException JavaDoc {
98
99         // TODO There has to be some way to get this in on the Servlet's init method.
100
// request.setAttribute(POJOWebServiceServlet.WEBSERVICE_CONTAINER, webServiceContainer);
101

102         JettyServletHolder.setCurrentServletName(getServletName());
103         PolicyContext.setHandlerData(ServletHttpRequest.unwrap(request));
104
105         super.handle(request, response);
106     }
107
108     public static final GBeanInfo GBEAN_INFO;
109
110     static {
111         GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(JettyPOJOWebServiceHolder.class, NameFactory.SERVLET_WEB_SERVICE_TEMPLATE);
112         //todo replace with interface
113
infoBuilder.addInterface(ServletHolder.class);
114
115         infoBuilder.addAttribute("pojoClassName", String JavaDoc.class, true);
116         infoBuilder.addAttribute("servletName", String JavaDoc.class, true);
117         infoBuilder.addAttribute("initParams", Map JavaDoc.class, true);
118         infoBuilder.addAttribute("loadOnStartup", Integer JavaDoc.class, true);
119         infoBuilder.addAttribute("servletMappings", Set JavaDoc.class, true);
120         infoBuilder.addAttribute("webRoleRefPermissions", Map JavaDoc.class, true);
121         infoBuilder.addAttribute("webServiceContainer", StoredObject.class, true);
122         infoBuilder.addReference("JettyServletRegistration", JettyServletRegistration.class);
123
124         infoBuilder.setConstructor(new String JavaDoc[]{"pojoClassName",
125                                                 "servletName",
126                                                 "initParams",
127                                                 "loadOnStartup",
128                                                 "servletMappings",
129                                                 "webRoleRefPermissions",
130                                                 "webServiceContainer",
131                                                 "JettyServletRegistration"});
132
133         GBEAN_INFO = infoBuilder.getBeanInfo();
134     }
135
136     public static GBeanInfo getGBeanInfo() {
137         return GBEAN_INFO;
138     }
139
140     public void doStart() throws Exception JavaDoc {
141         if (context != null) {
142             Class JavaDoc pojoClass = context.getWebClassLoader().loadClass(pojoClassName);
143             WebServiceContainer webServiceContainer = (WebServiceContainer) storedWebServiceContainer.getObject(context.getWebClassLoader());
144
145             /* DMB: Hack! I really just want to override initServlet and give a reference of the WebServiceContainer to the servlet before we call init on it.
146              * But this will have to do instead....
147              */

148             ServletContext JavaDoc servletContext = this.context.getServletHandler().getServletContext();
149
150             // Make up an ID for the WebServiceContainer
151
// put a reference the ID in the init-params
152
// put the WebServiceContainer in the webapp context keyed by its ID
153
String JavaDoc webServicecontainerID = getServletName() + WebServiceContainerInvoker.WEBSERVICE_CONTAINER + webServiceContainer.hashCode();
154             put(WebServiceContainerInvoker.WEBSERVICE_CONTAINER, webServicecontainerID);
155             servletContext.setAttribute(webServicecontainerID, webServiceContainer);
156
157             // Same for the POJO Class
158
String JavaDoc pojoClassID = getServletName() + POJOWebServiceServlet.POJO_CLASS + pojoClass.hashCode();
159             put(POJOWebServiceServlet.POJO_CLASS, pojoClassID);
160             servletContext.setAttribute(pojoClassID, pojoClass);
161
162             //this now starts the servlet in the appropriate context
163
context.registerServletHolder(this, getServletName(), this.servletMappings, this.webRoleRefPermissions);
164 // start();
165
}
166     }
167
168     public void doStop() throws Exception JavaDoc {
169     }
170
171     public void doFail() {
172     }
173
174
175 }
176
Popular Tags