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