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.HandlerContainer;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.Server;
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      protected void doStart() throws Exception
90      {
91          if (_handler!=null)
92              _handler.start();
93          super.doStart();
94      }
95      
96      /* ------------------------------------------------------------ */
97      /* 
98       * @see org.eclipse.thread.AbstractLifeCycle#doStop()
99       */
100     protected void doStop() throws Exception
101     {
102         super.doStop();
103         if (_handler!=null)
104             _handler.stop();
105     }
106     
107     /* ------------------------------------------------------------ */
108     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
109     {
110         if (_handler!=null && isStarted())
111         {
112             _handler.handle(target,baseRequest, request, response);
113         }
114     }
115     
116 
117     /* ------------------------------------------------------------ */
118     @Override
119     public void setServer(Server server)
120     {
121         Server old_server=getServer();
122         if (server==old_server)
123             return;
124         
125         if (isStarted())
126             throw new IllegalStateException(STARTED);
127         
128         super.setServer(server);
129         
130         Handler h=getHandler();
131         if (h!=null)
132             h.setServer(server);
133         
134         if (server!=null && server!=old_server)
135             server.getContainer().update(this, null,_handler, "handler");
136     }
137     
138 
139     /* ------------------------------------------------------------ */
140     @Override
141     protected Object expandChildren(Object list, Class byClass)
142     {
143         return expandHandler(_handler,list,byClass);
144     }
145 
146    
147 }