1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.LinkedHashMap ; 24 import java.util.Map ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.component.Component; 28 29 34 public class ComponentRegistry { 35 36 private Map idMap = new LinkedHashMap (); 37 private boolean runningStateInitialized = false; 38 private Registry registry; 39 40 41 protected ComponentRegistry(Registry reg){ 42 this.registry = reg; 43 } 44 54 public synchronized ComponentMBeanImpl registerComponent( 55 ComponentNameSpace name, 56 String description, 57 Component component, 58 boolean binding, 59 boolean service, 60 String [] sharedLibraries) { 61 ComponentMBeanImpl result = null; 62 if (!idMap.containsKey(name)) { 63 result = new ComponentMBeanImpl(registry.getContainer(), name, description, component, binding, service, sharedLibraries); 64 idMap.put(name, result); 65 } 66 return result; 67 } 68 69 73 public synchronized void start() throws JBIException{ 74 if (!setInitialRunningStateFromStart()) { 75 for(Iterator i = getComponents().iterator(); i.hasNext();) { 76 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 77 lcc.doStart(); 78 } 79 } 80 } 81 82 88 public synchronized void stop() throws JBIException { 89 for (Iterator i = getReverseComponents(). iterator();i.hasNext();) { 90 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 91 lcc.doStop(); 92 } 93 runningStateInitialized = false; 94 } 95 96 101 public synchronized void shutDown() throws JBIException { 102 for (Iterator i = getReverseComponents().iterator();i.hasNext();) { 103 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 104 lcc.persistRunningState(); 105 lcc.doShutDown(); 106 } 107 } 108 109 private Collection getReverseComponents() { 110 synchronized (idMap) { 111 ArrayList l = new ArrayList (idMap.values()); 112 Collections.reverse(l); 113 return l; 114 } 115 } 116 117 118 123 public synchronized void deregisterComponent(ComponentMBeanImpl component) { 124 idMap.remove(component.getComponentNameSpace()); 125 } 126 127 132 public ComponentMBeanImpl getComponent(ComponentNameSpace id) { 133 synchronized (idMap) { 134 return (ComponentMBeanImpl) idMap.get(id); 135 } 136 } 137 138 142 public Collection getComponents() { 143 synchronized (idMap) { 144 return new ArrayList (idMap.values()); 145 } 146 } 147 148 private boolean setInitialRunningStateFromStart() throws JBIException{ 149 boolean result = !runningStateInitialized; 150 if (!runningStateInitialized){ 151 runningStateInitialized = true; 152 for (Iterator i = getComponents().iterator();i.hasNext();) { 153 ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next(); 154 if(!lcc.isPojo() && !registry.isContainerEmbedded()){ 155 lcc.setInitialRunningState(); 156 }else { 157 lcc.doStart(); 158 } 159 } 160 } 161 return result; 162 } 163 } | Popular Tags |