KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > 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.5 2004/02/01 05:16:29 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.srv;
21
22 import java.awt.Container JavaDoc;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 import org.enhydra.barracuda.plankton.data.*;
28
29
30 /**
31  * <p>Given an EventGateway or Container heirarchy, this class
32  * provides a series of utilities to find a service (or a collection
33  * of services) by looking for an an instance of a class in a given
34  * heirarchy. A "service" is simply defined as an object class
35  * signature.
36  *
37  * <p>The basic search strategy is to start low and sweep upwards (UPSTREAM).
38  * What we do is look through everything in the Gateway/Container
39  * object (without diving deeper) and then we go upwards and repeat
40  * the process until we find a match or hit a root. This class is
41  * capable of searching Maps, Lists, ServiceProviders, and
42  * Containers</p>
43  *
44  * <p>This class also supports downward searches (DOWNSTREAM). In this case
45  * we evaluate children completely based on interface (SimpleServiceProviders
46  * first, then Lists, then Maps, and finally Containers). We go downwards
47  * and repeat the process until we find a match or run out of children.</p>
48  *
49  * <p>The key point of this is that the search strategy is totally
50  * separated from the container mechanism: the heirarchy simply
51  * provides the vehicle; we can customize the behavior with our
52  * particular implementation</p>
53  *
54  * TODO: are the classes in this package actually used by anyone? if not, can we just nuke them?
55  */

56 public class SimpleServiceFinder {
57
58     protected static final Logger logger = Logger.getLogger(SimpleServiceFinder.class.getName());
59
60     //public constants
61

62     //search directions
63
public static final int UPSTREAM = 0;
64     public static final int DOWNSTREAM = 1;
65     
66     /**
67      * Find an instance of a Class in an PData heirarchy. Search
68      * direction defaults to UPSTREAM
69      *
70      * @param c the class we're looking for
71      * @param pdata the entry point to the PData heirarchy
72      * @return the first instance of the specified class
73      */

74     public static Object JavaDoc findInstance (Class JavaDoc c, PData pdata) {
75         return findInstance_upstream(c, pdata);
76     }
77     
78     /**
79      * Find an instance of a Class in an PData heirarchy
80      *
81      * @param c the class we're looking for
82      * @param pdata the entry point to the PData heirarchy
83      * @param searchDirection the search direction
84      * @return the first instance of the specified class
85      */

86     public static Object JavaDoc findInstance (Class JavaDoc c, PData pdata, int searchDirection) {
87         if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, pdata);
88         else return findInstance_upstream(c, pdata);
89     }
90     
91     /**
92      * Find an instance of a Class in a Container heirarchy. Search
93      * direction defaults to UPSTREAM
94      *
95      * @param c the class we're looking for
96      * @param cont the entry point to the Container heirarchy
97      * @return the first instance of the specified class
98      */

99     public static Object JavaDoc findInstance (Class JavaDoc c, Container JavaDoc cont) {
100         return findInstance_upstream(c, cont);
101     }
102     
103     /**
104      * Find an instance of a Class in an Container heirarchy
105      *
106      * @param c the class we're looking for
107      * @param cont the entry point to the Container heirarchy
108      * @param searchDirection the search direction
109      * @return the first instance of the specified class
110      */

111     public static Object JavaDoc findInstance (Class JavaDoc c, Container JavaDoc cont, int searchDirection) {
112         if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, cont);
113         else return findInstance_upstream(c, cont);
114     }
115     
116     /**
117      * the private method to actually find the desired object
118      */

119     private static Object JavaDoc findInstance_upstream (Class JavaDoc c, Object JavaDoc parent) {
120         //eliminate the obvious
121
if (c==null || parent==null) return null;
122         if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+parent);
123
124         //see if the parent object matches
125
if (c.isInstance(parent)) return parent;
126
127         //now see if we can get an iterator from this object
128
Iterator it = null;
129         if (parent instanceof Map) it = ((Map) parent).values().iterator();
130         else if (parent instanceof List) it = ((List) parent).iterator();
131         else if (parent instanceof SimpleServiceProvider) it = ((SimpleServiceProvider) parent).getSupportedServices().iterator();
132         if (it!=null) while (it.hasNext()) {
133             Object JavaDoc o = it.next();
134             if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
135             if (c.isInstance(o)) return o;
136         }
137         
138         //next see if parent is a Container
139
if (parent instanceof Container JavaDoc) {
140             Object JavaDoc o[] = ((Container JavaDoc) parent).getComponents();
141             if (o!=null) for (int i=0, max=o.length; i<max; i++) {
142                 if (logger.isDebugEnabled()) logger.debug("Evaluating container item: "+o[i]);
143                 if (c.isInstance(o[i])) return o[i];
144             }
145         }
146         
147         //finally inspect the Parents
148
Object JavaDoc gramps = null;
149         if (parent instanceof PData) gramps = ((PData) parent).getParent();
150         else if (parent instanceof Container JavaDoc) gramps = ((Container JavaDoc) parent).getParent();
151         if (logger.isDebugEnabled()) logger.debug("Evaluating Gramps:"+gramps);
152         if (gramps==null) return null;
153         else return findInstance_upstream(c, gramps);
154     }
155
156     /**
157      * the private method to actually find the desired downstream object
158      */

159     private static Object JavaDoc findInstance_downstream (Class JavaDoc c, Object JavaDoc child) {
160         //eliminate the obvious
161
if (c==null || child==null) return null;
162         if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+child);
163
164         //see if the parent object matches
165
if (c.isInstance(child)) return child;
166
167         //search downstream
168
//...in SimpleServiceProviders
169
if (child instanceof SimpleServiceProvider) {
170             Iterator it = ((SimpleServiceProvider) child).getSupportedServices().iterator();
171             while (it.hasNext()) {
172                 Object JavaDoc o = it.next();
173                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
174                 
175                 //evaluate the object itself
176
if (c.isInstance(o)) return o;
177                 
178                 //search down the object branch
179
Object JavaDoc inst = findInstance_downstream(c, o);
180                 if (inst!=null) return inst;
181             }
182         }
183         //...in Lists
184
if (child instanceof List) {
185             Iterator it = ((List) child).iterator();
186             while (it.hasNext()) {
187                 Object JavaDoc o = it.next();
188                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
189                 
190                 //evaluate the object itself
191
if (c.isInstance(o)) return o;
192                 
193                 //search down the object branch
194
Object JavaDoc inst = findInstance_downstream(c, o);
195                 if (inst!=null) return inst;
196             }
197         }
198         //...in Maps
199
if (child instanceof Map) {
200             Iterator it = ((Map) child).values().iterator();
201             while (it.hasNext()) {
202                 Object JavaDoc o = it.next();
203                 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o);
204                 
205                 //evaluate the object itself
206
if (c.isInstance(o)) return o;
207                 
208                 //search down the object branch
209
Object JavaDoc inst = findInstance_downstream(c, o);
210                 if (inst!=null) return inst;
211             }
212         }
213         //...in Containers
214
if (child instanceof Container JavaDoc) {
215             Object JavaDoc o[] = ((Container JavaDoc) child).getComponents();
216             if (o!=null) for (int i=0, max=o.length; i<max; i++) {
217                 if (logger.isDebugEnabled()) logger.debug("Evaluating container item: "+o[i]);
218
219                 //evaluate the object itself
220
if (c.isInstance(o[i])) return o[i];
221
222                 //search down the object branch
223
Object JavaDoc inst = findInstance_downstream(c, o[i]);
224                 if (inst!=null) return inst;
225             }
226         }
227         
228         //if we didn't find anything return null;
229
return null;
230     }
231
232 }
233
Popular Tags