1 18 package org.apache.batik.util; 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.Reader ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 43 public class Service { 44 45 static HashMap providerMap = new HashMap (); 47 48 59 public static synchronized Iterator providers(Class cls) { 60 String serviceFile = "META-INF/services/"+cls.getName(); 61 62 64 List l = (List )providerMap.get(serviceFile); 65 if (l != null) 66 return l.iterator(); 67 68 l = new ArrayList (); 69 providerMap.put(serviceFile, l); 70 71 ClassLoader cl = null; 72 try { 73 cl = cls.getClassLoader(); 74 } catch (SecurityException se) { 75 } 77 if (cl == null) cl = Service.class.getClassLoader(); 79 80 if (cl == null) return l.iterator(); 82 83 Enumeration e; 84 try { 85 e = cl.getResources(serviceFile); 86 } catch (IOException ioe) { 87 return l.iterator(); 88 } 89 90 while (e.hasMoreElements()) { 91 try { 92 URL u = (URL )e.nextElement(); 93 95 InputStream is = u.openStream(); 96 Reader r = new InputStreamReader (is, "UTF-8"); 97 BufferedReader br = new BufferedReader (r); 98 99 String line = br.readLine(); 100 while (line != null) { 101 try { 102 int idx = line.indexOf('#'); 104 if (idx != -1) 105 line = line.substring(0, idx); 106 107 line = line.trim(); 109 110 if (line.length() == 0) { 112 line = br.readLine(); 113 continue; 114 } 115 117 Object obj = cl.loadClass(line).newInstance(); 119 l.add(obj); 121 } catch (Exception ex) { 122 } 124 line = br.readLine(); 125 } 126 } catch (Exception ex) { 127 } 129 } 130 return l.iterator(); 131 } 132 } 133 | Popular Tags |