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              if (isRunning())
72                  throw new IllegalStateException(RUNNING);
73  
74              Handler old_handler = _handler;
75  
76              if (getServer()!=null)
77                  getServer().getContainer().update(this, old_handler, handler, "handler");
78  
79              if (handler!=null)
80              {
81                  handler.setServer(getServer());
82                  if (isStarted())
83                      handler.start();
84              }
85  
86              _handler = handler;
87              
88              if (isStarted())
89                  old_handler.stop();
90  
91          }
92          catch(RuntimeException e)
93          {
94              throw e;
95          }
96          catch(Exception e)
97          {
98              throw new RuntimeException(e);
99          }
100     }
101     
102     /* ------------------------------------------------------------ */
103     /* 
104      * @see org.eclipse.thread.AbstractLifeCycle#doStart()
105      */
106     protected void doStart() throws Exception
107     {
108         if (_handler!=null)
109             _handler.start();
110         super.doStart();
111     }
112     
113     /* ------------------------------------------------------------ */
114     /* 
115      * @see org.eclipse.thread.AbstractLifeCycle#doStop()
116      */
117     protected void doStop() throws Exception
118     {
119         super.doStop();
120         if (_handler!=null)
121             _handler.stop();
122     }
123     
124     /* ------------------------------------------------------------ */
125     /* 
126      * @see org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
127      */
128     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
129     {
130         if (_handler!=null && isStarted())
131         {
132             _handler.handle(target,baseRequest, request, response);
133         }
134     }
135     
136 
137     /* ------------------------------------------------------------ */
138     public void setServer(Server server)
139     {
140         if (isRunning())
141             throw new IllegalStateException(RUNNING);
142             
143         Server old_server=getServer();
144         
145         super.setServer(server);
146         
147         Handler h=getHandler();
148         if (h!=null)
149             h.setServer(server);
150         
151         if (server!=null && server!=old_server)
152             server.getContainer().update(this, null,_handler, "handler");
153     }
154     
155 
156     /* ------------------------------------------------------------ */
157     protected Object expandChildren(Object list, Class byClass)
158     {
159         return expandHandler(_handler,list,byClass);
160     }
161 
162    
163 }