comparison src/org/eclipse/jetty/server/handler/HandlerWrapper.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children 22a4e93ed20e
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 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.server.handler;
20
21 import java.io.IOException;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.eclipse.jetty.server.Handler;
28 import org.eclipse.jetty.server.Request;
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.util.component.LifeCycle;
31
32 /* ------------------------------------------------------------ */
33 /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
34 * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
35 *
36 */
37 public class HandlerWrapper extends AbstractHandlerContainer
38 {
39 protected Handler _handler;
40
41 /* ------------------------------------------------------------ */
42 /**
43 *
44 */
45 public HandlerWrapper()
46 {
47 }
48
49 /* ------------------------------------------------------------ */
50 /**
51 * @return Returns the handlers.
52 */
53 public Handler getHandler()
54 {
55 return _handler;
56 }
57
58 /* ------------------------------------------------------------ */
59 /**
60 * @return Returns the handlers.
61 */
62 public Handler[] getHandlers()
63 {
64 if (_handler==null)
65 return new Handler[0];
66 return new Handler[] {_handler};
67 }
68
69 /* ------------------------------------------------------------ */
70 /**
71 * @param handler Set the {@link Handler} which should be wrapped.
72 */
73 public void setHandler(Handler handler)
74 {
75 if (isStarted())
76 throw new IllegalStateException(STARTED);
77
78 Handler old_handler = _handler;
79 _handler = handler;
80 if (handler!=null)
81 handler.setServer(getServer());
82
83 if (getServer()!=null)
84 getServer().getContainer().update(this, old_handler, handler, "handler");
85 }
86
87 /* ------------------------------------------------------------ */
88 /*
89 * @see org.eclipse.thread.AbstractLifeCycle#doStart()
90 */
91 @Override
92 protected void doStart() throws Exception
93 {
94 if (_handler!=null)
95 _handler.start();
96 super.doStart();
97 }
98
99 /* ------------------------------------------------------------ */
100 /*
101 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
102 */
103 @Override
104 protected void doStop() throws Exception
105 {
106 if (_handler!=null)
107 _handler.stop();
108 super.doStop();
109 }
110
111 /* ------------------------------------------------------------ */
112 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
113 {
114 if (_handler!=null && isStarted())
115 {
116 _handler.handle(target,baseRequest, request, response);
117 }
118 }
119
120
121 /* ------------------------------------------------------------ */
122 @Override
123 public void setServer(Server server)
124 {
125 Server old_server=getServer();
126 if (server==old_server)
127 return;
128
129 if (isStarted())
130 throw new IllegalStateException(STARTED);
131
132 super.setServer(server);
133
134 Handler h=getHandler();
135 if (h!=null)
136 h.setServer(server);
137
138 if (server!=null && server!=old_server)
139 server.getContainer().update(this, null,_handler, "handler");
140 }
141
142
143 /* ------------------------------------------------------------ */
144 @Override
145 protected Object expandChildren(Object list, Class byClass)
146 {
147 return expandHandler(_handler,list,byClass);
148 }
149
150 /* ------------------------------------------------------------ */
151 public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
152 {
153 HandlerWrapper h=this;
154 while (h!=null)
155 {
156 if (byclass.isInstance(h))
157 return (H)h;
158 Handler w = h.getHandler();
159 if (w instanceof HandlerWrapper)
160 h=(HandlerWrapper)w;
161 else break;
162 }
163 return null;
164
165 }
166
167 /* ------------------------------------------------------------ */
168 @Override
169 public void destroy()
170 {
171 if (!isStopped())
172 throw new IllegalStateException("!STOPPED");
173 Handler child=getHandler();
174 if (child!=null)
175 {
176 setHandler(null);
177 child.destroy();
178 }
179 super.destroy();
180 }
181
182 }