View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.servlet;
15  
16  import java.util.ArrayList;
17  import java.util.Arrays;
18  import java.util.Collections;
19  import java.util.EnumSet;
20  import java.util.EventListener;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.servlet.DispatcherType;
28  import javax.servlet.Filter;
29  import javax.servlet.FilterRegistration;
30  import javax.servlet.RequestDispatcher;
31  import javax.servlet.Servlet;
32  import javax.servlet.ServletContext;
33  import javax.servlet.ServletContextEvent;
34  import javax.servlet.ServletContextListener;
35  import javax.servlet.ServletException;
36  import javax.servlet.ServletRegistration;
37  import javax.servlet.ServletSecurityElement;
38  import javax.servlet.SessionCookieConfig;
39  import javax.servlet.SessionTrackingMode;
40  import javax.servlet.descriptor.JspConfigDescriptor;
41  
42  import org.eclipse.jetty.security.ConstraintAware;
43  import org.eclipse.jetty.security.ConstraintSecurityHandler;
44  import org.eclipse.jetty.security.SecurityHandler;
45  import org.eclipse.jetty.server.Dispatcher;
46  import org.eclipse.jetty.server.Handler;
47  import org.eclipse.jetty.server.HandlerContainer;
48  import org.eclipse.jetty.server.handler.ContextHandler;
49  import org.eclipse.jetty.server.handler.ErrorHandler;
50  import org.eclipse.jetty.server.handler.HandlerCollection;
51  import org.eclipse.jetty.server.handler.HandlerWrapper;
52  import org.eclipse.jetty.server.session.SessionHandler;
53  import org.eclipse.jetty.util.LazyList;
54  
55  
56  /* ------------------------------------------------------------ */
57  /** Servlet Context.
58   * This extension to the ContextHandler allows for
59   * simple construction of a context with ServletHandler and optionally
60   * session and security handlers, et.<pre>
61   *   new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);
62   * </pre>
63   * <p/>
64   * This class should have been called ServletContext, but this would have
65   * cause confusion with {@link ServletContext}.
66   */
67  public class ServletContextHandler extends ContextHandler
68  {   
69      public final static int SESSIONS=1;
70      public final static int SECURITY=2;
71      public final static int NO_SESSIONS=0;
72      public final static int NO_SECURITY=0;
73  
74      protected final List<Decorator> _decorators= new ArrayList<Decorator>();
75      protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
76      protected SessionHandler _sessionHandler;
77      protected SecurityHandler _securityHandler;
78      protected ServletHandler _servletHandler;
79      protected HandlerWrapper _wrapper;
80      protected int _options;
81      protected JspConfigDescriptor _jspConfig;
82      protected Object _restrictedContextListeners;
83      private boolean _restrictListeners = true;
84  
85      /* ------------------------------------------------------------ */
86      public ServletContextHandler()
87      {
88          this(null,null,null,null,null);
89      }
90      
91      /* ------------------------------------------------------------ */
92      public ServletContextHandler(int options)
93      {
94          this(null,null,options);
95      }
96      
97      /* ------------------------------------------------------------ */
98      public ServletContextHandler(HandlerContainer parent, String contextPath)
99      {
100         this(parent,contextPath,null,null,null,null);
101     }
102     
103     /* ------------------------------------------------------------ */
104     public ServletContextHandler(HandlerContainer parent, String contextPath, int options)
105     {
106         this(parent,contextPath,null,null,null,null);
107         _options=options;
108     }
109     
110     /* ------------------------------------------------------------ */
111     public ServletContextHandler(HandlerContainer parent, String contextPath, boolean sessions, boolean security)
112     {
113         this(parent,contextPath,(sessions?SESSIONS:0)|(security?SECURITY:0));
114     }
115 
116     /* ------------------------------------------------------------ */
117     public ServletContextHandler(HandlerContainer parent, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
118     {   
119         this(parent,null,sessionHandler,securityHandler,servletHandler,errorHandler);
120     }
121 
122     /* ------------------------------------------------------------ */
123     public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
124     {   
125         super((ContextHandler.Context)null);
126         _scontext = new Context();
127         _sessionHandler = sessionHandler;
128         _securityHandler = securityHandler;
129         _servletHandler = servletHandler;
130             
131         if (errorHandler!=null)
132             setErrorHandler(errorHandler);
133 
134         if (contextPath!=null)
135             setContextPath(contextPath);
136 
137         if (parent instanceof HandlerWrapper)
138             ((HandlerWrapper)parent).setHandler(this);
139         else if (parent instanceof HandlerCollection)
140             ((HandlerCollection)parent).addHandler(this);
141     }    
142 
143     /* ------------------------------------------------------------ */
144     /**
145      * @see org.eclipse.jetty.server.handler.ContextHandler#doStop()
146      */
147     @Override
148     protected void doStop() throws Exception
149     {
150         super.doStop();
151         if (_decorators != null)
152             _decorators.clear();
153         if (_wrapper != null)
154             _wrapper.setHandler(null);
155     }
156 
157     /* ------------------------------------------------------------ */
158     /** Get the defaultSecurityHandlerClass.
159      * @return the defaultSecurityHandlerClass
160      */
161     public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
162     {
163         return _defaultSecurityHandlerClass;
164     }
165 
166     /* ------------------------------------------------------------ */
167     /** Set the defaultSecurityHandlerClass.
168      * @param defaultSecurityHandlerClass the defaultSecurityHandlerClass to set
169      */
170     public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
171     {
172         _defaultSecurityHandlerClass = defaultSecurityHandlerClass;
173     }
174 
175     /* ------------------------------------------------------------ */
176     protected SessionHandler newSessionHandler()
177     {
178         return new SessionHandler();
179     }
180     
181     /* ------------------------------------------------------------ */
182     protected SecurityHandler newSecurityHandler()
183     {
184         try
185         {
186             return (SecurityHandler)_defaultSecurityHandlerClass.newInstance();
187         }
188         catch(Exception e)
189         {
190             throw new IllegalStateException(e);
191         }
192     }
193 
194     /* ------------------------------------------------------------ */
195     protected ServletHandler newServletHandler()
196     {
197         return new ServletHandler();
198     }
199 
200     /* ------------------------------------------------------------ */
201     /**
202      * Finish constructing handlers and link them together.
203      * 
204      * @see org.eclipse.jetty.server.handler.ContextHandler#startContext()
205      */
206     protected void startContext() throws Exception
207     {
208         // force creation of missing handlers.
209         getSessionHandler();
210         getSecurityHandler();
211         getServletHandler();
212         
213         Handler handler = _servletHandler;
214         if (_securityHandler!=null)
215         {
216             _securityHandler.setHandler(handler);
217             handler=_securityHandler;
218         }
219         
220         if (_sessionHandler!=null)
221         {
222             _sessionHandler.setHandler(handler);
223             handler=_sessionHandler;
224         }
225         
226         // skip any wrapped handlers 
227         _wrapper=this;
228         while (_wrapper!=handler && _wrapper.getHandler() instanceof HandlerWrapper)
229             _wrapper=(HandlerWrapper)_wrapper.getHandler();
230         
231         // if we are not already linked
232         if (_wrapper!=handler)
233         {
234             if (_wrapper.getHandler()!=null )
235                 throw new IllegalStateException("!ScopedHandler");
236             _wrapper.setHandler(handler);
237         }
238         
239     	super.startContext();
240 
241     	// OK to Initialize servlet handler now
242     	if (_servletHandler != null && _servletHandler.isStarted())
243     	{
244     	    for (int i=_decorators.size()-1;i>=0; i--)
245     	    {
246     	        Decorator decorator = _decorators.get(i);
247                 if (_servletHandler.getFilters()!=null)
248                     for (FilterHolder holder:_servletHandler.getFilters())
249                         decorator.decorateFilterHolder(holder);
250     	        if(_servletHandler.getServlets()!=null)
251     	            for (ServletHolder holder:_servletHandler.getServlets())
252     	                decorator.decorateServletHolder(holder);
253     	    }   
254     	        
255     	    _servletHandler.initialize();
256     	}
257     }
258 
259     /* ------------------------------------------------------------ */
260     /**
261      * @return Returns the securityHandler.
262      */
263     public SecurityHandler getSecurityHandler()
264     {
265         if (_securityHandler==null && (_options&SECURITY)!=0 && !isStarted()) 
266             _securityHandler=newSecurityHandler();
267         
268         return _securityHandler;
269     }
270 
271     /* ------------------------------------------------------------ */
272     /**
273      * @return Returns the servletHandler.
274      */
275     public ServletHandler getServletHandler()
276     {
277         if (_servletHandler==null && !isStarted()) 
278             _servletHandler=newServletHandler();
279         return _servletHandler;
280     }
281 
282     /* ------------------------------------------------------------ */
283     /**
284      * @return Returns the sessionHandler.
285      */
286     public SessionHandler getSessionHandler()
287     {
288         if (_sessionHandler==null && (_options&SESSIONS)!=0 && !isStarted()) 
289             _sessionHandler=newSessionHandler();
290         return _sessionHandler;
291     }
292 
293     /* ------------------------------------------------------------ */
294     /** conveniance method to add a servlet.
295      */
296     public ServletHolder addServlet(String className,String pathSpec)
297     {
298         return getServletHandler().addServletWithMapping(className, pathSpec);
299     }
300 
301     /* ------------------------------------------------------------ */
302     /** conveniance method to add a servlet.
303      */
304     public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
305     {
306         return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
307     }
308     
309     /* ------------------------------------------------------------ */
310     /** conveniance method to add a servlet.
311      */
312     public void addServlet(ServletHolder servlet,String pathSpec)
313     {
314         getServletHandler().addServletWithMapping(servlet, pathSpec);
315     }
316 
317     /* ------------------------------------------------------------ */
318     /** conveniance method to add a filter
319      */
320     public void addFilter(FilterHolder holder,String pathSpec,EnumSet<DispatcherType> dispatches)
321     {
322         getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches);
323     }
324 
325     /* ------------------------------------------------------------ */
326     /** convenience method to add a filter
327      */
328     public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
329     {
330         return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
331     }
332 
333     /* ------------------------------------------------------------ */
334     /** convenience method to add a filter
335      */
336     public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
337     {
338         return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
339     }
340 
341     /**
342      * notification that a ServletRegistration has been created so we can track the annotations
343      * @param holder new holder created through the api.
344      * @return the ServletRegistration.Dynamic
345      */
346     protected ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder) {
347         return holder.getRegistration();
348     }
349 
350     /**
351      * delegate for ServletContext.declareRole method
352      * @param roleNames role names to add
353      */
354     protected void addRoles(String... roleNames) {
355         //Get a reference to the SecurityHandler, which must be ConstraintAware
356         if (_securityHandler != null && _securityHandler instanceof ConstraintAware)
357         {
358             HashSet<String> union = new HashSet<String>();
359             Set<String> existing = ((ConstraintAware)_securityHandler).getRoles();
360             if (existing != null)
361                 union.addAll(existing);
362             union.addAll(Arrays.asList(roleNames));
363             ((ConstraintSecurityHandler)_securityHandler).setRoles(union);
364         }
365     }
366 
367     /**
368      * Delegate for ServletRegistration.Dynamic.setServletSecurity method
369      * @param registration ServletRegistration.Dynamic instance that setServletSecurity was called on
370      * @param servletSecurityElement new security info
371      * @return the set of exact URL mappings currently associated with the registration that are also present in the web.xml
372      * security constratins and thus will be unaffected by this call.
373      */
374     public Set<String> setServletSecurity(ServletRegistration.Dynamic registration, ServletSecurityElement servletSecurityElement)
375     {
376         return Collections.emptySet();
377     }
378 
379 
380     
381     public void restrictEventListener (EventListener e)
382     {
383         if (_restrictListeners && e instanceof ServletContextListener)
384             _restrictedContextListeners = LazyList.add(_restrictedContextListeners, e);
385     }
386 
387     public boolean isRestrictListeners() {
388         return _restrictListeners;
389     }
390 
391     public void setRestrictListeners(boolean restrictListeners) {
392         this._restrictListeners = restrictListeners;
393     }
394 
395     public void callContextInitialized(ServletContextListener l, ServletContextEvent e)
396     {
397         try
398         {
399             //toggle state of the dynamic API so that the listener cannot use it
400             if (LazyList.contains(_restrictedContextListeners, l))
401                 this.getServletContext().setEnabled(false);
402             
403             super.callContextInitialized(l, e);
404         }
405         finally
406         {
407             //untoggle the state of the dynamic API
408             this.getServletContext().setEnabled(true);
409         }
410     }
411 
412     
413     public void callContextDestroyed(ServletContextListener l, ServletContextEvent e)
414     {
415         super.callContextDestroyed(l, e);
416     }
417 
418   
419 
420     /* ------------------------------------------------------------ */
421     /**
422      * @param sessionHandler The sessionHandler to set.
423      */
424     public void setSessionHandler(SessionHandler sessionHandler)
425     {
426         if (isStarted())
427             throw new IllegalStateException("STARTED");
428 
429         _sessionHandler = sessionHandler;
430     }
431 
432     /* ------------------------------------------------------------ */
433     /**
434      * @param securityHandler The {@link SecurityHandler} to set on this context.
435      */
436     public void setSecurityHandler(SecurityHandler securityHandler)
437     {
438         if (isStarted())
439             throw new IllegalStateException("STARTED");
440 
441         _securityHandler = securityHandler;
442     }
443 
444     /* ------------------------------------------------------------ */
445     /**
446      * @param servletHandler The servletHandler to set.
447      */
448     public void setServletHandler(ServletHandler servletHandler)
449     {
450         if (isStarted())
451             throw new IllegalStateException("STARTED");
452 
453         _servletHandler = servletHandler;
454     }
455 
456     /* ------------------------------------------------------------ */
457     /**
458      * @return The decorator list used to resource inject new Filters, Servlets and EventListeners
459      */
460     public List<Decorator> getDecorators()
461     {
462         return Collections.unmodifiableList(_decorators);
463     }
464 
465     /* ------------------------------------------------------------ */
466     /**
467      * @param decorators The lis of {@link Decorator}s
468      */
469     public void setDecorators(List<Decorator> decorators)
470     {
471         _decorators.clear();
472         _decorators.addAll(decorators);
473     }
474     
475     /* ------------------------------------------------------------ */
476     /**
477      * @param decorator The decorator to add
478      */
479     public void addDecorator(Decorator decorator)
480     {
481         _decorators.add(decorator);
482     }
483 
484     /* ------------------------------------------------------------ */
485     void destroyServlet(Servlet servlet)
486     {
487         for (Decorator decorator : _decorators)
488             decorator.destroyServletInstance(servlet);
489     }
490 
491     /* ------------------------------------------------------------ */
492     void destroyFilter(Filter filter)
493     {
494         for (Decorator decorator : _decorators)
495             decorator.destroyFilterInstance(filter);
496     }
497     
498     /* ------------------------------------------------------------ */
499     public class Context extends ContextHandler.Context
500     {
501         /* ------------------------------------------------------------ */
502         /* 
503          * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
504          */
505         @Override
506         public RequestDispatcher getNamedDispatcher(String name)
507         {
508             ContextHandler context=org.eclipse.jetty.servlet.ServletContextHandler.this;
509             if (_servletHandler==null)
510                 return null;
511             ServletHolder holder = _servletHandler.getServlet(name);
512             if (holder==null || !holder.isEnabled())
513                 return null;
514             return new Dispatcher(context, name);
515         }
516         
517         /* ------------------------------------------------------------ */
518         /**
519          * @since servlet-api-3.0
520          */
521         @Override
522         public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
523         {
524             if (isStarted())
525                 throw new IllegalStateException();
526             
527             if (!_enabled)
528                 throw new UnsupportedOperationException();
529 
530             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
531             final FilterHolder holder= handler.newFilterHolder(Holder.Source.JAVAX_API);
532             holder.setName(filterName);
533             holder.setHeldClass(filterClass);
534             handler.addFilter(holder);
535             return holder.getRegistration();
536         }
537 
538         /* ------------------------------------------------------------ */
539         /**
540          * @since servlet-api-3.0
541          */
542         @Override
543         public FilterRegistration.Dynamic addFilter(String filterName, String className)
544         {
545             if (isStarted())
546                 throw new IllegalStateException();
547             
548             if (!_enabled)
549                 throw new UnsupportedOperationException();
550 
551             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
552             final FilterHolder holder= handler.newFilterHolder(Holder.Source.JAVAX_API);
553             holder.setName(filterName);
554             holder.setClassName(className);
555             handler.addFilter(holder);
556             return holder.getRegistration();
557         }
558 
559 
560         /* ------------------------------------------------------------ */
561         /**
562          * @since servlet-api-3.0
563          */
564         @Override
565         public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
566         {
567             if (isStarted())
568                 throw new IllegalStateException();
569 
570             if (!_enabled)
571                 throw new UnsupportedOperationException();
572             
573             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
574             final FilterHolder holder= handler.newFilterHolder(Holder.Source.JAVAX_API);
575             holder.setName(filterName);
576             holder.setFilter(filter);
577             handler.addFilter(holder);
578             return holder.getRegistration();
579         }
580         
581         /* ------------------------------------------------------------ */
582         /**
583          * @since servlet-api-3.0
584          */
585         @Override
586         public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
587         {
588             if (!isStarting())
589                 throw new IllegalStateException();
590             
591             if (!_enabled)
592                 throw new UnsupportedOperationException();
593 
594             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
595             final ServletHolder holder= handler.newServletHolder(Holder.Source.JAVAX_API);
596             holder.setName(servletName);
597             holder.setHeldClass(servletClass);
598             handler.addServlet(holder);
599             return dynamicHolderAdded(holder);
600         }
601 
602         /* ------------------------------------------------------------ */
603         /**
604          * @since servlet-api-3.0
605          */
606         @Override
607         public ServletRegistration.Dynamic addServlet(String servletName, String className)
608         {
609             if (!isStarting())
610                 throw new IllegalStateException();
611             
612             if (!_enabled)
613                 throw new UnsupportedOperationException();
614 
615             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
616             final ServletHolder holder= handler.newServletHolder(Holder.Source.JAVAX_API);
617             holder.setName(servletName);
618             holder.setClassName(className);
619             handler.addServlet(holder);
620             return dynamicHolderAdded(holder);
621         }
622 
623         /* ------------------------------------------------------------ */
624         /**
625          * @since servlet-api-3.0
626          */
627         @Override
628         public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
629         {
630             if (!isStarting())
631                 throw new IllegalStateException();
632 
633             if (!_enabled)
634                 throw new UnsupportedOperationException();
635             
636             final ServletHandler handler = ServletContextHandler.this.getServletHandler();
637             final ServletHolder holder= handler.newServletHolder(Holder.Source.JAVAX_API);
638             holder.setName(servletName);
639             holder.setServlet(servlet);
640             handler.addServlet(holder);
641             return dynamicHolderAdded(holder);
642         }
643 
644         /* ------------------------------------------------------------ */
645         @Override
646         public boolean setInitParameter(String name, String value)
647         {
648             // TODO other started conditions
649             if (!isStarting())
650                 throw new IllegalStateException();
651             
652             if (!_enabled)
653                 throw new UnsupportedOperationException();
654             
655             return super.setInitParameter(name,value);
656         }
657 
658         /* ------------------------------------------------------------ */
659         @Override
660         public <T extends Filter> T createFilter(Class<T> c) throws ServletException
661         {
662             try
663             {
664                 T f = c.newInstance();
665                 for (int i=_decorators.size()-1; i>=0; i--)
666                 {
667                     Decorator decorator = _decorators.get(i);
668                     f=decorator.decorateFilterInstance(f);
669                 }
670                 return f;
671             }
672             catch (InstantiationException e)
673             {
674                 throw new ServletException(e);
675             }
676             catch (IllegalAccessException e)
677             {
678                 throw new ServletException(e);
679             }
680         }
681 
682         /* ------------------------------------------------------------ */
683         @Override
684         public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
685         {
686             try
687             {
688                 T s = c.newInstance();
689                 for (int i=_decorators.size()-1; i>=0; i--)
690                 {
691                     Decorator decorator = _decorators.get(i);
692                     s=decorator.decorateServletInstance(s);
693                 }
694                 return s;
695             }
696             catch (InstantiationException e)
697             {
698                 throw new ServletException(e);
699             }
700             catch (IllegalAccessException e)
701             {
702                 throw new ServletException(e);
703             }
704         }
705 
706         @Override
707         public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
708         {
709             if (_sessionHandler!=null)
710                 return _sessionHandler.getSessionManager().getDefaultSessionTrackingModes();
711             return null;
712         }
713 
714         @Override
715         public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
716         {
717             if (_sessionHandler!=null)
718                 return _sessionHandler.getSessionManager().getEffectiveSessionTrackingModes();
719             return null;
720         }
721 
722         @Override
723         public FilterRegistration getFilterRegistration(String filterName)
724         {
725             if (!_enabled)
726                 throw new UnsupportedOperationException();
727             
728             final FilterHolder holder=ServletContextHandler.this.getServletHandler().getFilter(filterName);
729             return (holder==null)?null:holder.getRegistration();
730         }
731 
732         @Override
733         public Map<String, ? extends FilterRegistration> getFilterRegistrations()
734         {
735             if (!_enabled)
736                 throw new UnsupportedOperationException();
737             
738             HashMap<String, FilterRegistration> registrations = new HashMap<String, FilterRegistration>();
739             ServletHandler handler=ServletContextHandler.this.getServletHandler();
740             FilterHolder[] holders=handler.getFilters();
741             if (holders!=null)
742             {
743                 for (FilterHolder holder : holders)
744                     registrations.put(holder.getName(),holder.getRegistration());
745             }
746             return registrations;
747         }
748 
749         @Override
750         public ServletRegistration getServletRegistration(String servletName)
751         {
752             if (!_enabled)
753                 throw new UnsupportedOperationException();
754             
755             final ServletHolder holder=ServletContextHandler.this.getServletHandler().getServlet(servletName);
756             return (holder==null)?null:holder.getRegistration();
757         }
758 
759         @Override
760         public Map<String, ? extends ServletRegistration> getServletRegistrations()
761         {
762             if (!_enabled)
763                 throw new UnsupportedOperationException();
764             
765             HashMap<String, ServletRegistration> registrations = new HashMap<String, ServletRegistration>();
766             ServletHandler handler=ServletContextHandler.this.getServletHandler();
767             ServletHolder[] holders=handler.getServlets();
768             if (holders!=null)
769             {
770                 for (ServletHolder holder : holders)
771                     registrations.put(holder.getName(),holder.getRegistration());
772             }
773             return registrations;
774         }
775 
776         @Override
777         public SessionCookieConfig getSessionCookieConfig()
778         {
779             // TODO other started conditions
780             if (!_enabled)
781                 throw new UnsupportedOperationException();
782             
783             if (_sessionHandler!=null)
784                 return _sessionHandler.getSessionManager().getSessionCookieConfig();
785             return null;
786         }
787 
788         @Override
789         public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
790         {
791             // TODO other started conditions
792             if (!isStarting())
793                 throw new IllegalStateException();
794             if (!_enabled)
795                 throw new UnsupportedOperationException();
796             
797             
798             if (_sessionHandler!=null)
799                 _sessionHandler.getSessionManager().setSessionTrackingModes(sessionTrackingModes);
800         }
801 
802         @Override
803         public void addListener(String className)
804         {
805             // TODO other started conditions
806             if (!isStarting())
807                 throw new IllegalStateException();
808             if (!_enabled)
809                 throw new UnsupportedOperationException();
810             super.addListener(className);
811         }
812 
813         @Override
814         public <T extends EventListener> void addListener(T t)
815         {
816             // TODO other started conditions
817             if (!isStarting())
818                 throw new IllegalStateException();
819             if (!_enabled)
820                 throw new UnsupportedOperationException();
821             super.addListener(t);
822         }
823 
824         @Override
825         public void addListener(Class<? extends EventListener> listenerClass)
826         {
827             // TODO other started conditions
828             if (!isStarting())
829                 throw new IllegalStateException();
830             if (!_enabled)
831                 throw new UnsupportedOperationException();
832             super.addListener(listenerClass);
833         }
834 
835         @Override
836         public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
837         {
838             try
839             {
840                 T l = super.createListener(clazz);
841 
842                 for (int i=_decorators.size()-1; i>=0; i--)
843                 {
844                     Decorator decorator = _decorators.get(i);
845                     l=decorator.decorateListenerInstance(l);
846                 }
847                 return l;
848             }
849             catch(ServletException e)
850             {
851                 throw e;
852             }
853             catch(Exception e)
854             {
855                 throw new ServletException(e);
856             }
857         }
858 
859 
860         @Override
861         public JspConfigDescriptor getJspConfigDescriptor()
862         {
863             return _jspConfig;
864         }
865         
866         @Override
867         public void declareRoles(String... roleNames)
868         {
869             if (!isStarting())
870                 throw new IllegalStateException();
871             if (!_enabled)
872                 throw new UnsupportedOperationException();
873             addRoles(roleNames);
874 
875 
876         }
877 
878     }
879 
880 
881 
882     /* ------------------------------------------------------------ */
883     /** Interface to decorate loaded classes.
884      */
885     public interface Decorator
886     {
887         <T extends Filter> T decorateFilterInstance(T filter) throws ServletException;
888         <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException;
889         <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException;
890 
891         void decorateFilterHolder(FilterHolder filter) throws ServletException;
892         void decorateServletHolder(ServletHolder servlet) throws ServletException;
893         
894         void destroyServletInstance(Servlet s);
895         void destroyFilterInstance(Filter f);
896         void destroyListenerInstance(EventListener f);
897     }
898 }