1 package org.objectweb.celtix; 2 3 4 import java.io.BufferedReader ; 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.util.Map ; 8 import org.objectweb.celtix.configuration.CommandLineOption; 9 10 13 public final class BusFactory { 14 15 private static final CommandLineOption BUS_CLASS_OPT; 16 private static final String DEFAULT_BUS_CLASSNAME = "org.objectweb.celtix.bus.busimpl.CeltixBus"; 17 private static BusFactory theInstance; 18 19 static { 20 BUS_CLASS_OPT = new CommandLineOption("-BUSclass"); 21 } 22 23 private BusFactory() { 24 } 25 26 public static BusFactory getInstance() { 27 synchronized (BusFactory.class) { 28 if (null == theInstance) { 29 theInstance = new BusFactory(); 30 } 31 } 32 return theInstance; 33 } 34 35 public Bus getBus(String [] args, 36 Map <String , Object > properties, 37 ClassLoader classLoader) throws BusException { 38 39 42 String busClass = getBusClass(args, properties, classLoader); 43 44 46 return createBus(busClass, selectClassLoader(classLoader), args, properties); 47 } 48 49 private ClassLoader selectClassLoader(ClassLoader classLoader) { 50 ClassLoader ret = classLoader; 51 if (null == classLoader) { 52 ret = BusFactory.class.getClassLoader(); 53 } 54 return ret; 55 } 56 57 private static Bus createBus(String className, 58 ClassLoader classLoader, 59 String [] args, 60 Map <String , Object > properties) throws BusException { 61 62 Class <? extends Bus> busClass; 63 try { 64 busClass = Class.forName(className, true, classLoader).asSubclass(Bus.class); 65 Bus bus = busClass.newInstance(); 66 bus.initialize(args, properties); 67 return bus; 68 } catch (Exception ex) { 69 throw new BusException(ex); 70 } 71 } 72 73 String getBusClass(String [] args, Map <String , Object > properties, ClassLoader classLoader) 74 throws BusException { 75 76 String busClass = null; 77 78 BUS_CLASS_OPT.initialize(args); 80 busClass = (String )BUS_CLASS_OPT.getValue(); 81 if (isValidBusClass(busClass)) { 82 return busClass; 83 } 84 85 busClass = (String )properties.get(Bus.BUS_CLASS_PROPERTY); 87 if (isValidBusClass(busClass)) { 88 return busClass; 89 } 90 91 busClass = System.getProperty(Bus.BUS_CLASS_PROPERTY); 93 if (isValidBusClass(busClass)) { 94 return busClass; 95 } 96 97 try { 98 String serviceId = "META-INF/services/" + Bus.BUS_CLASS_PROPERTY; 100 InputStream is = null; 101 102 if (classLoader == null) { 103 classLoader = Thread.currentThread().getContextClassLoader(); 104 } 105 106 if (classLoader == null) { 107 is = ClassLoader.getSystemResourceAsStream(serviceId); 108 } else { 109 is = classLoader.getResourceAsStream(serviceId); 110 } 111 if (is != null) { 112 BufferedReader rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 113 busClass = rd.readLine(); 114 rd.close(); 115 } 116 if (isValidBusClass(busClass)) { 117 return busClass; 118 } 119 120 busClass = DEFAULT_BUS_CLASSNAME; 122 return busClass; 123 } catch (Exception ex) { 124 throw new BusException(ex); 125 } 126 } 127 128 private boolean isValidBusClass(String busClassName) { 129 return busClassName != null && !"".equals(busClassName); 130 } 131 132 } 133 | Popular Tags |