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.component.LifeCycle;
26  
27  /* ------------------------------------------------------------ */
28  /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
29   * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
30   * 
31   */
32  public class HandlerWrapper extends AbstractHandlerContainer
33  {
34      protected Handler _handler;
35  
36      /* ------------------------------------------------------------ */
37      /**
38       * 
39       */
40      public HandlerWrapper()
41      {
42      }
43  
44      /* ------------------------------------------------------------ */
45      /**
46       * @return Returns the handlers.
47       */
48      public Handler getHandler()
49      {
50          return _handler;
51      }
52      
53      /* ------------------------------------------------------------ */
54      /**
55       * @return Returns the handlers.
56       */
57      public Handler[] getHandlers()
58      {
59          return new Handler[] {_handler};
60      }
61      
62      /* ------------------------------------------------------------ */
63      /**
64       * @param handler Set the {@link Handler} which should be wrapped.
65       */
66      public void setHandler(Handler handler)
67      {
68          if (isStarted())
69              throw new IllegalStateException(STARTED);
70          
71          Handler old_handler = _handler;
72  
73          if (getServer()!=null)
74              getServer().getContainer().update(this, old_handler, handler, "handler");
75  
76          if (handler!=null)
77          {
78              handler.setServer(getServer());
79          }
80  
81          _handler = handler;
82      }
83      
84      /* ------------------------------------------------------------ */
85      /* 
86       * @see org.eclipse.thread.AbstractLifeCycle#doStart()
87       */
88      @Override
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     @Override
101     protected void doStop() throws Exception
102     {
103         super.doStop();
104         if (_handler!=null)
105             _handler.stop();
106     }
107     
108     /* ------------------------------------------------------------ */
109     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
110     {
111         if (_handler!=null && isStarted())
112         {
113             _handler.handle(target,baseRequest, request, response);
114         }
115     }
116     
117 
118     /* ------------------------------------------------------------ */
119     @Override
120     public void setServer(Server server)
121     {
122         Server old_server=getServer();
123         if (server==old_server)
124             return;
125         
126         if (isStarted())
127             throw new IllegalStateException(STARTED);
128         
129         super.setServer(server);
130         
131         Handler h=getHandler();
132         if (h!=null)
133             h.setServer(server);
134         
135         if (server!=null && server!=old_server)
136             server.getContainer().update(this, null,_handler, "handler");
137     }
138     
139 
140     /* ------------------------------------------------------------ */
141     @Override
142     protected Object expandChildren(Object list, Class byClass)
143     {
144         return expandHandler(_handler,list,byClass);
145     }
146 
147    
148 }