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