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