KickJava   Java API By Example, From Geeks To Geeks.

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


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.jetty6;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.security.jacc.PolicyContext JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.UnavailableException JavaDoc;
29 import javax.servlet.http.HttpServletRequest 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.webservices.POJOWebServiceServlet;
36 import org.apache.geronimo.webservices.WebServiceContainer;
37 import org.apache.geronimo.webservices.WebServiceContainerInvoker;
38 import org.apache.geronimo.webservices.WebServiceContainerFactory;
39 import org.mortbay.jetty.Request;
40 import org.mortbay.jetty.servlet.ServletHolder;
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: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
48  */

49 public class JettyPOJOWebServiceHolder implements GBeanLifecycle {
50     private final ServletHolder servletHolder;
51     private final WebServiceContainer webServiceContainer;
52     private final Set JavaDoc servletMappings;
53     private final JettyServletRegistration context;
54     private final String JavaDoc pojoClassName;
55
56     //todo consider interface instead of this constructor for endpoint use.
57
public JettyPOJOWebServiceHolder() {
58         servletHolder = null;
59         webServiceContainer = null;
60         servletMappings = null;
61         context = null;
62         pojoClassName = null;
63     }
64
65     public JettyPOJOWebServiceHolder(String JavaDoc pojoClassName,
66             String JavaDoc servletName,
67             Map JavaDoc initParams,
68             Integer JavaDoc loadOnStartup,
69             Set JavaDoc servletMappings,
70             WebServiceContainerFactory webServiceContainerFactory,
71             JettyServletRegistration context) throws Exception JavaDoc {
72         servletHolder = new ServletHolder();
73         //context will be null only for use as "default servlet info holder" in deployer.
74

75         this.pojoClassName = pojoClassName;
76         this.context = context;
77         this.webServiceContainer = webServiceContainerFactory == null? null: webServiceContainerFactory.getWebServiceContainer();
78         this.servletMappings = servletMappings;
79         if (context != null) {
80             servletHolder.setName(servletName);
81             servletHolder.setClassName(POJOWebServiceServlet.class.getName());
82             servletHolder.setInitParameters(initParams);
83             if (loadOnStartup != null) {
84                 servletHolder.setInitOrder(loadOnStartup.intValue());
85             }
86         }
87     }
88
89     //todo how do we stop/destroy the servlet?
90
//todo is start called twice???
91

92     public String JavaDoc getServletName() {
93         return servletHolder.getName();
94     }
95
96     /**
97      * TODO THIS IS NOT CALLED!!! only the ServletHolder is!!
98      * Service a request with this servlet. Set the ThreadLocal to hold the
99      * current JettyServletHolder.
100      */

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

107         InternalJettyServletHolder.setCurrentServletName(getServletName());
108         PolicyContext.setHandlerData(Request.getRequest((HttpServletRequest JavaDoc) request));
109
110         servletHolder.handle(request, response);
111     }
112
113     public void doStart() throws Exception JavaDoc {
114         if (context != null) {
115             Class JavaDoc pojoClass = context.getWebClassLoader().loadClass(pojoClassName);
116
117             /* 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.
118              * But this will have to do instead....
119              */

120             ServletContext JavaDoc servletContext = this.context.getServletHandler().getServletContext();
121
122             // Make up an ID for the WebServiceContainer
123
// put a reference the ID in the init-params
124
// put the WebServiceContainer in the webapp context keyed by its ID
125
String JavaDoc webServicecontainerID = getServletName() + WebServiceContainerInvoker.WEBSERVICE_CONTAINER + webServiceContainer.hashCode();
126             servletHolder.setInitParameter(WebServiceContainerInvoker.WEBSERVICE_CONTAINER, webServicecontainerID);
127             servletContext.setAttribute(webServicecontainerID, webServiceContainer);
128
129             // Same for the POJO Class
130
String JavaDoc pojoClassID = getServletName() + POJOWebServiceServlet.POJO_CLASS + pojoClass.hashCode();
131             servletHolder.setInitParameter(POJOWebServiceServlet.POJO_CLASS, pojoClassID);
132             servletContext.setAttribute(pojoClassID, pojoClass);
133
134             //this now starts the servlet in the appropriate context
135
//TODO check that we should not call this a servlet for jsr-77 benefit.
136
context.registerServletHolder(servletHolder, getServletName(), this.servletMappings, null);
137             servletHolder.start();
138         }
139     }
140
141     public void doStop() throws Exception JavaDoc {
142         servletHolder.stop();
143     }
144
145     public void doFail() {
146         try {
147             servletHolder.stop();
148         } catch (Exception JavaDoc e) {
149             //ignore ??
150
}
151     }
152
153     public static final GBeanInfo GBEAN_INFO;
154
155     static {
156         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JettyPOJOWebServiceHolder.class, NameFactory.SERVLET_WEB_SERVICE_TEMPLATE);
157         //todo replace with interface
158
// infoBuilder.addInterface(ServletHolder.class);
159

160         infoBuilder.addAttribute("pojoClassName", String JavaDoc.class, true);
161         infoBuilder.addAttribute("servletName", String JavaDoc.class, true);
162         infoBuilder.addAttribute("initParams", Map JavaDoc.class, true);
163         infoBuilder.addAttribute("loadOnStartup", Integer JavaDoc.class, true);
164         infoBuilder.addAttribute("servletMappings", Set JavaDoc.class, true);
165         infoBuilder.addReference("WebServiceContainerFactory", WebServiceContainerFactory.class);
166         infoBuilder.addReference("JettyServletRegistration", JettyServletRegistration.class);
167
168         infoBuilder.setConstructor(new String JavaDoc[]{"pojoClassName",
169                 "servletName",
170                 "initParams",
171                 "loadOnStartup",
172                 "servletMappings",
173                 "WebServiceContainerFactory",
174                 "JettyServletRegistration"});
175
176         GBEAN_INFO = infoBuilder.getBeanInfo();
177     }
178
179     public static GBeanInfo getGBeanInfo() {
180         return GBEAN_INFO;
181     }
182
183
184 }
185
Popular Tags