1
2
3
4
5
6
7
8
9
10
11
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
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
51
52 public void decorateFilterHolder(FilterHolder filter) throws ServletException
53 {
54 }
55
56
57
58
59
60 public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
61 {
62 decorate(filter);
63 return filter;
64 }
65
66
67
68
69
70 public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
71 {
72 decorate(listener);
73 return listener;
74 }
75
76
77
78
79
80 public void decorateServletHolder(ServletHolder holder) throws ServletException
81 {
82 decorate(holder);
83 }
84
85
86
87
88
89 public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
90 {
91 decorate(servlet);
92 return servlet;
93 }
94
95
96
97
98
99 public void destroyFilterInstance(Filter f)
100 {
101 destroy(f);
102 }
103
104
105
106
107
108
109 public void destroyServletInstance(Servlet s)
110 {
111 destroy(s);
112 }
113
114
115
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 }