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.HandlerContainer;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.util.component.LifeCycle;
27  
28  /* ------------------------------------------------------------ */
29  /** A <code>HandlerContainer</code> that allows a hot swap
30   * of a wrapped handler.
31   * 
32   */
33  public class HotSwapHandler extends AbstractHandlerContainer
34  {
35      private volatile Handler _handler;
36  
37      /* ------------------------------------------------------------ */
38      /**
39       * 
40       */
41      public HotSwapHandler()
42      {
43      }
44  
45      /* ------------------------------------------------------------ */
46      /**
47       * @return Returns the handlers.
48       */
49      public Handler getHandler()
50      {
51          return _handler;
52      }
53      
54      /* ------------------------------------------------------------ */
55      /**
56       * @return Returns the handlers.
57       */
58      public Handler[] getHandlers()
59      {
60          return new Handler[] {_handler};
61      }
62      
63      /* ------------------------------------------------------------ */
64      /**
65       * @param handler Set the {@link Handler} which should be wrapped.
66       */
67      public void setHandler(Handler handler)
68      {
69          try
70          {
71              Handler old_handler = _handler;
72  
73              if (getServer()!=null)
74                  getServer().getContainer().update(this, old_handler, handler, "handler");
75  
76              if (handler!=null)
77              {
78                  handler.setServer(getServer());
79                  if (isStarted())
80                      handler.start();
81              }
82  
83              _handler = handler;
84              
85              if (isStarted())
86                  old_handler.stop();
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     protected void doStart() throws Exception
104     {
105         if (_handler!=null)
106             _handler.start();
107         super.doStart();
108     }
109     
110     /* ------------------------------------------------------------ */
111     /* 
112      * @see org.eclipse.thread.AbstractLifeCycle#doStop()
113      */
114     protected void doStop() throws Exception
115     {
116         super.doStop();
117         if (_handler!=null)
118             _handler.stop();
119     }
120     
121     /* ------------------------------------------------------------ */
122     /* 
123      * @see org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
124      */
125     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
126     {
127         if (_handler!=null && isStarted())
128         {
129             _handler.handle(target,baseRequest, request, response);
130         }
131     }
132     
133 
134     /* ------------------------------------------------------------ */
135     public void setServer(Server server)
136     {
137         Server old_server=getServer();
138         if (server==old_server)
139             return;
140         
141         if (isRunning())
142             throw new IllegalStateException(RUNNING);
143             
144         super.setServer(server);
145         
146         Handler h=getHandler();
147         if (h!=null)
148             h.setServer(server);
149         
150         if (server!=null && server!=old_server)
151             server.getContainer().update(this, null,_handler, "handler");
152     }
153     
154 
155     /* ------------------------------------------------------------ */
156     protected Object expandChildren(Object list, Class byClass)
157     {
158         return expandHandler(_handler,list,byClass);
159     }
160 
161    
162 }