View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.webapp;
20  
21  import java.util.EventListener;
22  
23  import javax.servlet.Filter;
24  import javax.servlet.Servlet;
25  import javax.servlet.ServletException;
26  
27  import org.eclipse.jetty.plus.annotation.InjectionCollection;
28  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
29  import org.eclipse.jetty.plus.annotation.RunAsCollection;
30  import org.eclipse.jetty.servlet.FilterHolder;
31  import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
32  import org.eclipse.jetty.servlet.ServletHolder;
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
35  import org.eclipse.jetty.webapp.WebAppContext;
36  
37  /**
38   * WebAppDecorator
39   *
40   *
41   */
42  public class PlusDecorator implements Decorator
43  {
44      private static final Logger LOG = Log.getLogger(PlusDecorator.class);
45  
46      protected WebAppContext _context;
47  
48      public PlusDecorator (WebAppContext context)
49      {
50          _context = context;
51      }
52  
53      /* ------------------------------------------------------------ */
54      /**
55       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterHolder(org.eclipse.jetty.servlet.FilterHolder)
56       */
57      public void decorateFilterHolder(FilterHolder filter) throws ServletException
58      {
59      }
60  
61      /* ------------------------------------------------------------ */
62      /**
63       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterInstance(javax.servlet.Filter)
64       */
65      public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
66      {
67          decorate(filter);
68          return filter;
69      }
70  
71      /* ------------------------------------------------------------ */
72      /**
73       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateListenerInstance(java.util.EventListener)
74       */
75      public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
76      {
77          decorate(listener);
78          return listener;
79      }
80  
81      /* ------------------------------------------------------------ */
82      /**
83       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletHolder(org.eclipse.jetty.servlet.ServletHolder)
84       */
85      public void decorateServletHolder(ServletHolder holder) throws ServletException
86      {
87          decorate(holder);
88      }
89  
90      /* ------------------------------------------------------------ */
91      /**
92       * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletInstance(javax.servlet.Servlet)
93       */
94      public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
95      {
96          decorate(servlet);
97          return servlet;
98      }
99  
100     /* ------------------------------------------------------------ */
101     /**
102      * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyFilterInstance(javax.servlet.Filter)
103      */
104     public void destroyFilterInstance(Filter f)
105     {
106         destroy(f);
107     }
108 
109 
110     /* ------------------------------------------------------------ */
111     /**
112      * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyServletInstance(javax.servlet.Servlet)
113      */
114     public void destroyServletInstance(Servlet s)
115     {
116         destroy(s);
117     }
118 
119     /**
120      * @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyListenerInstance(java.util.EventListener)
121      */
122     public void destroyListenerInstance(EventListener l)
123     {
124         destroy(l);
125     }
126 
127 
128     protected void decorate (Object o)
129     throws ServletException
130     {
131 
132         RunAsCollection runAses = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
133         if (runAses != null)
134             runAses.setRunAs(o);
135 
136         InjectionCollection injections = (InjectionCollection)_context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
137         if (injections != null)
138             injections.inject(o);
139 
140         LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
141         if (callbacks != null)
142         {
143             try
144             {
145                 callbacks.callPostConstructCallback(o);
146             }
147             catch (Exception e)
148             {
149                 throw new ServletException(e);
150             }
151         }
152     }
153 
154     protected void destroy (Object o)
155     {
156         LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
157         if (callbacks != null)
158         {
159             try
160             {
161                 callbacks.callPreDestroyCallback(o);
162             }
163             catch (Exception e)
164             {
165                 LOG.warn("Destroying instance of "+o.getClass(), e);
166             }
167         }
168     }
169 }