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