001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.framework;
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.HashSet;
022 import java.util.List;
023 import java.util.Map;
024 import java.util.Set;
025 import java.util.concurrent.ConcurrentHashMap;
026
027 import javax.jbi.JBIException;
028 import javax.jbi.management.DeploymentException;
029 import javax.management.JMException;
030 import javax.management.ObjectName;
031
032 import org.apache.commons.logging.Log;
033 import org.apache.commons.logging.LogFactory;
034 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment;
035 import org.apache.servicemix.jbi.deployment.ServiceAssembly;
036 import org.apache.servicemix.jbi.deployment.ServiceUnit;
037
038 /**
039 * Registry for Components
040 *
041 * @version $Revision: 564607 $
042 */
043 public class ServiceAssemblyRegistry {
044
045 private static final Log LOG = LogFactory.getLog(ServiceAssemblyRegistry.class);
046
047 private Map<String, ServiceAssemblyLifeCycle> serviceAssemblies = new ConcurrentHashMap<String, ServiceAssemblyLifeCycle>();
048
049 private Registry registry;
050
051 /**
052 * Constructor
053 * @param registry
054 */
055 public ServiceAssemblyRegistry(Registry registry) {
056 this.registry = registry;
057 }
058
059 /**
060 * Start all registered service assemblies
061 */
062 public void start() {
063 }
064
065 /**
066 * Stop service assembilies
067 */
068 public void stop() {
069 }
070
071 /**
072 * shutDown the service
073 */
074 public void shutDown() {
075 }
076
077 public ServiceAssemblyLifeCycle register(ServiceAssembly sa, String[] suKeys,
078 ServiceAssemblyEnvironment env) throws DeploymentException {
079 String saName = sa.getIdentification().getName();
080 if (!serviceAssemblies.containsKey(saName)) {
081 ServiceAssemblyLifeCycle salc = new ServiceAssemblyLifeCycle(sa, env, registry);
082 List<ServiceUnitLifeCycle> sus = new ArrayList<ServiceUnitLifeCycle>();
083 for (int i = 0; i < suKeys.length; i++) {
084 sus.add(registry.getServiceUnit(suKeys[i]));
085 }
086 salc.setServiceUnits((ServiceUnitLifeCycle[]) sus.toArray(new ServiceUnitLifeCycle[sus.size()]));
087 serviceAssemblies.put(saName, salc);
088 try {
089 ObjectName objectName = registry.getContainer().getManagementContext().createObjectName(salc);
090 registry.getContainer().getManagementContext().registerMBean(objectName, salc, ServiceAssemblyMBean.class);
091 } catch (JMException e) {
092 LOG.error("Could not register MBean for service assembly", e);
093 }
094 return salc;
095 }
096 return null;
097 }
098
099 public ServiceAssemblyLifeCycle register(ServiceAssembly sa, ServiceAssemblyEnvironment env) throws DeploymentException {
100 List<String> sus = new ArrayList<String>();
101 if (sa.getServiceUnits() != null) {
102 for (int i = 0; i < sa.getServiceUnits().length; i++) {
103 String suKey = registry.registerServiceUnit(sa.getServiceUnits()[i], sa.getIdentification().getName(), env
104 .getServiceUnitDirectory(sa.getServiceUnits()[i].getTarget().getComponentName(), sa.getServiceUnits()[i]
105 .getIdentification().getName()));
106 sus.add(suKey);
107 }
108 }
109 return register(sa, sus.toArray(new String[sus.size()]), env);
110 }
111
112 /**
113 * unregister a service assembly
114 * @param name
115 * @return true if successful
116 */
117 public boolean unregister(String name) {
118 ServiceAssemblyLifeCycle salc = serviceAssemblies.remove(name);
119 if (salc != null) {
120 try {
121 ServiceUnitLifeCycle[] sus = salc.getDeployedSUs();
122 if (sus != null) {
123 for (int i = 0; i < sus.length; i++) {
124 registry.unregisterServiceUnit(sus[i].getKey());
125 }
126 }
127 registry.getContainer().getManagementContext().unregisterMBean(salc);
128 } catch (JBIException e) {
129 LOG.error("Unable to unregister MBean for service assembly", e);
130 }
131 return true;
132 } else {
133 return false;
134 }
135 }
136
137 /**
138 * Get a named ServiceAssembly
139 * @param name
140 * @return the ServiceAssembly or null if it doesn't exist
141 */
142 public ServiceAssemblyLifeCycle getServiceAssembly(String saName) {
143 return serviceAssemblies.get(saName);
144 }
145
146 /**
147 * Returns a list of Service Assemblies deployed to the JBI enviroment.
148 *
149 * @return list of Service Assembly Name's.
150 */
151 public String[] getDeployedServiceAssemblies() {
152 String[] result = null;
153 Set<String> keys = serviceAssemblies.keySet();
154 result = new String[keys.size()];
155 keys.toArray(result);
156 return result;
157 }
158
159 /**
160 * Returns a list of Service Assemblies that contain SUs for the given component.
161 *
162 * @param componentName name of the component.
163 * @return list of Service Assembly names.
164 */
165 public String[] getDeployedServiceAssembliesForComponent(String componentName) {
166 String[] result = null;
167 // iterate through the service assembilies
168 Set<String> tmpList = new HashSet<String>();
169 for (ServiceAssemblyLifeCycle salc : serviceAssemblies.values()) {
170 ServiceUnit[] sus = salc.getServiceAssembly().getServiceUnits();
171 if (sus != null) {
172 for (int i = 0; i < sus.length; i++) {
173 if (sus[i].getTarget().getComponentName().equals(componentName)) {
174 tmpList.add(salc.getServiceAssembly().getIdentification().getName());
175 }
176 }
177 }
178 }
179 result = new String[tmpList.size()];
180 tmpList.toArray(result);
181 return result;
182 }
183
184 /**
185 * Returns a list of components(to which SUs are targeted for) in a Service Assembly.
186 *
187 * @param saName name of the service assembly.
188 * @return list of component names.
189 */
190 public String[] getComponentsForDeployedServiceAssembly(String saName) {
191 String[] result = null;
192 Set<String> tmpList = new HashSet<String>();
193 ServiceAssemblyLifeCycle sa = getServiceAssembly(saName);
194 if (sa != null) {
195 ServiceUnit[] sus = sa.getServiceAssembly().getServiceUnits();
196 if (sus != null) {
197 for (int i = 0; i < sus.length; i++) {
198 tmpList.add(sus[i].getTarget().getComponentName());
199 }
200 }
201 }
202 result = new String[tmpList.size()];
203 tmpList.toArray(result);
204 return result;
205 }
206
207 /**
208 * Returns a boolean value indicating whether the SU is currently deployed.
209 *
210 * @param componentName - name of component.
211 * @param suName - name of the Service Unit.
212 * @return boolean value indicating whether the SU is currently deployed.
213 */
214 public boolean isDeployedServiceUnit(String componentName, String suName) {
215 boolean result = false;
216 for (ServiceAssemblyLifeCycle salc : serviceAssemblies.values()) {
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 * Returns a list of service assemblies.
233 *
234 * @return list of service assemblies
235 */
236 public Collection<ServiceAssemblyLifeCycle> getServiceAssemblies() {
237 return serviceAssemblies.values();
238 }
239
240 }