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