KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > ServiceAssemblyRegistry


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.framework;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.management.DeploymentException;
28 import javax.management.JMException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
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 /**
40  * Registry for Components
41  *
42  * @version $Revision: 426415 $
43  */

44 public class ServiceAssemblyRegistry {
45     
46     private static final Log log = LogFactory.getLog(ServiceAssemblyRegistry.class);
47     private Map JavaDoc serviceAssemblies = new ConcurrentHashMap();
48     private Registry registry;
49
50     /**
51      * Constructor
52      * @param registry
53      */

54     public ServiceAssemblyRegistry(Registry registry) {
55         this.registry = registry;
56     }
57
58     /**
59      * Start all registered service assemblies
60      */

61     public void start() {
62     }
63     
64     /**
65      * Stop service assembilies
66      */

67     public void stop(){
68     }
69     
70     /**
71      * shutDown the service
72      */

73     public void shutDown(){
74     }
75
76     public ServiceAssemblyLifeCycle register(ServiceAssembly sa,
77                                              String JavaDoc[] suKeys,
78                                              ServiceAssemblyEnvironment env) throws DeploymentException {
79         String JavaDoc saName = sa.getIdentification().getName();
80         if (!serviceAssemblies.containsKey(saName)) {
81             ServiceAssemblyLifeCycle salc = new ServiceAssemblyLifeCycle(sa, env, registry);
82             List JavaDoc sus = new ArrayList JavaDoc();
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 JavaDoc objectName = registry.getContainer().getManagementContext().createObjectName(salc);
90                 registry.getContainer().getManagementContext().registerMBean(objectName, salc, ServiceAssemblyMBean.class);
91             } catch (JMException JavaDoc 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 JavaDoc sus = new ArrayList JavaDoc();
101         if (sa.getServiceUnits() != null) {
102             for (int i = 0; i < sa.getServiceUnits().length; i++) {
103                 String JavaDoc 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 JavaDoc[]) sus.toArray(new String JavaDoc[sus.size()]),
113                         env);
114     }
115     
116     /**
117      * unregister a service assembly
118      * @param name
119      * @return true if successful
120      */

121     public boolean unregister(String JavaDoc 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     /**
136      * Get a named ServiceAssembly
137      * @param name
138      * @return the ServiceAssembly or null if it doesn't exist
139      */

140     public ServiceAssemblyLifeCycle getServiceAssembly(String JavaDoc saName) {
141         return (ServiceAssemblyLifeCycle) serviceAssemblies.get(saName);
142     }
143     
144    /**
145     * Returns a list of Service Assemblies deployed to the JBI enviroment.
146     *
147     * @return list of Service Assembly Name's.
148     */

149    public String JavaDoc[] getDeployedServiceAssemblies() {
150        String JavaDoc[] result = null;
151        Set JavaDoc keys = serviceAssemblies.keySet();
152        result = new String JavaDoc[keys.size()];
153        keys.toArray(result);
154        return result;
155    }
156    
157    /**
158     * Returns a list of Service Assemblies that contain SUs for the given component.
159     *
160     * @param componentName name of the component.
161     * @return list of Service Assembly names.
162     */

163    public String JavaDoc[] getDeployedServiceAssembliesForComponent(String JavaDoc componentName) {
164        String JavaDoc[] result = null;
165        // iterate through the service assembilies
166
Set JavaDoc tmpList = new HashSet JavaDoc();
167        for (Iterator JavaDoc 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 JavaDoc[tmpList.size()];
179        tmpList.toArray(result);
180        return result;
181    }
182
183    /**
184     * Returns a list of components(to which SUs are targeted for) in a Service Assembly.
185     *
186     * @param saName name of the service assembly.
187     * @return list of component names.
188     */

189    public String JavaDoc[] getComponentsForDeployedServiceAssembly(String JavaDoc saName) {
190        String JavaDoc[] result = null;
191        Set JavaDoc tmpList = new HashSet JavaDoc();
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 JavaDoc[tmpList.size()];
202        tmpList.toArray(result);
203        return result;
204    }
205
206    /**
207     * Returns a boolean value indicating whether the SU is currently deployed.
208     *
209     * @param componentName - name of component.
210     * @param suName - name of the Service Unit.
211     * @return boolean value indicating whether the SU is currently deployed.
212     */

213    public boolean isDeployedServiceUnit(String JavaDoc componentName, String JavaDoc suName) {
214        boolean result = false;
215        for (Iterator JavaDoc 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