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  import javax.servlet.ServletException;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.server.Handler;
22  import org.eclipse.jetty.server.Request;
23  import org.eclipse.jetty.server.Server;
24  import org.eclipse.jetty.util.component.LifeCycle;
25  
26  /* ------------------------------------------------------------ */
27  /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
28   * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
29   *
30   */
31  public class HandlerWrapper extends AbstractHandlerContainer
32  {
33      protected Handler _handler;
34  
35      /* ------------------------------------------------------------ */
36      /**
37       *
38       */
39      public HandlerWrapper()
40      {
41      }
42  
43      /* ------------------------------------------------------------ */
44      /**
45       * @return Returns the handlers.
46       */
47      public Handler getHandler()
48      {
49          return _handler;
50      }
51  
52      /* ------------------------------------------------------------ */
53      /**
54       * @return Returns the handlers.
55       */
56      public Handler[] getHandlers()
57      {
58          if (_handler==null)
59              return new Handler[0];
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         if (_handler!=null)
105             _handler.stop();
106         super.doStop();
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     @Override
167     public void destroy()
168     {
169         if (!isStopped())
170             throw new IllegalStateException("!STOPPED");
171         Handler child=getHandler();
172         if (child!=null)
173         {
174             setHandler(null);
175             child.destroy();
176         }
177         super.destroy();
178     }
179 
180 }