1 package org.objectweb.celtix.bus.bindings; 2 3 import java.util.List ; 4 import java.util.Map ; 5 import java.util.concurrent.ConcurrentHashMap ; 6 7 import org.objectweb.celtix.Bus; 8 import org.objectweb.celtix.BusException; 9 import org.objectweb.celtix.bindings.BindingFactory; 10 import org.objectweb.celtix.bindings.BindingManager; 11 import org.objectweb.celtix.configuration.types.ClassNamespaceMappingListType; 12 import org.objectweb.celtix.configuration.types.ClassNamespaceMappingType; 13 14 public final class BindingManagerImpl implements BindingManager { 15 16 final Map <String , BindingFactory> bindingFactories; 17 private final Bus bus; 18 19 public BindingManagerImpl(Bus b) throws BusException { 20 bindingFactories = new ConcurrentHashMap <String , BindingFactory>(); 21 bus = b; 22 23 Object obj = bus.getConfiguration().getObject("bindingFactories"); 24 25 List <ClassNamespaceMappingType> factoryMappings = ((ClassNamespaceMappingListType)obj).getMap(); 26 for (ClassNamespaceMappingType mapping : factoryMappings) { 27 String classname = mapping.getClassname(); 28 List <String > namespaceList = mapping.getNamespace(); 29 String [] namespaces = new String [namespaceList.size()]; 30 namespaceList.toArray(namespaces); 31 loadBindingFactory(classname, namespaces); 32 } 33 34 } 35 36 private void loadBindingFactory(String className, String ...namespaceURIs) throws BusException { 37 try { 38 Class <? extends BindingFactory> clazz = 39 Class.forName(className).asSubclass(BindingFactory.class); 40 41 BindingFactory factory = clazz.newInstance(); 42 factory.init(bus); 43 44 for (String namespace : namespaceURIs) { 45 registerBinding(namespace, factory); 46 } 47 } catch (ClassNotFoundException cnfe) { 48 throw new BusException(cnfe); 49 } catch (InstantiationException ie) { 50 throw new BusException(ie); 51 } catch (IllegalAccessException iae) { 52 throw new BusException(iae); 53 } 54 } 55 56 public void registerBinding(String name, 57 BindingFactory factory) throws BusException { 58 bindingFactories.put(name, factory); 59 } 60 61 public void deregisterBinding(String name) throws BusException { 62 bindingFactories.remove(name); 63 } 64 65 public BindingFactory getBindingFactory(String name) throws BusException { 66 return bindingFactories.get(name); 67 } 68 69 public void shutdown() { 70 } 72 73 } 74 | Popular Tags |