View Javadoc

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