KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Collections JavaDoc;
23 import javax.security.jacc.PolicyContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletResponse JavaDoc;
27 import javax.servlet.UnavailableException JavaDoc;
28
29 import org.apache.geronimo.gbean.GBeanInfo;
30 import org.apache.geronimo.gbean.GBeanInfoBuilder;
31 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
32 import org.mortbay.jetty.servlet.ServletHolder;
33 import org.mortbay.jetty.servlet.ServletHttpRequest;
34
35
36 /**
37  * This ServletHolder's sole purpose is to provide the thread's current
38  * ServletHolder for realms that are interested in the current servlet, e.g.
39  * current servlet name.
40  *
41  * It is also being our servlet gbean for now. We could gbean-ize the superclass to avoid the thread local access.
42  *
43  * @version $Rev: 165343 $ $Date: 2005-04-29 14:16:48 -0700 (Fri, 29 Apr 2005) $
44  * @see org.apache.geronimo.jetty.JAASJettyRealm#isUserInRole(java.security.Principal, java.lang.String)
45  */

46 public class JettyServletHolder extends ServletHolder {
47     private static final ThreadLocal JavaDoc currentServletName = new ThreadLocal JavaDoc();
48
49     //todo consider interface instead of this constructor for endpoint use.
50
public JettyServletHolder() {
51
52     }
53
54     public JettyServletHolder(String JavaDoc servletName,
55                               String JavaDoc servletClassName,
56                               String JavaDoc jspFile,
57                               Map JavaDoc initParams,
58                               Integer JavaDoc loadOnStartup,
59                               Set JavaDoc servletMappings,
60                               Map JavaDoc webRoleRefPermissions,
61                               JettyServletRegistration context) throws Exception JavaDoc {
62         super(context == null? null: context.getServletHandler(), servletName, servletClassName, jspFile);
63         //context will be null only for use as "default servlet info holder" in deployer.
64

65         if (context != null) {
66             putAll(initParams);
67             if (loadOnStartup != null) {
68                 setInitOrder(loadOnStartup.intValue());
69             }
70             //this now starts the servlet in the appropriate context
71
context.registerServletHolder(this, servletName, servletMappings, webRoleRefPermissions == null? Collections.EMPTY_MAP: webRoleRefPermissions);
72 // start();
73
}
74     }
75
76     //todo how do we stop/destroy the servlet?
77
//todo is start called twice???
78

79     public String JavaDoc getServletName() {
80         return getName();
81     }
82
83     /**
84      * Service a request with this servlet. Set the ThreadLocal to hold the
85      * current JettyServletHolder.
86      */

87     public void handle(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
88             throws ServletException JavaDoc, UnavailableException JavaDoc, IOException JavaDoc {
89
90         setCurrentServletName(getServletName());
91
92         super.handle(request, response);
93     }
94
95     /**
96      * Provide the thread's current JettyServletHolder
97      *
98      * @return the thread's current JettyServletHolder
99      * @see org.apache.geronimo.jetty.JAASJettyRealm#isUserInRole(java.security.Principal, java.lang.String)
100      */

101     static String JavaDoc getCurrentServletName() {
102         return (String JavaDoc) currentServletName.get();
103     }
104
105     static void setCurrentServletName(String JavaDoc servletName) {
106         currentServletName.set(servletName);
107     }
108
109     public static final GBeanInfo GBEAN_INFO;
110
111     static {
112         GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(JettyServletHolder.class, NameFactory.DEFAULT_SERVLET);
113         //todo replace with interface
114
infoBuilder.addInterface(ServletHolder.class);
115
116         infoBuilder.addAttribute("servletName", String JavaDoc.class, true);
117         infoBuilder.addAttribute("servletClass", String JavaDoc.class, true);
118         infoBuilder.addAttribute("jspFile", String JavaDoc.class, true);
119         infoBuilder.addAttribute("initParams", Map JavaDoc.class, true);
120         infoBuilder.addAttribute("loadOnStartup", Integer JavaDoc.class, true);
121         infoBuilder.addAttribute("servletMappings", Set JavaDoc.class, true);
122         infoBuilder.addAttribute("webRoleRefPermissions", Map JavaDoc.class, true);
123         infoBuilder.addReference("JettyServletRegistration", JettyServletRegistration.class, NameFactory.WEB_MODULE);
124
125         infoBuilder.setConstructor(new String JavaDoc[] {"servletName",
126                                                  "servletClass",
127                                                  "jspFile",
128                                                  "initParams",
129                                                  "loadOnStartup",
130                                                  "servletMappings",
131                                                  "webRoleRefPermissions",
132                                                  "JettyServletRegistration"});
133
134         GBEAN_INFO = infoBuilder.getBeanInfo();
135     }
136
137     public static GBeanInfo getGBeanInfo() {
138         return GBEAN_INFO;
139     }
140 }
141
Popular Tags