1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.util.ArrayList ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.management.DeploymentException; 28 import javax.management.JMException ; 29 import javax.management.ObjectName ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment; 34 import org.apache.servicemix.jbi.deployment.ServiceAssembly; 35 import org.apache.servicemix.jbi.deployment.ServiceUnit; 36 37 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 38 39 44 public class ServiceAssemblyRegistry { 45 46 private static final Log log = LogFactory.getLog(ServiceAssemblyRegistry.class); 47 private Map serviceAssemblies = new ConcurrentHashMap(); 48 private Registry registry; 49 50 54 public ServiceAssemblyRegistry(Registry registry) { 55 this.registry = registry; 56 } 57 58 61 public void start() { 62 } 63 64 67 public void stop(){ 68 } 69 70 73 public void shutDown(){ 74 } 75 76 public ServiceAssemblyLifeCycle register(ServiceAssembly sa, 77 String [] suKeys, 78 ServiceAssemblyEnvironment env) throws DeploymentException { 79 String saName = sa.getIdentification().getName(); 80 if (!serviceAssemblies.containsKey(saName)) { 81 ServiceAssemblyLifeCycle salc = new ServiceAssemblyLifeCycle(sa, env, registry); 82 List sus = new ArrayList (); 83 for (int i = 0; i < suKeys.length; i++) { 84 sus.add(registry.getServiceUnit(suKeys[i])); 85 } 86 salc.setServiceUnits((ServiceUnitLifeCycle[]) sus.toArray(new ServiceUnitLifeCycle[sus.size()])); 87 serviceAssemblies.put(saName, salc); 88 try { 89 ObjectName objectName = registry.getContainer().getManagementContext().createObjectName(salc); 90 registry.getContainer().getManagementContext().registerMBean(objectName, salc, ServiceAssemblyMBean.class); 91 } catch (JMException e) { 92 log.error("Could not register MBean for service assembly", e); 93 } 94 return salc; 95 } 96 return null; 97 } 98 99 public ServiceAssemblyLifeCycle register(ServiceAssembly sa, ServiceAssemblyEnvironment env) throws DeploymentException { 100 List sus = new ArrayList (); 101 if (sa.getServiceUnits() != null) { 102 for (int i = 0; i < sa.getServiceUnits().length; i++) { 103 String suKey = registry.registerServiceUnit( 104 sa.getServiceUnits()[i], 105 sa.getIdentification().getName(), 106 env.getServiceUnitDirectory(sa.getServiceUnits()[i].getTarget().getComponentName(), 107 sa.getServiceUnits()[i].getIdentification().getName())); 108 sus.add(suKey); 109 } 110 } 111 return register(sa, 112 (String []) sus.toArray(new String [sus.size()]), 113 env); 114 } 115 116 121 public boolean unregister(String name) { 122 ServiceAssemblyLifeCycle salc = (ServiceAssemblyLifeCycle) serviceAssemblies.remove(name); 123 if (salc != null) { 124 try { 125 registry.getContainer().getManagementContext().unregisterMBean(salc); 126 } catch (JBIException e) { 127 log.error("Unable to unregister MBean for service assembly", e); 128 } 129 return true; 130 } else { 131 return false; 132 } 133 } 134 135 140 public ServiceAssemblyLifeCycle getServiceAssembly(String saName) { 141 return (ServiceAssemblyLifeCycle) serviceAssemblies.get(saName); 142 } 143 144 149 public String [] getDeployedServiceAssemblies() { 150 String [] result = null; 151 Set keys = serviceAssemblies.keySet(); 152 result = new String [keys.size()]; 153 keys.toArray(result); 154 return result; 155 } 156 157 163 public String [] getDeployedServiceAssembliesForComponent(String componentName) { 164 String [] result = null; 165 Set tmpList = new HashSet (); 167 for (Iterator iter = serviceAssemblies.values().iterator();iter.hasNext();) { 168 ServiceAssemblyLifeCycle salc = (ServiceAssemblyLifeCycle) iter.next(); 169 ServiceUnit[] sus = salc.getServiceAssembly().getServiceUnits(); 170 if (sus != null) { 171 for (int i = 0;i < sus.length;i++) { 172 if (sus[i].getTarget().getComponentName().equals(componentName)) { 173 tmpList.add(salc.getServiceAssembly().getIdentification().getName()); 174 } 175 } 176 } 177 } 178 result = new String [tmpList.size()]; 179 tmpList.toArray(result); 180 return result; 181 } 182 183 189 public String [] getComponentsForDeployedServiceAssembly(String saName) { 190 String [] result = null; 191 Set tmpList = new HashSet (); 192 ServiceAssemblyLifeCycle sa = getServiceAssembly(saName); 193 if (sa != null) { 194 ServiceUnit[] sus = sa.getServiceAssembly().getServiceUnits(); 195 if (sus != null) { 196 for (int i = 0;i < sus.length;i++) { 197 tmpList.add(sus[i].getTarget().getComponentName()); 198 } 199 } 200 } 201 result = new String [tmpList.size()]; 202 tmpList.toArray(result); 203 return result; 204 } 205 206 213 public boolean isDeployedServiceUnit(String componentName, String suName) { 214 boolean result = false; 215 for (Iterator iter = serviceAssemblies.values().iterator();iter.hasNext();) { 216 ServiceAssemblyLifeCycle salc = (ServiceAssemblyLifeCycle) iter.next(); 217 ServiceUnit[] sus = salc.getServiceAssembly().getServiceUnits(); 218 if (sus != null) { 219 for (int i = 0;i < sus.length;i++) { 220 if (sus[i].getTarget().getComponentName().equals(componentName) 221 && sus[i].getIdentification().getName().equals(suName)) { 222 result = true; 223 break; 224 } 225 } 226 } 227 } 228 return result; 229 } 230 231 } 232 | Popular Tags |