]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/HotSwapHandler.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / HotSwapHandler.java
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 import java.util.List;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.eclipse.jetty.server.Handler;
29 import org.eclipse.jetty.server.Request;
30 import org.eclipse.jetty.server.Server;
31
32 /* ------------------------------------------------------------ */
33 /**
34  * A <code>HandlerContainer</code> that allows a hot swap of a wrapped handler.
35  *
36  */
37 public class HotSwapHandler extends AbstractHandlerContainer
38 {
39     private volatile Handler _handler;
40
41     /* ------------------------------------------------------------ */
42     /**
43      *
44      */
45     public HotSwapHandler()
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     @Override
63     public Handler[] getHandlers()
64     {
65         return new Handler[]
66         { _handler };
67     }
68
69     /* ------------------------------------------------------------ */
70     /**
71      * @param handler
72      *            Set the {@link Handler} which should be wrapped.
73      */
74     public void setHandler(Handler handler)
75     {
76         if (handler == null)
77             throw new IllegalArgumentException("Parameter handler is null.");
78         try
79         {
80             updateBean(_handler,handler);
81             _handler=handler;
82             Server server = getServer();
83             handler.setServer(server);
84
85         }
86         catch (Exception e)
87         {
88             throw new RuntimeException(e);
89         }
90     }
91
92     /* ------------------------------------------------------------ */
93     /*
94      * @see org.eclipse.thread.AbstractLifeCycle#doStart()
95      */
96     @Override
97     protected void doStart() throws Exception
98     {
99         super.doStart();
100     }
101
102     /* ------------------------------------------------------------ */
103     /*
104      * @see org.eclipse.thread.AbstractLifeCycle#doStop()
105      */
106     @Override
107     protected void doStop() throws Exception
108     {
109         super.doStop();
110     }
111
112     /* ------------------------------------------------------------ */
113     /*
114      * @see org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
115      */
116     @Override
117     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
118     {
119         if (_handler != null && isStarted())
120         {
121             _handler.handle(target,baseRequest,request,response);
122         }
123     }
124
125     /* ------------------------------------------------------------ */
126     @Override
127     public void setServer(Server server)
128     {
129         if (isRunning())
130             throw new IllegalStateException(RUNNING);
131
132         super.setServer(server);
133
134         Handler h = getHandler();
135         if (h != null)
136             h.setServer(server);
137     }
138
139     /* ------------------------------------------------------------ */
140     @Override
141     protected void expandChildren(List<Handler> list, Class<?> byClass)
142     {
143         expandHandler(_handler,list,byClass);
144     }
145
146     /* ------------------------------------------------------------ */
147     @Override
148     public void destroy()
149     {
150         if (!isStopped())
151             throw new IllegalStateException("!STOPPED");
152         Handler child = getHandler();
153         if (child != null)
154         {
155             setHandler(null);
156             child.destroy();
157         }
158         super.destroy();
159     }
160 }