1 20 package org.enhydra.barracuda.plankton.srv; 21 22 import java.awt.Container ; 23 import java.util.*; 24 25 import org.apache.log4j.*; 26 27 import org.enhydra.barracuda.plankton.data.*; 28 29 30 56 public class SimpleServiceFinder { 57 58 protected static final Logger logger = Logger.getLogger(SimpleServiceFinder.class.getName()); 59 60 62 public static final int UPSTREAM = 0; 64 public static final int DOWNSTREAM = 1; 65 66 74 public static Object findInstance (Class c, PData pdata) { 75 return findInstance_upstream(c, pdata); 76 } 77 78 86 public static Object findInstance (Class c, PData pdata, int searchDirection) { 87 if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, pdata); 88 else return findInstance_upstream(c, pdata); 89 } 90 91 99 public static Object findInstance (Class c, Container cont) { 100 return findInstance_upstream(c, cont); 101 } 102 103 111 public static Object findInstance (Class c, Container cont, int searchDirection) { 112 if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, cont); 113 else return findInstance_upstream(c, cont); 114 } 115 116 119 private static Object findInstance_upstream (Class c, Object parent) { 120 if (c==null || parent==null) return null; 122 if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+parent); 123 124 if (c.isInstance(parent)) return parent; 126 127 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 o = it.next(); 134 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 135 if (c.isInstance(o)) return o; 136 } 137 138 if (parent instanceof Container ) { 140 Object o[] = ((Container ) 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 Object gramps = null; 149 if (parent instanceof PData) gramps = ((PData) parent).getParent(); 150 else if (parent instanceof Container ) gramps = ((Container ) 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 159 private static Object findInstance_downstream (Class c, Object child) { 160 if (c==null || child==null) return null; 162 if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+child); 163 164 if (c.isInstance(child)) return child; 166 167 if (child instanceof SimpleServiceProvider) { 170 Iterator it = ((SimpleServiceProvider) child).getSupportedServices().iterator(); 171 while (it.hasNext()) { 172 Object o = it.next(); 173 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 174 175 if (c.isInstance(o)) return o; 177 178 Object inst = findInstance_downstream(c, o); 180 if (inst!=null) return inst; 181 } 182 } 183 if (child instanceof List) { 185 Iterator it = ((List) child).iterator(); 186 while (it.hasNext()) { 187 Object o = it.next(); 188 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 189 190 if (c.isInstance(o)) return o; 192 193 Object inst = findInstance_downstream(c, o); 195 if (inst!=null) return inst; 196 } 197 } 198 if (child instanceof Map) { 200 Iterator it = ((Map) child).values().iterator(); 201 while (it.hasNext()) { 202 Object o = it.next(); 203 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 204 205 if (c.isInstance(o)) return o; 207 208 Object inst = findInstance_downstream(c, o); 210 if (inst!=null) return inst; 211 } 212 } 213 if (child instanceof Container ) { 215 Object o[] = ((Container ) 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 if (c.isInstance(o[i])) return o[i]; 221 222 Object inst = findInstance_downstream(c, o[i]); 224 if (inst!=null) return inst; 225 } 226 } 227 228 return null; 230 } 231 232 } 233 | Popular Tags |