View | Details | Raw Unified | Return to bug 12428
Collapse All | Expand All

(-)ContextConfig.java (-1359 / +1355 lines)
Lines 1-1359 Link Here
1
/*
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
7
 * the License.  You may obtain a copy of the License at
8
 * 
8
 * 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 * 
10
 * 
11
 * Unless required by applicable law or agreed to in writing, software
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
15
 * limitations under the License.
16
 */
16
 */
17
17
18
18
19
package org.apache.catalina.startup;
19
package org.apache.catalina.startup;
20
20
21
21
22
import java.io.File;
22
import java.io.File;
23
import java.io.FileInputStream;
23
import java.io.FileInputStream;
24
import java.io.FileNotFoundException;
24
import java.io.FileNotFoundException;
25
import java.io.IOException;
25
import java.io.IOException;
26
import java.io.InputStream;
26
import java.io.InputStream;
27
import java.net.URL;
27
import java.net.URL;
28
import java.util.Map;
28
import java.util.Map;
29
import java.util.Properties;
29
import java.util.Properties;
30
30
31
import javax.servlet.ServletContext;
31
import javax.servlet.ServletContext;
32
32
33
import org.apache.catalina.Authenticator;
33
import org.apache.catalina.Authenticator;
34
import org.apache.catalina.Container;
34
import org.apache.catalina.Container;
35
import org.apache.catalina.Context;
35
import org.apache.catalina.Context;
36
import org.apache.catalina.Engine;
36
import org.apache.catalina.Engine;
37
import org.apache.catalina.Globals;
37
import org.apache.catalina.Globals;
38
import org.apache.catalina.Host;
38
import org.apache.catalina.Host;
39
import org.apache.catalina.Lifecycle;
39
import org.apache.catalina.Lifecycle;
40
import org.apache.catalina.LifecycleEvent;
40
import org.apache.catalina.LifecycleEvent;
41
import org.apache.catalina.LifecycleListener;
41
import org.apache.catalina.LifecycleListener;
42
import org.apache.catalina.Pipeline;
42
import org.apache.catalina.Pipeline;
43
import org.apache.catalina.Valve;
43
import org.apache.catalina.Valve;
44
import org.apache.catalina.Wrapper;
44
import org.apache.catalina.Wrapper;
45
import org.apache.catalina.core.ContainerBase;
45
import org.apache.catalina.core.ContainerBase;
46
import org.apache.catalina.core.StandardContext;
46
import org.apache.catalina.core.StandardContext;
47
import org.apache.catalina.core.StandardEngine;
47
import org.apache.catalina.core.StandardEngine;
48
import org.apache.catalina.core.StandardHost;
48
import org.apache.catalina.core.StandardHost;
49
import org.apache.catalina.deploy.ErrorPage;
49
import org.apache.catalina.deploy.ErrorPage;
50
import org.apache.catalina.deploy.FilterDef;
50
import org.apache.catalina.deploy.FilterDef;
51
import org.apache.catalina.deploy.FilterMap;
51
import org.apache.catalina.deploy.FilterMap;
52
import org.apache.catalina.deploy.LoginConfig;
52
import org.apache.catalina.deploy.LoginConfig;
53
import org.apache.catalina.deploy.SecurityConstraint;
53
import org.apache.catalina.deploy.SecurityConstraint;
54
import org.apache.catalina.util.StringManager;
54
import org.apache.catalina.util.StringManager;
55
import org.apache.tomcat.util.digester.Digester;
55
import org.apache.tomcat.util.digester.Digester;
56
import org.apache.tomcat.util.digester.RuleSet;
56
import org.apache.tomcat.util.digester.RuleSet;
57
import org.xml.sax.ErrorHandler;
57
import org.xml.sax.ErrorHandler;
58
import org.xml.sax.InputSource;
58
import org.xml.sax.InputSource;
59
import org.xml.sax.SAXParseException;
59
import org.xml.sax.SAXParseException;
60
60
61
/**
61
/**
62
 * Startup event listener for a <b>Context</b> that configures the properties
62
 * Startup event listener for a <b>Context</b> that configures the properties
63
 * of that Context, and the associated defined servlets.
63
 * of that Context, and the associated defined servlets.
64
 *
64
 *
65
 * @author Craig R. McClanahan
65
 * @author Craig R. McClanahan
66
 * @author Jean-Francois Arcand
66
 * @author Jean-Francois Arcand
67
 * @version $Revision: 511708 $ $Date: 2007-02-25 20:12:18 -0700 (Sun, 25 Feb 2007) $
67
 * @version $Revision: 556488 $ $Date: 2007-07-16 03:55:50 +0200 (Mon, 16 Jul 2007) $
68
 */
68
 */
69
69
70
public class ContextConfig
70
public class ContextConfig
71
    implements LifecycleListener {
71
    implements LifecycleListener {
72
72
73
    protected static org.apache.commons.logging.Log log=
73
    protected static org.apache.commons.logging.Log log=
74
        org.apache.commons.logging.LogFactory.getLog( ContextConfig.class );
74
        org.apache.commons.logging.LogFactory.getLog( ContextConfig.class );
75
75
76
    // ----------------------------------------------------- Instance Variables
76
    // ----------------------------------------------------- Instance Variables
77
77
78
78
79
    /*
79
    /*
80
     * Custom mappings of login methods to authenticators
80
     * Custom mappings of login methods to authenticators
81
     */
81
     */
82
    protected Map customAuthenticators;
82
    protected Map customAuthenticators;
83
83
84
84
85
    /**
85
    /**
86
     * The set of Authenticators that we know how to configure.  The key is
86
     * The set of Authenticators that we know how to configure.  The key is
87
     * the name of the implemented authentication method, and the value is
87
     * the name of the implemented authentication method, and the value is
88
     * the fully qualified Java class name of the corresponding Valve.
88
     * the fully qualified Java class name of the corresponding Valve.
89
     */
89
     */
90
    protected static Properties authenticators = null;
90
    protected static Properties authenticators = null;
91
91
92
92
93
    /**
93
    /**
94
     * The Context we are associated with.
94
     * The Context we are associated with.
95
     */
95
     */
96
    protected Context context = null;
96
    protected Context context = null;
97
97
98
98
99
    /**
99
    /**
100
     * The default web application's context file location.
100
     * The default web application's context file location.
101
     */
101
     */
102
    protected String defaultContextXml = null;
102
    protected String defaultContextXml = null;
103
    
103
    
104
    
104
    
105
    /**
105
    /**
106
     * The default web application's deployment descriptor location.
106
     * The default web application's deployment descriptor location.
107
     */
107
     */
108
    protected String defaultWebXml = null;
108
    protected String defaultWebXml = null;
109
    
109
    
110
    
110
    
111
    /**
111
    /**
112
     * Track any fatal errors during startup configuration processing.
112
     * Track any fatal errors during startup configuration processing.
113
     */
113
     */
114
    protected boolean ok = false;
114
    protected boolean ok = false;
115
115
116
116
117
    /**
117
    /**
118
     * Any parse error which occurred while parsing XML descriptors.
118
     * Any parse error which occurred while parsing XML descriptors.
119
     */
119
     */
120
    protected SAXParseException parseException = null;
120
    protected SAXParseException parseException = null;
121
121
122
    
122
    
123
    /**
123
    /**
124
     * Original docBase.
124
     * Original docBase.
125
     */
125
     */
126
    protected String originalDocBase = null;
126
    protected String originalDocBase = null;
127
    
127
    
128
128
129
    /**
129
    /**
130
     * The string resources for this package.
130
     * The string resources for this package.
131
     */
131
     */
132
    protected static final StringManager sm =
132
    protected static final StringManager sm =
133
        StringManager.getManager(Constants.Package);
133
        StringManager.getManager(Constants.Package);
134
134
135
135
136
    /**
136
    /**
137
     * The <code>Digester</code> we will use to process web application
137
     * The <code>Digester</code> we will use to process web application
138
     * context files.
138
     * context files.
139
     */
139
     */
140
    protected static Digester contextDigester = null;
140
    protected static Digester contextDigester = null;
141
    
141
    
142
    
142
    
143
    /**
143
    /**
144
     * The <code>Digester</code> we will use to process web application
144
     * The <code>Digester</code> we will use to process web application
145
     * deployment descriptor files.
145
     * deployment descriptor files.
146
     */
146
     */
147
    protected static Digester webDigester = null;
147
    protected static Digester webDigester = null;
148
    
148
    
149
    
149
    
150
    /**
150
    /**
151
     * The <code>Rule</code> used to parse the web.xml
151
     * The <code>Rule</code> used to parse the web.xml
152
     */
152
     */
153
    protected static WebRuleSet webRuleSet = new WebRuleSet();
153
    protected static WebRuleSet webRuleSet = new WebRuleSet();
154
154
155
    /**
155
    /**
156
     * Attribute value used to turn on/off XML validation
156
     * Attribute value used to turn on/off XML validation
157
     */
157
     */
158
     protected static boolean xmlValidation = false;
158
     protected static boolean xmlValidation = false;
159
159
160
160
161
    /**
161
    /**
162
     * Attribute value used to turn on/off XML namespace awarenes.
162
     * Attribute value used to turn on/off XML namespace awarenes.
163
     */
163
     */
164
    protected static boolean xmlNamespaceAware = false;
164
    protected static boolean xmlNamespaceAware = false;
165
165
166
    
166
    
167
    /**
167
    /**
168
     * Deployment count.
168
     * Deployment count.
169
     */
169
     */
170
    protected static long deploymentCount = 0L;
170
    protected static long deploymentCount = 0L;
171
    
171
    
172
    
172
    
173
    protected static final LoginConfig DUMMY_LOGIN_CONFIG =
173
    protected static final LoginConfig DUMMY_LOGIN_CONFIG =
174
                                new LoginConfig("NONE", null, null, null);
174
                                new LoginConfig("NONE", null, null, null);
175
175
176
176
177
    // ------------------------------------------------------------- Properties
177
    // ------------------------------------------------------------- Properties
178
178
179
179
180
    /**
180
    /**
181
     * Return the location of the default deployment descriptor
181
     * Return the location of the default deployment descriptor
182
     */
182
     */
183
    public String getDefaultWebXml() {
183
    public String getDefaultWebXml() {
184
        if( defaultWebXml == null ) {
184
        if( defaultWebXml == null ) {
185
            defaultWebXml=Constants.DefaultWebXml;
185
            defaultWebXml=Constants.DefaultWebXml;
186
        }
186
        }
187
187
188
        return (this.defaultWebXml);
188
        return (this.defaultWebXml);
189
189
190
    }
190
    }
191
191
192
192
193
    /**
193
    /**
194
     * Set the location of the default deployment descriptor
194
     * Set the location of the default deployment descriptor
195
     *
195
     *
196
     * @param path Absolute/relative path to the default web.xml
196
     * @param path Absolute/relative path to the default web.xml
197
     */
197
     */
198
    public void setDefaultWebXml(String path) {
198
    public void setDefaultWebXml(String path) {
199
199
200
        this.defaultWebXml = path;
200
        this.defaultWebXml = path;
201
201
202
    }
202
    }
203
203
204
204
205
    /**
205
    /**
206
     * Return the location of the default context file
206
     * Return the location of the default context file
207
     */
207
     */
208
    public String getDefaultContextXml() {
208
    public String getDefaultContextXml() {
209
        if( defaultContextXml == null ) {
209
        if( defaultContextXml == null ) {
210
            defaultContextXml=Constants.DefaultContextXml;
210
            defaultContextXml=Constants.DefaultContextXml;
211
        }
211
        }
212
212
213
        return (this.defaultContextXml);
213
        return (this.defaultContextXml);
214
214
215
    }
215
    }
216
216
217
217
218
    /**
218
    /**
219
     * Set the location of the default context file
219
     * Set the location of the default context file
220
     *
220
     *
221
     * @param path Absolute/relative path to the default context.xml
221
     * @param path Absolute/relative path to the default context.xml
222
     */
222
     */
223
    public void setDefaultContextXml(String path) {
223
    public void setDefaultContextXml(String path) {
224
224
225
        this.defaultContextXml = path;
225
        this.defaultContextXml = path;
226
226
227
    }
227
    }
228
228
229
229
230
    /**
230
    /**
231
     * Sets custom mappings of login methods to authenticators.
231
     * Sets custom mappings of login methods to authenticators.
232
     *
232
     *
233
     * @param customAuthenticators Custom mappings of login methods to
233
     * @param customAuthenticators Custom mappings of login methods to
234
     * authenticators
234
     * authenticators
235
     */
235
     */
236
    public void setCustomAuthenticators(Map customAuthenticators) {
236
    public void setCustomAuthenticators(Map customAuthenticators) {
237
        this.customAuthenticators = customAuthenticators;
237
        this.customAuthenticators = customAuthenticators;
238
    }
238
    }
239
239
240
240
241
    // --------------------------------------------------------- Public Methods
241
    // --------------------------------------------------------- Public Methods
242
242
243
243
244
    /**
244
    /**
245
     * Process events for an associated Context.
245
     * Process events for an associated Context.
246
     *
246
     *
247
     * @param event The lifecycle event that has occurred
247
     * @param event The lifecycle event that has occurred
248
     */
248
     */
249
    public void lifecycleEvent(LifecycleEvent event) {
249
    public void lifecycleEvent(LifecycleEvent event) {
250
250
251
        // Identify the context we are associated with
251
        // Identify the context we are associated with
252
        try {
252
        try {
253
            context = (Context) event.getLifecycle();
253
            context = (Context) event.getLifecycle();
254
        } catch (ClassCastException e) {
254
        } catch (ClassCastException e) {
255
            log.error(sm.getString("contextConfig.cce", event.getLifecycle()), e);
255
            log.error(sm.getString("contextConfig.cce", event.getLifecycle()), e);
256
            return;
256
            return;
257
        }
257
        }
258
258
259
        // Process the event that has occurred
259
        // Process the event that has occurred
260
        if (event.getType().equals(Lifecycle.START_EVENT)) {
260
        if (event.getType().equals(Lifecycle.START_EVENT)) {
261
            start();
261
            start();
262
        } else if (event.getType().equals(StandardContext.BEFORE_START_EVENT)) {
262
        } else if (event.getType().equals(StandardContext.BEFORE_START_EVENT)) {
263
            beforeStart();
263
            beforeStart();
264
        } else if (event.getType().equals(StandardContext.AFTER_START_EVENT)) {
264
        } else if (event.getType().equals(StandardContext.AFTER_START_EVENT)) {
265
            // Restore docBase for management tools
265
            // Restore docBase for management tools
266
            if (originalDocBase != null) {
266
            if (originalDocBase != null) {
267
                String docBase = context.getDocBase();
267
                String docBase = context.getDocBase();
268
                context.setDocBase(originalDocBase);
268
                context.setDocBase(originalDocBase);
269
                originalDocBase = docBase;
269
                originalDocBase = docBase;
270
            }
270
            }
271
        } else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
271
        } else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
272
            if (originalDocBase != null) {
272
            if (originalDocBase != null) {
273
                String docBase = context.getDocBase();
273
                String docBase = context.getDocBase();
274
                context.setDocBase(originalDocBase);
274
                context.setDocBase(originalDocBase);
275
                originalDocBase = docBase;
275
                originalDocBase = docBase;
276
            }
276
            }
277
            stop();
277
            stop();
278
        } else if (event.getType().equals(Lifecycle.INIT_EVENT)) {
278
        } else if (event.getType().equals(Lifecycle.INIT_EVENT)) {
279
            init();
279
            init();
280
        } else if (event.getType().equals(Lifecycle.DESTROY_EVENT)) {
280
        } else if (event.getType().equals(Lifecycle.DESTROY_EVENT)) {
281
            destroy();
281
            destroy();
282
        }
282
        }
283
283
284
    }
284
    }
285
285
286
286
287
    // -------------------------------------------------------- protected Methods
287
    // -------------------------------------------------------- protected Methods
288
288
289
289
290
    /**
290
    /**
291
     * Process the application configuration file, if it exists.
291
     * Process the application configuration file, if it exists.
292
     */
292
     */
293
    protected void applicationWebConfig() {
293
    protected void applicationWebConfig() {
294
294
295
        String altDDName = null;
295
        String altDDName = null;
296
296
297
        // Open the application web.xml file, if it exists
297
        // Open the application web.xml file, if it exists
298
        InputStream stream = null;
298
        InputStream stream = null;
299
        ServletContext servletContext = context.getServletContext();
299
        ServletContext servletContext = context.getServletContext();
300
        if (servletContext != null) {
300
        if (servletContext != null) {
301
            altDDName = (String)servletContext.getAttribute(
301
            altDDName = (String)servletContext.getAttribute(
302
                                                        Globals.ALT_DD_ATTR);
302
                                                        Globals.ALT_DD_ATTR);
303
            if (altDDName != null) {
303
            if (altDDName != null) {
304
                try {
304
                try {
305
                    stream = new FileInputStream(altDDName);
305
                    stream = new FileInputStream(altDDName);
306
                } catch (FileNotFoundException e) {
306
                } catch (FileNotFoundException e) {
307
                    log.error(sm.getString("contextConfig.altDDNotFound",
307
                    log.error(sm.getString("contextConfig.altDDNotFound",
308
                                           altDDName));
308
                                           altDDName));
309
                }
309
                }
310
            }
310
            }
311
            else {
311
            else {
312
                stream = servletContext.getResourceAsStream
312
                stream = servletContext.getResourceAsStream
313
                    (Constants.ApplicationWebXml);
313
                    (Constants.ApplicationWebXml);
314
            }
314
            }
315
        }
315
        }
316
        if (stream == null) {
316
        if (stream == null) {
317
            if (log.isDebugEnabled()) {
317
            if (log.isDebugEnabled()) {
318
                log.debug(sm.getString("contextConfig.applicationMissing") + " " + context);
318
                log.debug(sm.getString("contextConfig.applicationMissing") + " " + context);
319
            }
319
            }
320
            return;
320
            return;
321
        }
321
        }
322
        
322
        
323
        long t1=System.currentTimeMillis();
323
        long t1=System.currentTimeMillis();
324
324
325
        if (webDigester == null){
325
        if (webDigester == null){
326
            webDigester = createWebDigester();
326
            webDigester = createWebDigester();
327
        }
327
        }
328
        
328
        
329
        URL url=null;
329
        URL url=null;
330
        // Process the application web.xml file
330
        // Process the application web.xml file
331
        synchronized (webDigester) {
331
        synchronized (webDigester) {
332
            try {
332
            try {
333
                if (altDDName != null) {
333
                if (altDDName != null) {
334
                    url = new File(altDDName).toURL();
334
                    url = new File(altDDName).toURL();
335
                } else {
335
                } else {
336
                    url = servletContext.getResource(
336
                    url = servletContext.getResource(
337
                                                Constants.ApplicationWebXml);
337
                                                Constants.ApplicationWebXml);
338
                }
338
                }
339
                if( url!=null ) {
339
                if( url!=null ) {
340
                    InputSource is = new InputSource(url.toExternalForm());
340
                    InputSource is = new InputSource(url.toExternalForm());
341
                    is.setByteStream(stream);
341
                    is.setByteStream(stream);
342
                    if (context instanceof StandardContext) {
342
                    if (context instanceof StandardContext) {
343
                        ((StandardContext) context).setReplaceWelcomeFiles(true);
343
                        ((StandardContext) context).setReplaceWelcomeFiles(true);
344
                    }
344
                    }
345
                    webDigester.push(context);
345
                    webDigester.push(context);
346
                    webDigester.setErrorHandler(new ContextErrorHandler());
346
                    webDigester.setErrorHandler(new ContextErrorHandler());
347
347
348
                    if(log.isDebugEnabled()) {
348
                    if(log.isDebugEnabled()) {
349
                        log.debug("Parsing application web.xml file at " + url.toExternalForm());
349
                        log.debug("Parsing application web.xml file at " + url.toExternalForm());
350
                    }
350
                    }
351
351
352
                    webDigester.parse(is);
352
                    webDigester.parse(is);
353
353
354
                    if (parseException != null) {
354
                    if (parseException != null) {
355
                        ok = false;
355
                        ok = false;
356
                    }
356
                    }
357
                } else {
357
                } else {
358
                    log.info("No web.xml, using defaults " + context );
358
                    log.info("No web.xml, using defaults " + context );
359
                }
359
                }
360
            } catch (SAXParseException e) {
360
            } catch (SAXParseException e) {
361
                log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
361
                log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
362
                log.error(sm.getString("contextConfig.applicationPosition",
362
                log.error(sm.getString("contextConfig.applicationPosition",
363
                                 "" + e.getLineNumber(),
363
                                 "" + e.getLineNumber(),
364
                                 "" + e.getColumnNumber()));
364
                                 "" + e.getColumnNumber()));
365
                ok = false;
365
                ok = false;
366
            } catch (Exception e) {
366
            } catch (Exception e) {
367
                log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
367
                log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
368
                ok = false;
368
                ok = false;
369
            } finally {
369
            } finally {
370
                webDigester.reset();
370
                webDigester.reset();
371
                parseException = null;
371
                parseException = null;
372
                try {
372
                try {
373
                    if (stream != null) {
373
                    if (stream != null) {
374
                        stream.close();
374
                        stream.close();
375
                    }
375
                    }
376
                } catch (IOException e) {
376
                } catch (IOException e) {
377
                    log.error(sm.getString("contextConfig.applicationClose"), e);
377
                    log.error(sm.getString("contextConfig.applicationClose"), e);
378
                }
378
                }
379
            }
379
            }
380
        }
380
        }
381
        webRuleSet.recycle();
381
        webRuleSet.recycle();
382
382
383
        long t2=System.currentTimeMillis();
383
        long t2=System.currentTimeMillis();
384
        if (context instanceof StandardContext) {
384
        if (context instanceof StandardContext) {
385
            ((StandardContext) context).setStartupTime(t2-t1);
385
            ((StandardContext) context).setStartupTime(t2-t1);
386
        }
386
        }
387
    }
387
    }
388
388
389
389
390
    /**
390
    /**
391
     * Set up an Authenticator automatically if required, and one has not
391
     * Set up an Authenticator automatically if required, and one has not
392
     * already been configured.
392
     * already been configured.
393
     */
393
     */
394
    protected synchronized void authenticatorConfig() {
394
    protected synchronized void authenticatorConfig() {
395
395
396
        // Does this Context require an Authenticator?
396
        LoginConfig loginConfig = context.getLoginConfig();
397
        SecurityConstraint constraints[] = context.findConstraints();
397
        if (loginConfig == null) {
398
        if ((constraints == null) || (constraints.length == 0))
398
            loginConfig = DUMMY_LOGIN_CONFIG;
399
            return;
399
            context.setLoginConfig(loginConfig);
400
        LoginConfig loginConfig = context.getLoginConfig();
400
        }
401
        if (loginConfig == null) {
401
402
            loginConfig = DUMMY_LOGIN_CONFIG;
402
        // Has an authenticator been configured already?
403
            context.setLoginConfig(loginConfig);
403
        if (context instanceof Authenticator)
404
        }
404
            return;
405
405
        if (context instanceof ContainerBase) {
406
        // Has an authenticator been configured already?
406
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
407
        if (context instanceof Authenticator)
407
            if (pipeline != null) {
408
            return;
408
                Valve basic = pipeline.getBasic();
409
        if (context instanceof ContainerBase) {
409
                if ((basic != null) && (basic instanceof Authenticator))
410
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
410
                    return;
411
            if (pipeline != null) {
411
                Valve valves[] = pipeline.getValves();
412
                Valve basic = pipeline.getBasic();
412
                for (int i = 0; i < valves.length; i++) {
413
                if ((basic != null) && (basic instanceof Authenticator))
413
                    if (valves[i] instanceof Authenticator)
414
                    return;
414
                        return;
415
                Valve valves[] = pipeline.getValves();
415
                }
416
                for (int i = 0; i < valves.length; i++) {
416
            }
417
                    if (valves[i] instanceof Authenticator)
417
        } else {
418
                        return;
418
            return;     // Cannot install a Valve even if it would be needed
419
                }
419
        }
420
            }
420
421
        } else {
421
        // Has a Realm been configured for us to authenticate against?
422
            return;     // Cannot install a Valve even if it would be needed
422
        if (context.getRealm() == null) {
423
        }
423
            log.error(sm.getString("contextConfig.missingRealm"));
424
424
            ok = false;
425
        // Has a Realm been configured for us to authenticate against?
425
            return;
426
        if (context.getRealm() == null) {
426
        }
427
            log.error(sm.getString("contextConfig.missingRealm"));
427
428
            ok = false;
428
        /*
429
            return;
429
         * First check to see if there is a custom mapping for the login
430
        }
430
         * method. If so, use it. Otherwise, check if there is a mapping in
431
431
         * org/apache/catalina/startup/Authenticators.properties.
432
        /*
432
         */
433
         * First check to see if there is a custom mapping for the login
433
        Valve authenticator = null;
434
         * method. If so, use it. Otherwise, check if there is a mapping in
434
        if (customAuthenticators != null) {
435
         * org/apache/catalina/startup/Authenticators.properties.
435
            authenticator = (Valve)
436
         */
436
                customAuthenticators.get(loginConfig.getAuthMethod());
437
        Valve authenticator = null;
437
        }
438
        if (customAuthenticators != null) {
438
        if (authenticator == null) {
439
            authenticator = (Valve)
439
            // Load our mapping properties if necessary
440
                customAuthenticators.get(loginConfig.getAuthMethod());
440
            if (authenticators == null) {
441
        }
441
                try {
442
        if (authenticator == null) {
442
                    InputStream is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
443
            // Load our mapping properties if necessary
443
                    if( is!=null ) {
444
            if (authenticators == null) {
444
                        authenticators = new Properties();
445
                try {
445
                        authenticators.load(is);
446
                    InputStream is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
446
                    } else {
447
                    if( is!=null ) {
447
                        log.error(sm.getString(
448
                        authenticators = new Properties();
448
                                "contextConfig.authenticatorResources"));
449
                        authenticators.load(is);
449
                        ok=false;
450
                    } else {
450
                        return;
451
                        log.error(sm.getString(
451
                    }
452
                                "contextConfig.authenticatorResources"));
452
                } catch (IOException e) {
453
                        ok=false;
453
                    log.error(sm.getString(
454
                        return;
454
                                "contextConfig.authenticatorResources"), e);
455
                    }
455
                    ok = false;
456
                } catch (IOException e) {
456
                    return;
457
                    log.error(sm.getString(
457
                }
458
                                "contextConfig.authenticatorResources"), e);
458
            }
459
                    ok = false;
459
460
                    return;
460
            // Identify the class name of the Valve we should configure
461
                }
461
            String authenticatorName = null;
462
            }
462
            authenticatorName =
463
463
                    authenticators.getProperty(loginConfig.getAuthMethod());
464
            // Identify the class name of the Valve we should configure
464
            if (authenticatorName == null) {
465
            String authenticatorName = null;
465
                log.error(sm.getString("contextConfig.authenticatorMissing",
466
            authenticatorName =
466
                                 loginConfig.getAuthMethod()));
467
                    authenticators.getProperty(loginConfig.getAuthMethod());
467
                ok = false;
468
            if (authenticatorName == null) {
468
                return;
469
                log.error(sm.getString("contextConfig.authenticatorMissing",
469
            }
470
                                 loginConfig.getAuthMethod()));
470
471
                ok = false;
471
            // Instantiate and install an Authenticator of the requested class
472
                return;
472
            try {
473
            }
473
                Class authenticatorClass = Class.forName(authenticatorName);
474
474
                authenticator = (Valve) authenticatorClass.newInstance();
475
            // Instantiate and install an Authenticator of the requested class
475
            } catch (Throwable t) {
476
            try {
476
                log.error(sm.getString(
477
                Class authenticatorClass = Class.forName(authenticatorName);
477
                                    "contextConfig.authenticatorInstantiate",
478
                authenticator = (Valve) authenticatorClass.newInstance();
478
                                    authenticatorName),
479
            } catch (Throwable t) {
479
                          t);
480
                log.error(sm.getString(
480
                ok = false;
481
                                    "contextConfig.authenticatorInstantiate",
481
            }
482
                                    authenticatorName),
482
        }
483
                          t);
483
484
                ok = false;
484
        if (authenticator != null && context instanceof ContainerBase) {
485
            }
485
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
486
        }
486
            if (pipeline != null) {
487
487
                ((ContainerBase) context).addValve(authenticator);
488
        if (authenticator != null && context instanceof ContainerBase) {
488
                if (log.isDebugEnabled()) {
489
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
489
                    log.debug(sm.getString(
490
            if (pipeline != null) {
490
                                    "contextConfig.authenticatorConfigured",
491
                ((ContainerBase) context).addValve(authenticator);
491
                                    loginConfig.getAuthMethod()));
492
                if (log.isDebugEnabled()) {
492
                }
493
                    log.debug(sm.getString(
493
            }
494
                                    "contextConfig.authenticatorConfigured",
494
        }
495
                                    loginConfig.getAuthMethod()));
495
496
                }
496
    }
497
            }
497
498
        }
498
499
499
    /**
500
    }
500
     * Create (if necessary) and return a Digester configured to process the
501
501
     * web application deployment descriptor (web.xml).
502
502
     */
503
    /**
503
    protected static Digester createWebDigester() {
504
     * Create (if necessary) and return a Digester configured to process the
504
        Digester webDigester =
505
     * web application deployment descriptor (web.xml).
505
            createWebXmlDigester(xmlNamespaceAware, xmlValidation);
506
     */
506
        return webDigester;
507
    protected static Digester createWebDigester() {
507
    }
508
        Digester webDigester =
508
509
            createWebXmlDigester(xmlNamespaceAware, xmlValidation);
509
510
        return webDigester;
510
    /**
511
    }
511
     * Create (if necessary) and return a Digester configured to process the
512
512
     * web application deployment descriptor (web.xml).
513
513
     */
514
    /**
514
    public static Digester createWebXmlDigester(boolean namespaceAware,
515
     * Create (if necessary) and return a Digester configured to process the
515
                                                boolean validation) {
516
     * web application deployment descriptor (web.xml).
516
        
517
     */
517
        Digester webDigester =  DigesterFactory.newDigester(xmlValidation,
518
    public static Digester createWebXmlDigester(boolean namespaceAware,
518
                                                            xmlNamespaceAware,
519
                                                boolean validation) {
519
                                                            webRuleSet);
520
        
520
        return webDigester;
521
        Digester webDigester =  DigesterFactory.newDigester(xmlValidation,
521
    }
522
                                                            xmlNamespaceAware,
522
523
                                                            webRuleSet);
523
    
524
        return webDigester;
524
    /**
525
    }
525
     * Create (if necessary) and return a Digester configured to process the
526
526
     * context configuration descriptor for an application.
527
    
527
     */
528
    /**
528
    protected Digester createContextDigester() {
529
     * Create (if necessary) and return a Digester configured to process the
529
        Digester digester = new Digester();
530
     * context configuration descriptor for an application.
530
        digester.setValidating(false);
531
     */
531
        RuleSet contextRuleSet = new ContextRuleSet("", false);
532
    protected Digester createContextDigester() {
532
        digester.addRuleSet(contextRuleSet);
533
        Digester digester = new Digester();
533
        RuleSet namingRuleSet = new NamingRuleSet("Context/");
534
        digester.setValidating(false);
534
        digester.addRuleSet(namingRuleSet);
535
        RuleSet contextRuleSet = new ContextRuleSet("", false);
535
        return digester;
536
        digester.addRuleSet(contextRuleSet);
536
    }
537
        RuleSet namingRuleSet = new NamingRuleSet("Context/");
537
538
        digester.addRuleSet(namingRuleSet);
538
539
        return digester;
539
    protected String getBaseDir() {
540
    }
540
        Container engineC=context.getParent().getParent();
541
541
        if( engineC instanceof StandardEngine ) {
542
542
            return ((StandardEngine)engineC).getBaseDir();
543
    protected String getBaseDir() {
543
        }
544
        Container engineC=context.getParent().getParent();
544
        return System.getProperty("catalina.base");
545
        if( engineC instanceof StandardEngine ) {
545
    }
546
            return ((StandardEngine)engineC).getBaseDir();
546
547
        }
547
    /**
548
        return System.getProperty("catalina.base");
548
     * Process the default configuration file, if it exists.
549
    }
549
     * The default config must be read with the container loader - so
550
550
     * container servlets can be loaded
551
    /**
551
     */
552
     * Process the default configuration file, if it exists.
552
    protected void defaultWebConfig() {
553
     * The default config must be read with the container loader - so
553
        long t1=System.currentTimeMillis();
554
     * container servlets can be loaded
554
555
     */
555
        // Open the default web.xml file, if it exists
556
    protected void defaultWebConfig() {
556
        if( defaultWebXml==null && context instanceof StandardContext ) {
557
        long t1=System.currentTimeMillis();
557
            defaultWebXml=((StandardContext)context).getDefaultWebXml();
558
558
        }
559
        // Open the default web.xml file, if it exists
559
        // set the default if we don't have any overrides
560
        if( defaultWebXml==null && context instanceof StandardContext ) {
560
        if( defaultWebXml==null ) getDefaultWebXml();
561
            defaultWebXml=((StandardContext)context).getDefaultWebXml();
561
562
        }
562
        File file = new File(this.defaultWebXml);
563
        // set the default if we don't have any overrides
563
        if (!file.isAbsolute()) {
564
        if( defaultWebXml==null ) getDefaultWebXml();
564
            file = new File(getBaseDir(),
565
565
                            this.defaultWebXml);
566
        File file = new File(this.defaultWebXml);
566
        }
567
        if (!file.isAbsolute()) {
567
568
            file = new File(getBaseDir(),
568
        InputStream stream = null;
569
                            this.defaultWebXml);
569
        InputSource source = null;
570
        }
570
571
571
        try {
572
        InputStream stream = null;
572
            if ( ! file.exists() ) {
573
        InputSource source = null;
573
                // Use getResource and getResourceAsStream
574
574
                stream = getClass().getClassLoader()
575
        try {
575
                    .getResourceAsStream(defaultWebXml);
576
            if ( ! file.exists() ) {
576
                if( stream != null ) {
577
                // Use getResource and getResourceAsStream
577
                    source = new InputSource
578
                stream = getClass().getClassLoader()
578
                            (getClass().getClassLoader()
579
                    .getResourceAsStream(defaultWebXml);
579
                            .getResource(defaultWebXml).toString());
580
                if( stream != null ) {
580
                } 
581
                    source = new InputSource
581
                if( stream== null ) { 
582
                            (getClass().getClassLoader()
582
                    // maybe embedded
583
                            .getResource(defaultWebXml).toString());
583
                    stream = getClass().getClassLoader()
584
                } 
584
                        .getResourceAsStream("web-embed.xml");
585
                if( stream== null ) { 
585
                    if( stream != null ) {
586
                    // maybe embedded
586
                        source = new InputSource
587
                    stream = getClass().getClassLoader()
587
                        (getClass().getClassLoader()
588
                        .getResourceAsStream("web-embed.xml");
588
                                .getResource("web-embed.xml").toString());
589
                    if( stream != null ) {
589
                    }                                         
590
                        source = new InputSource
590
                }
591
                        (getClass().getClassLoader()
591
                
592
                                .getResource("web-embed.xml").toString());
592
                if( stream== null ) {
593
                    }                                         
593
                    log.info("No default web.xml");
594
                }
594
                }
595
                
595
            } else {
596
                if( stream== null ) {
596
                source =
597
                    log.info("No default web.xml");
597
                    new InputSource("file://" + file.getAbsolutePath());
598
                }
598
                stream = new FileInputStream(file);
599
            } else {
599
            }
600
                source =
600
        } catch (Exception e) {
601
                    new InputSource("file://" + file.getAbsolutePath());
601
            log.error(sm.getString("contextConfig.defaultMissing") 
602
                stream = new FileInputStream(file);
602
                      + " " + defaultWebXml + " " + file , e);
603
            }
603
        }
604
        } catch (Exception e) {
604
605
            log.error(sm.getString("contextConfig.defaultMissing") 
605
        if (webDigester == null){
606
                      + " " + defaultWebXml + " " + file , e);
606
            webDigester = createWebDigester();
607
        }
607
        }
608
608
        
609
        if (webDigester == null){
609
        if (stream != null) {
610
            webDigester = createWebDigester();
610
            processDefaultWebConfig(webDigester, stream, source);
611
        }
611
            webRuleSet.recycle();
612
        
612
        }
613
        if (stream != null) {
613
614
            processDefaultWebConfig(webDigester, stream, source);
614
        long t2=System.currentTimeMillis();
615
            webRuleSet.recycle();
615
        if( (t2-t1) > 200 )
616
        }
616
            log.debug("Processed default web.xml " + file + " "  + ( t2-t1));
617
617
618
        long t2=System.currentTimeMillis();
618
        stream = null;
619
        if( (t2-t1) > 200 )
619
        source = null;
620
            log.debug("Processed default web.xml " + file + " "  + ( t2-t1));
620
621
621
        String resourceName = getHostConfigPath(Constants.HostWebXml);
622
        stream = null;
622
        file = new File(getConfigBase(), resourceName);
623
        source = null;
623
        
624
624
        try {
625
        String resourceName = getHostConfigPath(Constants.HostWebXml);
625
            if ( ! file.exists() ) {
626
        file = new File(getConfigBase(), resourceName);
626
                // Use getResource and getResourceAsStream
627
        
627
                stream = getClass().getClassLoader()
628
        try {
628
                    .getResourceAsStream(resourceName);
629
            if ( ! file.exists() ) {
629
                if( stream != null ) {
630
                // Use getResource and getResourceAsStream
630
                    source = new InputSource
631
                stream = getClass().getClassLoader()
631
                            (getClass().getClassLoader()
632
                    .getResourceAsStream(resourceName);
632
                            .getResource(resourceName).toString());
633
                if( stream != null ) {
633
                }
634
                    source = new InputSource
634
            } else {
635
                            (getClass().getClassLoader()
635
                source =
636
                            .getResource(resourceName).toString());
636
                    new InputSource("file://" + file.getAbsolutePath());
637
                }
637
                stream = new FileInputStream(file);
638
            } else {
638
            }
639
                source =
639
        } catch (Exception e) {
640
                    new InputSource("file://" + file.getAbsolutePath());
640
            log.error(sm.getString("contextConfig.defaultMissing") 
641
                stream = new FileInputStream(file);
641
                      + " " + resourceName + " " + file , e);
642
            }
642
        }
643
        } catch (Exception e) {
643
644
            log.error(sm.getString("contextConfig.defaultMissing") 
644
        if (stream != null) {
645
                      + " " + resourceName + " " + file , e);
645
            processDefaultWebConfig(webDigester, stream, source);
646
        }
646
            webRuleSet.recycle();
647
647
        }
648
        if (stream != null) {
648
649
            processDefaultWebConfig(webDigester, stream, source);
649
    }
650
            webRuleSet.recycle();
650
651
        }
651
652
652
    /**
653
    }
653
     * Process a default web.xml.
654
654
     */
655
655
    protected void processDefaultWebConfig(Digester digester, InputStream stream, 
656
    /**
656
            InputSource source) {
657
     * Process a default web.xml.
657
658
     */
658
        if (log.isDebugEnabled())
659
    protected void processDefaultWebConfig(Digester digester, InputStream stream, 
659
            log.debug("Processing context [" + context.getName() 
660
            InputSource source) {
660
                    + "] web configuration resource " + source.getSystemId());
661
661
662
        if (log.isDebugEnabled())
662
        // Process the default web.xml file
663
            log.debug("Processing context [" + context.getName() 
663
        synchronized (digester) {
664
                    + "] web configuration resource " + source.getSystemId());
664
            try {
665
665
                source.setByteStream(stream);
666
        // Process the default web.xml file
666
                
667
        synchronized (digester) {
667
                if (context instanceof StandardContext)
668
            try {
668
                    ((StandardContext) context).setReplaceWelcomeFiles(true);
669
                source.setByteStream(stream);
669
                digester.setClassLoader(this.getClass().getClassLoader());
670
                
670
                digester.setUseContextClassLoader(false);
671
                if (context instanceof StandardContext)
671
                digester.push(context);
672
                    ((StandardContext) context).setReplaceWelcomeFiles(true);
672
                digester.setErrorHandler(new ContextErrorHandler());
673
                digester.setClassLoader(this.getClass().getClassLoader());
673
                digester.parse(source);
674
                digester.setUseContextClassLoader(false);
674
                if (parseException != null) {
675
                digester.push(context);
675
                    ok = false;
676
                digester.setErrorHandler(new ContextErrorHandler());
676
                }
677
                digester.parse(source);
677
            } catch (SAXParseException e) {
678
                if (parseException != null) {
678
                log.error(sm.getString("contextConfig.defaultParse"), e);
679
                    ok = false;
679
                log.error(sm.getString("contextConfig.defaultPosition",
680
                }
680
                                 "" + e.getLineNumber(),
681
            } catch (SAXParseException e) {
681
                                 "" + e.getColumnNumber()));
682
                log.error(sm.getString("contextConfig.defaultParse"), e);
682
                ok = false;
683
                log.error(sm.getString("contextConfig.defaultPosition",
683
            } catch (Exception e) {
684
                                 "" + e.getLineNumber(),
684
                log.error(sm.getString("contextConfig.defaultParse"), e);
685
                                 "" + e.getColumnNumber()));
685
                ok = false;
686
                ok = false;
686
            } finally {
687
            } catch (Exception e) {
687
                digester.reset();
688
                log.error(sm.getString("contextConfig.defaultParse"), e);
688
                parseException = null;
689
                ok = false;
689
                try {
690
            } finally {
690
                    if (stream != null) {
691
                digester.reset();
691
                        stream.close();
692
                parseException = null;
692
                    }
693
                try {
693
                } catch (IOException e) {
694
                    if (stream != null) {
694
                    log.error(sm.getString("contextConfig.defaultClose"), e);
695
                        stream.close();
695
                }
696
                    }
696
            }
697
                } catch (IOException e) {
697
        }
698
                    log.error(sm.getString("contextConfig.defaultClose"), e);
698
    }
699
                }
699
700
            }
700
701
        }
701
    /**
702
    }
702
     * Process the default configuration file, if it exists.
703
703
     */
704
704
    protected void contextConfig() {
705
    /**
705
        
706
     * Process the default configuration file, if it exists.
706
        // Open the default web.xml file, if it exists
707
     */
707
        if( defaultContextXml==null && context instanceof StandardContext ) {
708
    protected void contextConfig() {
708
            defaultContextXml = ((StandardContext)context).getDefaultContextXml();
709
        
709
        }
710
        // Open the default web.xml file, if it exists
710
        // set the default if we don't have any overrides
711
        if( defaultContextXml==null && context instanceof StandardContext ) {
711
        if( defaultContextXml==null ) getDefaultContextXml();
712
            defaultContextXml = ((StandardContext)context).getDefaultContextXml();
712
713
        }
713
        if (!context.getOverride()) {
714
        // set the default if we don't have any overrides
714
            processContextConfig(new File(getBaseDir()), defaultContextXml);
715
        if( defaultContextXml==null ) getDefaultContextXml();
715
            processContextConfig(getConfigBase(), getHostConfigPath(Constants.HostContextXml));
716
716
        }
717
        if (!context.getOverride()) {
717
        if (context.getConfigFile() != null)
718
            processContextConfig(new File(getBaseDir()), defaultContextXml);
718
            processContextConfig(new File(context.getConfigFile()), null);
719
            processContextConfig(getConfigBase(), getHostConfigPath(Constants.HostContextXml));
719
        
720
        }
720
    }
721
        if (context.getConfigFile() != null)
721
722
            processContextConfig(new File(context.getConfigFile()), null);
722
    
723
        
723
    /**
724
    }
724
     * Process a context.xml.
725
725
     */
726
    
726
    protected void processContextConfig(File baseDir, String resourceName) {
727
    /**
727
        
728
     * Process a context.xml.
728
        if (log.isDebugEnabled())
729
     */
729
            log.debug("Processing context [" + context.getName() 
730
    protected void processContextConfig(File baseDir, String resourceName) {
730
                    + "] configuration file " + baseDir + " " + resourceName);
731
        
731
732
        if (log.isDebugEnabled())
732
        InputSource source = null;
733
            log.debug("Processing context [" + context.getName() 
733
        InputStream stream = null;
734
                    + "] configuration file " + baseDir + " " + resourceName);
734
735
735
        File file = baseDir;
736
        InputSource source = null;
736
        if (resourceName != null) {
737
        InputStream stream = null;
737
            file = new File(baseDir, resourceName);
738
738
        }
739
        File file = baseDir;
739
        
740
        if (resourceName != null) {
740
        try {
741
            file = new File(baseDir, resourceName);
741
            if ( !file.exists() ) {
742
        }
742
                if (resourceName != null) {
743
        
743
                    // Use getResource and getResourceAsStream
744
        try {
744
                    stream = getClass().getClassLoader()
745
            if ( !file.exists() ) {
745
                        .getResourceAsStream(resourceName);
746
                if (resourceName != null) {
746
                    if( stream != null ) {
747
                    // Use getResource and getResourceAsStream
747
                        source = new InputSource
748
                    stream = getClass().getClassLoader()
748
                            (getClass().getClassLoader()
749
                        .getResourceAsStream(resourceName);
749
                            .getResource(resourceName).toString());
750
                    if( stream != null ) {
750
                    }
751
                        source = new InputSource
751
                }
752
                            (getClass().getClassLoader()
752
            } else {
753
                            .getResource(resourceName).toString());
753
                source =
754
                    }
754
                    new InputSource("file://" + file.getAbsolutePath());
755
                }
755
                stream = new FileInputStream(file);
756
            } else {
756
                // Add as watched resource so that cascade reload occurs if a default
757
                source =
757
                // config file is modified/added/removed
758
                    new InputSource("file://" + file.getAbsolutePath());
758
                context.addWatchedResource(file.getAbsolutePath());
759
                stream = new FileInputStream(file);
759
            }
760
                // Add as watched resource so that cascade reload occurs if a default
760
        } catch (Exception e) {
761
                // config file is modified/added/removed
761
            log.error(sm.getString("contextConfig.contextMissing",
762
                context.addWatchedResource(file.getAbsolutePath());
762
                      resourceName + " " + file), e);
763
            }
763
        }
764
        } catch (Exception e) {
764
        
765
            log.error(sm.getString("contextConfig.contextMissing",
765
        if (source == null)
766
                      resourceName + " " + file), e);
766
            return;
767
        }
767
        if (contextDigester == null){
768
        
768
            contextDigester = createContextDigester();
769
        if (source == null)
769
        }
770
            return;
770
        synchronized (contextDigester) {
771
        if (contextDigester == null){
771
            try {
772
            contextDigester = createContextDigester();
772
                source.setByteStream(stream);
773
        }
773
                contextDigester.setClassLoader(this.getClass().getClassLoader());
774
        synchronized (contextDigester) {
774
                contextDigester.setUseContextClassLoader(false);
775
            try {
775
                contextDigester.push(context.getParent());
776
                source.setByteStream(stream);
776
                contextDigester.push(context);
777
                contextDigester.setClassLoader(this.getClass().getClassLoader());
777
                contextDigester.setErrorHandler(new ContextErrorHandler());
778
                contextDigester.setUseContextClassLoader(false);
778
                contextDigester.parse(source);
779
                contextDigester.push(context.getParent());
779
                if (parseException != null) {
780
                contextDigester.push(context);
780
                    ok = false;
781
                contextDigester.setErrorHandler(new ContextErrorHandler());
781
                }
782
                contextDigester.parse(source);
782
                if (log.isDebugEnabled())
783
                if (parseException != null) {
783
                    log.debug("Successfully processed context [" + context.getName() 
784
                    ok = false;
784
                            + "] configuration file " + baseDir + " " + resourceName);
785
                }
785
            } catch (SAXParseException e) {
786
                if (log.isDebugEnabled())
786
                log.error(sm.getString("contextConfig.contextParse",
787
                    log.debug("Successfully processed context [" + context.getName() 
787
                        context.getName()), e);
788
                            + "] configuration file " + baseDir + " " + resourceName);
788
                log.error(sm.getString("contextConfig.defaultPosition",
789
            } catch (SAXParseException e) {
789
                                 "" + e.getLineNumber(),
790
                log.error(sm.getString("contextConfig.contextParse",
790
                                 "" + e.getColumnNumber()));
791
                        context.getName()), e);
791
                ok = false;
792
                log.error(sm.getString("contextConfig.defaultPosition",
792
            } catch (Exception e) {
793
                                 "" + e.getLineNumber(),
793
                log.error(sm.getString("contextConfig.contextParse",
794
                                 "" + e.getColumnNumber()));
794
                        context.getName()), e);
795
                ok = false;
795
                ok = false;
796
            } catch (Exception e) {
796
            } finally {
797
                log.error(sm.getString("contextConfig.contextParse",
797
                contextDigester.reset();
798
                        context.getName()), e);
798
                parseException = null;
799
                ok = false;
799
                try {
800
            } finally {
800
                    if (stream != null) {
801
                contextDigester.reset();
801
                        stream.close();
802
                parseException = null;
802
                    }
803
                try {
803
                } catch (IOException e) {
804
                    if (stream != null) {
804
                    log.error(sm.getString("contextConfig.contextClose"), e);
805
                        stream.close();
805
                }
806
                    }
806
            }
807
                } catch (IOException e) {
807
        }
808
                    log.error(sm.getString("contextConfig.contextClose"), e);
808
    }
809
                }
809
810
            }
810
    
811
        }
811
    /**
812
    }
812
     * Adjust docBase.
813
813
     */
814
    
814
    protected void fixDocBase()
815
    /**
815
        throws IOException {
816
     * Adjust docBase.
816
        
817
     */
817
        Host host = (Host) context.getParent();
818
    protected void fixDocBase()
818
        String appBase = host.getAppBase();
819
        throws IOException {
819
820
        
820
        boolean unpackWARs = true;
821
        Host host = (Host) context.getParent();
821
        if (host instanceof StandardHost) {
822
        String appBase = host.getAppBase();
822
            unpackWARs = ((StandardHost) host).isUnpackWARs() 
823
823
                && ((StandardContext) context).getUnpackWAR();
824
        boolean unpackWARs = true;
824
        }
825
        if (host instanceof StandardHost) {
825
826
            unpackWARs = ((StandardHost) host).isUnpackWARs() 
826
        File canonicalAppBase = new File(appBase);
827
                && ((StandardContext) context).getUnpackWAR();
827
        if (canonicalAppBase.isAbsolute()) {
828
        }
828
            canonicalAppBase = canonicalAppBase.getCanonicalFile();
829
829
        } else {
830
        File canonicalAppBase = new File(appBase);
830
            canonicalAppBase = 
831
        if (canonicalAppBase.isAbsolute()) {
831
                new File(System.getProperty("catalina.base"), appBase)
832
            canonicalAppBase = canonicalAppBase.getCanonicalFile();
832
                .getCanonicalFile();
833
        } else {
833
        }
834
            canonicalAppBase = 
834
835
                new File(System.getProperty("catalina.base"), appBase)
835
        String docBase = context.getDocBase();
836
                .getCanonicalFile();
836
        if (docBase == null) {
837
        }
837
            // Trying to guess the docBase according to the path
838
838
            String path = context.getPath();
839
        String docBase = context.getDocBase();
839
            if (path == null) {
840
        if (docBase == null) {
840
                return;
841
            // Trying to guess the docBase according to the path
841
            }
842
            String path = context.getPath();
842
            if (path.equals("")) {
843
            if (path == null) {
843
                docBase = "ROOT";
844
                return;
844
            } else {
845
            }
845
                if (path.startsWith("/")) {
846
            if (path.equals("")) {
846
                    docBase = path.substring(1);
847
                docBase = "ROOT";
847
                } else {
848
            } else {
848
                    docBase = path;
849
                if (path.startsWith("/")) {
849
                }
850
                    docBase = path.substring(1);
850
            }
851
                } else {
851
        }
852
                    docBase = path;
852
853
                }
853
        File file = new File(docBase);
854
            }
854
        if (!file.isAbsolute()) {
855
        }
855
            docBase = (new File(canonicalAppBase, docBase)).getPath();
856
856
        } else {
857
        File file = new File(docBase);
857
            docBase = file.getCanonicalPath();
858
        if (!file.isAbsolute()) {
858
        }
859
            docBase = (new File(canonicalAppBase, docBase)).getPath();
859
        file = new File(docBase);
860
        } else {
860
        String origDocBase = docBase ;
861
            docBase = file.getCanonicalPath();
861
 
862
        }
862
        String contextPath = context.getPath();
863
        file = new File(docBase);
863
        if (contextPath.equals("")) {
864
        String origDocBase = docBase ;
864
            contextPath = "ROOT";
865
 
865
        }
866
        if (docBase.toLowerCase().endsWith(".war") && !file.isDirectory() && unpackWARs) {
866
        if (docBase.toLowerCase().endsWith(".war") && !file.isDirectory() && unpackWARs) {
867
            URL war = new URL("jar:" + (new File(docBase)).toURL() + "!/");
867
            URL war = new URL("jar:" + (new File(docBase)).toURL() + "!/");
868
            String contextPath = context.getPath();
868
            docBase = ExpandWar.expand(host, war, contextPath);
869
            if (contextPath.equals("")) {
869
            file = new File(docBase);
870
                contextPath = "ROOT";
870
            docBase = file.getCanonicalPath();
871
            }
871
            if (context instanceof StandardContext) {
872
            docBase = ExpandWar.expand(host, war, contextPath);
872
                ((StandardContext) context).setOriginalDocBase(origDocBase);
873
            file = new File(docBase);
873
            }
874
            docBase = file.getCanonicalPath();
874
        } else {
875
            if (context instanceof StandardContext) {
875
            File docDir = new File(docBase);
876
                ((StandardContext) context).setOriginalDocBase(origDocBase);
876
            if (!docDir.exists()) {
877
            }
877
                File warFile = new File(docBase + ".war");
878
        } else {
878
                if (warFile.exists()) {
879
            File docDir = new File(docBase);
879
                    if (unpackWARs) {
880
            if (!docDir.exists()) {
880
                        URL war = new URL("jar:" + warFile.toURL() + "!/");
881
                File warFile = new File(docBase + ".war");
881
                        docBase = ExpandWar.expand(host, war, contextPath);
882
                if (warFile.exists()) {
882
                        file = new File(docBase);
883
                    if (unpackWARs) {
883
                        docBase = file.getCanonicalPath();
884
                        URL war = new URL("jar:" + warFile.toURL() + "!/");
884
                    } else {
885
                        docBase = ExpandWar.expand(host, war, context.getPath());
885
                        docBase = warFile.getCanonicalPath();
886
                        file = new File(docBase);
886
                    }
887
                        docBase = file.getCanonicalPath();
887
                }
888
                    } else {
888
                if (context instanceof StandardContext) {
889
                        docBase = warFile.getCanonicalPath();
889
                    ((StandardContext)context).setOriginalDocBase(origDocBase);
890
                    }
890
                }
891
                }
891
            }
892
                if (context instanceof StandardContext) {
892
        }
893
                    ((StandardContext)context).setOriginalDocBase(origDocBase);
893
894
                }
894
        if (docBase.startsWith(canonicalAppBase.getPath())) {
895
            }
895
            docBase = docBase.substring(canonicalAppBase.getPath().length());
896
        }
896
            docBase = docBase.replace(File.separatorChar, '/');
897
897
            if (docBase.startsWith("/")) {
898
        if (docBase.startsWith(canonicalAppBase.getPath())) {
898
                docBase = docBase.substring(1);
899
            docBase = docBase.substring(canonicalAppBase.getPath().length());
899
            }
900
            docBase = docBase.replace(File.separatorChar, '/');
900
        } else {
901
            if (docBase.startsWith("/")) {
901
            docBase = docBase.replace(File.separatorChar, '/');
902
                docBase = docBase.substring(1);
902
        }
903
            }
903
904
        } else {
904
        context.setDocBase(docBase);
905
            docBase = docBase.replace(File.separatorChar, '/');
905
906
        }
906
    }
907
907
    
908
        context.setDocBase(docBase);
908
    
909
909
    protected void antiLocking()
910
    }
910
        throws IOException {
911
    
911
912
    
912
        if ((context instanceof StandardContext) 
913
    protected void antiLocking()
913
            && ((StandardContext) context).getAntiResourceLocking()) {
914
        throws IOException {
914
            
915
915
            Host host = (Host) context.getParent();
916
        if ((context instanceof StandardContext) 
916
            String appBase = host.getAppBase();
917
            && ((StandardContext) context).getAntiResourceLocking()) {
917
            String docBase = context.getDocBase();
918
            
918
            if (docBase == null)
919
            Host host = (Host) context.getParent();
919
                return;
920
            String appBase = host.getAppBase();
920
            if (originalDocBase == null) {
921
            String docBase = context.getDocBase();
921
                originalDocBase = docBase;
922
            if (docBase == null)
922
            } else {
923
                return;
923
                docBase = originalDocBase;
924
            if (originalDocBase == null) {
924
            }
925
                originalDocBase = docBase;
925
            File docBaseFile = new File(docBase);
926
            } else {
926
            if (!docBaseFile.isAbsolute()) {
927
                docBase = originalDocBase;
927
                File file = new File(appBase);
928
            }
928
                if (!file.isAbsolute()) {
929
            File docBaseFile = new File(docBase);
929
                    file = new File(System.getProperty("catalina.base"), appBase);
930
            if (!docBaseFile.isAbsolute()) {
930
                }
931
                File file = new File(appBase);
931
                docBaseFile = new File(file, docBase);
932
                if (!file.isAbsolute()) {
932
            }
933
                    file = new File(System.getProperty("catalina.base"), appBase);
933
            
934
                }
934
            String path = context.getPath();
935
                docBaseFile = new File(file, docBase);
935
            if (path == null) {
936
            }
936
                return;
937
            
937
            }
938
            String path = context.getPath();
938
            if (path.equals("")) {
939
            if (path == null) {
939
                docBase = "ROOT";
940
                return;
940
            } else {
941
            }
941
                if (path.startsWith("/")) {
942
            if (path.equals("")) {
942
                    docBase = path.substring(1);
943
                docBase = "ROOT";
943
                } else {
944
            } else {
944
                    docBase = path;
945
                if (path.startsWith("/")) {
945
                }
946
                    docBase = path.substring(1);
946
            }
947
                } else {
947
948
                    docBase = path;
948
            File file = null;
949
                }
949
            if (docBase.toLowerCase().endsWith(".war")) {
950
            }
950
                file = new File(System.getProperty("java.io.tmpdir"),
951
951
                        deploymentCount++ + "-" + docBase + ".war");
952
            File file = null;
952
            } else {
953
            if (docBase.toLowerCase().endsWith(".war")) {
953
                file = new File(System.getProperty("java.io.tmpdir"), 
954
                file = new File(System.getProperty("java.io.tmpdir"),
954
                        deploymentCount++ + "-" + docBase);
955
                        deploymentCount++ + "-" + docBase + ".war");
955
            }
956
            } else {
956
            
957
                file = new File(System.getProperty("java.io.tmpdir"), 
957
            if (log.isDebugEnabled())
958
                        deploymentCount++ + "-" + docBase);
958
                log.debug("Anti locking context[" + context.getPath() 
959
            }
959
                        + "] setting docBase to " + file);
960
            
960
            
961
            if (log.isDebugEnabled())
961
            // Cleanup just in case an old deployment is lying around
962
                log.debug("Anti locking context[" + context.getPath() 
962
            ExpandWar.delete(file);
963
                        + "] setting docBase to " + file);
963
            if (ExpandWar.copy(docBaseFile, file)) {
964
            
964
                context.setDocBase(file.getAbsolutePath());
965
            // Cleanup just in case an old deployment is lying around
965
            }
966
            ExpandWar.delete(file);
966
            
967
            if (ExpandWar.copy(docBaseFile, file)) {
967
        }
968
                context.setDocBase(file.getAbsolutePath());
968
        
969
            }
969
    }
970
            
970
    
971
        }
971
972
        
972
    /**
973
    }
973
     * Process a "init" event for this Context.
974
    
974
     */
975
975
    protected void init() {
976
    /**
976
        // Called from StandardContext.init()
977
     * Process a "init" event for this Context.
977
978
     */
978
        if (log.isDebugEnabled())
979
    protected void init() {
979
            log.debug(sm.getString("contextConfig.init"));
980
        // Called from StandardContext.init()
980
        context.setConfigured(false);
981
981
        ok = true;
982
        if (log.isDebugEnabled())
982
        
983
            log.debug(sm.getString("contextConfig.init"));
983
        contextConfig();
984
        context.setConfigured(false);
984
        
985
        ok = true;
985
        try {
986
        
986
            fixDocBase();
987
        contextConfig();
987
        } catch (IOException e) {
988
        
988
            log.error(sm.getString("contextConfig.fixDocBase"), e);
989
        try {
989
        }
990
            fixDocBase();
990
        
991
        } catch (IOException e) {
991
    }
992
            log.error(sm.getString("contextConfig.fixDocBase"), e);
992
    
993
        }
993
    
994
        
994
    /**
995
    }
995
     * Process a "before start" event for this Context.
996
    
996
     */
997
    
997
    protected synchronized void beforeStart() {
998
    /**
998
        
999
     * Process a "before start" event for this Context.
999
        try {
1000
     */
1000
            antiLocking();
1001
    protected synchronized void beforeStart() {
1001
        } catch (IOException e) {
1002
        
1002
            log.error(sm.getString("contextConfig.antiLocking"), e);
1003
        try {
1003
        }
1004
            antiLocking();
1004
        
1005
        } catch (IOException e) {
1005
    }
1006
            log.error(sm.getString("contextConfig.antiLocking"), e);
1006
    
1007
        }
1007
    
1008
        
1008
    /**
1009
    }
1009
     * Process a "start" event for this Context.
1010
    
1010
     */
1011
    
1011
    protected synchronized void start() {
1012
    /**
1012
        // Called from StandardContext.start()
1013
     * Process a "start" event for this Context.
1013
1014
     */
1014
        if (log.isDebugEnabled())
1015
    protected synchronized void start() {
1015
            log.debug(sm.getString("contextConfig.start"));
1016
        // Called from StandardContext.start()
1016
1017
1017
        // Set properties based on DefaultContext
1018
        if (log.isDebugEnabled())
1018
        Container container = context.getParent();
1019
            log.debug(sm.getString("contextConfig.start"));
1019
        if( !context.getOverride() ) {
1020
1020
            if( container instanceof Host ) {
1021
        // Set properties based on DefaultContext
1021
                // Reset the value only if the attribute wasn't
1022
        Container container = context.getParent();
1022
                // set on the context.
1023
        if( !context.getOverride() ) {
1023
                xmlValidation = context.getXmlValidation();
1024
            if( container instanceof Host ) {
1024
                if (!xmlValidation) {
1025
                // Reset the value only if the attribute wasn't
1025
                    xmlValidation = ((Host)container).getXmlValidation();
1026
                // set on the context.
1026
                }
1027
                xmlValidation = context.getXmlValidation();
1027
                
1028
                if (!xmlValidation) {
1028
                xmlNamespaceAware = context.getXmlNamespaceAware();
1029
                    xmlValidation = ((Host)container).getXmlValidation();
1029
                if (!xmlNamespaceAware){
1030
                }
1030
                    xmlNamespaceAware 
1031
                
1031
                                = ((Host)container).getXmlNamespaceAware();
1032
                xmlNamespaceAware = context.getXmlNamespaceAware();
1032
                }
1033
                if (!xmlNamespaceAware){
1033
1034
                    xmlNamespaceAware 
1034
                container = container.getParent();
1035
                                = ((Host)container).getXmlNamespaceAware();
1035
            }
1036
                }
1036
        }
1037
1037
1038
                container = container.getParent();
1038
        // Process the default and application web.xml files
1039
            }
1039
        defaultWebConfig();
1040
        }
1040
        applicationWebConfig();
1041
1041
        if (ok) {
1042
        // Process the default and application web.xml files
1042
            validateSecurityRoles();
1043
        defaultWebConfig();
1043
        }
1044
        applicationWebConfig();
1044
1045
        if (ok) {
1045
        // Configure an authenticator if we need one
1046
            validateSecurityRoles();
1046
        if (ok)
1047
        }
1047
            authenticatorConfig();
1048
1048
1049
        // Configure an authenticator if we need one
1049
        // Dump the contents of this pipeline if requested
1050
        if (ok)
1050
        if ((log.isDebugEnabled()) && (context instanceof ContainerBase)) {
1051
            authenticatorConfig();
1051
            log.debug("Pipeline Configuration:");
1052
1052
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
1053
        // Dump the contents of this pipeline if requested
1053
            Valve valves[] = null;
1054
        if ((log.isDebugEnabled()) && (context instanceof ContainerBase)) {
1054
            if (pipeline != null)
1055
            log.debug("Pipeline Configuration:");
1055
                valves = pipeline.getValves();
1056
            Pipeline pipeline = ((ContainerBase) context).getPipeline();
1056
            if (valves != null) {
1057
            Valve valves[] = null;
1057
                for (int i = 0; i < valves.length; i++) {
1058
            if (pipeline != null)
1058
                    log.debug("  " + valves[i].getInfo());
1059
                valves = pipeline.getValves();
1059
                }
1060
            if (valves != null) {
1060
            }
1061
                for (int i = 0; i < valves.length; i++) {
1061
            log.debug("======================");
1062
                    log.debug("  " + valves[i].getInfo());
1062
        }
1063
                }
1063
1064
            }
1064
        // Make our application available if no problems were encountered
1065
            log.debug("======================");
1065
        if (ok)
1066
        }
1066
            context.setConfigured(true);
1067
1067
        else {
1068
        // Make our application available if no problems were encountered
1068
            log.error(sm.getString("contextConfig.unavailable"));
1069
        if (ok)
1069
            context.setConfigured(false);
1070
            context.setConfigured(true);
1070
        }
1071
        else {
1071
1072
            log.error(sm.getString("contextConfig.unavailable"));
1072
    }
1073
            context.setConfigured(false);
1073
1074
        }
1074
1075
1075
    /**
1076
    }
1076
     * Process a "stop" event for this Context.
1077
1077
     */
1078
1078
    protected synchronized void stop() {
1079
    /**
1079
1080
     * Process a "stop" event for this Context.
1080
        if (log.isDebugEnabled())
1081
     */
1081
            log.debug(sm.getString("contextConfig.stop"));
1082
    protected synchronized void stop() {
1082
1083
1083
        int i;
1084
        if (log.isDebugEnabled())
1084
1085
            log.debug(sm.getString("contextConfig.stop"));
1085
        // Removing children
1086
1086
        Container[] children = context.findChildren();
1087
        int i;
1087
        for (i = 0; i < children.length; i++) {
1088
1088
            context.removeChild(children[i]);
1089
        // Removing children
1089
        }
1090
        Container[] children = context.findChildren();
1090
1091
        for (i = 0; i < children.length; i++) {
1091
        // Removing application parameters
1092
            context.removeChild(children[i]);
1092
        /*
1093
        }
1093
        ApplicationParameter[] applicationParameters =
1094
1094
            context.findApplicationParameters();
1095
        // Removing application parameters
1095
        for (i = 0; i < applicationParameters.length; i++) {
1096
        /*
1096
            context.removeApplicationParameter
1097
        ApplicationParameter[] applicationParameters =
1097
                (applicationParameters[i].getName());
1098
            context.findApplicationParameters();
1098
        }
1099
        for (i = 0; i < applicationParameters.length; i++) {
1099
        */
1100
            context.removeApplicationParameter
1100
1101
                (applicationParameters[i].getName());
1101
        // Removing security constraints
1102
        }
1102
        SecurityConstraint[] securityConstraints = context.findConstraints();
1103
        */
1103
        for (i = 0; i < securityConstraints.length; i++) {
1104
1104
            context.removeConstraint(securityConstraints[i]);
1105
        // Removing security constraints
1105
        }
1106
        SecurityConstraint[] securityConstraints = context.findConstraints();
1106
1107
        for (i = 0; i < securityConstraints.length; i++) {
1107
        // Removing Ejbs
1108
            context.removeConstraint(securityConstraints[i]);
1108
        /*
1109
        }
1109
        ContextEjb[] contextEjbs = context.findEjbs();
1110
1110
        for (i = 0; i < contextEjbs.length; i++) {
1111
        // Removing Ejbs
1111
            context.removeEjb(contextEjbs[i].getName());
1112
        /*
1112
        }
1113
        ContextEjb[] contextEjbs = context.findEjbs();
1113
        */
1114
        for (i = 0; i < contextEjbs.length; i++) {
1114
1115
            context.removeEjb(contextEjbs[i].getName());
1115
        // Removing environments
1116
        }
1116
        /*
1117
        */
1117
        ContextEnvironment[] contextEnvironments = context.findEnvironments();
1118
1118
        for (i = 0; i < contextEnvironments.length; i++) {
1119
        // Removing environments
1119
            context.removeEnvironment(contextEnvironments[i].getName());
1120
        /*
1120
        }
1121
        ContextEnvironment[] contextEnvironments = context.findEnvironments();
1121
        */
1122
        for (i = 0; i < contextEnvironments.length; i++) {
1122
1123
            context.removeEnvironment(contextEnvironments[i].getName());
1123
        // Removing errors pages
1124
        }
1124
        ErrorPage[] errorPages = context.findErrorPages();
1125
        */
1125
        for (i = 0; i < errorPages.length; i++) {
1126
1126
            context.removeErrorPage(errorPages[i]);
1127
        // Removing errors pages
1127
        }
1128
        ErrorPage[] errorPages = context.findErrorPages();
1128
1129
        for (i = 0; i < errorPages.length; i++) {
1129
        // Removing filter defs
1130
            context.removeErrorPage(errorPages[i]);
1130
        FilterDef[] filterDefs = context.findFilterDefs();
1131
        }
1131
        for (i = 0; i < filterDefs.length; i++) {
1132
1132
            context.removeFilterDef(filterDefs[i]);
1133
        // Removing filter defs
1133
        }
1134
        FilterDef[] filterDefs = context.findFilterDefs();
1134
1135
        for (i = 0; i < filterDefs.length; i++) {
1135
        // Removing filter maps
1136
            context.removeFilterDef(filterDefs[i]);
1136
        FilterMap[] filterMaps = context.findFilterMaps();
1137
        }
1137
        for (i = 0; i < filterMaps.length; i++) {
1138
1138
            context.removeFilterMap(filterMaps[i]);
1139
        // Removing filter maps
1139
        }
1140
        FilterMap[] filterMaps = context.findFilterMaps();
1140
1141
        for (i = 0; i < filterMaps.length; i++) {
1141
        // Removing local ejbs
1142
            context.removeFilterMap(filterMaps[i]);
1142
        /*
1143
        }
1143
        ContextLocalEjb[] contextLocalEjbs = context.findLocalEjbs();
1144
1144
        for (i = 0; i < contextLocalEjbs.length; i++) {
1145
        // Removing local ejbs
1145
            context.removeLocalEjb(contextLocalEjbs[i].getName());
1146
        /*
1146
        }
1147
        ContextLocalEjb[] contextLocalEjbs = context.findLocalEjbs();
1147
        */
1148
        for (i = 0; i < contextLocalEjbs.length; i++) {
1148
1149
            context.removeLocalEjb(contextLocalEjbs[i].getName());
1149
        // Removing Mime mappings
1150
        }
1150
        String[] mimeMappings = context.findMimeMappings();
1151
        */
1151
        for (i = 0; i < mimeMappings.length; i++) {
1152
1152
            context.removeMimeMapping(mimeMappings[i]);
1153
        // Removing Mime mappings
1153
        }
1154
        String[] mimeMappings = context.findMimeMappings();
1154
1155
        for (i = 0; i < mimeMappings.length; i++) {
1155
        // Removing parameters
1156
            context.removeMimeMapping(mimeMappings[i]);
1156
        String[] parameters = context.findParameters();
1157
        }
1157
        for (i = 0; i < parameters.length; i++) {
1158
1158
            context.removeParameter(parameters[i]);
1159
        // Removing parameters
1159
        }
1160
        String[] parameters = context.findParameters();
1160
1161
        for (i = 0; i < parameters.length; i++) {
1161
        // Removing resource env refs
1162
            context.removeParameter(parameters[i]);
1162
        /*
1163
        }
1163
        String[] resourceEnvRefs = context.findResourceEnvRefs();
1164
1164
        for (i = 0; i < resourceEnvRefs.length; i++) {
1165
        // Removing resource env refs
1165
            context.removeResourceEnvRef(resourceEnvRefs[i]);
1166
        /*
1166
        }
1167
        String[] resourceEnvRefs = context.findResourceEnvRefs();
1167
        */
1168
        for (i = 0; i < resourceEnvRefs.length; i++) {
1168
1169
            context.removeResourceEnvRef(resourceEnvRefs[i]);
1169
        // Removing resource links
1170
        }
1170
        /*
1171
        */
1171
        ContextResourceLink[] contextResourceLinks =
1172
1172
            context.findResourceLinks();
1173
        // Removing resource links
1173
        for (i = 0; i < contextResourceLinks.length; i++) {
1174
        /*
1174
            context.removeResourceLink(contextResourceLinks[i].getName());
1175
        ContextResourceLink[] contextResourceLinks =
1175
        }
1176
            context.findResourceLinks();
1176
        */
1177
        for (i = 0; i < contextResourceLinks.length; i++) {
1177
1178
            context.removeResourceLink(contextResourceLinks[i].getName());
1178
        // Removing resources
1179
        }
1179
        /*
1180
        */
1180
        ContextResource[] contextResources = context.findResources();
1181
1181
        for (i = 0; i < contextResources.length; i++) {
1182
        // Removing resources
1182
            context.removeResource(contextResources[i].getName());
1183
        /*
1183
        }
1184
        ContextResource[] contextResources = context.findResources();
1184
        */
1185
        for (i = 0; i < contextResources.length; i++) {
1185
1186
            context.removeResource(contextResources[i].getName());
1186
        // Removing sercurity role
1187
        }
1187
        String[] securityRoles = context.findSecurityRoles();
1188
        */
1188
        for (i = 0; i < securityRoles.length; i++) {
1189
1189
            context.removeSecurityRole(securityRoles[i]);
1190
        // Removing sercurity role
1190
        }
1191
        String[] securityRoles = context.findSecurityRoles();
1191
1192
        for (i = 0; i < securityRoles.length; i++) {
1192
        // Removing servlet mappings
1193
            context.removeSecurityRole(securityRoles[i]);
1193
        String[] servletMappings = context.findServletMappings();
1194
        }
1194
        for (i = 0; i < servletMappings.length; i++) {
1195
1195
            context.removeServletMapping(servletMappings[i]);
1196
        // Removing servlet mappings
1196
        }
1197
        String[] servletMappings = context.findServletMappings();
1197
1198
        for (i = 0; i < servletMappings.length; i++) {
1198
        // FIXME : Removing status pages
1199
            context.removeServletMapping(servletMappings[i]);
1199
1200
        }
1200
        // Removing taglibs
1201
1201
        String[] taglibs = context.findTaglibs();
1202
        // FIXME : Removing status pages
1202
        for (i = 0; i < taglibs.length; i++) {
1203
1203
            context.removeTaglib(taglibs[i]);
1204
        // Removing taglibs
1204
        }
1205
        String[] taglibs = context.findTaglibs();
1205
1206
        for (i = 0; i < taglibs.length; i++) {
1206
        // Removing welcome files
1207
            context.removeTaglib(taglibs[i]);
1207
        String[] welcomeFiles = context.findWelcomeFiles();
1208
        }
1208
        for (i = 0; i < welcomeFiles.length; i++) {
1209
1209
            context.removeWelcomeFile(welcomeFiles[i]);
1210
        // Removing welcome files
1210
        }
1211
        String[] welcomeFiles = context.findWelcomeFiles();
1211
1212
        for (i = 0; i < welcomeFiles.length; i++) {
1212
        // Removing wrapper lifecycles
1213
            context.removeWelcomeFile(welcomeFiles[i]);
1213
        String[] wrapperLifecycles = context.findWrapperLifecycles();
1214
        }
1214
        for (i = 0; i < wrapperLifecycles.length; i++) {
1215
1215
            context.removeWrapperLifecycle(wrapperLifecycles[i]);
1216
        // Removing wrapper lifecycles
1216
        }
1217
        String[] wrapperLifecycles = context.findWrapperLifecycles();
1217
1218
        for (i = 0; i < wrapperLifecycles.length; i++) {
1218
        // Removing wrapper listeners
1219
            context.removeWrapperLifecycle(wrapperLifecycles[i]);
1219
        String[] wrapperListeners = context.findWrapperListeners();
1220
        }
1220
        for (i = 0; i < wrapperListeners.length; i++) {
1221
1221
            context.removeWrapperListener(wrapperListeners[i]);
1222
        // Removing wrapper listeners
1222
        }
1223
        String[] wrapperListeners = context.findWrapperListeners();
1223
1224
        for (i = 0; i < wrapperListeners.length; i++) {
1224
        // Remove (partially) folders and files created by antiLocking
1225
            context.removeWrapperListener(wrapperListeners[i]);
1225
        Host host = (Host) context.getParent();
1226
        }
1226
        String appBase = host.getAppBase();
1227
1227
        String docBase = context.getDocBase();
1228
        // Remove (partially) folders and files created by antiLocking
1228
        if ((docBase != null) && (originalDocBase != null)) {
1229
        Host host = (Host) context.getParent();
1229
            File docBaseFile = new File(docBase);
1230
        String appBase = host.getAppBase();
1230
            if (!docBaseFile.isAbsolute()) {
1231
        String docBase = context.getDocBase();
1231
                docBaseFile = new File(appBase, docBase);
1232
        if ((docBase != null) && (originalDocBase != null)) {
1232
            }
1233
            File docBaseFile = new File(docBase);
1233
            ExpandWar.delete(docBaseFile);
1234
            if (!docBaseFile.isAbsolute()) {
1234
        }
1235
                docBaseFile = new File(appBase, docBase);
1235
        
1236
            }
1236
        ok = true;
1237
            ExpandWar.delete(docBaseFile);
1237
1238
        }
1238
    }
1239
        
1239
    
1240
        ok = true;
1240
    
1241
1241
    /**
1242
    }
1242
     * Process a "destroy" event for this Context.
1243
    
1243
     */
1244
    
1244
    protected synchronized void destroy() {
1245
    /**
1245
        // Called from StandardContext.destroy()
1246
     * Process a "destroy" event for this Context.
1246
        if (log.isDebugEnabled())
1247
     */
1247
            log.debug(sm.getString("contextConfig.destroy"));
1248
    protected synchronized void destroy() {
1248
1249
        // Called from StandardContext.destroy()
1249
        // Changed to getWorkPath per Bugzilla 35819.
1250
        if (log.isDebugEnabled())
1250
        String workDir = ((StandardContext) context).getWorkPath();
1251
            log.debug(sm.getString("contextConfig.destroy"));
1251
        if (workDir != null)
1252
1252
            ExpandWar.delete(new File(workDir));
1253
        // Changed to getWorkPath per Bugzilla 35819.
1253
    }
1254
        String workDir = ((StandardContext) context).getWorkPath();
1254
    
1255
        if (workDir != null)
1255
    
1256
            ExpandWar.delete(new File(workDir));
1256
    /**
1257
    }
1257
     * Validate the usage of security role names in the web application
1258
    
1258
     * deployment descriptor.  If any problems are found, issue warning
1259
    
1259
     * messages (for backwards compatibility) and add the missing roles.
1260
    /**
1260
     * (To make these problems fatal instead, simply set the <code>ok</code>
1261
     * Validate the usage of security role names in the web application
1261
     * instance variable to <code>false</code> as well).
1262
     * deployment descriptor.  If any problems are found, issue warning
1262
     */
1263
     * messages (for backwards compatibility) and add the missing roles.
1263
    protected void validateSecurityRoles() {
1264
     * (To make these problems fatal instead, simply set the <code>ok</code>
1264
1265
     * instance variable to <code>false</code> as well).
1265
        // Check role names used in <security-constraint> elements
1266
     */
1266
        SecurityConstraint constraints[] = context.findConstraints();
1267
    protected void validateSecurityRoles() {
1267
        for (int i = 0; i < constraints.length; i++) {
1268
1268
            String roles[] = constraints[i].findAuthRoles();
1269
        // Check role names used in <security-constraint> elements
1269
            for (int j = 0; j < roles.length; j++) {
1270
        SecurityConstraint constraints[] = context.findConstraints();
1270
                if (!"*".equals(roles[j]) &&
1271
        for (int i = 0; i < constraints.length; i++) {
1271
                    !context.findSecurityRole(roles[j])) {
1272
            String roles[] = constraints[i].findAuthRoles();
1272
                    log.warn(sm.getString("contextConfig.role.auth", roles[j]));
1273
            for (int j = 0; j < roles.length; j++) {
1273
                    context.addSecurityRole(roles[j]);
1274
                if (!"*".equals(roles[j]) &&
1274
                }
1275
                    !context.findSecurityRole(roles[j])) {
1275
            }
1276
                    log.warn(sm.getString("contextConfig.role.auth", roles[j]));
1276
        }
1277
                    context.addSecurityRole(roles[j]);
1277
1278
                }
1278
        // Check role names used in <servlet> elements
1279
            }
1279
        Container wrappers[] = context.findChildren();
1280
        }
1280
        for (int i = 0; i < wrappers.length; i++) {
1281
1281
            Wrapper wrapper = (Wrapper) wrappers[i];
1282
        // Check role names used in <servlet> elements
1282
            String runAs = wrapper.getRunAs();
1283
        Container wrappers[] = context.findChildren();
1283
            if ((runAs != null) && !context.findSecurityRole(runAs)) {
1284
        for (int i = 0; i < wrappers.length; i++) {
1284
                log.warn(sm.getString("contextConfig.role.runas", runAs));
1285
            Wrapper wrapper = (Wrapper) wrappers[i];
1285
                context.addSecurityRole(runAs);
1286
            String runAs = wrapper.getRunAs();
1286
            }
1287
            if ((runAs != null) && !context.findSecurityRole(runAs)) {
1287
            String names[] = wrapper.findSecurityReferences();
1288
                log.warn(sm.getString("contextConfig.role.runas", runAs));
1288
            for (int j = 0; j < names.length; j++) {
1289
                context.addSecurityRole(runAs);
1289
                String link = wrapper.findSecurityReference(names[j]);
1290
            }
1290
                if ((link != null) && !context.findSecurityRole(link)) {
1291
            String names[] = wrapper.findSecurityReferences();
1291
                    log.warn(sm.getString("contextConfig.role.link", link));
1292
            for (int j = 0; j < names.length; j++) {
1292
                    context.addSecurityRole(link);
1293
                String link = wrapper.findSecurityReference(names[j]);
1293
                }
1294
                if ((link != null) && !context.findSecurityRole(link)) {
1294
            }
1295
                    log.warn(sm.getString("contextConfig.role.link", link));
1295
        }
1296
                    context.addSecurityRole(link);
1296
1297
                }
1297
    }
1298
            }
1298
1299
        }
1299
1300
1300
    /**
1301
    }
1301
     * Get config base.
1302
1302
     */
1303
1303
    protected File getConfigBase() {
1304
    /**
1304
        File configBase = 
1305
     * Get config base.
1305
            new File(System.getProperty("catalina.base"), "conf");
1306
     */
1306
        if (!configBase.exists()) {
1307
    protected File getConfigBase() {
1307
            return null;
1308
        File configBase = 
1308
        } else {
1309
            new File(System.getProperty("catalina.base"), "conf");
1309
            return configBase;
1310
        if (!configBase.exists()) {
1310
        }
1311
            return null;
1311
    }  
1312
        } else {
1312
1313
            return configBase;
1313
    
1314
        }
1314
    protected String getHostConfigPath(String resourceName) {
1315
    }  
1315
        StringBuffer result = new StringBuffer();
1316
1316
        Container container = context;
1317
    
1317
        Container host = null;
1318
    protected String getHostConfigPath(String resourceName) {
1318
        Container engine = null;
1319
        StringBuffer result = new StringBuffer();
1319
        while (container != null) {
1320
        Container container = context;
1320
            if (container instanceof Host)
1321
        Container host = null;
1321
                host = container;
1322
        Container engine = null;
1322
            if (container instanceof Engine)
1323
        while (container != null) {
1323
                engine = container;
1324
            if (container instanceof Host)
1324
            container = container.getParent();
1325
                host = container;
1325
        }
1326
            if (container instanceof Engine)
1326
        if (engine != null) {
1327
                engine = container;
1327
            result.append(engine.getName()).append('/');
1328
            container = container.getParent();
1328
        }
1329
        }
1329
        if (host != null) {
1330
        if (engine != null) {
1330
            result.append(host.getName()).append('/');
1331
            result.append(engine.getName()).append('/');
1331
        }
1332
        }
1332
        result.append(resourceName);
1333
        if (host != null) {
1333
        return result.toString();
1334
            result.append(host.getName()).append('/');
1334
    }
1335
        }
1335
1336
        result.append(resourceName);
1336
1337
        return result.toString();
1337
    protected class ContextErrorHandler
1338
    }
1338
        implements ErrorHandler {
1339
1339
1340
1340
        public void error(SAXParseException exception) {
1341
    protected class ContextErrorHandler
1341
            parseException = exception;
1342
        implements ErrorHandler {
1342
        }
1343
1343
1344
        public void error(SAXParseException exception) {
1344
        public void fatalError(SAXParseException exception) {
1345
            parseException = exception;
1345
            parseException = exception;
1346
        }
1346
        }
1347
1347
1348
        public void fatalError(SAXParseException exception) {
1348
        public void warning(SAXParseException exception) {
1349
            parseException = exception;
1349
            parseException = exception;
1350
        }
1350
        }
1351
1351
1352
        public void warning(SAXParseException exception) {
1352
    }
1353
            parseException = exception;
1353
1354
        }
1354
1355
1355
}
1356
    }
1357
1358
1359
}

Return to bug 12428