| 1 |
// Copyright 2003-2009, FreeHEP. |
| 2 |
package org.freehep.graphicsbase.util; |
| 3 |
|
| 4 |
import java.io.*; |
| 5 |
import java.net.*; |
| 6 |
import java.util.*; |
| 7 |
|
| 8 |
/** |
| 9 |
* This class does the same as sun.misc.Service, which may become public |
| 10 |
* in some java or javax package at some point. See Sun BUG# 4640520. |
| 11 |
* |
| 12 |
* @author Mark Donszelmann |
| 13 |
*/ |
| 14 |
public class Service { |
| 15 |
|
| 16 |
private Service() { |
| 17 |
} |
| 18 |
|
| 19 |
public static Collection<Object> providers(Class<?> service, ClassLoader loader) { |
| 20 |
List<Object> classList = new ArrayList<Object>(); |
| 21 |
List<String> nameSet = new ArrayList<String>(); |
| 22 |
String name = "META-INF/services/" + service.getName(); |
| 23 |
Enumeration<URL> services; |
| 24 |
try { |
| 25 |
services = (loader == null) ? |
| 26 |
ClassLoader.getSystemResources(name) : |
| 27 |
loader.getResources(name); |
| 28 |
} catch (IOException ioe) { |
| 29 |
System.err.println("Service: cannot load "+name); |
| 30 |
return classList; |
| 31 |
} |
| 32 |
|
| 33 |
while (services.hasMoreElements()) { |
| 34 |
URL url = services.nextElement(); |
| 35 |
// System.out.println(url); |
| 36 |
InputStream input = null; |
| 37 |
BufferedReader reader = null; |
| 38 |
try { |
| 39 |
input = url.openStream(); |
| 40 |
reader = new BufferedReader(new InputStreamReader(input, "utf-8")); |
| 41 |
String line = reader.readLine(); |
| 42 |
while (line != null) { |
| 43 |
int ci = line.indexOf('#'); |
| 44 |
if (ci >= 0) line = line.substring(0, ci); |
| 45 |
line = line.trim(); |
| 46 |
int si = line.indexOf(' '); |
| 47 |
if (si >= 0) line = line.substring(0, si); |
| 48 |
line = line.trim(); |
| 49 |
if (line.length() > 0) { |
| 50 |
if (!nameSet.contains(line)) nameSet.add(line); |
| 51 |
} |
| 52 |
line = reader.readLine(); |
| 53 |
} |
| 54 |
} catch (IOException ioe) { |
| 55 |
System.err.println("Service: problem with: "+url); |
| 56 |
} finally { |
| 57 |
try { |
| 58 |
if (input != null) input.close(); |
| 59 |
if (reader != null) reader.close(); |
| 60 |
} catch (IOException ioe2) { |
| 61 |
System.err.println("Service: problem with: "+url); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
Iterator<String> names = nameSet.iterator(); |
| 67 |
while (names.hasNext()) { |
| 68 |
String className = names.next(); |
| 69 |
try { |
| 70 |
classList.add(Class.forName(className, true, loader).newInstance()); |
| 71 |
} catch (ClassNotFoundException e) { |
| 72 |
System.err.println("Service: cannot find class: "+className); |
| 73 |
} catch (InstantiationException e) { |
| 74 |
System.err.println("Service: cannot instantiate: "+className); |
| 75 |
} catch (IllegalAccessException e) { |
| 76 |
System.err.println("Service: illegal access to: "+className); |
| 77 |
} catch (NoClassDefFoundError e) { |
| 78 |
System.err.println("Service: "+e+" for "+className); |
| 79 |
} catch (Exception e) { |
| 80 |
System.err.println("Service: exception for: "+className+" "+e); |
| 81 |
} |
| 82 |
} |
| 83 |
return classList; |
| 84 |
} |
| 85 |
|
| 86 |
public static Collection<Object> providers(Class<?> service) { |
| 87 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 88 |
return Service.providers(service, loader); |
| 89 |
} |
| 90 |
|
| 91 |
public static Collection<Object> installedProviders(Class<Object> service) { |
| 92 |
ClassLoader loader = ClassLoader.getSystemClassLoader(); |
| 93 |
ClassLoader previous = null; |
| 94 |
while (loader != null) { |
| 95 |
previous = loader; |
| 96 |
loader = loader.getParent(); |
| 97 |
} |
| 98 |
return providers(service, previous); |
| 99 |
} |
| 100 |
} |