1 //======================================================================== 2 //$Id: WrappedHandler.java,v 1.2 2005/11/11 22:55:39 gregwilkins Exp $ 3 //Copyright 2004-2006 Mort Bay Consulting Pty. Ltd. 4 //------------------------------------------------------------------------ 5 //Licensed under the Apache License, Version 2.0 (the "License"); 6 //you may not use this file except in compliance with the License. 7 //You may obtain a copy of the License at 8 //http://www.apache.org/licenses/LICENSE-2.0 9 //Unless required by applicable law or agreed to in writing, software 10 //distributed under the License is distributed on an "AS IS" BASIS, 11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 //See the License for the specific language governing permissions and 13 //limitations under the License. 14 //======================================================================== 15 16 package org.mortbay.jetty.handler; 17 18 import java.io.IOException; 19 20 import javax.servlet.ServletException; 21 import javax.servlet.http.HttpServletRequest; 22 import javax.servlet.http.HttpServletResponse; 23 24 import org.mortbay.component.LifeCycle; 25 import org.mortbay.jetty.Handler; 26 import org.mortbay.jetty.HandlerContainer; 27 import org.mortbay.jetty.Server; 28 29 /* ------------------------------------------------------------ */ 30 /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and 31 * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern. 32 * @author gregw 33 */ 34 public class HandlerWrapper extends AbstractHandlerContainer 35 { 36 private Handler _handler; 37 38 /* ------------------------------------------------------------ */ 39 /** 40 * 41 */ HandlerWrapper()42 public HandlerWrapper() 43 { 44 super(); 45 } 46 47 /* ------------------------------------------------------------ */ 48 /** 49 * @return Returns the handlers. 50 */ getHandler()51 public Handler getHandler() 52 { 53 return _handler; 54 } 55 56 /* ------------------------------------------------------------ */ 57 /** 58 * @param handler Set the {@link Handler} which should be wrapped. 59 */ setHandler(Handler handler)60 public void setHandler(Handler handler) 61 { 62 try 63 { 64 Handler old_handler = _handler; 65 66 if (getServer()!=null) 67 getServer().getContainer().update(this, old_handler, handler, "handler"); 68 69 if (handler!=null) 70 { 71 handler.setServer(getServer()); 72 } 73 74 _handler = handler; 75 76 if (old_handler!=null) 77 { 78 if (old_handler.isStarted()) 79 old_handler.stop(); 80 } 81 } 82 catch(Exception e) 83 { 84 IllegalStateException ise= new IllegalStateException(); 85 ise.initCause(e); 86 throw ise; 87 } 88 } 89 90 /* ------------------------------------------------------------ */ 91 /** Add a handler. 92 * This implementation of addHandler calls setHandler with the 93 * passed handler. If this HandlerWrapper had a previous wrapped 94 * handler, then it is passed to a call to addHandler on the passed 95 * handler. Thus this call can add a handler in a chain of 96 * wrapped handlers. 97 * 98 * @param handler 99 */ addHandler(Handler handler)100 public void addHandler(Handler handler) 101 { 102 Handler old = getHandler(); 103 if (old!=null && !(handler instanceof HandlerContainer)) 104 throw new IllegalArgumentException("Cannot add"); 105 setHandler(handler); 106 if (old!=null) 107 ((HandlerContainer)handler).addHandler(old); 108 } 109 110 removeHandler(Handler handler)111 public void removeHandler (Handler handler) 112 { 113 Handler old = getHandler(); 114 if (old!=null && (old instanceof HandlerContainer)) 115 ((HandlerContainer)old).removeHandler(handler); 116 else if (old!=null && handler.equals(old)) 117 setHandler(null); 118 else 119 throw new IllegalStateException("Cannot remove"); 120 } 121 122 123 /* ------------------------------------------------------------ */ 124 /* 125 * @see org.mortbay.thread.AbstractLifeCycle#doStart() 126 */ doStart()127 protected void doStart() throws Exception 128 { 129 if (_handler!=null) 130 _handler.start(); 131 super.doStart(); 132 } 133 134 /* ------------------------------------------------------------ */ 135 /* 136 * @see org.mortbay.thread.AbstractLifeCycle#doStop() 137 */ doStop()138 protected void doStop() throws Exception 139 { 140 super.doStop(); 141 if (_handler!=null) 142 _handler.stop(); 143 } 144 145 /* ------------------------------------------------------------ */ 146 /* 147 * @see org.mortbay.jetty.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 148 */ handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)149 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException 150 { 151 if (_handler!=null && isStarted()) 152 _handler.handle(target,request, response, dispatch); 153 } 154 155 156 /* ------------------------------------------------------------ */ setServer(Server server)157 public void setServer(Server server) 158 { 159 Server old_server=getServer(); 160 161 super.setServer(server); 162 163 Handler h=getHandler(); 164 if (h!=null) 165 h.setServer(server); 166 167 if (server!=null && server!=old_server) 168 server.getContainer().update(this, null,_handler, "handler"); 169 } 170 171 172 /* ------------------------------------------------------------ */ expandChildren(Object list, Class byClass)173 protected Object expandChildren(Object list, Class byClass) 174 { 175 return expandHandler(_handler,list,byClass); 176 } 177 178 179 } 180