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  import org.eclipse.jetty.util.LazyList;
26  import org.eclipse.jetty.util.MultiException;
27  
28  /* ------------------------------------------------------------ */
29  /** A collection of handlers.  
30   * <p>
31   * The default implementations  calls all handlers in list order, 
32   * regardless of the response status or exceptions. Derived implementation
33   * may alter the order or the conditions of calling the contained 
34   * handlers.
35   * <p>
36   * 
37   * @org.apache.xbean.XBean
38   */
39  public class HandlerCollection extends AbstractHandlerContainer
40  {
41      private final boolean _mutableWhenRunning;
42      private volatile Handler[] _handlers;
43  
44      /* ------------------------------------------------------------ */
45      public HandlerCollection()
46      {
47          _mutableWhenRunning=false;
48      }
49      
50      /* ------------------------------------------------------------ */
51      public HandlerCollection(boolean mutableWhenRunning)
52      {
53          _mutableWhenRunning=mutableWhenRunning;
54      }
55  
56      /* ------------------------------------------------------------ */
57      /**
58       * @return Returns the handlers.
59       */
60      public Handler[] getHandlers()
61      {
62          return _handlers;
63      }
64      
65      /* ------------------------------------------------------------ */
66      /**
67       * 
68       * @param handlers The handlers to set.
69       */
70      public void setHandlers(Handler[] handlers)
71      {
72          if (!_mutableWhenRunning && isStarted())
73              throw new IllegalStateException(STARTED);
74          
75          Handler [] old_handlers = _handlers==null?null:_handlers.clone();
76          
77          if (getServer()!=null)
78              getServer().getContainer().update(this, old_handlers, handlers, "handler");
79          
80          Server server = getServer();
81          MultiException mex = new MultiException();
82          for (int i=0;handlers!=null && i<handlers.length;i++)
83          {
84              if (handlers[i].getServer()!=server)
85                  handlers[i].setServer(server);
86          }
87  
88          // handlers is volatile
89          _handlers = handlers;
90  
91          // stop old handlers
92          for (int i=0;old_handlers!=null && i<old_handlers.length;i++)
93          {
94              if (old_handlers[i]!=null)
95              {
96                  try
97                  {
98                      if (old_handlers[i].isStarted())
99                          old_handlers[i].stop();
100                 }
101                 catch (Throwable e)
102                 {
103                     mex.add(e);
104                 }
105             }
106         }
107                 
108         mex.ifExceptionThrowRuntime();
109     }
110 
111     /* ------------------------------------------------------------ */
112     /**
113      * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse)
114      */
115     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 
116         throws IOException, ServletException
117     {
118         if (_handlers!=null && isStarted())
119         {
120             MultiException mex=null;
121             
122             for (int i=0;i<_handlers.length;i++)
123             {
124                 try
125                 {
126                     _handlers[i].handle(target,baseRequest, request, response);
127                 }
128                 catch(IOException e)
129                 {
130                     throw e;
131                 }
132                 catch(RuntimeException e)
133                 {
134                     throw e;
135                 }
136                 catch(Exception e)
137                 {
138                     if (mex==null)
139                         mex=new MultiException();
140                     mex.add(e);
141                 }
142             }
143             if (mex!=null)
144             {
145                 if (mex.size()==1)
146                     throw new ServletException(mex.getThrowable(0));
147                 else
148                     throw new ServletException(mex);
149             }
150             
151         }    
152     }
153 
154     /* ------------------------------------------------------------ */
155     /* 
156      * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart()
157      */
158     @Override
159     protected void doStart() throws Exception
160     {
161         MultiException mex=new MultiException();
162         if (_handlers!=null)
163         {
164             for (int i=0;i<_handlers.length;i++)
165                 try{_handlers[i].start();}catch(Throwable e){mex.add(e);}
166         }
167         super.doStart();
168         mex.ifExceptionThrow();
169     }
170 
171     /* ------------------------------------------------------------ */
172     /* 
173      * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop()
174      */
175     @Override
176     protected void doStop() throws Exception
177     {
178         MultiException mex=new MultiException();
179         try { super.doStop(); } catch(Throwable e){mex.add(e);}
180         if (_handlers!=null)
181         {
182             for (int i=_handlers.length;i-->0;)
183                 try{_handlers[i].stop();}catch(Throwable e){mex.add(e);}
184         }
185         mex.ifExceptionThrow();
186     }
187     
188     /* ------------------------------------------------------------ */
189     @Override
190     public void setServer(Server server)
191     {
192         if (isStarted())
193             throw new IllegalStateException(STARTED);
194         
195         Server old_server=getServer();
196         
197         super.setServer(server);
198 
199         Handler[] h=getHandlers();
200         for (int i=0;h!=null && i<h.length;i++)
201             h[i].setServer(server);
202         
203         if (server!=null && server!=old_server)
204             server.getContainer().update(this, null,_handlers, "handler");
205         
206     }
207 
208     /* ------------------------------------------------------------ */
209     /* Add a handler.
210      * This implementation adds the passed handler to the end of the existing collection of handlers. 
211      * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler)
212      */
213     public void addHandler(Handler handler)
214     {
215         setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class));
216     }
217     
218     /* ------------------------------------------------------------ */
219     public void removeHandler(Handler handler)
220     {
221         Handler[] handlers = getHandlers();
222         
223         if (handlers!=null && handlers.length>0 )
224             setHandlers((Handler[])LazyList.removeFromArray(handlers, handler));
225     }
226 
227     /* ------------------------------------------------------------ */
228     @Override
229     protected Object expandChildren(Object list, Class byClass)
230     {
231         Handler[] handlers = getHandlers();
232         for (int i=0;handlers!=null && i<handlers.length;i++)
233             list=expandHandler(handlers[i], list, byClass);
234         return list;
235     }
236 
237 
238 }