1 57 58 package org.apache.commons.discovery.tools; 59 60 import java.util.Enumeration ; 61 import java.util.Vector ; 62 63 import org.apache.commons.discovery.ResourceClass; 64 import org.apache.commons.discovery.listeners.GatherResourceClassesListener; 65 import org.apache.commons.discovery.resource.ClassLoaders; 66 import org.apache.commons.discovery.resource.classes.DiscoverClasses; 67 import org.apache.commons.discovery.resource.names.DiscoverServiceNames; 68 69 70 92 public class Service 93 { 94 96 protected Service() { 97 } 98 99 107 public static Enumeration providers(Class spiClass) { 108 return providers(new SPInterface(spiClass), null); 109 } 110 111 118 public static Enumeration providers(final SPInterface spi, 119 ClassLoaders loaders) 120 { 121 if (loaders == null) { 122 loaders = ClassLoaders.getAppLoaders(spi.getSPClass(), 123 Service.class, 124 true); 125 } 126 127 128 GatherResourceClassesListener listener = new GatherResourceClassesListener(); 129 130 DiscoverClasses classDiscovery = new DiscoverClasses(loaders); 131 classDiscovery.setListener(listener); 132 133 DiscoverServiceNames discoverServices = new DiscoverServiceNames(loaders); 134 discoverServices.setListener(classDiscovery); 135 discoverServices.find(spi.getSPName()); 136 137 final Vector results = listener.getResourceClasses(); 138 139 return new Enumeration () { 140 private Object obj = null; 141 private int idx = 0; 142 143 public boolean hasMoreElements() { 144 if (obj == null) { 145 obj = getNextElement(); 146 } 147 return obj != null; 148 } 149 150 public Object nextElement() { 151 Object o = obj; 152 obj = null; 153 return o; 154 } 155 156 private Object getNextElement() { 157 while (idx < results.size()) { 158 try { 159 return spi.newInstance(((ResourceClass)results.get(idx++)).loadClass()); 160 } catch (Exception e) { 161 } 163 } 164 return null; 165 } 166 }; 167 } 168 } 169 | Popular Tags |