1 18 package org.apache.beehive.netui.util.internal; 19 20 import org.apache.beehive.netui.util.logging.Logger; 21 22 import java.io.IOException ; 23 import java.io.BufferedReader ; 24 import java.io.InputStreamReader ; 25 import java.io.InputStream ; 26 import java.util.Enumeration ; 27 import java.util.ArrayList ; 28 import java.util.Map ; 29 import java.net.URL ; 30 31 32 39 public class DiscoveryUtils 40 { 41 private static final Logger _log = Logger.getInstance( DiscoveryUtils.class ); 42 43 44 47 public static ClassLoader getClassLoader() 48 { 49 try 50 { 51 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 52 if ( cl != null ) return cl; 53 } 54 catch ( SecurityException e ) 55 { 56 if ( _log.isDebugEnabled() ) 57 { 58 _log.debug( "Could not get thread context classloader.", e ); 59 } 60 } 61 62 if ( _log.isTraceEnabled() ) 63 { 64 _log.trace( "Can't use thread context classloader; using classloader for " + DiscoveryUtils.class.getName() ); 65 } 66 67 return DiscoveryUtils.class.getClassLoader(); 68 } 69 70 76 public static Class [] getImplementorClasses( Class interfaceType ) 77 { 78 String interfaceName = interfaceType.getName(); 79 ArrayList classes = new ArrayList (); 80 ClassLoader classLoader = getClassLoader(); 81 82 try 83 { 84 Enumeration e = classLoader.getResources( "META-INF/services/" + interfaceName ); 85 86 while ( e.hasMoreElements() ) 87 { 88 URL url = ( URL ) e.nextElement(); 89 90 if ( _log.isTraceEnabled() ) 91 { 92 _log.trace( "Found implementor entry for interface " + interfaceName + " at " + url ); 93 } 94 95 InputStream is = null; 96 String className = null; 97 98 try 99 { 100 is = url.openStream(); 101 BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ); 102 className = reader.readLine().trim(); 103 Class implementorClass = loadImplementorClass( className, interfaceType, classLoader ); 104 if ( implementorClass != null ) classes.add( implementorClass ); 105 } 106 catch ( IOException ioe ) 107 { 108 _log.error( "Could not read implementor class entry at + " + url ); 109 } 110 finally 111 { 112 if ( is != null ) is.close(); 113 } 114 } 115 } 116 catch ( IOException e ) 117 { 118 _log.error( "Could not discover implementors for " + interfaceName, e ); 119 } 120 121 return ( Class [] ) classes.toArray( new Class [0] ); 122 } 123 124 public static Object newImplementorInstance( String className, Class interfaceType ) 125 { 126 Class implementorClass = loadImplementorClass( className, interfaceType ); 127 128 if ( implementorClass != null ) 129 { 130 try 131 { 132 return implementorClass.newInstance(); 133 } 134 catch ( IllegalAccessException e ) 135 { 136 _log.error( "Could not instantiate " + className + " for interface " + interfaceType.getName(), e ); 137 } 138 catch ( InstantiationException e ) 139 { 140 _log.error( "Could not instantiate " + className + " for interface " + interfaceType.getName(), e ); 141 } 142 } 143 144 return null; 145 } 146 147 154 public static Class loadImplementorClass( String className, Class interfaceType ) 155 { 156 return loadImplementorClass( className, interfaceType, getClassLoader() ); 157 } 158 159 167 private static Class loadImplementorClass( String className, Class interfaceType, ClassLoader classLoader ) 168 { 169 try 170 { 171 if ( _log.isDebugEnabled() ) 172 { 173 _log.debug( "Trying to load implementor class for interface " + interfaceType.getName() 174 + ": " + className ); 175 } 176 177 Class implementorClass = classLoader.loadClass( className ); 178 179 if ( interfaceType.isAssignableFrom( implementorClass ) ) 180 { 181 return implementorClass; 182 } 183 else 184 { 185 _log.error( "Implementor class " + className + " does not implement interface " 186 + interfaceType.getName() ); 187 } 188 } 189 catch ( ClassNotFoundException cnfe ) 190 { 191 _log.error( "Could not find implementor class " + className + " for interface " + interfaceType.getName(), 195 cnfe ); 196 } 197 catch ( LinkageError le ) 198 { 199 _log.error( "Linkage error when loading implementor class " + className + " for interface " 200 + interfaceType.getName(), le ); 201 } 202 203 return null; 204 } 205 } 206 | Popular Tags |