1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server.handler;
15
16 import java.io.IOException;
17 import javax.servlet.ServletException;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.eclipse.jetty.server.Handler;
22 import org.eclipse.jetty.server.Request;
23 import org.eclipse.jetty.server.Server;
24 import org.eclipse.jetty.util.component.LifeCycle;
25
26
27
28
29
30
31 public class HandlerWrapper extends AbstractHandlerContainer
32 {
33 protected Handler _handler;
34
35
36
37
38
39 public HandlerWrapper()
40 {
41 }
42
43
44
45
46
47 public Handler getHandler()
48 {
49 return _handler;
50 }
51
52
53
54
55
56 public Handler[] getHandlers()
57 {
58 if (_handler==null)
59 return new Handler[0];
60 return new Handler[] {_handler};
61 }
62
63
64
65
66
67 public void setHandler(Handler handler)
68 {
69 if (isStarted())
70 throw new IllegalStateException(STARTED);
71
72 Handler old_handler = _handler;
73
74 if (getServer()!=null)
75 getServer().getContainer().update(this, old_handler, handler, "handler");
76
77 if (handler!=null)
78 {
79 handler.setServer(getServer());
80 }
81
82 _handler = handler;
83 }
84
85
86
87
88
89 @Override
90 protected void doStart() throws Exception
91 {
92 if (_handler!=null)
93 _handler.start();
94 super.doStart();
95 }
96
97
98
99
100
101 @Override
102 protected void doStop() throws Exception
103 {
104 if (_handler!=null)
105 _handler.stop();
106 super.doStop();
107 }
108
109
110 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
111 {
112 if (_handler!=null && isStarted())
113 {
114 _handler.handle(target,baseRequest, request, response);
115 }
116 }
117
118
119
120 @Override
121 public void setServer(Server server)
122 {
123 Server old_server=getServer();
124 if (server==old_server)
125 return;
126
127 if (isStarted())
128 throw new IllegalStateException(STARTED);
129
130 super.setServer(server);
131
132 Handler h=getHandler();
133 if (h!=null)
134 h.setServer(server);
135
136 if (server!=null && server!=old_server)
137 server.getContainer().update(this, null,_handler, "handler");
138 }
139
140
141
142 @Override
143 protected Object expandChildren(Object list, Class byClass)
144 {
145 return expandHandler(_handler,list,byClass);
146 }
147
148
149 public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
150 {
151 HandlerWrapper h=this;
152 while (h!=null)
153 {
154 if (byclass.isInstance(h))
155 return (H)h;
156 Handler w = h.getHandler();
157 if (w instanceof HandlerWrapper)
158 h=(HandlerWrapper)w;
159 else break;
160 }
161 return null;
162
163 }
164
165
166 @Override
167 public void destroy()
168 {
169 if (!isStopped())
170 throw new IllegalStateException("!STOPPED");
171 Handler child=getHandler();
172 if (child!=null)
173 {
174 setHandler(null);
175 child.destroy();
176 }
177 super.destroy();
178 }
179
180 }