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