01: //========================================================================
02: //$Id: AbstractHandler.java,v 1.4 2005/11/11 22:55:39 gregwilkins Exp $
03: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
04: //------------------------------------------------------------------------
05: //Licensed under the Apache License, Version 2.0 (the "License");
06: //you may not use this file except in compliance with the License.
07: //You may obtain a copy of the License at
08: //http://www.apache.org/licenses/LICENSE-2.0
09: //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 org.mortbay.jetty.Handler;
19: import org.mortbay.jetty.HandlerContainer;
20: import org.mortbay.util.LazyList;
21:
22: /* ------------------------------------------------------------ */
23: /** Abstract Handler Container.
24: * This is the base class for handlers that may contain other handlers.
25: *
26: * @author gregw
27: *
28: */
29: public abstract class AbstractHandlerContainer extends AbstractHandler
30: implements HandlerContainer {
31: /* ------------------------------------------------------------ */
32: /**
33: *
34: */
35: public AbstractHandlerContainer() {
36: }
37:
38: /* ------------------------------------------------------------ */
39: public Handler[] getChildHandlers() {
40: Object list = expandChildren(null, null);
41: return (Handler[]) LazyList.toArray(list, Handler.class);
42: }
43:
44: /* ------------------------------------------------------------ */
45: public Handler[] getChildHandlersByClass(Class byclass) {
46: Object list = expandChildren(null, byclass);
47: return (Handler[]) LazyList.toArray(list, Handler.class);
48: }
49:
50: /* ------------------------------------------------------------ */
51: public Handler getChildHandlerByClass(Class byclass) {
52: // TODO this can be more efficient?
53: Object list = expandChildren(null, byclass);
54: if (list == null)
55: return null;
56: return (Handler) LazyList.get(list, 0);
57: }
58:
59: /* ------------------------------------------------------------ */
60: protected Object expandChildren(Object list, Class byClass) {
61: return list;
62: }
63:
64: /* ------------------------------------------------------------ */
65: protected Object expandHandler(Handler handler, Object list,
66: Class byClass) {
67: if (handler == null)
68: return list;
69:
70: if (handler != null
71: && (byClass == null || byClass.isAssignableFrom(handler
72: .getClass())))
73: list = LazyList.add(list, handler);
74:
75: if (handler instanceof AbstractHandlerContainer)
76: list = ((AbstractHandlerContainer) handler).expandChildren(
77: list, byClass);
78: else if (handler instanceof HandlerContainer) {
79: HandlerContainer container = (HandlerContainer) handler;
80: Handler[] handlers = byClass == null ? container
81: .getChildHandlers() : container
82: .getChildHandlersByClass(byClass);
83: list = LazyList.addArray(list, handlers);
84: }
85:
86: return list;
87: }
88:
89: }
|