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
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.server.Handler;
23 import org.eclipse.jetty.server.Request;
24 import org.eclipse.jetty.server.Server;
25
26 /* ------------------------------------------------------------ */
27 /**
28 * A <code>HandlerContainer</code> that allows a hot swap of a wrapped handler.
29 *
30 */
31 public class HotSwapHandler extends AbstractHandlerContainer
32 {
33 private volatile Handler _handler;
34
35 /* ------------------------------------------------------------ */
36 /**
37 *
38 */
39 public HotSwapHandler()
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 return new Handler[]
59 { _handler };
60 }
61
62 /* ------------------------------------------------------------ */
63 /**
64 * @param handler
65 * Set the {@link Handler} which should be wrapped.
66 */
67 public void setHandler(Handler handler)
68 {
69 if (handler == null)
70 throw new IllegalArgumentException("Parameter handler is null.");
71 try
72 {
73 Handler old_handler = _handler;
74 _handler = handler;
75 Server server = getServer();
76 handler.setServer(server);
77 addBean(handler);
78
79 if (server != null)
80 server.getContainer().update(this,old_handler,handler,"handler");
81
82 // if there is an old handler and it was started, stop it
83 if (old_handler != null)
84 {
85 removeBean(old_handler);
86 }
87
88 }
89 catch (Exception e)
90 {
91 throw new RuntimeException(e);
92 }
93 }
94
95 /* ------------------------------------------------------------ */
96 /*
97 * @see org.eclipse.thread.AbstractLifeCycle#doStart()
98 */
99 @Override
100 protected void doStart() throws Exception
101 {
102 super.doStart();
103 }
104
105 /* ------------------------------------------------------------ */
106 /*
107 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
108 */
109 @Override
110 protected void doStop() throws Exception
111 {
112 super.doStop();
113 }
114
115 /* ------------------------------------------------------------ */
116 /*
117 * @see org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
118 */
119 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
120 {
121 if (_handler != null && isStarted())
122 {
123 _handler.handle(target,baseRequest,request,response);
124 }
125 }
126
127 /* ------------------------------------------------------------ */
128 @Override
129 public void setServer(Server server)
130 {
131 Server old_server = getServer();
132 if (server == old_server)
133 return;
134
135 if (isRunning())
136 throw new IllegalStateException(RUNNING);
137
138 super.setServer(server);
139
140 Handler h = getHandler();
141 if (h != null)
142 h.setServer(server);
143
144 if (server != null && server != old_server)
145 server.getContainer().update(this,null,_handler,"handler");
146 }
147
148 /* ------------------------------------------------------------ */
149 @SuppressWarnings(
150 { "rawtypes", "unchecked" })
151 @Override
152 protected Object expandChildren(Object list, Class byClass)
153 {
154 return expandHandler(_handler,list,byClass);
155 }
156
157 /* ------------------------------------------------------------ */
158 @Override
159 public void destroy()
160 {
161 if (!isStopped())
162 throw new IllegalStateException("!STOPPED");
163 Handler child = getHandler();
164 if (child != null)
165 {
166 setHandler(null);
167 child.destroy();
168 }
169 super.destroy();
170 }
171 }