1 17 package org.apache.geronimo.kernel; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 24 import org.apache.geronimo.kernel.basic.BasicKernelFactory; 25 26 29 public abstract class KernelFactory { 30 public static final String KERNEL_FACTORY_KEY = KernelFactory.class.getName(); 31 32 public static KernelFactory newInstance() { 33 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 34 if (classLoader == null) { 35 classLoader = KernelFactory.class.getClassLoader(); 36 } 37 38 try { 40 String kernelFactoryName = System.getProperty(KERNEL_FACTORY_KEY); 41 if (kernelFactoryName != null) { 42 return createKernelFactory(kernelFactoryName, classLoader); 43 } 44 } catch (SecurityException se) { 45 } 46 47 String serviceId = "META-INF/services/" + KERNEL_FACTORY_KEY; 49 InputStream inputStream = null; 50 try { 51 inputStream = classLoader.getResourceAsStream(serviceId); 52 if (inputStream != null) { 53 BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream, "UTF-8")); 54 String kernelFactoryName = reader.readLine(); 55 reader.close(); 56 57 if (kernelFactoryName != null && kernelFactoryName.length() > 0) { 58 return createKernelFactory(kernelFactoryName, classLoader); 59 } 60 } 61 } catch (Exception ignored) { 62 } finally { 63 if (inputStream != null) { 64 try { 65 inputStream.close(); 66 } catch (IOException ignored) { 67 } 68 inputStream = null; 69 } 70 } 71 72 return new BasicKernelFactory(); 74 } 75 76 private static KernelFactory createKernelFactory(String className, ClassLoader classLoader) { 77 try { 78 return (KernelFactory) classLoader.loadClass(className).newInstance(); 79 } catch (ClassCastException e) { 80 throw new KernelFactoryError("Kernel factory class does not implement KernelFactory: " + className); 81 } catch (ClassNotFoundException e) { 82 throw new KernelFactoryError("Kernel factory class not found: " + className); 83 } catch (Exception e) { 84 throw new KernelFactoryError("Unable to instantiate kernel factory class: " + className, e); 85 } 86 } 87 88 public abstract Kernel createKernel(String kernelName); 89 } 90 | Popular Tags |