KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > AdHocContextValve


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.io.IOException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.servlet.Servlet JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import org.apache.catalina.Request;
34 import org.apache.catalina.Response;
35 import org.apache.catalina.Valve;
36 import org.apache.catalina.Wrapper;
37 import com.sun.logging.LogDomains;
38
39 /**
40  * Implementation of StandardContextValve which is added as the base valve
41  * to a web module's ad-hoc pipeline.
42  *
43  * A web module's ad-hoc pipeline is invoked for any of the web module's
44  * ad-hoc paths.
45  *
46  * The AdHocContextValve is responsible for invoking the ad-hoc servlet
47  * associated with the ad-hoc path.
48  *
49  * @author Jan Luehe
50  */

51 public class AdHocContextValve implements Valve {
52
53     private static final Logger JavaDoc LOGGER =
54         LogDomains.getLogger(LogDomains.WEB_LOGGER);
55
56     private static final String JavaDoc VALVE_INFO =
57         "com.sun.enterprise.web.AdHocContextValve";
58
59     // The web module with which this valve is associated
60
private WebModule context;
61
62
63     /**
64      * Constructor.
65      */

66     public AdHocContextValve(WebModule context) {
67         this.context = context;
68     }
69
70
71     /**
72      * Returns descriptive information about this valve.
73      */

74     public String JavaDoc getInfo() {
75         return VALVE_INFO;
76     }
77
78
79     /**
80      * Processes the given request by passing it to the ad-hoc servlet
81      * associated with the request path (which has been determined, by the
82      * associated web module, to be an ad-hoc path).
83      *
84      * @param request The request to process
85      * @param response The response to return
86      */

87     public int invoke(Request JavaDoc request, Response JavaDoc response)
88             throws IOException JavaDoc, ServletException JavaDoc {
89
90         HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) request.getRequest();
91         HttpServletResponse JavaDoc hres = (HttpServletResponse JavaDoc) response.getResponse();
92
93         String JavaDoc adHocServletName =
94             context.getAdHocServletName(hreq.getServletPath());
95
96         Wrapper adHocWrapper = (Wrapper) context.findChild(adHocServletName);
97         if (adHocWrapper != null) {
98             Servlet JavaDoc adHocServlet = null;
99             try {
100                 adHocServlet = adHocWrapper.allocate();
101                 adHocServlet.service(hreq, hres);
102             } catch (Throwable JavaDoc t) {
103                 hres.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
104                 String JavaDoc msg = LOGGER.getResourceBundle().getString(
105                     "webmodule.adHocContextValve.adHocServletServiceError");
106                 msg = MessageFormat.format(
107                             msg,
108                             new Object JavaDoc[] { hreq.getServletPath() });
109                 response.setDetailMessage(msg);
110                 return END_PIPELINE;
111             } finally {
112                 if (adHocServlet != null) {
113                     adHocWrapper.deallocate(adHocServlet);
114                 }
115             }
116         } else {
117             hres.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
118             String JavaDoc msg = LOGGER.getResourceBundle().getString(
119                 "webmodule.adHocContextValve.noAdHocServlet");
120             msg = MessageFormat.format(
121                             msg,
122                             new Object JavaDoc[] { hreq.getServletPath() });
123             response.setDetailMessage(msg);
124             return END_PIPELINE;
125         }
126
127         return END_PIPELINE;
128     }
129
130
131     public void postInvoke(Request JavaDoc request, Response JavaDoc response)
132             throws IOException JavaDoc, ServletException JavaDoc {
133         // Do nothing
134
}
135
136 }
137
138
Popular Tags