01: package org.objectweb.celtix.bus.bindings;
02:
03: import java.util.List;
04: import java.util.Map;
05: import java.util.concurrent.ConcurrentHashMap;
06:
07: import org.objectweb.celtix.Bus;
08: import org.objectweb.celtix.BusException;
09: 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(
24: "bindingFactories");
25:
26: List<ClassNamespaceMappingType> factoryMappings = ((ClassNamespaceMappingListType) obj)
27: .getMap();
28: for (ClassNamespaceMappingType mapping : factoryMappings) {
29: String classname = mapping.getClassname();
30: List<String> namespaceList = mapping.getNamespace();
31: String[] namespaces = new String[namespaceList.size()];
32: namespaceList.toArray(namespaces);
33: loadBindingFactory(classname, namespaces);
34: }
35:
36: }
37:
38: private void loadBindingFactory(String className,
39: String... namespaceURIs) throws BusException {
40: try {
41: Class<? extends BindingFactory> clazz = Class.forName(
42: className).asSubclass(BindingFactory.class);
43:
44: BindingFactory factory = clazz.newInstance();
45: factory.init(bus);
46:
47: for (String namespace : namespaceURIs) {
48: registerBinding(namespace, factory);
49: }
50: } catch (ClassNotFoundException cnfe) {
51: throw new BusException(cnfe);
52: } catch (InstantiationException ie) {
53: throw new BusException(ie);
54: } catch (IllegalAccessException iae) {
55: throw new BusException(iae);
56: }
57: }
58:
59: public void registerBinding(String name, BindingFactory factory)
60: throws BusException {
61: bindingFactories.put(name, factory);
62: }
63:
64: public void deregisterBinding(String name) throws BusException {
65: bindingFactories.remove(name);
66: }
67:
68: public BindingFactory getBindingFactory(String name)
69: throws BusException {
70: return bindingFactories.get(name);
71: }
72:
73: public void shutdown() {
74: //no nothing to do
75: }
76:
77: }
|