1 //========================================================================
2 //$Id: AbstractHandler.java,v 1.4 2005/11/11 22:55:39 gregwilkins Exp $
3 //Copyright 2004-2005 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
19 import org.mortbay.jetty.Handler;
20 import org.mortbay.jetty.HandlerContainer;
21 import org.mortbay.util.LazyList;
22
23
24 /* ------------------------------------------------------------ */
25 /** Abstract Handler Container.
26 * This is the base class for handlers that may contain other handlers.
27 *
28 * @author gregw
29 *
30 */
31 public abstract class AbstractHandlerContainer extends AbstractHandler implements HandlerContainer
32 {
33 /* ------------------------------------------------------------ */
34 /**
35 *
36 */
37 public AbstractHandlerContainer()
38 {
39 }
40
41 /* ------------------------------------------------------------ */
42 public Handler[] getChildHandlers()
43 {
44 Object list = expandChildren(null,null);
45 return (Handler[])LazyList.toArray(list, Handler.class);
46 }
47
48 /* ------------------------------------------------------------ */
49 public Handler[] getChildHandlersByClass(Class byclass)
50 {
51 Object list = expandChildren(null,byclass);
52 return (Handler[])LazyList.toArray(list, Handler.class);
53 }
54
55 /* ------------------------------------------------------------ */
56 public Handler getChildHandlerByClass(Class byclass)
57 {
58 // TODO this can be more efficient?
59 Object list = expandChildren(null,byclass);
60 if (list==null)
61 return null;
62 return (Handler)LazyList.get(list, 0);
63 }
64
65 /* ------------------------------------------------------------ */
66 protected Object expandChildren(Object list, Class byClass)
67 {
68 return list;
69 }
70
71 /* ------------------------------------------------------------ */
72 protected Object expandHandler(Handler handler, Object list, Class byClass)
73 {
74 if (handler==null)
75 return list;
76
77 if (handler!=null && (byClass==null || byClass.isAssignableFrom(handler.getClass())))
78 list=LazyList.add(list, handler);
79
80 if (handler instanceof AbstractHandlerContainer)
81 list=((AbstractHandlerContainer)handler).expandChildren(list, byClass);
82 else if (handler instanceof HandlerContainer)
83 {
84 HandlerContainer container = (HandlerContainer)handler;
85 Handler[] handlers=byClass==null?container.getChildHandlers():container.getChildHandlersByClass(byClass);
86 list=LazyList.addArray(list, handlers);
87 }
88
89 return list;
90 }
91
92
93 }