KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > jws > JWSAdHocServlet


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.appclient.jws;
25
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33
34
35 /**
36  *Ad hoc servlet registered to receive requests for the user-specified (or
37  *defaulted) context-root.
38  *<p>
39  *The user can (but is not required to) specify a context root in the runtime
40  *deployment descriptor for the app client. That context root - of, if omitted,
41  *a default value computed by the app server - is registered dynamically so
42  *requests to that context root are routed to an instance of this servlet. In
43  *turn, this servlet forwards such requests to the system web application that
44  *actually processes all Java Web Start requests for app clients.
45  *
46  * @author tjquinn
47  */

48 public class JWSAdHocServlet extends HttpServlet JavaDoc {
49
50     /** servlet init parameter name for specifying the context root value */
51     public static final String JavaDoc CONTEXT_ROOT_PARAMETER_NAME = "context-root";
52     
53     /** servlet init parameter name for specifying the category value (appclient or application) */
54     public static final String JavaDoc CATEGORY_PARAMETER_NAME = "category";
55     
56     /** the context for the system web app that handles all Java Web Start requests */
57     private ServletContext JavaDoc jwsAppContext = null;
58     
59     /** a dispatcher used in forwarding requests to the system web app */
60     private RequestDispatcher JavaDoc systemWebAppDispatcher = null;
61     
62     /** the actual context root, obtained from the init parameter, to which reqs should be dispatched */
63     private String JavaDoc targetContextRoot = null;
64     
65     /** the category from the init parameter */
66     private String JavaDoc category = null;
67     
68     /** Creates a new instance of JWSAdHocServlet */
69     public JWSAdHocServlet() {
70     }
71     
72     /**
73      *Responds to any method.
74      *@param the incoming request
75      *@param the outgoing response
76      *@throws ServletException in case of any errors
77      */

78     protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
79         /*
80          *Use the context for the Java Web Start system web app and forward this request
81          *to that app.
82          */

83         JWSAdHocServletRequestWrapper wrappedRequest = new JWSAdHocServletRequestWrapper(request, targetContextRoot, category);
84         try {
85             getJWSRequestDispatcher().forward(wrappedRequest, response);
86         } catch (Throwable JavaDoc thr) {
87             throw new ServletException JavaDoc("Error dispatching request to Java Web Start app client application", thr);
88         }
89     }
90     
91     /**
92      *Init method responsible for retrieiving init params.
93      */

94     public void init() {
95         ServletConfig JavaDoc config = getServletConfig();
96         
97         targetContextRoot = config.getInitParameter(CONTEXT_ROOT_PARAMETER_NAME);
98         category = config.getInitParameter(CATEGORY_PARAMETER_NAME);
99     }
100     
101     /**
102      *Returns the context for the Java Web Start app client system web app.
103      *@return the servlet context for the system web app
104      */

105     private ServletContext JavaDoc getJWSAppContext() {
106         if (jwsAppContext == null) {
107             String JavaDoc uri = NamingConventions.webAppURI();
108             jwsAppContext = getServletContext().getContext(uri);
109         }
110         return jwsAppContext;
111     }
112     
113     /**
114      *Returns the dispatcher to the system web app.
115      *@return the request dispatcher
116      */

117     private RequestDispatcher JavaDoc getJWSRequestDispatcher() {
118         if (systemWebAppDispatcher == null) {
119             String JavaDoc uri = NamingConventions.webAppURI();
120             WebPath webPath = new WebPath(uri);
121             
122             ServletContext JavaDoc sc = getServletContext().getContext(webPath.contextRoot());
123             String JavaDoc servletContextName = sc.getServletContextName();
124             systemWebAppDispatcher = sc.getRequestDispatcher(webPath.path() + "/" + category + targetContextRoot);
125         }
126         return systemWebAppDispatcher;
127     }
128 }
129
Popular Tags