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  
71              if (getServer()!=null)
72                  getServer().getContainer().update(this, old_handler, handler, "handler");
73  
74              if (handler!=null)
75              {
76                  handler.setServer(getServer());
77                  if (isStarted())
78                      handler.start();
79              }
80  
81              _handler = handler;
82              
83              // if there is an old handler and it was started, stop it
84              if (old_handler != null && isStarted())
85              {
86                  old_handler.stop();
87              }
88  
89          }
90          catch(RuntimeException e)
91          {
92              throw e;
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         if (_handler!=null)
108             _handler.start();
109         super.doStart();
110     }
111     
112     /* ------------------------------------------------------------ */
113     /* 
114      * @see org.eclipse.thread.AbstractLifeCycle#doStop()
115      */
116     @Override
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     @Override
139     public void setServer(Server server)
140     {
141         Server old_server=getServer();
142         if (server==old_server)
143             return;
144         
145         if (isRunning())
146             throw new IllegalStateException(RUNNING);
147             
148         super.setServer(server);
149         
150         Handler h=getHandler();
151         if (h!=null)
152             h.setServer(server);
153         
154         if (server!=null && server!=old_server)
155             server.getContainer().update(this, null,_handler, "handler");
156     }
157     
158 
159     /* ------------------------------------------------------------ */
160     @Override
161     protected Object expandChildren(Object list, Class byClass)
162     {
163         return expandHandler(_handler,list,byClass);
164     }
165 
166    
167 }