001: //========================================================================
002: //$Id: HandlerCollection.java,v 1.5 2005/11/11 22:55:39 gregwilkins Exp $
003: //Copyright 2004-2005 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.jetty.EofException;
025: import org.mortbay.jetty.Handler;
026: import org.mortbay.jetty.Server;
027: import org.mortbay.util.LazyList;
028: import org.mortbay.util.MultiException;
029:
030: /* ------------------------------------------------------------ */
031: /** A collection of handlers.
032: * For each request, all handler are called, regardless of
033: * the response status or exceptions.
034: *
035: * @author gregw
036: * @org.apache.xbean.XBean
037: */
038: public class HandlerCollection extends AbstractHandlerContainer {
039: private Handler[] _handlers;
040:
041: /* ------------------------------------------------------------ */
042: public HandlerCollection() {
043: super ();
044: }
045:
046: /* ------------------------------------------------------------ */
047: /**
048: * @return Returns the handlers.
049: */
050: public Handler[] getHandlers() {
051: return _handlers;
052: }
053:
054: /* ------------------------------------------------------------ */
055: /**
056: *
057: * @param handlers The handlers to set.
058: */
059: public void setHandlers(Handler[] handlers) {
060: Handler[] old_handlers = _handlers == null ? null
061: : (Handler[]) _handlers.clone();
062:
063: if (getServer() != null)
064: getServer().getContainer().update(this , old_handlers,
065: handlers, "handler");
066:
067: Server server = getServer();
068: MultiException mex = new MultiException();
069: for (int i = 0; handlers != null && i < handlers.length; i++) {
070: if (handlers[i].getServer() != server)
071: handlers[i].setServer(server);
072: }
073:
074: // quasi atomic.... so don't go doing this under load on a SMP system.
075: _handlers = handlers;
076:
077: for (int i = 0; old_handlers != null && i < old_handlers.length; i++) {
078: if (old_handlers[i] != null) {
079: try {
080: if (old_handlers[i].isStarted())
081: old_handlers[i].stop();
082: } catch (Throwable e) {
083: mex.add(e);
084: }
085: }
086: }
087:
088: mex.ifExceptionThrowRuntime();
089: }
090:
091: /* ------------------------------------------------------------ */
092: /*
093: * @see org.mortbay.jetty.EventHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
094: */
095: public void handle(String target, HttpServletRequest request,
096: HttpServletResponse response, int dispatch)
097: throws IOException, ServletException {
098: if (_handlers != null && isStarted()) {
099: MultiException mex = null;
100:
101: for (int i = 0; i < _handlers.length; i++) {
102: try {
103: _handlers[i].handle(target, request, response,
104: dispatch);
105: } catch (EofException e) {
106: throw e;
107: } catch (RuntimeException e) {
108: throw e;
109: } catch (Exception e) {
110: if (mex == null)
111: mex = new MultiException();
112: mex.add(e);
113: }
114: }
115: if (mex != null)
116: throw new ServletException(mex);
117:
118: }
119: }
120:
121: /* ------------------------------------------------------------ */
122: /*
123: * @see org.mortbay.jetty.handler.AbstractHandler#doStart()
124: */
125: protected void doStart() throws Exception {
126: MultiException mex = new MultiException();
127: if (_handlers != null) {
128: for (int i = 0; i < _handlers.length; i++)
129: try {
130: _handlers[i].start();
131: } catch (Throwable e) {
132: mex.add(e);
133: }
134: }
135: super .doStart();
136: mex.ifExceptionThrow();
137: }
138:
139: /* ------------------------------------------------------------ */
140: /*
141: * @see org.mortbay.jetty.handler.AbstractHandler#doStop()
142: */
143: protected void doStop() throws Exception {
144: MultiException mex = new MultiException();
145: try {
146: super .doStop();
147: } catch (Throwable e) {
148: mex.add(e);
149: }
150: if (_handlers != null) {
151: for (int i = _handlers.length; i-- > 0;)
152: try {
153: _handlers[i].stop();
154: } catch (Throwable e) {
155: mex.add(e);
156: }
157: }
158: mex.ifExceptionThrow();
159: }
160:
161: /* ------------------------------------------------------------ */
162: public void setServer(Server server) {
163: Server old_server = getServer();
164:
165: super .setServer(server);
166:
167: Handler[] h = getHandlers();
168: for (int i = 0; h != null && i < h.length; i++)
169: h[i].setServer(server);
170:
171: if (server != null && server != old_server)
172: server.getContainer().update(this , null, _handlers,
173: "handler");
174:
175: }
176:
177: /* ------------------------------------------------------------ */
178: /* Add a handler.
179: * This implementation adds the passed handler to the end of the existing collection of handlers.
180: * @see org.mortbay.jetty.HandlerContainer#addHandler(org.mortbay.jetty.Handler)
181: */
182: public void addHandler(Handler handler) {
183: setHandlers((Handler[]) LazyList.addToArray(getHandlers(),
184: handler, Handler.class));
185: }
186:
187: /* ------------------------------------------------------------ */
188: public void removeHandler(Handler handler) {
189: Handler[] handlers = getHandlers();
190:
191: if (handlers != null && handlers.length > 0)
192: setHandlers((Handler[]) LazyList.removeFromArray(handlers,
193: handler));
194: }
195:
196: /* ------------------------------------------------------------ */
197: protected Object expandChildren(Object list, Class byClass) {
198: Handler[] handlers = getHandlers();
199: for (int i = 0; handlers != null && i < handlers.length; i++)
200: list = expandHandler(handlers[i], list, byClass);
201: return list;
202: }
203:
204: }
|