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 org.eclipse.jetty.server.server.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.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     protected void doStart() throws Exception
159     {
160         MultiException mex=new MultiException();
161         if (_handlers!=null)
162         {
163             for (int i=0;i<_handlers.length;i++)
164                 try{_handlers[i].start();}catch(Throwable e){mex.add(e);}
165         }
166         super.doStart();
167         mex.ifExceptionThrow();
168     }
169 
170     /* ------------------------------------------------------------ */
171     /* 
172      * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop()
173      */
174     protected void doStop() throws Exception
175     {
176         MultiException mex=new MultiException();
177         try { super.doStop(); } catch(Throwable e){mex.add(e);}
178         if (_handlers!=null)
179         {
180             for (int i=_handlers.length;i-->0;)
181                 try{_handlers[i].stop();}catch(Throwable e){mex.add(e);}
182         }
183         mex.ifExceptionThrow();
184     }
185     
186     /* ------------------------------------------------------------ */
187     public void setServer(Server server)
188     {
189         if (isStarted())
190             throw new IllegalStateException(STARTED);
191         
192         Server old_server=getServer();
193         
194         super.setServer(server);
195 
196         Handler[] h=getHandlers();
197         for (int i=0;h!=null && i<h.length;i++)
198             h[i].setServer(server);
199         
200         if (server!=null && server!=old_server)
201             server.getContainer().update(this, null,_handlers, "handler");
202         
203     }
204 
205     /* ------------------------------------------------------------ */
206     /* Add a handler.
207      * This implementation adds the passed handler to the end of the existing collection of handlers. 
208      * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler)
209      */
210     public void addHandler(Handler handler)
211     {
212         setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class));
213     }
214     
215     /* ------------------------------------------------------------ */
216     public void removeHandler(Handler handler)
217     {
218         Handler[] handlers = getHandlers();
219         
220         if (handlers!=null && handlers.length>0 )
221             setHandlers((Handler[])LazyList.removeFromArray(handlers, handler));
222     }
223 
224     /* ------------------------------------------------------------ */
225     protected Object expandChildren(Object list, Class byClass)
226     {
227         Handler[] handlers = getHandlers();
228         for (int i=0;handlers!=null && i<handlers.length;i++)
229             list=expandHandler(handlers[i], list, byClass);
230         return list;
231     }
232 
233 
234 }