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.component.LifeCycle;
27  
28  /* ------------------------------------------------------------ */
29  /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
30   * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
31   * 
32   */
33  public class HandlerWrapper extends AbstractHandlerContainer
34  {
35      protected Handler _handler;
36  
37      /* ------------------------------------------------------------ */
38      /**
39       * 
40       */
41      public HandlerWrapper()
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          if (_handler==null)
61              return new Handler[0];
62          return new Handler[] {_handler};
63      }
64      
65      /* ------------------------------------------------------------ */
66      /**
67       * @param handler Set the {@link Handler} which should be wrapped.
68       */
69      public void setHandler(Handler handler)
70      {
71          if (isStarted())
72              throw new IllegalStateException(STARTED);
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          }
83  
84          _handler = handler;
85      }
86      
87      /* ------------------------------------------------------------ */
88      /* 
89       * @see org.eclipse.thread.AbstractLifeCycle#doStart()
90       */
91      @Override
92      protected void doStart() throws Exception
93      {
94          if (_handler!=null)
95              _handler.start();
96          super.doStart();
97      }
98      
99      /* ------------------------------------------------------------ */
100     /* 
101      * @see org.eclipse.thread.AbstractLifeCycle#doStop()
102      */
103     @Override
104     protected void doStop() throws Exception
105     {
106         super.doStop();
107         if (_handler!=null)
108             _handler.stop();
109     }
110     
111     /* ------------------------------------------------------------ */
112     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
113     {
114         if (_handler!=null && isStarted())
115         {
116             _handler.handle(target,baseRequest, request, response);
117         }
118     }
119     
120 
121     /* ------------------------------------------------------------ */
122     @Override
123     public void setServer(Server server)
124     {
125         Server old_server=getServer();
126         if (server==old_server)
127             return;
128         
129         if (isStarted())
130             throw new IllegalStateException(STARTED);
131         
132         super.setServer(server);
133         
134         Handler h=getHandler();
135         if (h!=null)
136             h.setServer(server);
137         
138         if (server!=null && server!=old_server)
139             server.getContainer().update(this, null,_handler, "handler");
140     }
141     
142 
143     /* ------------------------------------------------------------ */
144     @Override
145     protected Object expandChildren(Object list, Class byClass)
146     {
147         return expandHandler(_handler,list,byClass);
148     }
149 
150     /* ------------------------------------------------------------ */
151     public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
152     {
153         HandlerWrapper h=this;
154         while (h!=null)
155         {
156             if (byclass.isInstance(h))
157                 return (H)h;
158             Handler w = h.getHandler();
159             if (w instanceof HandlerWrapper)
160                 h=(HandlerWrapper)w;
161             else break;
162         }
163         return null;
164         
165     }
166     
167    
168 }