KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > WebApplicationContext


1 // ========================================================================
2
// $Id: WebApplicationContext.java,v 1.136 2005/10/26 08:11:04 gregwilkins Exp $
3
// Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.io.Externalizable JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.security.PermissionCollection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.EventListener JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.ServletContextEvent JavaDoc;
29 import javax.servlet.ServletContextListener JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.mortbay.log.LogFactory;
33 import org.mortbay.http.HttpException;
34 import org.mortbay.http.HttpHandler;
35 import org.mortbay.http.HttpRequest;
36 import org.mortbay.http.HttpResponse;
37 import org.mortbay.http.UserRealm;
38 import org.mortbay.jetty.Server;
39 import org.mortbay.util.JarResource;
40 import org.mortbay.util.LazyList;
41 import org.mortbay.util.Loader;
42 import org.mortbay.util.MultiException;
43 import org.mortbay.util.Resource;
44
45 /* ------------------------------------------------------------ */
46 /** Standard web.xml configured HttpContext.
47  *
48  * This specialization of HttpContext uses the standardized web.xml
49  * to describe a web application and configure the handlers for the
50  * HttpContext.
51  *
52  * If a file named web-jetty.xml or jetty-web.xml is found in the
53  * WEB-INF directory it is applied to the context using the
54  * XmlConfiguration format.
55  *
56  * A single WebApplicationHandler instance is used to provide
57  * security, filter, sevlet and resource handling.
58  *
59  * @see org.mortbay.jetty.servlet.WebApplicationHandler
60  * @version $Id: WebApplicationContext.java,v 1.136 2005/10/26 08:11:04 gregwilkins Exp $
61  * @author Greg Wilkins (gregw)
62  */

63 public class WebApplicationContext extends ServletHttpContext implements Externalizable JavaDoc
64 {
65     private static Log log= LogFactory.getLog(WebApplicationContext.class);
66
67     /* ------------------------------------------------------------ */
68     private String JavaDoc _defaultsDescriptor= "org/mortbay/jetty/servlet/webdefault.xml";
69     private String JavaDoc _war;
70     private boolean _extract;
71     private boolean _ignorewebjetty;
72     private boolean _distributable;
73     private Configuration[] _configurations;
74     private String JavaDoc[] _configurationClassNames;
75
76     private transient Map JavaDoc _resourceAliases;
77     private transient Resource _webApp;
78     private transient Resource _webInf;
79     private transient WebApplicationHandler _webAppHandler;
80     private transient Object JavaDoc _contextListeners;
81     private transient Map JavaDoc _errorPages;
82
83     /* ------------------------------------------------------------ */
84     /** Constructor.
85      */

86     public WebApplicationContext()
87     {
88     }
89
90     /* ------------------------------------------------------------ */
91     /** Constructor.
92      * @param webApp The Web application directory or WAR file.
93      */

94     public WebApplicationContext(String JavaDoc webApp)
95     {
96         _war= webApp;
97     }
98
99     /* ------------------------------------------------------------ */
100     public void writeExternal(java.io.ObjectOutput JavaDoc out) throws java.io.IOException JavaDoc
101     {
102         out.writeObject(getContextPath());
103         out.writeObject(getVirtualHosts());
104         HttpHandler[] handlers= getHandlers();
105         for (int i= 0; i < handlers.length; i++)
106         {
107             if (handlers[i] instanceof WebApplicationHandler)
108                 break;
109             out.writeObject(handlers[i]);
110         }
111         out.writeObject(getAttributes());
112         out.writeBoolean(isRedirectNullPath());
113         out.writeInt(getMaxCachedFileSize());
114         out.writeInt(getMaxCacheSize());
115         out.writeBoolean(getStatsOn());
116         out.writeObject(getPermissions());
117         out.writeBoolean(isClassLoaderJava2Compliant());
118
119         out.writeObject(_defaultsDescriptor);
120         out.writeObject(_war);
121         out.writeBoolean(_extract);
122         out.writeBoolean(_ignorewebjetty);
123         out.writeBoolean(_distributable);
124         
125         out.writeObject(_configurationClassNames);
126     }
127
128     /* ------------------------------------------------------------ */
129     public void readExternal(java.io.ObjectInput JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
130     {
131         setContextPath((String JavaDoc)in.readObject());
132         setVirtualHosts((String JavaDoc[])in.readObject());
133         Object JavaDoc o= in.readObject();
134
135         while (o instanceof HttpHandler)
136         {
137             addHandler((HttpHandler)o);
138             o= in.readObject();
139         }
140         setAttributes((Map JavaDoc)o);
141         setRedirectNullPath(in.readBoolean());
142         setMaxCachedFileSize(in.readInt());
143         setMaxCacheSize(in.readInt());
144         setStatsOn(in.readBoolean());
145         setPermissions((PermissionCollection JavaDoc)in.readObject());
146         setClassLoaderJava2Compliant(in.readBoolean());
147
148         _defaultsDescriptor= (String JavaDoc)in.readObject();
149         _war= (String JavaDoc)in.readObject();
150         _extract= in.readBoolean();
151         _ignorewebjetty= in.readBoolean();
152         _distributable= in.readBoolean();
153         _configurationClassNames=(String JavaDoc[])in.readObject();
154     }
155
156     
157
158     /* ------------------------------------------------------------ */
159     public void setConfigurationClassNames (String JavaDoc[] configurationClassNames)
160     {
161         if (null != configurationClassNames)
162         {
163             _configurationClassNames = new String JavaDoc[configurationClassNames.length];
164             System.arraycopy (configurationClassNames, 0, _configurationClassNames, 0, configurationClassNames.length);
165         }
166     }
167
168     /* ------------------------------------------------------------ */
169     public String JavaDoc[] getConfigurationClassNames ()
170     {
171         return _configurationClassNames;
172     }
173     
174     /* ------------------------------------------------------------ */
175     /**
176      * @param war Filename or URL of the web application directory or WAR file.
177      */

178     public void setWAR(String JavaDoc war)
179     {
180         _war= war;
181     }
182
183     /* ------------------------------------------------------------ */
184     public String JavaDoc getWAR()
185     {
186         return _war;
187     }
188
189     /* ------------------------------------------------------------ */
190     public WebApplicationHandler getWebApplicationHandler()
191     {
192         if (_webAppHandler == null)
193             getServletHandler();
194         return _webAppHandler;
195     }
196
197     /* ------------------------------------------------------------ */
198     private void resolveWebApp() throws IOException JavaDoc
199     {
200         if (_webApp == null && _war != null && _war.length() > 0)
201         {
202             // Set dir or WAR
203
_webApp= Resource.newResource(_war);
204
205             // Accept aliases for WAR files
206
if (_webApp.getAlias() != null)
207             {
208                 log.info(_webApp + " anti-aliased to " + _webApp.getAlias());
209                 _webApp= Resource.newResource(_webApp.getAlias());
210             }
211
212             if (log.isDebugEnabled())
213                 log.debug(
214                     "Try webapp=" + _webApp + ", exists=" + _webApp.exists() + ", directory=" + _webApp.isDirectory());
215
216             // Is the WAR usable directly?
217
if (_webApp.exists() && !_webApp.isDirectory() && !_webApp.toString().startsWith("jar:"))
218             {
219                 // No - then lets see if it can be turned into a jar URL.
220
Resource jarWebApp= Resource.newResource("jar:" + _webApp + "!/");
221                 if (jarWebApp.exists() && jarWebApp.isDirectory())
222                 {
223                     _webApp= jarWebApp;
224                     _war= _webApp.toString();
225                     if (log.isDebugEnabled())
226                         log.debug(
227                             "Try webapp="
228                                 + _webApp
229                                 + ", exists="
230                                 + _webApp.exists()
231                                 + ", directory="
232                                 + _webApp.isDirectory());
233                 }
234             }
235
236             // If we should extract or the URL is still not usable
237
if (_webApp.exists()
238                 && (!_webApp.isDirectory()
239                     || (_extract && _webApp.getFile() == null)
240                     || (_extract && _webApp.getFile() != null && !_webApp.getFile().isDirectory())))
241             {
242                 // Then extract it.
243
File JavaDoc tempDir= new File JavaDoc(getTempDirectory(), "webapp");
244                 if (tempDir.exists())
245                     tempDir.delete();
246                 tempDir.mkdir();
247                 tempDir.deleteOnExit();
248                 log.info("Extract " + _war + " to " + tempDir);
249                 JarResource.extract(_webApp, tempDir, true);
250                 _webApp= Resource.newResource(tempDir.getCanonicalPath());
251
252                 if (log.isDebugEnabled())
253                     log.debug(
254                         "Try webapp="
255                             + _webApp
256                             + ", exists="
257                             + _webApp.exists()
258                             + ", directory="
259                             + _webApp.isDirectory());
260             }
261
262             // Now do we have something usable?
263
if (!_webApp.exists() || !_webApp.isDirectory())
264             {
265                 log.warn("Web application not found " + _war);
266                 throw new java.io.FileNotFoundException JavaDoc(_war);
267             }
268
269             if (log.isDebugEnabled())
270                 log.debug("webapp=" + _webApp);
271
272             // Iw there a WEB-INF directory?
273
_webInf= _webApp.addPath("WEB-INF/");
274             if (!_webInf.exists() || !_webInf.isDirectory())
275                 _webInf= null;
276             else
277             {
278                 // Is there a WEB-INF work directory
279
Resource work= _webInf.addPath("work");
280                 if (work.exists()
281                     && work.isDirectory()
282                     && work.getFile() != null
283                     && work.getFile().canWrite()
284                     && getAttribute("javax.servlet.context.tempdir") == null)
285                     setAttribute("javax.servlet.context.tempdir", work.getFile());
286             }
287
288             // ResourcePath
289
super.setBaseResource(_webApp);
290         }
291     }
292
293
294     /* ------------------------------------------------------------ */
295     public Resource getWebInf() throws IOException JavaDoc
296     {
297         if (_webInf==null)
298             resolveWebApp();
299         return _webInf;
300     }
301     
302     /* ------------------------------------------------------------ */
303     /** Get the context ServletHandler.
304      * Conveniance method. If no ServletHandler exists, a new one is added to
305      * the context. This derivation of the method creates a
306      * WebApplicationHandler extension of ServletHandler.
307      * @return WebApplicationHandler
308      */

309     public synchronized ServletHandler getServletHandler()
310     {
311         if (_webAppHandler == null)
312         {
313             _webAppHandler= (WebApplicationHandler)getHandler(WebApplicationHandler.class);
314             if (_webAppHandler == null)
315             {
316                 if (getHandler(ServletHandler.class) != null)
317                     throw new IllegalStateException JavaDoc("Cannot have ServletHandler in WebApplicationContext");
318                 _webAppHandler= new WebApplicationHandler();
319                 addHandler(_webAppHandler);
320             }
321         }
322         return _webAppHandler;
323     }
324
325     /* ------------------------------------------------------------ */
326     public void setPermissions(PermissionCollection JavaDoc permissions)
327     {
328         if (!_ignorewebjetty)
329             log.warn("Permissions set with web-jetty.xml enabled");
330         super.setPermissions(permissions);
331     }
332
333     /* ------------------------------------------------------------ */
334     public boolean isIgnoreWebJetty()
335     {
336         return _ignorewebjetty;
337     }
338
339     /* ------------------------------------------------------------ */
340     /**
341      * @param b If TRUE, web-jetty.xml and jetty-web.xml configuration
342      * files are ignored.
343      */

344     public void setIgnoreWebJetty(boolean b)
345     {
346         _ignorewebjetty= b;
347         if (b && getPermissions() != null)
348             log.warn("Permissions set with web-jetty.xml enabled");
349     }
350
351     /* ------------------------------------------------------------ */
352     public boolean isDistributable()
353     {
354         return _distributable;
355     }
356
357     /* ------------------------------------------------------------ */
358     public void setDistributable(boolean distributable)
359     {
360         _distributable=distributable;
361     }
362
363     /* ------------------------------------------------------------ */
364     public Configuration[] getConfigurations ()
365     {
366         return _configurations;
367     }
368
369     /* ------------------------------------------------------------ */
370     protected Configuration[] loadConfigurations() throws Exception JavaDoc
371     {
372         String JavaDoc[] names = _configurationClassNames;
373         
374         //if this webapp does not have its own set of configurators, use the defaults
375
if (null==names)
376             names = ((Server)getHttpServer()).getWebApplicationConfigurationClassNames();
377         
378         if (null!=names)
379         {
380             //instantiate instances for each
381
Object JavaDoc[] nullArgs = new Object JavaDoc[0];
382             Configuration[] configurations = new Configuration[names.length];
383             for (int i=0; i< names.length; i++)
384             {
385                 configurations[i] =
386                     (Configuration)Loader.loadClass(WebApplicationContext.class, names[i]).getConstructors()[0].newInstance(nullArgs);
387                 if (log.isDebugEnabled()){log.debug("Loaded instance of "+names[i]);};
388             }
389             return configurations;
390         }
391         else
392             return new Configuration[0];
393     }
394
395     /* ------------------------------------------------------------ */
396     protected void configureClassPath() throws Exception JavaDoc
397     {
398         //call each of the instances
399
// first, configure the classpaths
400
for (int i=0; i<_configurations.length;i++)
401         {
402             _configurations[i].setWebApplicationContext(this);
403             _configurations[i].configureClassPath();
404         }
405     }
406
407     /* ------------------------------------------------------------ */
408     protected void configureDefaults() throws Exception JavaDoc
409     {
410         //next, configure default settings
411
for (int i=0;i<_configurations.length;i++)
412         {
413             _configurations[i].setWebApplicationContext(this);
414             _configurations[i].configureDefaults();
415         }
416     }
417
418     /* ------------------------------------------------------------ */
419     protected void configureWebApp () throws Exception JavaDoc
420     {
421         //finally, finish configuring the webapp
422
for (int i=0;i<_configurations.length;i++)
423         {
424             _configurations[i].setWebApplicationContext(this);
425             _configurations[i].configureWebApp();
426         }
427         
428     }
429  
430     /* ------------------------------------------------------------ */
431     /** Start the Web Application.
432      * @exception IOException
433      */

434     protected void doStart() throws Exception JavaDoc
435     {
436         if (isStarted())
437             return;
438
439         // save context classloader
440
Thread JavaDoc thread= Thread.currentThread();
441         ClassLoader JavaDoc lastContextLoader= thread.getContextClassLoader();
442
443         MultiException mex= null;
444         try
445         {
446             // Find the webapp
447
resolveWebApp();
448
449             // Get the handler
450
getServletHandler();
451           
452             _configurations=loadConfigurations();
453             
454             // initialize the classloader
455
configureClassPath();
456             initClassLoader(true);
457             thread.setContextClassLoader(getClassLoader());
458             initialize();
459             
460             // Do the default configuration
461
configureDefaults();
462
463             // Set classpath for Jasper.
464
Map.Entry JavaDoc entry= _webAppHandler.getHolderEntry("test.jsp");
465             if (entry != null)
466             {
467                 ServletHolder jspHolder= (ServletHolder)entry.getValue();
468                 if (jspHolder != null && jspHolder.getInitParameter("classpath") == null)
469                 {
470                     String JavaDoc fileClassPath= getFileClassPath();
471                     jspHolder.setInitParameter("classpath", fileClassPath);
472                     if (log.isDebugEnabled())
473                         log.debug("Set classpath=" + fileClassPath + " for " + jspHolder);
474                 }
475             }
476             
477             // configure webapp
478
configureWebApp();
479
480             // If we have servlets, don't init them yet
481
_webAppHandler.setAutoInitializeServlets(false);
482
483             // Start handlers
484
super.doStart();
485
486             mex= new MultiException();
487             // Context listeners
488
if (_contextListeners != null && _webAppHandler != null)
489             {
490                 ServletContextEvent JavaDoc event= new ServletContextEvent JavaDoc(getServletContext());
491                 for (int i= 0; i < LazyList.size(_contextListeners); i++)
492                 {
493                     try
494                     {
495                         ((ServletContextListener JavaDoc)LazyList.get(_contextListeners, i)).contextInitialized(event);
496                     }
497                     catch (Exception JavaDoc ex)
498                     {
499                         mex.add(ex);
500                     }
501                 }
502             }
503
504             // OK to Initialize servlets now
505
if (_webAppHandler != null && _webAppHandler.isStarted())
506             {
507                 try
508                 {
509                     _webAppHandler.initializeServlets();
510                 }
511                 catch (Exception JavaDoc ex)
512                 {
513                     mex.add(ex);
514                 }
515             }
516         }
517         catch (Exception JavaDoc e)
518         {
519             log.warn("Configuration error on " + _war, e);
520             throw e;
521         }
522         finally
523         {
524             thread.setContextClassLoader(lastContextLoader);
525         }
526
527         if (mex != null)
528             mex.ifExceptionThrow();
529     }
530
531     /* ------------------------------------------------------------ */
532     /** Stop the web application.
533      * Handlers for resource, servlet, filter and security are removed
534      * as they are recreated and configured by any subsequent call to start().
535      * @exception InterruptedException
536      */

537     protected void doStop() throws Exception JavaDoc
538     {
539         MultiException mex=new MultiException();
540         
541         
542         Thread JavaDoc thread= Thread.currentThread();
543         ClassLoader JavaDoc lastContextLoader= thread.getContextClassLoader();
544         
545         try
546         {
547             // Context listeners
548
if (_contextListeners != null)
549             {
550                 if (_webAppHandler != null)
551                 {
552                     ServletContextEvent JavaDoc event= new ServletContextEvent JavaDoc(getServletContext());
553                     
554                     for (int i= LazyList.size(_contextListeners); i-- > 0;)
555                     {
556                         try
557                         {
558                             ((ServletContextListener JavaDoc)LazyList.get(_contextListeners, i)).contextDestroyed(event);
559                         }
560                         catch (Exception JavaDoc e)
561                         {
562                             mex.add(e);
563                         }
564                     }
565                 }
566             }
567             _contextListeners= null;
568             
569             // Stop the context
570
try
571             {
572                 super.doStop();
573             }
574             catch (Exception JavaDoc e)
575             {
576                 mex.add(e);
577             }
578             
579             // clean up
580
clearSecurityConstraints();
581             
582             if (_webAppHandler != null)
583                 removeHandler(_webAppHandler);
584             _webAppHandler= null;
585             
586             if (_errorPages != null)
587                 _errorPages.clear();
588             _errorPages= null;
589             
590             _webApp=null;
591             _webInf=null;
592             
593             _configurations=null;
594             
595         }
596         finally
597         {
598             thread.setContextClassLoader(lastContextLoader);
599         }
600         
601         if (mex!=null)
602             mex.ifExceptionThrow();
603     }
604     
605
606     /* ------------------------------------------------------------ */
607     public void destroy()
608     {
609         super.destroy();
610         if (isStarted())
611             throw new IllegalStateException JavaDoc();
612
613         _defaultsDescriptor=null;
614         _war=null;
615         _configurationClassNames=null;
616         if (_resourceAliases!=null)
617             _resourceAliases.clear();
618         _resourceAliases=null;
619         _contextListeners=null;
620         if (_errorPages!=null)
621             _errorPages.clear();
622         _errorPages=null;
623     }
624
625     /* ------------------------------------------------------------ */
626     public void handle(String JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest httpRequest, HttpResponse httpResponse)
627         throws HttpException, IOException JavaDoc
628     {
629         if (!isStarted())
630             return;
631         try
632         {
633             super.handle(pathInContext, pathParams, httpRequest, httpResponse);
634         }
635         finally
636         {
637             if (!httpRequest.isHandled())
638                 httpResponse.sendError(HttpResponse.__404_Not_Found);
639             httpRequest.setHandled(true);
640             if (!httpResponse.isCommitted())
641             {
642                 httpResponse.completing();
643                 httpResponse.commit();
644             }
645         }
646     }
647
648     /* ------------------------------------------------------------ */
649     public synchronized void addEventListener(EventListener JavaDoc listener) throws IllegalArgumentException JavaDoc
650     {
651         if (listener instanceof ServletContextListener JavaDoc)
652         {
653             _contextListeners= LazyList.add(_contextListeners, listener);
654         }
655  
656         super.addEventListener(listener);
657     }
658
659     /* ------------------------------------------------------------ */
660     public synchronized void removeEventListener(EventListener JavaDoc listener)
661     {
662         _contextListeners= LazyList.remove(_contextListeners, listener);
663         super.removeEventListener(listener);
664     }
665
666     /* ------------------------------------------------------------ */
667     public String JavaDoc getDisplayName()
668     {
669         return getHttpContextName();
670     }
671     
672     /* ------------------------------------------------------------ */
673     public void setDisplayName(String JavaDoc name)
674     {
675         setHttpContextName(name);
676     }
677
678     /* ------------------------------------------------------------ */
679     /** Set the defaults web.xml file.
680      * The default web.xml is used to configure all webapplications
681      * before the WEB-INF/web.xml file is applied. By default the
682      * org/mortbay/jetty/servlet/webdefault.xml resource from the
683      * org.mortbay.jetty.jar is used.
684      * @param defaults File, Resource, URL or null.
685      */

686     public void setDefaultsDescriptor(String JavaDoc defaults)
687     {
688         _defaultsDescriptor= defaults;
689     }
690
691     /* ------------------------------------------------------------ */
692     public String JavaDoc getDefaultsDescriptor()
693     {
694         return _defaultsDescriptor;
695     }
696
697     /* ------------------------------------------------------------ */
698     /**
699      * @param extract If true, a WAR is extracted to a temporary
700      * directory before being deployed.
701      */

702     public void setExtractWAR(boolean extract)
703     {
704         _extract= extract;
705     }
706
707     /* ------------------------------------------------------------ */
708     public boolean getExtractWAR()
709     {
710         return _extract;
711     }
712
713     /* ------------------------------------------------------------ */
714     /**
715      * Initialize is called by the start method after the contexts classloader
716      * has been initialied, but before the defaults descriptor has been applied.
717      * The default implementation does nothing.
718      *
719      * @exception Exception if an error occurs
720      */

721     protected void initialize() throws Exception JavaDoc
722     {
723     }
724
725
726     /* ------------------------------------------------------------ */
727     protected UserRealm getUserRealm(String JavaDoc name)
728     {
729         return getHttpServer().getRealm(name);
730     }
731
732     /* ------------------------------------------------------------ */
733     public String JavaDoc toString()
734     {
735         String JavaDoc name = getDisplayName();
736         return "WebApplicationContext[" + getContextPath() + "," + (name == null ? _war : name) + "]";
737     }
738
739     /* ------------------------------------------------------------ */
740     /** Set Resource Alias.
741      * Resource aliases map resource uri's within a context.
742      * They may optionally be used by a handler when looking for
743      * a resource.
744      * @param alias
745      * @param uri
746      */

747     public void setResourceAlias(String JavaDoc alias, String JavaDoc uri)
748     {
749         if (_resourceAliases == null)
750             _resourceAliases= new HashMap JavaDoc(5);
751         _resourceAliases.put(alias, uri);
752     }
753
754     /* ------------------------------------------------------------ */
755     public Map JavaDoc getResourceAliases()
756     {
757         if (_resourceAliases == null)
758             return null;
759         return Collections.unmodifiableMap(_resourceAliases);
760     }
761     
762     /* ------------------------------------------------------------ */
763     public String JavaDoc getResourceAlias(String JavaDoc alias)
764     {
765         if (_resourceAliases == null)
766             return null;
767         return (String JavaDoc)_resourceAliases.get(alias);
768     }
769
770     /* ------------------------------------------------------------ */
771     public String JavaDoc removeResourceAlias(String JavaDoc alias)
772     {
773         if (_resourceAliases == null)
774             return null;
775         return (String JavaDoc)_resourceAliases.remove(alias);
776     }
777
778     /* ------------------------------------------------------------ */
779     public Resource getResource(String JavaDoc uriInContext) throws IOException JavaDoc
780     {
781         IOException JavaDoc ioe= null;
782         Resource resource= null;
783         try
784         {
785             resource= super.getResource(uriInContext);
786             if (resource != null && resource.exists())
787                 return resource;
788         }
789         catch (IOException JavaDoc e)
790         {
791             ioe= e;
792         }
793
794         String JavaDoc aliasedUri= getResourceAlias(uriInContext);
795         if (aliasedUri != null)
796             return super.getResource(aliasedUri);
797
798         if (ioe != null)
799             throw ioe;
800
801         return resource;
802     }
803
804     /* ------------------------------------------------------------ */
805     /** set error page URI.
806      * @param error A string representing an error code or a
807      * exception classname
808      * @param uriInContext
809      */

810     public void setErrorPage(String JavaDoc error, String JavaDoc uriInContext)
811     {
812         if (_errorPages == null)
813             _errorPages= new HashMap JavaDoc();
814         _errorPages.put(error, uriInContext);
815     }
816
817     /* ------------------------------------------------------------ */
818     /** get error page URI.
819      * @param error A string representing an error code or a
820      * exception classname
821      * @return URI within context
822      */

823     public String JavaDoc getErrorPage(String JavaDoc error)
824     {
825         if (_errorPages == null)
826             return null;
827         return (String JavaDoc)_errorPages.get(error);
828     }
829
830     /* ------------------------------------------------------------ */
831     public String JavaDoc removeErrorPage(String JavaDoc error)
832     {
833         if (_errorPages == null)
834             return null;
835         return (String JavaDoc)_errorPages.remove(error);
836     }
837     
838  
839     
840     /* ------------------------------------------------------------------------------- */
841     /** Base Class for WebApplicationContext Configuration.
842      * This class can be extended to customize or extend the configuration
843      * of the WebApplicationContext. If WebApplicationContext.setConfiguration is not
844      * called, then an XMLConfiguration instance is created.
845      *
846      * @version $Revision: 1.136 $
847      * @author gregw
848      */

849     public static interface Configuration extends Serializable JavaDoc
850     {
851         /* ------------------------------------------------------------------------------- */
852         /** Set up a context on which to perform the configuration.
853          * @param context
854          */

855         public void setWebApplicationContext (WebApplicationContext context);
856
857         /* ------------------------------------------------------------------------------- */
858         /** Get the context on which the configuration is performed.
859          * @return
860          */

861         public WebApplicationContext getWebApplicationContext ();
862         
863         /* ------------------------------------------------------------------------------- */
864         /** Configure ClassPath.
865          * This method is called before the context ClassLoader is created.
866          * Paths and libraries should be added to the context using the setClassPath,
867          * addClassPath and addClassPaths methods. The default implementation looks
868          * for WEB-INF/classes, WEB-INF/lib/*.zip and WEB-INF/lib/*.jar
869          * @throws Exception
870          */

871         public void configureClassPath()
872         throws Exception JavaDoc;
873
874         /* ------------------------------------------------------------------------------- */
875         /** Configure Defaults.
876          * This method is called to intialize the context to the containers default configuration.
877          * Typically this would mean application of the webdefault.xml file. The default
878          * implementation does nothing.
879          * @throws Exception
880          */

881         public void configureDefaults()
882         throws Exception JavaDoc;
883         
884
885         /* ------------------------------------------------------------------------------- */
886         /** Configure WebApp.
887          * This method is called to apply the standard and vendor deployment descriptors.
888          * Typically this is web.xml and jetty-web.xml. The default implementation does nothing.
889          * @throws Exception
890          */

891         public void configureWebApp()
892         throws Exception JavaDoc;
893         
894     }
895
896 }
897
Popular Tags