View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-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.plus.webapp;
15  
16  import java.util.EventListener;
17  
18  import javax.servlet.Filter;
19  import javax.servlet.Servlet;
20  import javax.servlet.ServletException;
21  
22  import org.eclipse.jetty.plus.annotation.InjectionCollection;
23  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
24  import org.eclipse.jetty.plus.annotation.RunAsCollection;
25  import org.eclipse.jetty.servlet.FilterHolder;
26  import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
27  import org.eclipse.jetty.servlet.ServletHolder;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  
31  /**
32   * WebAppDecorator
33   *
34   *
35   */
36  public class PlusDecorator implements Decorator
37  {
38      protected WebAppContext _context;
39  
40      public PlusDecorator (WebAppContext context)
41      {
42          _context = context;
43      }
44  
45      /* ------------------------------------------------------------ */
46      /**
47       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterHolder(org.eclipse.jetty.servlet.FilterHolder)
48       */
49      public void decorateFilterHolder(FilterHolder filter) throws ServletException
50      {
51      }
52      
53      /* ------------------------------------------------------------ */
54      /**
55       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterInstance(javax.servlet.Filter)
56       */
57      public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
58      {
59          decorate(filter);
60          return filter;
61      }
62  
63      /* ------------------------------------------------------------ */
64      /**
65       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateListenerInstance(java.util.EventListener)
66       */
67      public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
68      {
69          decorate(listener);
70          return listener;
71      }
72  
73      /* ------------------------------------------------------------ */
74      /**
75       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletHolder(org.eclipse.jetty.servlet.ServletHolder)
76       */
77      public void decorateServletHolder(ServletHolder holder) throws ServletException
78      {
79          decorate(holder);
80      }
81      
82      /* ------------------------------------------------------------ */
83      /**
84       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletInstance(javax.servlet.Servlet)
85       */
86      public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
87      {
88          decorate(servlet);
89          return servlet;
90      }
91  
92      /* ------------------------------------------------------------ */
93      /**
94       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyFilterInstance(javax.servlet.Filter)
95       */
96      public void destroyFilterInstance(Filter f)
97      {
98          destroy(f);
99      }
100 
101 
102     /* ------------------------------------------------------------ */
103     /**
104      * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyServletInstance(javax.servlet.Servlet)
105      */
106     public void destroyServletInstance(Servlet s)
107     {
108         destroy(s);
109     }
110 
111     /** 
112      * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyListenerInstance(java.util.EventListener)
113      */
114     public void destroyListenerInstance(EventListener l)
115     {
116         destroy(l);
117     }
118 
119 
120     protected void decorate (Object o) 
121     throws ServletException
122     {       
123 
124         RunAsCollection runAses = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
125         if (runAses != null)
126             runAses.setRunAs(o);
127         
128         InjectionCollection injections = (InjectionCollection)_context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
129         if (injections != null)
130             injections.inject(o);
131 
132         LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
133         if (callbacks != null)
134         {
135             try
136             {
137                 callbacks.callPostConstructCallback(o);
138             }
139             catch (Exception e)
140             {
141                 throw new ServletException(e);
142             }
143         }
144     } 
145     
146     protected void destroy (Object o)
147     {
148         LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); 
149         if (callbacks != null)
150         {
151             try
152             {
153                 callbacks.callPreDestroyCallback(o);
154             }
155             catch (Exception e)
156             {
157                 Log.warn("Destroying instance of "+o.getClass(), e);
158             }
159         }
160     }
161 }