1 20 package org.enhydra.barracuda.core.util.srv; 21 22 import java.awt.Container ; 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 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 41 49 public static Object findInstance (Class c, EventGateway gateway) { 50 return findInstance_upstream(c, gateway); 51 } 52 53 61 public static Object findInstance (Class c, EventGateway gateway, int searchDirection) { 62 if (searchDirection==DOWNSTREAM) return findInstance_downstream(c, gateway); 63 else return findInstance_upstream(c, gateway); 64 } 65 66 69 private static Object findInstance_upstream (Class c, Object parent) { 70 if (c==null || parent==null) return null; 72 if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+parent); 73 74 if (c.isInstance(parent)) return parent; 76 77 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 o = it.next(); 84 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 85 if (c.isInstance(o)) return o; 86 } 87 88 if (parent instanceof Container ) { 90 Object o[] = ((Container ) 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 Object 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 ) gramps = ((Container ) 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 110 private static Object findInstance_downstream (Class c, Object child) { 111 if (c==null || child==null) return null; 113 if (logger.isDebugEnabled()) logger.debug("Looking for instance of "+c+" in "+child); 114 115 if (c.isInstance(child)) return child; 117 118 if (child instanceof SimpleServiceProvider) { 121 Iterator it = ((SimpleServiceProvider) child).getSupportedServices().iterator(); 122 while (it.hasNext()) { 123 Object o = it.next(); 124 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 125 126 if (c.isInstance(o)) return o; 128 129 Object inst = findInstance_downstream(c, o); 131 if (inst!=null) return inst; 132 } 133 } 134 if (child instanceof EventGateway) { 136 Iterator it = ((EventGateway) child).getChildren().iterator(); 137 while (it.hasNext()) { 138 Object o = it.next(); 139 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 140 141 if (c.isInstance(o)) return o; 143 144 Object inst = findInstance_downstream(c, o); 146 if (inst!=null) return inst; 147 } 148 } 149 if (child instanceof List) { 151 Iterator it = ((List) child).iterator(); 152 while (it.hasNext()) { 153 Object o = it.next(); 154 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 155 156 if (c.isInstance(o)) return o; 158 159 Object inst = findInstance_downstream(c, o); 161 if (inst!=null) return inst; 162 } 163 } 164 if (child instanceof Map) { 166 Iterator it = ((Map) child).values().iterator(); 167 while (it.hasNext()) { 168 Object o = it.next(); 169 if (logger.isDebugEnabled()) logger.debug("Evaluating iterator item: "+o); 170 171 if (c.isInstance(o)) return o; 173 174 Object inst = findInstance_downstream(c, o); 176 if (inst!=null) return inst; 177 } 178 } 179 if (child instanceof Container ) { 181 Object o[] = ((Container ) 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 if (c.isInstance(o[i])) return o[i]; 187 188 Object inst = findInstance_downstream(c, o[i]); 190 if (inst!=null) return inst; 191 } 192 } 193 194 return null; 196 } 197 198 } 199 | Popular Tags |