KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Properties JavaDoc;
31 import org.apache.catalina.Authenticator;
32
33 import org.apache.catalina.Container;
34 import org.apache.catalina.Context;
35 import org.apache.catalina.Lifecycle;
36 import org.apache.catalina.LifecycleEvent;
37 import org.apache.catalina.LifecycleListener;
38 import org.apache.catalina.Pipeline;
39 import org.apache.catalina.Realm;
40 import org.apache.catalina.Valve;
41 import org.apache.catalina.core.ContainerBase;
42 import org.apache.catalina.deploy.LoginConfig;
43 import org.apache.catalina.deploy.SecurityConstraint;
44 import org.apache.catalina.startup.ContextConfig;
45 import org.apache.catalina.core.StandardEngine;
46 import com.sun.org.apache.commons.digester.Digester;
47 import org.xml.sax.InputSource JavaDoc;
48 import org.xml.sax.SAXParseException JavaDoc;
49
50 import com.sun.enterprise.deployment.WebBundleDescriptor;
51
52 import java.util.logging.Logger JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import com.sun.logging.LogDomains;
55
56 /**
57  * Startup event listener for a <b>Context</b> that configures the properties
58  * of that Context, and the associated defined servlets.
59  *
60  * @author Jean-Francois Arcand
61  */

62
63 public class WebModuleContextConfig extends ContextConfig {
64
65     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
66     
67     public final static int CHILDREN = 0;
68     public final static int SERVLET_MAPPINGS = 1;
69     public final static int LOCAL_EJBS = 2;
70     public final static int EJBS = 3;
71     public final static int ENVIRONMENTS = 4;
72     public final static int ERROR_PAGES = 5;
73     public final static int FILTER_DEFS = 6;
74     public final static int FILTER_MAPS = 7;
75     public final static int APPLICATION_LISTENERS = 8;
76     public final static int RESOURCES = 9;
77     public final static int APPLICATION_PARAMETERS = 10;
78     public final static int MESSAGE_DESTINATIONS = 11;
79     public final static int MESSAGE_DESTINATION_REFS = 12;
80     public final static int MIME_MAPPINGS = 13;
81    
82     
83     /**
84      * The <code>File</code> reffering to the default-web.xml
85      */

86     protected File JavaDoc file;
87         
88     
89     /**
90      * The DOL object representing the web.xml content.
91     */

92     private WebBundleDescriptor webBundleDescriptor;
93
94     
95     /**
96      * Customized <code>ContextConfig</code> which use the DOL for deployment.
97      */

98     public WebModuleContextConfig(){
99     }
100     
101     
102     /**
103      * Set the DOL object associated with this class.
104      */

105     public void setDescriptor(WebBundleDescriptor wbd){
106         webBundleDescriptor = wbd;
107     }
108     
109     
110     /**
111      * Process the START event for an associated Context.
112      *
113      * @param event The lifecycle event that has occurred
114      */

115     public void lifecycleEvent(LifecycleEvent event) {
116
117         // Identify the context we are associated with
118
try {
119             context = (Context) event.getLifecycle();
120         } catch (ClassCastException JavaDoc e) {
121             return;
122         }
123
124         // Called from ContainerBase.addChild() -> StandardContext.start()
125
// Process the event that has occurred
126
if (event.getType().equals(Lifecycle.START_EVENT))
127             start();
128         else if (event.getType().equals(Lifecycle.STOP_EVENT))
129             stop();
130     }
131     
132     
133     /**
134      * Process a "start" event for this Context - in background
135      */

136     protected synchronized void start() {
137         
138         try{
139             TomcatDeploymentConfig.configureWebModule((WebModule)context,
140                                                       webBundleDescriptor);
141         } catch (Throwable JavaDoc t){
142             context.setAvailable(false);
143
144             Object JavaDoc[] objs = {context.getName(), t};
145             logger.log(Level.SEVERE,
146                        "webModuleContextConfig.webModuleDisabled", objs);
147         }
148
149         context.setConfigured(false);
150         authenticatorConfig();
151         managerConfig();
152         context.setConfigured(true);
153
154     }
155     
156     
157     /**
158      * Always sets up an Authenticator regardless of any security constraints.
159      */

160     protected synchronized void authenticatorConfig() {
161         
162         LoginConfig loginConfig = context.getLoginConfig();
163         if (loginConfig == null) {
164             loginConfig = new LoginConfig("NONE", null, null, null);
165             context.setLoginConfig(loginConfig);
166         }
167
168         // Has an authenticator been configured already?
169
if (context instanceof Authenticator)
170             return;
171         if (context instanceof ContainerBase) {
172             Pipeline pipeline = ((ContainerBase) context).getPipeline();
173             if (pipeline != null) {
174                 Valve basic = pipeline.getBasic();
175                 if ((basic != null) && (basic instanceof Authenticator))
176                     return;
177                 Valve valves[] = pipeline.getValves();
178                 for (int i = 0; i < valves.length; i++) {
179                     if (valves[i] instanceof Authenticator)
180                         return;
181                 }
182             }
183         } else {
184             return; // Cannot install a Valve even if it would be needed
185
}
186
187         // Has a Realm been configured for us to authenticate against?
188
/* START IASRI 4856062
189         if (context.getRealm() == null) {
190         */

191         // BEGIN IASRI 4856062
192
Realm rlm = context.getRealm();
193         if (rlm == null) {
194         // END IASRI 4856062
195
logger.log(Level.SEVERE, "webModuleContextConfig.missingRealm");
196             ok = false;
197             return;
198         }
199
200         // BEGIN IASRI 4856062
201
// If a realm is available set its name in the Realm(Adapter)
202
rlm.setRealmName(loginConfig.getRealmName(),
203                          loginConfig.getAuthMethod());
204
205         // END IASRI 4856062
206

207         /*
208          * First check to see if there is a custom mapping for the login
209          * method. If so, use it. Otherwise, check if there is a mapping in
210          * org/apache/catalina/startup/Authenticators.properties.
211          */

212         Valve authenticator = null;
213         if (customAuthenticators != null) {
214             authenticator = (Valve)
215                 customAuthenticators.get(loginConfig.getAuthMethod());
216         }
217         if (authenticator == null) {
218             // Load our mapping properties if necessary
219
if (authenticators == null) {
220                 try {
221                     InputStream JavaDoc is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
222                     if( is!=null ) {
223                         authenticators = new Properties JavaDoc();
224                         authenticators.load(is);
225                     } else {
226                         logger.log(Level.SEVERE, "webModuleContextConfig.authenticatorResources");
227                         ok=false;
228                         return;
229                     }
230                 } catch (IOException JavaDoc e) {
231                     logger.log(Level.SEVERE, "webModuleContextConfig.authenticatorResources", e);
232                     ok = false;
233                     return;
234                 }
235             }
236
237             // Identify the class name of the Valve we should configure
238
String JavaDoc authenticatorName = null;
239
240             // BEGIN RIMOD 4808402
241
// If login-config is given but auth-method is null, use NONE
242
// so that NonLoginAuthenticator is picked
243
String JavaDoc authMethod = loginConfig.getAuthMethod();
244             if (authMethod == null) {
245                 authMethod = "NONE";
246             }
247             authenticatorName = authenticators.getProperty(authMethod);
248             // END RIMOD 4808402
249
/* RIMOD 4808402
250             authenticatorName =
251                     authenticators.getProperty(loginConfig.getAuthMethod());
252             */

253
254             if (authenticatorName == null) {
255                 logger.log(Level.SEVERE, "webModuleContextConfig.authenticatorMissing",
256                            loginConfig.getAuthMethod());
257                 ok = false;
258                 return;
259             }
260
261             // Instantiate and install an Authenticator of the requested class
262
try {
263                 Class JavaDoc authenticatorClass = Class.forName(authenticatorName);
264                 authenticator = (Valve) authenticatorClass.newInstance();
265             } catch (Throwable JavaDoc t) {
266                 logger.log(Level.SEVERE, "webModuleContextConfig.authenticatorInstantiate", authenticatorName);
267                 logger.log(Level.SEVERE, "webModuleContextConfig.authenticatorInstantiate", t);
268                 ok = false;
269             }
270         }
271
272         if (authenticator != null && context instanceof ContainerBase) {
273             Pipeline pipeline = ((ContainerBase) context).getPipeline();
274             if (pipeline != null) {
275                 ((ContainerBase) context).addValve(authenticator);
276                 if (logger.isLoggable(Level.FINEST)) {
277                     logger.log(Level.FINEST, "webModuleContextConfig.authenticatorConfigured",
278                                loginConfig.getAuthMethod());
279                 }
280             }
281         }
282     }
283     
284     
285     /**
286      * Process the default configuration file, if it exists.
287      * The default config must be read with the container loader - so
288      * container servlets can be loaded
289      */

290     protected void defaultConfig() {
291         ;
292     }
293 }
294
Popular Tags