1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
28 * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
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 * @return Returns the handlers.
46 */
47 public Handler getHandler()
48 {
49 return _handler;
50 }
51
52 /* ------------------------------------------------------------ */
53 /**
54 * @return Returns the handlers.
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 * @param handler Set the {@link Handler} which should be wrapped.
66 */
67 public void setHandler(Handler handler)
68 {
69 if (isStarted())
70 throw new IllegalStateException(STARTED);
71
72 Handler old_handler = _handler;
73 _handler = handler;
74 if (handler!=null)
75 handler.setServer(getServer());
76
77 if (getServer()!=null)
78 getServer().getContainer().update(this, old_handler, handler, "handler");
79 }
80
81 /* ------------------------------------------------------------ */
82 /*
83 * @see org.eclipse.thread.AbstractLifeCycle#doStart()
84 */
85 @Override
86 protected void doStart() throws Exception
87 {
88 if (_handler!=null)
89 _handler.start();
90 super.doStart();
91 }
92
93 /* ------------------------------------------------------------ */
94 /*
95 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
96 */
97 @Override
98 protected void doStop() throws Exception
99 {
100 if (_handler!=null)
101 _handler.stop();
102 super.doStop();
103 }
104
105 /* ------------------------------------------------------------ */
106 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
107 {
108 if (_handler!=null && isStarted())
109 {
110 _handler.handle(target,baseRequest, request, response);
111 }
112 }
113
114
115 /* ------------------------------------------------------------ */
116 @Override
117 public void setServer(Server server)
118 {
119 Server old_server=getServer();
120 if (server==old_server)
121 return;
122
123 if (isStarted())
124 throw new IllegalStateException(STARTED);
125
126 super.setServer(server);
127
128 Handler h=getHandler();
129 if (h!=null)
130 h.setServer(server);
131
132 if (server!=null && server!=old_server)
133 server.getContainer().update(this, null,_handler, "handler");
134 }
135
136
137 /* ------------------------------------------------------------ */
138 @Override
139 protected Object expandChildren(Object list, Class byClass)
140 {
141 return expandHandler(_handler,list,byClass);
142 }
143
144 /* ------------------------------------------------------------ */
145 public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
146 {
147 HandlerWrapper h=this;
148 while (h!=null)
149 {
150 if (byclass.isInstance(h))
151 return (H)h;
152 Handler w = h.getHandler();
153 if (w instanceof HandlerWrapper)
154 h=(HandlerWrapper)w;
155 else break;
156 }
157 return null;
158
159 }
160
161 /* ------------------------------------------------------------ */
162 @Override
163 public void destroy()
164 {
165 if (!isStopped())
166 throw new IllegalStateException("!STOPPED");
167 Handler child=getHandler();
168 if (child!=null)
169 {
170 setHandler(null);
171 child.destroy();
172 }
173 super.destroy();
174 }
175
176 }