1 package hudson.util; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.InputStreamReader ; 6 import java.net.URL ; 7 import java.util.Collection ; 8 import java.util.Enumeration ; 9 import java.util.logging.Level ; 10 import java.util.logging.Logger ; 11 12 17 public class Service { 18 22 public static <T> void load(Class <T> spi, ClassLoader cl, Collection <Class <? extends T>> result) { 23 try { 24 Enumeration <URL > e = cl.getResources("META-INF/services/" + spi.getName()); 25 while(e.hasMoreElements()) { 26 BufferedReader r = null; 27 URL url = e.nextElement(); 28 try { 29 r = new BufferedReader (new InputStreamReader (url.openStream(),"UTF-8")); 30 String line; 31 while((line=r.readLine())!=null) { 32 if(line.startsWith("#")) 33 continue; line = line.trim(); 35 if(line.length()==0) 36 continue; 38 try { 39 result.add(cl.loadClass(line).asSubclass(spi)); 40 } catch (ClassNotFoundException x) { 41 LOGGER.log(Level.WARNING, "Failed to load "+line, x); 42 } 43 } 44 } catch (IOException x) { 45 LOGGER.log(Level.WARNING, "Failed to load "+url, x); 46 } finally { 47 r.close(); 48 } 49 } 50 } catch (IOException x) { 51 LOGGER.log(Level.WARNING, "Failed to look up service providers for "+spi, x); 52 } 53 } 54 55 private static final Logger LOGGER = Logger.getLogger(Service.class.getName()); 56 } 57 | Popular Tags |