001: //========================================================================
002: //$Id: WrappedHandler.java,v 1.2 2005/11/11 22:55:39 gregwilkins Exp $
003: //Copyright 2004-2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.handler;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.ServletException;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import org.mortbay.component.LifeCycle;
025: import org.mortbay.jetty.Handler;
026: import org.mortbay.jetty.HandlerContainer;
027: import org.mortbay.jetty.Server;
028:
029: /* ------------------------------------------------------------ */
030: /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
031: * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
032: * @author gregw
033: */
034: public class HandlerWrapper extends AbstractHandlerContainer {
035: private Handler _handler;
036:
037: /* ------------------------------------------------------------ */
038: /**
039: *
040: */
041: public HandlerWrapper() {
042: super ();
043: }
044:
045: /* ------------------------------------------------------------ */
046: /**
047: * @return Returns the handlers.
048: */
049: public Handler getHandler() {
050: return _handler;
051: }
052:
053: /* ------------------------------------------------------------ */
054: /**
055: * @param handler Set the {@link Handler} which should be wrapped.
056: */
057: public void setHandler(Handler handler) {
058: try {
059: Handler old_handler = _handler;
060:
061: if (getServer() != null)
062: getServer().getContainer().update(this , old_handler,
063: handler, "handler");
064:
065: if (handler != null) {
066: handler.setServer(getServer());
067: }
068:
069: _handler = handler;
070:
071: if (old_handler != null) {
072: if (old_handler.isStarted())
073: old_handler.stop();
074: }
075: } catch (Exception e) {
076: IllegalStateException ise = new IllegalStateException();
077: ise.initCause(e);
078: throw ise;
079: }
080: }
081:
082: /* ------------------------------------------------------------ */
083: /** Add a handler.
084: * This implementation of addHandler calls setHandler with the
085: * passed handler. If this HandlerWrapper had a previous wrapped
086: * handler, then it is passed to a call to addHandler on the passed
087: * handler. Thus this call can add a handler in a chain of
088: * wrapped handlers.
089: *
090: * @param handler
091: */
092: public void addHandler(Handler handler) {
093: Handler old = getHandler();
094: if (old != null && !(handler instanceof HandlerContainer))
095: throw new IllegalArgumentException("Cannot add");
096: setHandler(handler);
097: if (old != null)
098: ((HandlerContainer) handler).addHandler(old);
099: }
100:
101: /* ------------------------------------------------------------ */
102: /*
103: * @see org.mortbay.thread.AbstractLifeCycle#doStart()
104: */
105: protected void doStart() throws Exception {
106: if (_handler != null)
107: _handler.start();
108: super .doStart();
109: }
110:
111: /* ------------------------------------------------------------ */
112: /*
113: * @see org.mortbay.thread.AbstractLifeCycle#doStop()
114: */
115: protected void doStop() throws Exception {
116: super .doStop();
117: if (_handler != null)
118: _handler.stop();
119: }
120:
121: /* ------------------------------------------------------------ */
122: /*
123: * @see org.mortbay.jetty.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
124: */
125: public void handle(String target, HttpServletRequest request,
126: HttpServletResponse response, int dispatch)
127: throws IOException, ServletException {
128: if (_handler != null && isStarted())
129: _handler.handle(target, request, response, dispatch);
130: }
131:
132: /* ------------------------------------------------------------ */
133: public void setServer(Server server) {
134: Server old_server = getServer();
135:
136: super .setServer(server);
137:
138: Handler h = getHandler();
139: if (h != null)
140: h.setServer(server);
141:
142: if (server != null && server != old_server)
143: server.getContainer().update(this , null, _handler,
144: "handler");
145: }
146:
147: /* ------------------------------------------------------------ */
148: protected Object expandChildren(Object list, Class byClass) {
149: return expandHandler(_handler, list, byClass);
150: }
151:
152: }
|