KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > service > EndpointServiceImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: EndpointService.java 11:27:27 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.service;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jbi.JBIException;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.servicedesc.ServiceEndpoint;
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
33 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
34 import org.objectweb.fractal.fraclet.annotation.Interface;
35 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
36 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
37 import org.objectweb.fractal.fraclet.annotation.Monolog;
38 import org.objectweb.fractal.fraclet.annotation.Provides;
39 import org.objectweb.fractal.fraclet.annotation.Requires;
40 import org.objectweb.petals.jbi.registry.AbstractEndpoint;
41 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
42 import org.objectweb.petals.jbi.registry.InternalEndpoint;
43 import org.objectweb.petals.jbi.registry.LinkedEndpoint;
44 import org.objectweb.petals.jbi.registry.Registry;
45 import org.objectweb.petals.jbi.registry.RegistryException;
46 import org.objectweb.petals.kernel.admin.DistributedJMXServer;
47 import org.objectweb.petals.kernel.admin.DistributedJMXServerFactory;
48 import org.objectweb.petals.tools.jbicommon.util.XMLUtil;
49 import org.objectweb.petals.util.LoggingUtil;
50 import org.objectweb.petals.util.ParameterCheckHelper;
51 import org.objectweb.petals.util.SystemUtil;
52 import org.objectweb.petals.util.WSDLUtil;
53 import org.objectweb.util.monolog.api.Logger;
54 import org.w3c.dom.Document JavaDoc;
55
56 /**
57  * This class is used to manage everything that deals with endpoint in the
58  * distributed environment
59  *
60  * @author ddesjardins - eBMWebsourcing
61  */

62 @FractalComponent
63 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.management.service.EndpointService.class))
64 public class EndpointServiceImpl implements EndpointService {
65
66     /**
67      * Name of the current container
68      */

69     protected String JavaDoc containerName;
70
71     /**
72      * Logger wrapper
73      */

74     protected LoggingUtil log;
75
76     /**
77      * Monolog Logger instance.
78      */

79     @Monolog(name="logger")
80     protected Logger logger;
81
82     /**
83      * Registry
84      */

85     @Requires(name="registry",signature=org.objectweb.petals.jbi.registry.Registry.class)
86     protected Registry registry;
87
88     /**
89      * Manager of the distributed JMX server
90      */

91     @Requires(name="dist-jmx-server",signature=org.objectweb.petals.kernel.admin.DistributedJMXServerFactory.class)
92     protected DistributedJMXServerFactory serverManager;
93
94     /**
95      * @see EndpointService#activateEndpoint(QName, String, ConsumerEndpoint)
96      */

97     public InternalEndpoint activateEndpoint(QName JavaDoc serviceName,
98             String JavaDoc endpointName, ConsumerEndpoint address) throws JBIException {
99         log.start();
100         if (serviceName == null) {
101             throw new IllegalArgumentException JavaDoc("Service name must be non null");
102         }
103         if (endpointName == null) {
104             throw new IllegalArgumentException JavaDoc("Endpoint name must be non null");
105         }
106         if (address == null) {
107             throw new IllegalArgumentException JavaDoc(
108                     "Consumer endpoint must be non null");
109         }
110         InternalEndpoint se = new InternalEndpoint(serviceName, endpointName,
111                 address.getComponentName(), address.getContainerName(), this);
112         se.setContainerUID(SystemUtil.getContainerUID());
113         registry.registerInternalEndpoint(se);
114         log.end();
115         return se;
116     }
117
118     /**
119      * @see EndpointService#createConnection(QName, QName, String)
120      */

121     public void createConnection(QName JavaDoc consInterface, QName JavaDoc provService,
122             String JavaDoc provEndpoint) throws JBIException {
123         log.start();
124         if (consInterface == null) {
125             throw new IllegalArgumentException JavaDoc(
126                     "Interface name must be non null");
127         }
128         if (provService == null) {
129             throw new IllegalArgumentException JavaDoc(
130                     "Provider service name must be non null");
131         }
132         if (provEndpoint == null) {
133             throw new IllegalArgumentException JavaDoc(
134                     "Provider endpoint name must be non null");
135         }
136         LinkedEndpoint linkedEndpoint = new LinkedEndpoint(consInterface,
137                 provService, provEndpoint, this);
138         registry.registerConnection(linkedEndpoint);
139         log.end();
140     }
141
142     /**
143      * @see EndpointService#createConnection(QName, String, QName, String)
144      */

145     public void createConnection(QName JavaDoc consService, String JavaDoc consEndpoint,
146             QName JavaDoc provService, String JavaDoc provEndpoint) throws JBIException {
147         log.start();
148         if (consService == null) {
149             throw new IllegalArgumentException JavaDoc(
150                     "Consumer service name must be non null");
151         }
152         if (consEndpoint == null) {
153             throw new IllegalArgumentException JavaDoc(
154                     "Consumer endpoint name must be non null");
155         }
156         if (provService == null) {
157             throw new IllegalArgumentException JavaDoc(
158                     "Provider service name must be non null");
159         }
160         if (provEndpoint == null) {
161             throw new IllegalArgumentException JavaDoc(
162                     "Provider endpoint name must be non null");
163         }
164         LinkedEndpoint linkedEndpoint = new LinkedEndpoint(consService,
165                 consEndpoint, provService, provEndpoint, this);
166         registry.registerConnection(linkedEndpoint);
167         log.end();
168     }
169
170     /**
171      * @see EndpointService#deactivateEndpoint(ServiceEndpoint)
172      */

173     public void deactivateEndpoint(ServiceEndpoint endpoint)
174         throws JBIException {
175         log.start();
176         if (endpoint == null) {
177             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
178         }
179         registry.deregisterInternalEndpoint((AbstractEndpoint) endpoint);
180         log.end();
181     }
182
183     /**
184      * @see EndpointService#deleteConnection(QName, QName, String)
185      */

186     public void deleteConnection(QName JavaDoc consInterface, QName JavaDoc provService,
187             String JavaDoc provEndpoint) throws JBIException {
188         log.start();
189         if (consInterface == null) {
190             throw new IllegalArgumentException JavaDoc(
191                     "Consumer interface name must be non null");
192         }
193         if (provService == null) {
194             throw new IllegalArgumentException JavaDoc(
195                     "Provider service name must be non null");
196         }
197         if (provEndpoint == null) {
198             throw new IllegalArgumentException JavaDoc(
199                     "Provider endpoint name must be non null");
200         }
201         LinkedEndpoint linkedEndpoint = new LinkedEndpoint(consInterface,
202                 provService, provEndpoint, this);
203         registry.deregisterConnection(linkedEndpoint);
204         log.end();
205     }
206
207     /**
208      * @see EndpointService#deleteConnection(QName, String, QName, String)
209      */

210     public void deleteConnection(QName JavaDoc consService, String JavaDoc consEndpoint,
211             QName JavaDoc provService, String JavaDoc provEndpoint) throws JBIException {
212         log.start();
213         if (consService == null) {
214             throw new IllegalArgumentException JavaDoc(
215                     "Consumer service name must be non null");
216         }
217         if (consEndpoint == null) {
218             throw new IllegalArgumentException JavaDoc(
219                     "Consumer endpoint name must be non null");
220         }
221         if (provService == null) {
222             throw new IllegalArgumentException JavaDoc(
223                     "Provider service name must be non null");
224         }
225         if (provEndpoint == null) {
226             throw new IllegalArgumentException JavaDoc(
227                     "Provider endpoint name must be non null");
228         }
229         LinkedEndpoint linkedEndpoint = new LinkedEndpoint(consService,
230                 consEndpoint, provService, provEndpoint, this);
231         registry.deregisterConnection(linkedEndpoint);
232         log.end();
233     }
234
235     /**
236      * @see EndpointService#deregisterExternalEndpoint(ServiceEndpoint)
237      */

238     public void deregisterExternalEndpoint(ServiceEndpoint endpoint)
239         throws JBIException {
240         log.start();
241         if (endpoint == null) {
242             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
243         }
244         registry.deregisterExternalEndpoint((AbstractEndpoint) endpoint);
245         log.end();
246     }
247
248     /**
249      * @see EndpointService#getEndpoint(QName, String)
250      *
251      * <ol>
252      * <li>Retreive the named endpoint</li>
253      * <li>If found, update the endpoint service of the found endpoint with
254      * this</li>
255      * </ol>
256      */

257     public ServiceEndpoint getEndpoint(QName JavaDoc service, String JavaDoc name) {
258         log.call();
259         return getEndpoint(service, name, true);
260     }
261
262     /**
263      * @see EndpointService#getEndpoint(QName, String,boolean)
264      *
265      * <ol>
266      * <li>Retreive the named endpoint</li>
267      * <li>If found, update the endpoint service of the found endpoint with
268      * this</li>
269      * </ol>
270      */

271     public ServiceEndpoint getEndpoint(QName JavaDoc service, String JavaDoc name,
272             boolean resolveLink) {
273         log.start();
274         if (service == null) {
275             throw new IllegalArgumentException JavaDoc("Service name must be non null");
276         }
277         if (name == null) {
278             throw new IllegalArgumentException JavaDoc("Endpoint name must be non null");
279         }
280         AbstractEndpoint abstractEndpoint = null;
281         try {
282             abstractEndpoint = registry.getInternalEndpoint(service, name,
283                     resolveLink);
284         } catch (RegistryException e) {
285             // no endpoint found, return null;
286
}
287         if (abstractEndpoint != null
288                 && abstractEndpoint.getEndpointService() == null) {
289             abstractEndpoint.setEndpointService(this);
290         }
291         log.end();
292         return abstractEndpoint;
293     }
294
295     /**
296      * @see EndpointService#getEndpointDescriptorForEndpoint(ServiceEndpoint)
297      */

298     public Document JavaDoc getEndpointDescriptorForEndpoint(ServiceEndpoint endpoint)
299         throws JBIException {
300         log.start();
301         if (endpoint == null) {
302             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
303         }
304         Document JavaDoc document = null;
305         try {
306             // Update the new endpoints
307
updateNewEndpoints();
308
309             // Recover the endpoint
310
AbstractEndpoint abstractEndpoint = registry.getInternalEndpoint(
311                     endpoint.getServiceName(), endpoint.getEndpointName());
312             document = abstractEndpoint.getEndpointDescriptor();
313         } catch (RegistryException e) {
314             log.error("Error while validating endpoint "
315                     + endpoint.getEndpointName(), e);
316         }
317         log.end();
318         return document;
319     }
320
321     /**
322      * @see EndpointService#getExternalEndpointsForInterface(QName)
323      */

324     public ServiceEndpoint[] getExternalEndpointsForInterface(
325             QName JavaDoc interfaceName) {
326         log.start();
327         if (interfaceName == null) {
328             throw new IllegalArgumentException JavaDoc(
329                     "Interface name must be non null");
330         }
331         ServiceEndpoint[] endpoints = null;
332         try {
333             endpoints = registry
334                     .getExternalEndpointsForInterface(interfaceName);
335             // Update the EndpointService field of the found endpoints
336
for (int i = 0; i < endpoints.length; i++) {
337                 AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoints[i];
338                 if (abstractEndpoint.getEndpointService() == null) {
339                     abstractEndpoint.setEndpointService(this);
340                     endpoints[i] = abstractEndpoint;
341                 }
342             }
343         } catch (RegistryException e) {
344             log.error("Error while getting external endpoints for interface "
345                     + interfaceName, e);
346         }
347         log.end();
348         return endpoints;
349     }
350
351     /**
352      * @see EndpointService#getExternalEndpointsForService(QName)
353      */

354     public ServiceEndpoint[] getExternalEndpointsForService(QName JavaDoc serviceName) {
355         log.start();
356         if (serviceName == null) {
357             throw new IllegalArgumentException JavaDoc("Service name must be non null");
358         }
359         ServiceEndpoint[] endpoints = null;
360         endpoints = registry.getExternalEndpointsForService(serviceName);
361         // Update the EndpointService field of the found endpoints
362
for (int i = 0; i < endpoints.length; i++) {
363             AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoints[i];
364             if (abstractEndpoint.getEndpointService() == null) {
365                 abstractEndpoint.setEndpointService(this);
366                 endpoints[i] = abstractEndpoint;
367             }
368         }
369         log.end();
370         return endpoints;
371     }
372
373     /**
374      * Return the interface for the specified endpoint
375      *
376      * @param endpoint
377      * endpoint on which we want the interface names; must be non
378      * null
379      * @return array of interfaces QName
380      */

381     public QName JavaDoc[] getInterfacesForEndpoint(AbstractEndpoint endpoint) {
382         log.start();
383         if (endpoint == null) {
384             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
385         }
386         QName JavaDoc[] interfs = new QName JavaDoc[0];
387         AbstractEndpoint abstractEndpoint = null;
388         try {
389             updateNewEndpoints();
390             abstractEndpoint = registry.getInternalEndpoint(endpoint
391                     .getServiceName(), endpoint.getEndpointName());
392             interfs = abstractEndpoint.getInterfaces();
393         } catch (Exception JavaDoc e) {
394             log.error("Error while getting interfaces for endpoint "
395                     + endpoint.getEndpointName(), e);
396         }
397         log.end();
398         return interfs;
399     }
400
401     /**
402      * Update all the new endpoints and remove them
403      *
404      * @throws JBIException
405      * if a problem occurs
406      */

407     protected void updateNewEndpoints() throws JBIException {
408         for (AbstractEndpoint abstractEndpoint : registry
409                 .retrieveNewEndpoints()) {
410             AbstractEndpoint endpoint = null;
411             try {
412                 endpoint = updateEndpointDescriptionAndInterfaces(abstractEndpoint);
413                 registry.validateEndpoint(endpoint);
414                 // We clean the new endpoint context if no problem occured
415
registry.cleanNewEndpoints();
416             } catch (JBIException e) {
417                 log.error(
418                         "Problem while getting description and interfaces for the endpoint "
419                                 + abstractEndpoint.getEndpointName(), e);
420             }
421         }
422     }
423
424     /**
425      * @see EndpointService#getInternalEndpointsForInterface(QName)
426      */

427     public ServiceEndpoint[] getInternalEndpointsForInterface(
428             QName JavaDoc interfaceName) {
429         log.start();
430         ServiceEndpoint[] endpoints = new ServiceEndpoint[0];
431         try {
432             // Update the new endpoints
433
updateNewEndpoints();
434
435             endpoints = registry
436                     .getInternalEndpointsForInterface(interfaceName);
437             for (int i = 0; i < endpoints.length; i++) {
438                 AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoints[i];
439                 if (abstractEndpoint.getEndpointService() == null) {
440                     abstractEndpoint.setEndpointService(this);
441                     endpoints[i] = abstractEndpoint;
442                 }
443             }
444         } catch (RegistryException e) {
445             log.error("Error while getting internal endpoints for interface "
446                     + interfaceName, e);
447         } catch (JBIException e) {
448             log.error("Error while getting internal endpoints for interface "
449                     + interfaceName, e);
450         }
451         log.end();
452         return endpoints;
453     }
454
455     /**
456      * @see EndpointService#getInternalEndpointsForService(QName)
457      */

458     public ServiceEndpoint[] getInternalEndpointsForService(QName JavaDoc serviceName) {
459         log.start();
460         if (serviceName == null) {
461             throw new IllegalArgumentException JavaDoc("Service name must be non null");
462         }
463         ServiceEndpoint[] endpoints = new ServiceEndpoint[0];
464         endpoints = registry.getInternalEndpointsForService(serviceName);
465         for (int i = 0; i < endpoints.length; i++) {
466             AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoints[i];
467             if (abstractEndpoint.getEndpointService() == null) {
468                 abstractEndpoint.setEndpointService(this);
469                 endpoints[i] = abstractEndpoint;
470             }
471         }
472         log.end();
473         return endpoints;
474     }
475
476     /**
477      * @see EndpointService#isExchangeWithConsumerOkayForComponent(InternalEndpoint,
478      * MessageExchange)
479      */

480     public boolean isExchangeWithConsumerOkayForComponent(
481             InternalEndpoint internalEndpoint, MessageExchange exchange) {
482         log.start();
483         ParameterCheckHelper.isNullParameterWithLog(internalEndpoint,
484                 "Endpoint", log);
485         boolean test = true;
486         try {
487             // Retreive a connection to the JMX server of the container were the
488
// component is deployed
489
DistributedJMXServer server = serverManager
490                     .createDistributedJMXServer(internalEndpoint
491                             .getContainerName());
492             // Ask the component if it is ok to accept the exchange
493
Boolean JavaDoc bool = (Boolean JavaDoc) server.invoke(server
494                     .getAdminServiceMBeanName(),
495                     DistributedJMXServer.IS_OK_WITH_CONS, new Object JavaDoc[] {
496                         internalEndpoint.getComponentName(),
497                         (ServiceEndpoint) internalEndpoint, exchange},
498                     new String JavaDoc[] {String JavaDoc.class.getName(),
499                         ServiceEndpoint.class.getName(),
500                         MessageExchange.class.getName()});
501             test = bool.booleanValue();
502         } catch (Exception JavaDoc e) {
503             log.error("Problem in isExchangeWithConsumerOkayForComponent", e);
504         }
505         log.end();
506         return test;
507     }
508
509     /**
510      * @see EndpointService#isExchangeWithProviderOkayForComponent(InternalEndpoint,
511      * MessageExchange)
512      */

513     public boolean isExchangeWithProviderOkayForComponent(
514             InternalEndpoint internalEndpoint, MessageExchange exchange) {
515         log.start();
516         if (internalEndpoint == null) {
517             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
518         }
519         boolean test = true;
520         try {
521             // Retreive a connection to the JMX server of the container were the
522
// component is deployed
523
DistributedJMXServer server = serverManager
524                     .createDistributedJMXServer(internalEndpoint
525                             .getContainerName());
526             // Retreive the endpoint description
527
Boolean JavaDoc bool = (Boolean JavaDoc) server.invoke(server
528                     .getAdminServiceMBeanName(),
529                     DistributedJMXServer.IS_OK_WITH_PROV, new Object JavaDoc[] {
530                         internalEndpoint.getComponentName(),
531                         (ServiceEndpoint) internalEndpoint, exchange},
532                     new String JavaDoc[] {String JavaDoc.class.getName(),
533                         ServiceEndpoint.class.getName(),
534                         MessageExchange.class.getName()});
535             test = bool.booleanValue();
536         } catch (Exception JavaDoc e) {
537             log.error("Problem in isExchangeWithProviderOkayForComponent", e);
538         }
539         log.end();
540         return test;
541     }
542
543     /**
544      * @see EndpointService#registerExternalEndpoint(ServiceEndpoint)
545      */

546     public void registerExternalEndpoint(ServiceEndpoint externalEndpoint)
547         throws JBIException {
548         log.start();
549         if (externalEndpoint == null) {
550             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
551         }
552         registry.registerExternalEndpoint(externalEndpoint);
553         log.end();
554     }
555
556     @LifeCycle(on=LifeCycleType.START)
557     public void start() throws IllegalLifeCycleException {
558         log = new LoggingUtil(logger);
559         log.start();
560         containerName = SystemUtil.getContainerName();
561         log.end();
562     }
563
564     @LifeCycle(on=LifeCycleType.STOP)
565     public void stop() throws IllegalLifeCycleException {
566         log.start();
567         log.end();
568     }
569
570     /**
571      * This method will update the endpoint description and its interfaces then
572      * return it
573      *
574      * @param endpoint
575      * endpoint to update
576      * @return updated endpoint
577      * @throws JBIException
578      */

579     private AbstractEndpoint updateEndpointDescriptionAndInterfaces(
580             AbstractEndpoint endpoint) throws JBIException {
581         log.start();
582         try {
583             // Retreive a connection to the JMX server of the container were the
584
// endpoint is deployed
585
DistributedJMXServer server = serverManager
586                     .createDistributedJMXServer(endpoint.getContainerName());
587             // Retreive the endpoint description
588
Document JavaDoc document = (Document JavaDoc) server.invoke(server
589                     .getAdminServiceMBeanName(),
590                     DistributedJMXServer.GET_SERVICE_DESCRITION,
591                     new Object JavaDoc[] {endpoint.getComponentName(),
592                         (ServiceEndpoint) endpoint}, new String JavaDoc[] {
593                         "java.lang.String",
594                         "javax.jbi.servicedesc.ServiceEndpoint"});
595             server.closeConnector();
596             List JavaDoc<QName JavaDoc> interfaces = new ArrayList JavaDoc<QName JavaDoc>();
597             if (document != null) {
598                 // Get the whole description
599
String JavaDoc description = XMLUtil
600                         .createStringFromDOMDocument(document);
601                 endpoint.setDescription(description);
602                 // Get the interfaces
603
interfaces = WSDLUtil.getInterfacesForService(document,
604                         endpoint.getServiceName());
605             }
606
607             if (interfaces.size() == 0) {
608                 QName JavaDoc unresolved = new QName JavaDoc(
609                         "http://www.petals.objectweb.org/",
610                         "UNRESOLVED_INTERFACE");
611                 log
612                         .warning("Problem while extracting interfaces for the endpoint "
613                                 + endpoint.getEndpointName()
614                                 + "\n Set its interface to " + unresolved);
615                 interfaces.add(unresolved);
616             }
617             endpoint.setInterfaces(interfaces.toArray(new QName JavaDoc[0]));
618         } catch (Exception JavaDoc e) {
619             log.error(e.getMessage(), e);
620             throw new JBIException(e);
621         }
622         log.end();
623         return endpoint;
624     }
625
626     /**
627      * @see EndpointService#isContainerStarted(AbstractEndpoint)
628      */

629     public boolean isContainerStarted(AbstractEndpoint endpoint) {
630         log.start();
631         if (endpoint == null) {
632             throw new IllegalArgumentException JavaDoc("Endpoint must be non null");
633         }
634         boolean isStarted = false;
635         isStarted = serverManager.isContainerStarted(endpoint
636                 .getContainerName());
637         log.end();
638         return isStarted;
639     }
640 }
641
Popular Tags