KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > srv > SimpleServiceFinder


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: SimpleServiceFinder.java,v 1.14 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.srv;
21
22 import java.awt.Container JavaDoc;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 import org.enhydra.barracuda.core.event.*;
28 import org.enhydra.barracuda.plankton.data.*;
29 import org.enhydra.barracuda.plankton.srv.SimpleServiceProvider;
30
31 /**
32  * <p>Similar to org.enhydra.barracuda.plankton.srv.SimpleServiceProvider,
33  * except that it will also search EventGateway heirarchies.
34  */

35 public class SimpleServiceFinder extends org.enhydra.barracuda.plankton.srv.SimpleServiceFinder {
36
37     protected static final Logger logger = Logger.getLogger(SimpleServiceFinder.class.getName());
38
39     //public constants
40

41     /**
42      * Find an instance of a Class in an EventGateway heirarchy. Search
43      * direction defaults to UPSTREAM
44      *
45      * @param c the class we're looking for
46      * @param gateway the entry point to the EventGateway heirarchy
47      * @return the first instance of the specified class
48      */

49     public static Object JavaDoc findInstance (Class JavaDoc c, EventGateway gateway) {
50         return findInstance_upstream(c, gateway);
51     }
52     
53     /**
54      * Find an instance of a Class in an EventGateway heirarchy
55      *
56      * @param c the class we're looking for
57      * @param gateway the entry point to the EventGateway heirarchy
58      * @param searchDirection the search direction
59      * @return the first instance of the specified class
60      */

61     public static Object JavaDoc findInstance (Class JavaDoc c, EventGateway gateway, int searchDirection) {
62         if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, gateway);
63         else return findInstance_upstream(c, gateway);
64     }
65
66     /**
67      * the private method to actually find the desired object
68      */

69     private static Object JavaDoc findInstance_upstream (Class JavaDoc c, Object JavaDoc parent) {
70         //eliminate the obvious
71
if (c==null || parent==null) return null;
72         if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+parent);
73
74         //see if the parent object matches
75
if (c.isInstance(parent)) return parent;
76
77         //now see if we can get an iterator from this object
78
Iterator it = null;
79         if (parent instanceof Map) it = ((Map) parent).values().iterator();
80         else if (parent instanceof List) it = ((List) parent).iterator();
81         else if (parent instanceof SimpleServiceProvider) it = ((SimpleServiceProvider) parent).getSupportedServices().iterator();
82         if (it!=null) while (it.hasNext()) {
83             Object JavaDoc o = it.next();
84             if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
85             if (c.isInstance(o)) return o;
86         }
87         
88         //next see if parent is a Container
89
if (parent instanceof Container JavaDoc) {
90             Object JavaDoc o[] = ((Container JavaDoc) parent).getComponents();
91             if (o!=null) for (int i=0, max=o.length; i<max; i++) {
92                 if (logger.isDebugEnabled()) logger.debug("Evaluating container item: "+o[i]);
93                 if (c.isInstance(o[i])) return o[i];
94             }
95         }
96         
97         //finally inspect the Parents
98
Object JavaDoc gramps = null;
99         if (parent instanceof PData) gramps = ((PData) parent).getParent();
100         else if (parent instanceof EventGateway) gramps = ((EventGateway) parent).getParent();
101         else if (parent instanceof Container JavaDoc) gramps = ((Container JavaDoc) parent).getParent();
102         if (logger.isDebugEnabled()) logger.debug("Evaluating Gramps:"+gramps);
103         if (gramps==null) return null;
104         else return findInstance_upstream(c, gramps);
105     }
106
107     /**
108      * the private method to actually find the desired downstream object
109      */

110     private static Object JavaDoc findInstance_downstream (Class JavaDoc c, Object JavaDoc child) {
111         //eliminate the obvious
112
if (c==null || child==null) return null;
113         if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+child);
114
115         //see if the parent object matches
116
if (c.isInstance(child)) return child;
117
118         //search downstream
119
//...in SimpleServiceProviders
120
if (child instanceof SimpleServiceProvider) {
121             Iterator it = ((SimpleServiceProvider) child).getSupportedServices().iterator();
122             while (it.hasNext()) {
123                 Object JavaDoc o = it.next();
124                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
125                 
126                 //evaluate the object itself
127
if (c.isInstance(o)) return o;
128                 
129                 //search down the object branch
130
Object JavaDoc inst = findInstance_downstream(c, o);
131                 if (inst!=null) return inst;
132             }
133         }
134         //...in EventGateways
135
if (child instanceof EventGateway) {
136             Iterator it = ((EventGateway) child).getChildren().iterator();
137             while (it.hasNext()) {
138                 Object JavaDoc o = it.next();
139                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
140                 
141                 //evaluate the object itself
142
if (c.isInstance(o)) return o;
143                 
144                 //search down the object branch
145
Object JavaDoc inst = findInstance_downstream(c, o);
146                 if (inst!=null) return inst;
147             }
148         }
149         //...in Lists
150
if (child instanceof List) {
151             Iterator it = ((List) child).iterator();
152             while (it.hasNext()) {
153                 Object JavaDoc o = it.next();
154                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
155                 
156                 //evaluate the object itself
157
if (c.isInstance(o)) return o;
158                 
159                 //search down the object branch
160
Object JavaDoc inst = findInstance_downstream(c, o);
161                 if (inst!=null) return inst;
162             }
163         }
164         //...in Maps
165
if (child instanceof Map) {
166             Iterator it = ((Map) child).values().iterator();
167             while (it.hasNext()) {
168                 Object JavaDoc o = it.next();
169                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
170                 
171                 //evaluate the object itself
172
if (c.isInstance(o)) return o;
173                 
174                 //search down the object branch
175
Object JavaDoc inst = findInstance_downstream(c, o);
176                 if (inst!=null) return inst;
177             }
178         }
179         //...in Containers
180
if (child instanceof Container JavaDoc) {
181             Object JavaDoc o[] = ((Container JavaDoc) child).getComponents();
182             if (o!=null) for (int i=0, max=o.length; i<max; i++) {
183                 if (logger.isDebugEnabled()) logger.debug("Evaluating container item: "+o[i]);
184
185                 //evaluate the object itself
186
if (c.isInstance(o[i])) return o[i];
187
188                 //search down the object branch
189
Object JavaDoc inst = findInstance_downstream(c, o[i]);
190                 if (inst!=null) return inst;
191             }
192         }
193         
194         //if we didn't find anything return null;
195
return null;
196     }
197
198 }
199
Popular Tags