View Javadoc

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