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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
22 import java.util.List;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
28 import org.eclipse.jetty.server.Handler;
29 import org.eclipse.jetty.server.Request;
30 import org.eclipse.jetty.server.Server;
32 /* ------------------------------------------------------------ */
34 * A <code>HandlerContainer</code> that allows a hot swap of a wrapped handler.
37 public class HotSwapHandler extends AbstractHandlerContainer
39 private volatile Handler _handler;
41 /* ------------------------------------------------------------ */
45 public HotSwapHandler()
49 /* ------------------------------------------------------------ */
51 * @return Returns the handlers.
53 public Handler getHandler()
58 /* ------------------------------------------------------------ */
60 * @return Returns the handlers.
63 public Handler[] getHandlers()
69 /* ------------------------------------------------------------ */
72 * Set the {@link Handler} which should be wrapped.
74 public void setHandler(Handler handler)
77 throw new IllegalArgumentException("Parameter handler is null.");
80 updateBean(_handler,handler);
82 Server server = getServer();
83 handler.setServer(server);
88 throw new RuntimeException(e);
92 /* ------------------------------------------------------------ */
94 * @see org.eclipse.thread.AbstractLifeCycle#doStart()
97 protected void doStart() throws Exception
102 /* ------------------------------------------------------------ */
104 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
107 protected void doStop() throws Exception
112 /* ------------------------------------------------------------ */
114 * @see org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
117 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
119 if (_handler != null && isStarted())
121 _handler.handle(target,baseRequest,request,response);
125 /* ------------------------------------------------------------ */
127 public void setServer(Server server)
130 throw new IllegalStateException(RUNNING);
132 super.setServer(server);
134 Handler h = getHandler();
139 /* ------------------------------------------------------------ */
141 protected void expandChildren(List<Handler> list, Class<?> byClass)
143 expandHandler(_handler,list,byClass);
146 /* ------------------------------------------------------------ */
148 public void destroy()
151 throw new IllegalStateException("!STOPPED");
152 Handler child = getHandler();