View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 org.eclipse.jetty.plus.annotation.InjectionCollection;
22  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
23  import org.eclipse.jetty.plus.annotation.RunAsCollection;
24  import org.eclipse.jetty.util.Decorator;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.log.Logger;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  
29  /**
30   * PlusDecorator
31   *
32   *
33   */
34  public class PlusDecorator implements Decorator
35  {
36      private static final Logger LOG = Log.getLogger(PlusDecorator.class);
37  
38      protected WebAppContext _context;
39  
40      public PlusDecorator (WebAppContext context)
41      {
42          _context = context;
43      }
44  
45      public Object decorate (Object o)
46      {
47  
48          RunAsCollection runAses = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
49          if (runAses != null)
50              runAses.setRunAs(o);
51  
52          InjectionCollection injections = (InjectionCollection)_context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
53          if (injections != null)
54              injections.inject(o);
55  
56          LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
57          if (callbacks != null)
58          {
59              try
60              {
61                  callbacks.callPostConstructCallback(o);
62              }
63              catch (Exception e)
64              {
65                  throw new RuntimeException(e);
66              }
67          }
68          return o;
69      }
70  
71      public void destroy (Object o)
72      {
73          LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
74          if (callbacks != null)
75          {
76              try
77              {
78                  callbacks.callPreDestroyCallback(o);
79              }
80              catch (Exception e)
81              {
82                  LOG.warn("Destroying instance of "+o.getClass(), e);
83              }
84          }
85      }
86  }