KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.component.Component;
28 import javax.jbi.component.ComponentContext;
29 import javax.jbi.management.DeploymentException;
30 import javax.jbi.servicedesc.ServiceEndpoint;
31 import javax.management.JMException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.resource.spi.work.Work JavaDoc;
34 import javax.resource.spi.work.WorkException JavaDoc;
35 import javax.xml.namespace.QName JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.servicemix.jbi.container.ActivationSpec;
40 import org.apache.servicemix.jbi.container.EnvironmentContext;
41 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment;
42 import org.apache.servicemix.jbi.container.SubscriptionSpec;
43 import org.apache.servicemix.jbi.deployment.ServiceAssembly;
44 import org.apache.servicemix.jbi.deployment.ServiceUnit;
45 import org.apache.servicemix.jbi.management.BaseSystemService;
46 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
47 import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint;
48 import org.apache.servicemix.jbi.servicedesc.DynamicEndpoint;
49 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
50 import org.apache.servicemix.jbi.util.DOMUtil;
51 import org.w3c.dom.Document JavaDoc;
52 import org.w3c.dom.DocumentFragment JavaDoc;
53 import org.w3c.dom.Element JavaDoc;
54 import org.w3c.dom.Node JavaDoc;
55 import org.w3c.dom.NodeList JavaDoc;
56
57 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
58 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
59
60 /**
61  * Registry - state infomation including running state, SA's deployed etc.
62  *
63  * @version $Revision: 437763 $
64  */

65 public class Registry extends BaseSystemService implements RegistryMBean {
66     
67     private static final Log log = LogFactory.getLog(Registry.class);
68     private ComponentRegistry componentRegistry;
69     private EndpointRegistry endpointRegistry;
70     private SubscriptionRegistry subscriptionRegistry;
71     private ServiceAssemblyRegistry serviceAssemblyRegistry;
72     private Map JavaDoc sharedLibraries;
73     private Map JavaDoc serviceUnits;
74     private List JavaDoc pendingAssemblies;
75     private List JavaDoc pendingComponents;
76
77     /**
78      * Constructor
79      */

80     public Registry() {
81         this.componentRegistry = new ComponentRegistry(this);
82         this.endpointRegistry = new EndpointRegistry(this);
83         this.subscriptionRegistry = new SubscriptionRegistry(this);
84         this.serviceAssemblyRegistry = new ServiceAssemblyRegistry(this);
85         this.serviceUnits = new ConcurrentHashMap();
86         this.pendingAssemblies = new CopyOnWriteArrayList();
87         this.sharedLibraries = new ConcurrentHashMap();
88         this.pendingComponents = new CopyOnWriteArrayList();
89     }
90     
91     /**
92      * Get the description
93      * @return description
94      */

95     public String JavaDoc getDescription(){
96         return "Registry of Components/SU's and Endpoints";
97     }
98
99     protected Class JavaDoc getServiceMBean() {
100         return RegistryMBean.class;
101     }
102
103     
104     public ComponentRegistry getComponentRegistry() {
105         return componentRegistry;
106     }
107
108     public EndpointRegistry getEndpointRegistry() {
109         return endpointRegistry;
110     }
111
112     /**
113      * start brokering
114      *
115      * @throws JBIException
116      */

117     public void start() throws JBIException {
118
119         componentRegistry.start();
120         serviceAssemblyRegistry.start();
121         super.start();
122     }
123
124     /**
125      * stop brokering
126      *
127      * @throws JBIException
128      */

129     public void stop() throws JBIException {
130         serviceAssemblyRegistry.stop();
131         componentRegistry.stop();
132         super.stop();
133     }
134
135     /**
136      * shutdown all Components
137      *
138      * @throws JBIException
139      */

140     public void shutDown() throws JBIException {
141         serviceAssemblyRegistry.shutDown();
142         componentRegistry.shutDown();
143         super.shutDown();
144         container.getManagementContext().unregisterMBean(this);
145     }
146     
147     /**
148      * @return the EnvironmentContext
149      */

150     protected EnvironmentContext getEnvironmentContext(){
151         return container.getEnvironmentContext();
152     }
153     
154     /**
155      * @return true if the container is embedded
156      */

157     protected boolean isContainerEmbedded(){
158         return container.isEmbedded();
159     }
160
161     protected InternalEndpoint matchEndpointByName(ServiceEndpoint[] endpoints, String JavaDoc endpointName) {
162         InternalEndpoint result = null;
163         if (endpoints != null && endpointName != null && endpointName.length() > 0) {
164             for (int i = 0;i < endpoints.length;i++) {
165                 if (endpoints[i].getEndpointName().equals(endpointName)) {
166                     result = (InternalEndpoint) endpoints[i];
167                     break;
168                 }
169             }
170         }
171         return result;
172     }
173
174     /**
175      * @param context
176      * @param serviceName
177      * @param endpointName
178      * @return EndPointReference
179      * @throws JBIException
180      */

181     public ServiceEndpoint activateEndpoint(ComponentContextImpl context,
182                                             QName JavaDoc serviceName,
183                                             String JavaDoc endpointName) throws JBIException {
184         InternalEndpoint result = endpointRegistry.registerInternalEndpoint(context, serviceName, endpointName);
185         return result;
186     }
187
188     public ServiceEndpoint[] getEndpointsForComponent(ComponentNameSpace cns) {
189         return endpointRegistry.getEndpointsForComponent(cns);
190     }
191     
192     /**
193      * @param interfaceName qualified name
194      * @return an array of available endpoints for the specified interface name;
195      */

196     public ServiceEndpoint[] getEndpointsForInterface(QName JavaDoc interfaceName) {
197         return endpointRegistry.getEndpointsForInterface(interfaceName);
198     }
199
200     /**
201      * @param provider
202      * @param serviceEndpoint
203      */

204     public void deactivateEndpoint(ComponentContext provider, InternalEndpoint serviceEndpoint) {
205         endpointRegistry.unregisterInternalEndpoint(provider, serviceEndpoint);
206     }
207
208     /**
209      * Retrieve the service description metadata for the specified endpoint.
210      * <p>
211      * Note that the result can use either the WSDL 1.1 or WSDL 2.0 description language.
212      *
213      * @param endpoint endpoint reference; must be non-null.
214      * @return metadata describing endpoint, or <code>null</code> if metadata is unavailable.
215      * @throws JBIException invalid endpoint reference.
216      */

217     public Document JavaDoc getEndpointDescriptor(ServiceEndpoint endpoint) throws JBIException {
218         if (endpoint instanceof AbstractServiceEndpoint == false) {
219             throw new JBIException("Descriptors can not be queried for external endpoints");
220         }
221         AbstractServiceEndpoint se = (AbstractServiceEndpoint) endpoint;
222         // TODO: what if the endpoint is linked or dynamic
223
ComponentMBeanImpl component = getComponent(se.getComponentNameSpace());
224         return component.getComponent().getServiceDescription(endpoint);
225     }
226
227     /**
228      * Resolve the given endpoint reference into a service endpoint. This is called by the component when it has an EPR
229      * that it wants to resolve into a service endpoint.
230      * <p>
231      * Note that the service endpoint returned refers to a dynamic endpoint; the endpoint will exist only as long as
232      * this component retains a strong reference to the object returned by this method. The endpoint may not be included
233      * in the list of "activated" endpoints.
234      *
235      * @param epr endpoint reference as an XML fragment; must be non-null.
236      * @return the service endpoint corresponding to the given endpoint reference; <code>null</code> if the reference
237      * cannot be resolved.
238      */

239     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc epr) {
240         Collection JavaDoc connectors = getComponents();
241         for (Iterator JavaDoc iter = connectors.iterator(); iter.hasNext();) {
242             ComponentMBeanImpl connector = (ComponentMBeanImpl) iter.next();
243             ServiceEndpoint se = connector.getComponent().resolveEndpointReference(epr);
244             if (se != null) {
245                 return new DynamicEndpoint(connector.getComponentNameSpace(), se, epr);
246             }
247         }
248         ServiceEndpoint se = resolveInternalEPR(epr);
249         if (se != null) {
250             return se;
251         }
252         return resolveStandardEPR(epr);
253     }
254     
255     /**
256      * <p>
257      * Resolve an internal JBI EPR conforming to the format defined in the JBI specification.
258      * </p>
259      *
260      * <p>The EPR would look like:
261      * <pre>
262      * <jbi:end-point-reference xmlns:jbi="http://java.sun.com/xml/ns/jbi/end-point-reference"
263      * jbi:end-point-name="endpointName"
264      * jbi:service-name="foo:serviceName"
265      * xmlns:foo="urn:FooNamespace"/>
266      * </pre>
267      * </p>
268      *
269      * @author Maciej Szefler m s z e f l e r @ g m a i l . c o m
270      * @param epr EPR fragment
271      * @return internal service endpoint corresponding to the EPR, or <code>null</code>
272      * if the EPR is not an internal EPR or if the EPR cannot be resolved
273      */

274     public ServiceEndpoint resolveInternalEPR(DocumentFragment JavaDoc epr) {
275         if (epr == null) {
276             throw new NullPointerException JavaDoc("resolveInternalEPR(epr) called with null epr.");
277         }
278         NodeList JavaDoc nl = epr.getChildNodes();
279         for (int i = 0 ; i < nl.getLength(); ++i) {
280             Node JavaDoc n = nl.item(i);
281             if (n.getNodeType() != Node.ELEMENT_NODE) {
282                 continue;
283             }
284             Element JavaDoc el = (Element JavaDoc) n;
285             // Namespace should be "http://java.sun.com/jbi/end-point-reference"
286
if (el.getNamespaceURI() == null ||
287                 !el.getNamespaceURI().equals("http://java.sun.com/jbi/end-point-reference"))
288             {
289                 continue;
290             }
291             if (el.getLocalName() == null || !el.getLocalName().equals("end-point-reference")) {
292                 continue;
293             }
294             String JavaDoc serviceName = el.getAttributeNS(el.getNamespaceURI(), "service-name");
295             // Now the DOM pain-in-the-you-know-what: we need to come up with QName for this;
296
// fortunately, there is only one place where the xmlns:xxx attribute could be, on
297
// the end-point-reference element!
298
QName JavaDoc serviceQName = DOMUtil.createQName(el, serviceName);
299             String JavaDoc endpointName = el.getAttributeNS(el.getNamespaceURI(), "end-point-name");
300             return getInternalEndpoint(serviceQName, endpointName);
301         }
302         return null;
303     }
304     
305     /**
306      * Resolve a standard EPR understood by ServiceMix container.
307      * Currently, the supported syntax is the WSA one, the address uri
308      * being parsed with the following possiblities:
309      * jbi:endpoint:service-namespace/service-name/endpoint
310      * jbi:endpoint:service-namespace:service-name:endpoint
311      *
312      * The full EPR will look like:
313      * <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
314      * <wsa:Address>jbi:endpoint:http://foo.bar.com/service/endpoint</wsa:Address>
315      * </epr>
316      *
317      * BCs should also be able to resolve such EPR but using their own URI parsing,
318      * for example:
319      * <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
320      * <wsa:Address>http://foo.bar.com/myService?http.soap=true</wsa:Address>
321      * </epr>
322      *
323      * or
324      * <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
325      * <wsa:Address>jms://activemq/queue/FOO.BAR?persistent=true</wsa:Address>
326      * </epr>
327      *
328      * Note that the separator should be same as the one used in the namespace
329      * depending on the namespace:
330      * http://foo.bar.com => '/'
331      * urn:foo:bar => ':'
332      *
333      * The syntax is the same as the one that can be used to specifiy a target
334      * for a JBI exchange with the restriction that it only allows the
335      * endpoint subprotocol to be used.
336      *
337      * @param epr
338      * @return
339      */

340     public ServiceEndpoint resolveStandardEPR(DocumentFragment JavaDoc epr) {
341         try {
342             if (epr.getChildNodes().getLength() == 1) {
343                 Node JavaDoc child = epr.getFirstChild();
344                 if (child instanceof Element JavaDoc) {
345                     Element JavaDoc elem = (Element JavaDoc) child;
346                     NodeList JavaDoc nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
347                     if (nl.getLength() == 1) {
348                         Element JavaDoc address = (Element JavaDoc) nl.item(0);
349                         String JavaDoc uri = DOMUtil.getElementText(address);
350                         if (uri != null) {
351                             uri = uri.trim();
352                         }
353                         if (uri.startsWith("endpoint:")) {
354                             uri = uri.substring("endpoint:".length());
355                             String JavaDoc[] parts = split(uri);
356                             return getInternalEndpoint(new QName JavaDoc(parts[0], parts[1]), parts[2]);
357                         }
358                         else if (uri.startsWith("service:")) {
359                             uri = uri.substring("service:".length());
360                             String JavaDoc[] parts = splitService(uri);
361                             return getEndpoint(new QName JavaDoc(parts[0], parts[1]), parts[1]);
362                         }
363                         // TODO should we support interface: and operation: here?
364
}
365                 }
366             }
367         } catch (Exception JavaDoc e) {
368             // Ignored
369
}
370         return null;
371     }
372
373     protected String JavaDoc[] splitService(String JavaDoc uri) {
374         char sep;
375         uri = uri.trim();
376         if (uri.indexOf('/') > 0) {
377             sep = '/';
378         } else {
379             sep = ':';
380         }
381         int idx1 = uri.lastIndexOf(sep);
382         String JavaDoc svcName = uri.substring(idx1 + 1);
383         String JavaDoc nsUri = uri.substring(0, idx1);
384         return new String JavaDoc[] { nsUri, svcName };
385     }
386     
387     protected String JavaDoc[] split(String JavaDoc uri) {
388         char sep;
389         uri = uri.trim();
390         if (uri.indexOf('/') > 0) {
391             sep = '/';
392         } else {
393             sep = ':';
394         }
395         int idx1 = uri.lastIndexOf(sep);
396         int idx2 = uri.lastIndexOf(sep, idx1 - 1);
397         String JavaDoc epName = uri.substring(idx1 + 1);
398         String JavaDoc svcName = uri.substring(idx2 + 1, idx1);
399         String JavaDoc nsUri = uri.substring(0, idx2);
400         return new String JavaDoc[] { nsUri, svcName, epName };
401     }
402     
403     /**
404      * @param provider
405      * @param externalEndpoint the external endpoint to be registered, must be non-null.
406      * @throws JBIException
407      */

408     public void registerExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) throws JBIException {
409         if (externalEndpoint != null) {
410             endpointRegistry.registerExternalEndpoint(cns, externalEndpoint);
411         }
412     }
413
414     /**
415      * @param provider
416      * @param externalEndpoint the external endpoint to be deregistered; must be non-null.
417      */

418     public void deregisterExternalEndpoint(ComponentNameSpace cns, ServiceEndpoint externalEndpoint) {
419         endpointRegistry.unregisterExternalEndpoint(cns, externalEndpoint);
420     }
421
422     /**
423      * @param service
424      * @param name
425      * @return endpoint
426      */

427     public ServiceEndpoint getEndpoint(QName JavaDoc service, String JavaDoc name) {
428         return endpointRegistry.getEndpoint(service, name);
429     }
430     
431     public ServiceEndpoint getInternalEndpoint(QName JavaDoc service, String JavaDoc name) {
432         return endpointRegistry.getInternalEndpoint(service, name);
433     }
434
435     /**
436      * @param serviceName
437      * @return endpoints
438      */

439     public ServiceEndpoint[] getEndpointsForService(QName JavaDoc serviceName) {
440         return endpointRegistry.getEndpointsForService(serviceName);
441     }
442
443     /**
444      * @param interfaceName
445      * @return endpoints
446      */

447     public ServiceEndpoint[] getExternalEndpoints(QName JavaDoc interfaceName) {
448         return endpointRegistry.getExternalEndpointsForInterface(interfaceName);
449     }
450
451     /**
452      * @param serviceName
453      * @return endpoints
454      */

455     public ServiceEndpoint[] getExternalEndpointsForService(QName JavaDoc serviceName) {
456         return endpointRegistry.getExternalEndpointsForService(serviceName);
457     }
458
459     /**
460      * REgister a local Component
461      *
462      * @param name
463      * @param description
464      * @param component
465      * @param dc
466      * @param binding
467      * @param service
468      * @return ComponentConnector
469      * @throws JBIException
470      */

471     public ComponentMBeanImpl registerComponent(ComponentNameSpace name,
472                                                 String JavaDoc description,
473                                                 Component component,
474                                                 boolean binding,
475                                                 boolean service,
476                                                 String JavaDoc[] sharedLibraries) throws JBIException {
477         return componentRegistry.registerComponent(name,description, component, binding, service, sharedLibraries);
478     }
479
480     /**
481      * @param component
482      * @return ComponentConnector
483      */

484     public void deregisterComponent(ComponentMBeanImpl component) {
485         componentRegistry.deregisterComponent(component);
486     }
487
488     /**
489      * @return all local ComponentConnectors
490      */

491     public Collection JavaDoc getComponents() {
492         return componentRegistry.getComponents();
493     }
494
495     /**
496      * Get a Component
497      * @param cns
498      * @return the Component
499      */

500     public ComponentMBeanImpl getComponent(ComponentNameSpace cns) {
501         return componentRegistry.getComponent(cns);
502     }
503     
504     /**
505      * Get a Component
506      * @param name
507      * @return the Componment
508      */

509     public ComponentMBeanImpl getComponent(String JavaDoc name) {
510         ComponentNameSpace cns = new ComponentNameSpace(container.getName(), name);
511         return getComponent(cns);
512     }
513     
514     /**
515      * Get a list of all engines currently installed.
516      * @return array of JMX object names of all installed SEs.
517      */

518     public ObjectName JavaDoc[] getEngineComponents() {
519         ObjectName JavaDoc[] result = null;
520         List JavaDoc tmpList = new ArrayList JavaDoc();
521         for (Iterator JavaDoc i = getComponents().iterator(); i.hasNext();){
522             ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
523             if (!lcc.isPojo() && lcc.isService() && lcc.getMBeanName() != null){
524                 tmpList.add(lcc.getMBeanName());
525             }
526         }
527         result = new ObjectName JavaDoc[tmpList.size()];
528         tmpList.toArray(result);
529         return result;
530         
531     }
532     
533     /**
534      * Get a list of all binding components currently installed.
535      * @return array of JMX object names of all installed BCs.
536      */

537     public ObjectName JavaDoc[] getBindingComponents() {
538         ObjectName JavaDoc[] result = null;
539         List JavaDoc tmpList = new ArrayList JavaDoc();
540         for (Iterator JavaDoc i = getComponents().iterator(); i.hasNext();){
541             ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
542             if (!lcc.isPojo() && lcc.isBinding() && lcc.getMBeanName() != null){
543                 tmpList.add(lcc.getMBeanName());
544             }
545         }
546         result = new ObjectName JavaDoc[tmpList.size()];
547         tmpList.toArray(result);
548         return result;
549     }
550
551     /**
552      * Get a list of all pojos currently installed.
553      * @return array of JMX object names of all installed PJOJO Conponents.
554      */

555     public ObjectName JavaDoc[] getPojoComponents() {
556         ObjectName JavaDoc[] result = null;
557         List JavaDoc tmpList = new ArrayList JavaDoc();
558         for (Iterator JavaDoc i = getComponents().iterator(); i.hasNext();){
559             ComponentMBeanImpl lcc = (ComponentMBeanImpl) i.next();
560             if (lcc.isPojo() && lcc.getMBeanName() != null){
561                 tmpList.add(lcc.getMBeanName());
562             }
563         }
564         result = new ObjectName JavaDoc[tmpList.size()];
565         tmpList.toArray(result);
566         return result;
567         
568     }
569     
570     /**
571      * Register All subscriptions
572      * @param context
573      * @param as
574      */

575     public void registerSubscriptions(ComponentContextImpl context,ActivationSpec as) {
576         QName JavaDoc service = as.getService();
577         String JavaDoc endpointName = as.getEndpoint();
578         InternalEndpoint endpoint = new InternalEndpoint(context.getComponentNameSpace(), endpointName, service);
579         SubscriptionSpec[] specs = as.getSubscriptions();
580         if (specs != null) {
581             for (int i =0; i<specs.length; i++) {
582                 registerSubscription(context, specs[i], endpoint);
583             }
584         }
585     }
586     
587     /**
588      * Deregister All subscriptions
589      * @param context
590      * @param as
591      */

592     public void deregisterSubscriptions(ComponentContextImpl context,ActivationSpec as) {
593         SubscriptionSpec[] specs = as.getSubscriptions();
594         if (specs != null) {
595             for (int i =0; i<specs.length; i++) {
596                 deregisterSubscription(context,specs[i]);
597             }
598         }
599     }
600     
601     /**
602      * @param context
603      * @param subscription
604      * @param endpoint
605      */

606     public void registerSubscription(ComponentContextImpl context,SubscriptionSpec subscription, ServiceEndpoint endpoint) {
607         InternalEndpoint sei = (InternalEndpoint)endpoint;
608         subscription.setName(context.getComponentNameSpace());
609         subscriptionRegistry.registerSubscription(subscription,sei);
610     }
611
612     /**
613      * @param context
614      * @param subscription
615      * @return the ServiceEndpoint
616      */

617     public InternalEndpoint deregisterSubscription(ComponentContextImpl context,SubscriptionSpec subscription) {
618         subscription.setName(context.getComponentNameSpace());
619         InternalEndpoint result = subscriptionRegistry.deregisterSubscription(subscription);
620         return result;
621     }
622     
623     
624     /**
625      * @param exchange
626      * @return a List of matching endpoints - can return null if no matches
627      */

628     public List JavaDoc getMatchingSubscriptionEndpoints(MessageExchangeImpl exchange) {
629         return subscriptionRegistry.getMatchingSubscriptionEndpoints(exchange);
630     }
631     
632     /**
633      * Register a service assembly
634      * @param sa
635      * @return true if not already registered
636      * @throws DeploymentException
637      */

638     public ServiceAssemblyLifeCycle registerServiceAssembly(ServiceAssembly sa,
639                                                             ServiceAssemblyEnvironment env) throws DeploymentException{
640         return serviceAssemblyRegistry.register(sa, env);
641     }
642     
643     /**
644      * Register a service assembly
645      * @param sa
646      * @return true if not already registered
647      * @throws DeploymentException
648      */

649     public ServiceAssemblyLifeCycle registerServiceAssembly(ServiceAssembly sa,
650                                                             String JavaDoc[] suKeys,
651                                                             ServiceAssemblyEnvironment env) throws DeploymentException{
652         return serviceAssemblyRegistry.register(sa, suKeys, env);
653     }
654     
655     /**
656      * Un-register a service assembly
657      * @param saName
658      * @return true if successfully unregistered
659      */

660     public boolean unregisterServiceAssembly(String JavaDoc saName) {
661         return serviceAssemblyRegistry.unregister(saName);
662     }
663     
664     /**
665      * Get a named ServiceAssembly
666      * @param name
667      * @return the ServiceAssembly or null if it doesn't exist
668      */

669     public ServiceAssemblyLifeCycle getServiceAssembly(String JavaDoc saName){
670         return serviceAssemblyRegistry.getServiceAssembly(saName);
671     }
672
673     /**
674      * Returns a list of Service Units that are currently deployed to the given component.
675      *
676      * @param componentName name of the component.
677      * @return List of deployed service units
678      */

679     public ServiceUnitLifeCycle[] getDeployedServiceUnits(String JavaDoc componentName) {
680         Collection JavaDoc sus = serviceUnits.values();
681         List JavaDoc tmpList = new ArrayList JavaDoc();
682         for (Iterator JavaDoc iter = sus.iterator(); iter.hasNext();) {
683             ServiceUnitLifeCycle su = (ServiceUnitLifeCycle) iter.next();
684             if (su.getComponentName().equals(componentName)) {
685                 tmpList.add(su);
686             }
687         }
688         ServiceUnitLifeCycle[] result = new ServiceUnitLifeCycle[tmpList.size()];
689         tmpList.toArray(result);
690         return result;
691     }
692
693     /**
694      * Returns a list of Service Assemblies deployed to the JBI enviroment.
695      *
696      * @return list of Service Assembly Name's.
697      */

698     public String JavaDoc[] getDeployedServiceAssemblies() {
699         return serviceAssemblyRegistry.getDeployedServiceAssemblies();
700     }
701     
702     /**
703      * Returns a list of Service Assemblies that contain SUs for the given component.
704      *
705      * @param componentName name of the component.
706      * @return list of Service Assembly names.
707      */

708     public String JavaDoc[] getDeployedServiceAssembliesForComponent(String JavaDoc componentName) {
709         return serviceAssemblyRegistry.getDeployedServiceAssembliesForComponent(componentName);
710     }
711
712     /**
713      * Returns a list of components(to which SUs are targeted for) in a Service Assembly.
714      *
715      * @param saName name of the service assembly.
716      * @return list of component names.
717      */

718     public String JavaDoc[] getComponentsForDeployedServiceAssembly(String JavaDoc saName) {
719         return serviceAssemblyRegistry.getComponentsForDeployedServiceAssembly(saName);
720     }
721
722     /**
723      * Returns a boolean value indicating whether the SU is currently deployed.
724      *
725      * @param componentName - name of component.
726      * @param suName - name of the Service Unit.
727      * @return boolean value indicating whether the SU is currently deployed.
728      */

729     public boolean isSADeployedServiceUnit(String JavaDoc componentName, String JavaDoc suName) {
730         return serviceAssemblyRegistry.isDeployedServiceUnit(componentName, suName);
731     }
732     
733     /**
734      * Get a ServiceUnit by its key.
735      *
736      * @param suKey the key of the service unit
737      * @return the ServiceUnit or null of it doesn't exist
738      */

739     public ServiceUnitLifeCycle getServiceUnit(String JavaDoc suKey) {
740         return (ServiceUnitLifeCycle) serviceUnits.get(suKey);
741     }
742     
743     /**
744      * Register a ServiceUnit.
745      *
746      * @param su the service unit to register
747      * @param serviceAssembly the service assembly the service unit belongs to
748      * @return the service unit key
749      */

750     public String JavaDoc registerServiceUnit(ServiceUnit su, String JavaDoc saName, File JavaDoc suDir) {
751         ServiceUnitLifeCycle sulc = new ServiceUnitLifeCycle(
752                 su,
753                 saName,
754                 this,
755                 suDir);
756         this.serviceUnits.put(sulc.getKey(), sulc);
757         try {
758             ObjectName JavaDoc objectName = getContainer().getManagementContext().createObjectName(sulc);
759             getContainer().getManagementContext().registerMBean(objectName, sulc, ServiceUnitMBean.class);
760         } catch (JMException JavaDoc e) {
761             log.error("Could not register MBean for service unit", e);
762         }
763         return sulc.getKey();
764     }
765     
766     /**
767      * Unregister a ServiceUnit by its key.
768      *
769      * @param suKey the key of the service unit
770      */

771     public void unregisterServiceUnit(String JavaDoc suKey) {
772         ServiceUnitLifeCycle sulc = (ServiceUnitLifeCycle) this.serviceUnits.remove(suKey);
773         if (sulc != null) {
774             try {
775                 getContainer().getManagementContext().unregisterMBean(sulc);
776             } catch (JBIException e) {
777                 log.error("Could not unregister MBean for service unit", e);
778             }
779         }
780     }
781     
782     public void registerSharedLibrary(org.apache.servicemix.jbi.deployment.SharedLibrary sl,
783                                       File JavaDoc installationDir) {
784         SharedLibrary library = new SharedLibrary(sl, installationDir);
785         this.sharedLibraries.put(library.getName(), library);
786         try {
787             ObjectName JavaDoc objectName = getContainer().getManagementContext().createObjectName(library);
788             getContainer().getManagementContext().registerMBean(objectName, library, SharedLibraryMBean.class);
789         } catch (JMException JavaDoc e) {
790             log.error("Could not register MBean for service unit", e);
791         }
792         checkPendingComponents();
793     }
794     
795     public void unregisterSharedLibrary(String JavaDoc name) {
796         // TODO: check for components depending on this library,
797
// shutdown them and add them to the list of pending components
798
SharedLibrary sl = (SharedLibrary) this.sharedLibraries.remove(name);
799         if (sl != null) {
800             try {
801                 getContainer().getManagementContext().unregisterMBean(sl);
802             } catch (JBIException e) {
803                 log.error("Could not unregister MBean for shared library", e);
804             }
805         }
806     }
807     
808     public SharedLibrary getSharedLibrary(String JavaDoc name) {
809         return (SharedLibrary) sharedLibraries.get(name);
810     }
811     
812     public Collection JavaDoc getSharedLibraries() {
813         return sharedLibraries.values();
814     }
815
816     public void registerEndpointConnection(QName JavaDoc fromSvc, String JavaDoc fromEp, QName JavaDoc toSvc, String JavaDoc toEp, String JavaDoc link) throws JBIException {
817         endpointRegistry.registerEndpointConnection(fromSvc, fromEp, toSvc, toEp, link);
818     }
819
820     public void unregisterEndpointConnection(QName JavaDoc fromSvc, String JavaDoc fromEp) {
821         endpointRegistry.unregisterEndpointConnection(fromSvc, fromEp);
822     }
823     
824     public void registerInterfaceConnection(QName JavaDoc fromItf, QName JavaDoc toSvc, String JavaDoc toEp) throws JBIException {
825         endpointRegistry.registerInterfaceConnection(fromItf, toSvc, toEp);
826     }
827
828     public void unregisterInterfaceConnection(QName JavaDoc fromItf) {
829         endpointRegistry.unregisterInterfaceConnection(fromItf);
830     }
831
832     public void registerRemoteEndpoint(ServiceEndpoint endpoint) {
833         endpointRegistry.registerRemoteEndpoint((InternalEndpoint) endpoint);
834     }
835
836     public void unregisterRemoteEndpoint(ServiceEndpoint endpoint) {
837         endpointRegistry.unregisterRemoteEndpoint((InternalEndpoint) endpoint);
838     }
839
840     public void checkPendingAssemblies() {
841         try {
842             getContainer().getWorkManager().scheduleWork(new Work JavaDoc() {
843                 public void release() {
844                 }
845                 public void run() {
846                     startPendingAssemblies();
847                 }
848             });
849         } catch (WorkException JavaDoc e) {
850             log.error("Could not schedule work", e);
851         }
852     }
853
854     public void addPendingAssembly(ServiceAssemblyLifeCycle sa) {
855         if (!pendingAssemblies.contains(sa)) {
856             pendingAssemblies.add(sa);
857         }
858     }
859     
860     protected synchronized void startPendingAssemblies() {
861         for (Iterator JavaDoc iter = pendingAssemblies.iterator(); iter.hasNext();) {
862             ServiceAssemblyLifeCycle sa = (ServiceAssemblyLifeCycle) iter.next();
863             ServiceUnitLifeCycle[] sus = sa.getDeployedSUs();
864             boolean ok = true;
865             for (int i = 0; i < sus.length; i++) {
866                 ComponentMBeanImpl c = getComponent(sus[i].getComponentName());
867                 if (c == null || !c.isStarted()) {
868                     ok = false;
869                     break;
870                 }
871             }
872             if (ok) {
873                 try {
874                     sa.restore();
875                     pendingAssemblies.remove(sa);
876                 } catch (Exception JavaDoc e) {
877                     log.error("Error trying to restore service assembly state", e);
878                 }
879             }
880         }
881     }
882
883     public void checkPendingComponents() {
884         try {
885             getContainer().getWorkManager().scheduleWork(new Work JavaDoc() {
886                 public void release() {
887                 }
888                 public void run() {
889                     startPendingComponents();
890                 }
891             });
892         } catch (WorkException JavaDoc e) {
893             log.error("Could not schedule work", e);
894         }
895     }
896
897     public void addPendingComponent(ComponentMBeanImpl comp) {
898         if (!pendingComponents.contains(comp)) {
899             pendingComponents.add(comp);
900         }
901     }
902     
903     protected synchronized void startPendingComponents() {
904         for (Iterator JavaDoc iter = pendingComponents.iterator(); iter.hasNext();) {
905             ComponentMBeanImpl comp = (ComponentMBeanImpl) iter.next();
906             // TODO: restore component state if
907
}
908     }
909
910 }
911
Popular Tags