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.jetty6;
017:
018: import java.util.Map;
019: import java.util.Set;
020:
021: import javax.security.auth.Subject;
022:
023: import org.apache.geronimo.gbean.GBeanInfo;
024: import org.apache.geronimo.gbean.GBeanInfoBuilder;
025: import org.apache.geronimo.gbean.GBeanLifecycle;
026: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
027: import org.apache.geronimo.management.Servlet;
028: import org.mortbay.jetty.servlet.ServletHolder;
029:
030: /**
031: * This ServletHolder's sole purpose is to provide the thread's current
032: * ServletHolder for realms that are interested in the current servlet, e.g.
033: * current servlet name.
034: * <p/>
035: * It is also being our servlet gbean for now. We could gbean-ize the superclass to avoid the thread local access.
036: *
037: * @version $Rev: 609448 $ $Date: 2008-01-06 14:31:09 -0800 (Sun, 06 Jan 2008) $
038: * @see JAASJettyRealm#isUserInRole(java.security.Principal, String)
039: */
040: public class JettyServletHolder implements ServletNameSource, Servlet,
041: GBeanLifecycle {
042:
043: private final JettyServletRegistration servletRegistration;
044: private final ServletHolder servletHolder;
045: private final String objectName;
046:
047: //todo consider interface instead of this constructor for endpoint use.
048: public JettyServletHolder() {
049: servletRegistration = null;
050: servletHolder = null;
051: objectName = null;
052: }
053:
054: public JettyServletHolder(String objectName, String servletName,
055: String servletClassName, String jspFile, Map initParams,
056: Integer loadOnStartup, Set<String> servletMappings,
057: String runAsRole, JettyServletRegistration context)
058: throws Exception {
059: servletRegistration = context;
060: Subject runAsSubject = context == null ? null : context
061: .getSubjectForRole(runAsRole);
062: servletHolder = new InternalJettyServletHolder(
063: context == null ? null : context.getLifecycleChain(),
064: runAsSubject, servletRegistration);
065: servletHolder.setName(servletName);
066: servletHolder.setClassName(servletClassName);
067: //context will be null only for use as "default servlet info holder" in deployer.
068:
069: if (context != null) {
070: servletHolder.setInitParameters(initParams);
071: servletHolder.setForcedPath(jspFile);
072: if (loadOnStartup != null) {
073: //This has no effect on the actual start order, the gbean references "previous" control that.
074: servletHolder.setInitOrder(loadOnStartup);
075: }
076: //this now starts the servlet in the appropriate context
077: context.registerServletHolder(servletHolder, servletName,
078: servletMappings, objectName);
079: }
080: this .objectName = objectName;
081: }
082:
083: public String getServletName() {
084: return servletHolder.getName();
085: }
086:
087: public String getServletClassName() {
088: return servletHolder.getClassName();
089: }
090:
091: public String getObjectName() {
092: return objectName;
093: }
094:
095: public boolean isStateManageable() {
096: return false;
097: }
098:
099: public boolean isStatisticsProvider() {
100: return false;
101: }
102:
103: public boolean isEventProvider() {
104: return false;
105: }
106:
107: public void doStart() throws Exception {
108: //start actually handled in constructor
109: // servletHolder.start();
110: }
111:
112: public void doStop() throws Exception {
113: servletHolder.stop();
114: if (servletRegistration != null) {
115: servletRegistration.unregisterServletHolder(servletHolder,
116: servletHolder.getName(), null, objectName);
117: }
118: }
119:
120: public void doFail() {
121: try {
122: doStop();
123: } catch (Exception e) {
124: //?? ignore
125: }
126: }
127:
128: public static final GBeanInfo GBEAN_INFO;
129:
130: static {
131: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
132: JettyServletHolder.class, NameFactory.SERVLET);
133: //todo replace with interface
134: // infoBuilder.addInterface(ServletHolder.class);
135:
136: infoBuilder.addAttribute("servletName", String.class, true);
137: infoBuilder.addAttribute("servletClass", String.class, true);
138: infoBuilder.addAttribute("jspFile", String.class, true);
139: infoBuilder.addAttribute("initParams", Map.class, true);
140: infoBuilder.addAttribute("loadOnStartup", Integer.class, true);
141: infoBuilder.addAttribute("servletMappings", Set.class, true);
142: infoBuilder.addAttribute("runAsRole", String.class, true);
143: infoBuilder.addAttribute("objectName", String.class, false);
144: infoBuilder.addInterface(Servlet.class);
145:
146: infoBuilder.addReference("JettyServletRegistration",
147: JettyServletRegistration.class, NameFactory.WEB_MODULE);
148:
149: infoBuilder.setConstructor(new String[] { "objectName",
150: "servletName", "servletClass", "jspFile", "initParams",
151: "loadOnStartup", "servletMappings", "runAsRole",
152: "JettyServletRegistration" });
153:
154: GBEAN_INFO = infoBuilder.getBeanInfo();
155: }
156:
157: public static GBeanInfo getGBeanInfo() {
158: return GBEAN_INFO;
159: }
160:
161: }
|