KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > registry > JNDIRegistry


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: JoramRegistry.java,v 0.3 2005/07/22 10:24:27 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.registry;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jbi.JBIException;
32 import javax.jbi.servicedesc.ServiceEndpoint;
33 import javax.naming.Binding JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.LinkRef JavaDoc;
37 import javax.naming.Name JavaDoc;
38 import javax.naming.NameClassPair JavaDoc;
39 import javax.naming.NamingEnumeration JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41 import javax.naming.NotContextException JavaDoc;
42 import javax.xml.namespace.QName JavaDoc;
43
44 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
45 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
46 import org.objectweb.fractal.fraclet.annotation.Interface;
47 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
48 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
49 import org.objectweb.fractal.fraclet.annotation.Monolog;
50 import org.objectweb.fractal.fraclet.annotation.Provides;
51 import org.objectweb.petals.util.JNDIUtil;
52 import org.objectweb.petals.util.LoggingUtil;
53 import org.objectweb.petals.util.NamingHelper;
54 import org.objectweb.petals.util.PropertyUtil;
55 import org.objectweb.petals.util.StringHelper;
56 import org.objectweb.util.monolog.api.Logger;
57
58 /**
59  * This registry is the registry used in a distrubed environment
60  *
61  * It used a JNDI distributed directory to manage the endpoint storage
62  *
63  * @author Adrien LOUIS
64  * @author ddesjardins - eBMWebsourcing
65  */

66 @FractalComponent
67 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.registry.Registry.class))
68 public class JNDIRegistry implements Registry {
69
70     /**
71      * Name of the endpoints context
72      */

73     public static final String JavaDoc ENDPOINTS_REF = "endpoints";
74
75     /**
76      * Name of the interfaces context
77      */

78     public static final String JavaDoc INTERFACES_REF = "interfaces";
79
80     /**
81      * Name of the new endpoints context
82      */

83     public static final String JavaDoc NEW_ENDPOINTS_REF = "new-endpoints";
84
85     /**
86      * Name of the service context
87      */

88     public static final String JavaDoc SERVICES_REF = "services";
89
90     /**
91      * Name of the user context
92      */

93     public static final String JavaDoc USERS_REF = "users";
94
95     /**
96      * Endpoint context
97      */

98     protected Context JavaDoc endpointsContext;
99
100     /**
101      * Interfaces context
102      */

103     protected Context JavaDoc interfacesContext;
104
105     /**
106      * Logger wrapper
107      */

108     protected LoggingUtil log;
109
110     /**
111      * Monolog Logger instance.
112      */

113     @Monolog(name="logger")
114     protected Logger logger;
115
116     /**
117      * New endpoint context
118      */

119     protected Context JavaDoc newEndpointContext;
120
121     /**
122      * Root context
123      */

124     protected InitialContext JavaDoc rootContext;
125
126     /**
127      * Service context
128      */

129     protected Context JavaDoc servicesContext;
130
131     /**
132      * Fractal Component Current State.
133      */

134     protected String JavaDoc state;
135
136     /**
137      * Users context
138      */

139     protected Context JavaDoc usersContext;
140
141     /**
142      * @see Registry#cleanNewEndpoints()
143      */

144     public void cleanNewEndpoints() throws RegistryException {
145         try {
146             NamingEnumeration JavaDoc<NameClassPair JavaDoc> names = rootContext
147                     .list(NEW_ENDPOINTS_REF);
148             while (names.hasMore()) {
149                 NameClassPair JavaDoc nameClassPair = (NameClassPair JavaDoc) names.next();
150                 newEndpointContext.unbind(nameClassPair.getName());
151             }
152         } catch (NamingException JavaDoc e) {
153             log.error("Problem while cleaning new endpoints context", e);
154             throw new RegistryException(
155                     "Problem while cleaning new endpoints context", e);
156         }
157     }
158
159     /**
160      * @see Registry#deregisterConnection(LinkedEndpoint)
161      */

162     public void deregisterConnection(LinkedEndpoint linkedEndpoint)
163         throws RegistryException {
164         log.start();
165         if (linkedEndpoint == null) {
166             throw new IllegalArgumentException JavaDoc(
167                     "The connection must be non null");
168         }
169         if (linkedEndpoint.getInterfaces() != null) {
170             Name JavaDoc interf = NamingHelper.qNameToName(linkedEndpoint
171                     .getInterfaces()[0]);
172             if (!JNDIUtil.isBound(rootContext, INTERFACES_REF, interf)) {
173                 log.error("The connection interface name "
174                         + linkedEndpoint.getInterfaces()[0]
175                         + " is not registered");
176                 throw new RegistryException("The connection interface name "
177                         + linkedEndpoint.getInterfaces()[0]
178                         + " is not registered");
179             } else {
180
181                 try {
182                     // check if the provider endpoint is still bound in the
183
// new endpoint context if so its interfaces have not
184
// been resolved
185
if (JNDIUtil.isBound(rootContext, NEW_ENDPOINTS_REF,
186                             linkedEndpoint.getToEndpointName())) {
187                         Context JavaDoc interfaceCtx = (Context JavaDoc) interfacesContext
188                                 .lookup(interf);
189
190                         interfaceCtx.unbind(linkedEndpoint.getToEndpointName());
191                     } else {
192                         // we need to check that we do not remove it from
193
// one of its interfaces context
194
InternalEndpoint internalEndpoint = (InternalEndpoint) getInternalEndpoint(
195                                 linkedEndpoint.getToServiceName(),
196                                 linkedEndpoint.getToEndpointName());
197                         boolean isNotInInt = true;
198                         for (QName JavaDoc i : internalEndpoint.getInterfaces()) {
199                             if (linkedEndpoint.getInterfaces()[0].equals(i)) {
200                                 isNotInInt = false;
201                             }
202                         }
203                         if (isNotInInt) {
204                             Context JavaDoc interfaceCtx = (Context JavaDoc) interfacesContext
205                                     .lookup(interf);
206                             interfaceCtx.unbind(linkedEndpoint
207                                     .getToEndpointName());
208                             if (!interfacesContext.list(interf)
209                                     .hasMoreElements()) {
210                                 interfacesContext.destroySubcontext(interf);
211                             }
212                         }
213                     }
214
215                 } catch (NamingException JavaDoc e) {
216                     log.error(e.getMessage() ,e);
217                     throw new RegistryException(
218                             "problem while unregistering connection with interfacename "
219                                     + linkedEndpoint.getInterfaces()[0]
220                                     + " to "
221                                     + linkedEndpoint.getToEndpointName() + " ");
222                 }
223             }
224         } else {
225             try {
226                 if (!JNDIUtil.isBound(rootContext, ENDPOINTS_REF,
227                         linkedEndpoint.getEndpointName())) {
228                     log.error("The provider endpoint "
229                             + linkedEndpoint.getEndpointName()
230                             + " is not registered in the endpoints");
231                     throw new RegistryException("The provider endpoint "
232                             + linkedEndpoint.getEndpointName()
233                             + " is not registered in the endpoints");
234                 }
235
236                 // If the prov service name is different from the cons service
237
// name we removed it
238
if (!linkedEndpoint.getToServiceName().equals(
239                         linkedEndpoint.getServiceName())) {
240                     retrieveServiceContext(
241                             NamingHelper.qNameToName(linkedEndpoint
242                                     .getServiceName())).unbind(
243                             linkedEndpoint.getEndpointName());
244                     // If the service context is empty, we need to remove it
245
Name JavaDoc serviceName = NamingHelper.qNameToName(linkedEndpoint
246                             .getServiceName());
247                     if (!servicesContext.list(serviceName).hasMore()) {
248                         servicesContext.destroySubcontext(serviceName);
249                     }
250                 }
251
252                 // If the prov endpoint name is different from the cons endpoint
253
// name we removed it
254
if (!linkedEndpoint.getEndpointName().equals(
255                         linkedEndpoint.getToEndpointName())) {
256                     endpointsContext.unbind(linkedEndpoint.getEndpointName());
257                 }
258
259             } catch (NamingException JavaDoc e) {
260                 log.error(e.getMessage(), e);
261                 throw new RegistryException(
262                         "problem while registering connection with service name "
263                                 + linkedEndpoint.getServiceName()
264                                 + " and endpoint name "
265                                 + linkedEndpoint.getEndpointName() + " to "
266                                 + linkedEndpoint.getToEndpointName() + " ");
267             }
268         }
269         log.end();
270     }
271
272     /**
273      * @see org.objectweb.petals.jbi.registry.Registry#deregisterExternalEndpoint(ServiceEndpoint)
274      * Uses the method deregisterInternalEndpoint
275      */

276     public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint)
277         throws RegistryException {
278         log.start();
279         if (externalEndpoint == null) {
280             throw new IllegalArgumentException JavaDoc(
281                     "The external endpoint must be non null");
282         }
283         deregisterInternalEndpoint((AbstractEndpoint) externalEndpoint);
284         log.end();
285     }
286
287     /**
288      * @see Registry#deregisterInternalEndpoint(AbstractEndpoint)
289      * <ol>
290      * <li> Unbind the endpoint from the endpoint context</li>
291      * <li> If the endpoint is still in the new-endpoints context, unbind
292      * the endpoint from the new-endpoints context</li>
293      * <li> Unbind the endpoint from the service context with contains it</li>
294      * <li> If the service subcontext is empty, remove it from the services
295      * context</li>
296      * <li> Unbind the endpoint from the interface context with contains it</li>
297      * <li> If the interface subcontext is empty, remove it from the
298      * interfaces context</li>
299      * </ol>
300      */

301     public void deregisterInternalEndpoint(AbstractEndpoint address)
302         throws RegistryException {
303         log.start();
304         if (address == null) {
305             throw new IllegalArgumentException JavaDoc("The endpoint must be non null");
306         }
307         try {
308             // Check if the endpoint is already registered before deregistering
309
// it
310
if (!JNDIUtil.isBound(rootContext, ENDPOINTS_REF, address
311                     .getEndpointName())) {
312                 throw new RegistryException("The endpoint "
313                         + address.getEndpointName()
314                         + " must be previously registered");
315             }
316
317             //
318
// 1.Unbind from interfaces context
319
if (address.getInterfaces() != null) {
320                 for (QName JavaDoc interfaceName : address.getInterfaces()) {
321                     Name JavaDoc name = NamingHelper.qNameToName(interfaceName);
322                     if (JNDIUtil.isBound(rootContext, INTERFACES_REF, name)) {
323                         Context JavaDoc interfaceCtx = (Context JavaDoc) interfacesContext
324                                 .lookup(name);
325
326                         interfaceCtx.unbind(address.getEndpointName());
327
328                         if (!interfacesContext.list(name).hasMoreElements()) {
329                             interfacesContext.destroySubcontext(name);
330                         }
331                     } else {
332                         log.error("Could not deregister internal endpoint "
333                                 + address.getEndpointName() + " its interface "
334                                 + interfaceName + " is not registered");
335                         throw new RegistryException(
336                                 "Could not deregister internal endpoint "
337                                         + address.getEndpointName()
338                                         + " its interface " + interfaceName
339                                         + " is not registered");
340                     }
341                 }
342             }
343
344             //
345
// 2.Unbind from services context
346
if (!StringHelper.isEmpty(NamingHelper.qNameToString(address
347                     .getServiceName()))) {
348                 Name JavaDoc serviceName = NamingHelper.qNameToName(address
349                         .getServiceName());
350
351                 if (JNDIUtil.isBound(rootContext, SERVICES_REF, serviceName)) {
352                     Context JavaDoc serviceCtx = (Context JavaDoc) servicesContext
353                             .lookup(serviceName);
354
355                     serviceCtx.unbind(address.getEndpointName());
356
357                     // destroy service subcontext if no more endpoints serve
358
// this
359
// services
360
if (!servicesContext.list(serviceName).hasMore()) {
361                         servicesContext.destroySubcontext(serviceName);
362                     }
363                 } else {
364                     log.error("Could not deregister internal endpoint "
365                             + address.getEndpointName() + " its service "
366                             + address.getServiceName() + " is not registered");
367                     throw new RegistryException(
368                             "Could not deregister internal endpoint "
369                                     + address.getEndpointName()
370                                     + " its service "
371                                     + address.getServiceName()
372                                     + " is not registered");
373                 }
374             }
375
376             //
377
// 3.Remove the element from endpoints map
378
endpointsContext.unbind(address.getEndpointName());
379
380             // 3.1.Unbind from new-endpoints context
381
if (JNDIUtil.isBound(rootContext, NEW_ENDPOINTS_REF, address
382                     .getEndpointName())) {
383                 newEndpointContext.unbind(address.getEndpointName());
384             }
385
386         } catch (Exception JavaDoc e) {
387             log.error(e.getMessage(), e);
388             throw new RegistryException(e);
389         }
390         log.end();
391     }
392
393     /**
394      * @see Registry#getExternalEndpoint(QName, String)
395      */

396     public AbstractEndpoint getExternalEndpoint(QName JavaDoc service, String JavaDoc name)
397         throws RegistryException {
398         log.call();
399         return retrieveEndpoint(service, name, ExternalEndpoint.class.getName());
400     }
401
402     /**
403      * @see Registry#getExternalEndpointsForInterface(QName)
404      */

405     public AbstractEndpoint[] getExternalEndpointsForInterface(
406             QName JavaDoc interfaceName) throws RegistryException {
407         log.call();
408         return retrieveEndpointsForInterface(interfaceName,
409                 ExternalEndpoint.class.getName());
410     }
411
412     /**
413      * @see Registry#getExternalEndpointsForService(QName)
414      */

415     public AbstractEndpoint[] getExternalEndpointsForService(QName JavaDoc serviceName) {
416         log.call();
417         return retrieveEndpointsForService(serviceName, ExternalEndpoint.class
418                 .getName());
419     }
420
421     /**
422      * @see Registry#getInternalEndpoint(QName, String)
423      */

424     public AbstractEndpoint getInternalEndpoint(QName JavaDoc service, String JavaDoc name)
425         throws RegistryException {
426         log.call();
427         return retrieveEndpoint(service, name, InternalEndpoint.class.getName());
428     }
429
430     /**
431      * @see Registry#getInternalEndpoint(QName, String,boolean)
432      */

433     public AbstractEndpoint getInternalEndpoint(QName JavaDoc service, String JavaDoc name,
434             boolean resolveLink) throws RegistryException {
435         log.call();
436         return retrieveEndpoint(service, name,
437                 InternalEndpoint.class.getName(), resolveLink);
438     }
439
440     /**
441      * Return an array of all endpoints for an interface
442      *
443      * @param interfaceName
444      * @return a non null array
445      */

446     public AbstractEndpoint[] getInternalEndpointsForInterface(
447             QName JavaDoc interfaceName) throws RegistryException {
448         log.call();
449         return retrieveEndpointsForInterface(interfaceName,
450                 InternalEndpoint.class.getName());
451     }
452
453     /**
454      * @see Registry#getInternalEndpointsForService(QName)
455      */

456     public AbstractEndpoint[] getInternalEndpointsForService(QName JavaDoc serviceName) {
457         log.call();
458         return retrieveEndpointsForService(serviceName, InternalEndpoint.class
459                 .getName());
460     }
461
462     /**
463      * @see org.objectweb.petals.jbi.registry.Registry#getUsersContext()
464      */

465     public Context JavaDoc getUsersContext() {
466         return usersContext;
467     }
468
469     /**
470      * @see Registry#registerConnection(LinkedEndpoint)
471      */

472     public void registerConnection(LinkedEndpoint linkedEndpoint)
473         throws RegistryException {
474         log.start();
475         if (linkedEndpoint == null) {
476             throw new IllegalArgumentException JavaDoc(
477                     "The connection must be non null");
478         }
479         // if (!JNDIUtil.isBound(rootContext, ENDPOINTS_REF, linkedEndpoint
480
// .getToEndpointName())) {
481
// log.error("The provider endpoint "
482
// + linkedEndpoint.getToEndpointName() + " is not registered");
483
// throw new RegistryException("The provider endpoint "
484
// + linkedEndpoint.getToEndpointName() + " is not registered");
485
// } else {
486
if (linkedEndpoint.getInterfaces() != null) {
487             try {
488                 LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
489                         + linkedEndpoint.getToEndpointName());
490                 if (!JNDIUtil.isBound(rootContext, INTERFACES_REF, NamingHelper
491                         .qNameToName(linkedEndpoint.getInterfaces()[0]))) {
492                     retrieveInterfaceContext(
493                             NamingHelper.qNameToName(linkedEndpoint
494                                     .getInterfaces()[0])).bind(
495                             linkedEndpoint.getToEndpointName(), linkRef);
496                 } else {
497                     retrieveInterfaceContext(
498                             NamingHelper.qNameToName(linkedEndpoint
499                                     .getInterfaces()[0])).rebind(
500                             linkedEndpoint.getToEndpointName(), linkRef);
501                 }
502             } catch (NamingException JavaDoc e) {
503                 log.error(e.getMessage(), e);
504                 throw new RegistryException(
505                         "problem while registering connection with interfacename "
506                                 + linkedEndpoint.getInterfaces()[0] + " to "
507                                 + linkedEndpoint.getToEndpointName() + " ");
508             }
509         } else {
510             try {
511                 LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
512                         + linkedEndpoint.getToEndpointName());
513                 if (!JNDIUtil.isBound(rootContext, SERVICES_REF, NamingHelper
514                         .qNameToName(linkedEndpoint.getServiceName()))) {
515                     retrieveServiceContext(
516                             NamingHelper.qNameToName(linkedEndpoint
517                                     .getServiceName())).bind(
518                             linkedEndpoint.getEndpointName(), linkRef);
519                 } else {
520                     retrieveServiceContext(
521                             NamingHelper.qNameToName(linkedEndpoint
522                                     .getServiceName())).rebind(
523                             linkedEndpoint.getEndpointName(), linkRef);
524                 }
525                 if (!JNDIUtil.isBound(rootContext, ENDPOINTS_REF,
526                         linkedEndpoint.getEndpointName())) {
527                     endpointsContext.bind(linkedEndpoint.getEndpointName(),
528                             linkRef);
529                 } else {
530                     endpointsContext.rebind(linkedEndpoint.getEndpointName(),
531                             linkRef);
532                 }
533
534             } catch (NamingException JavaDoc e) {
535                 log.error(e.getMessage(), e);
536                 throw new RegistryException(
537                         "problem while registering connection with service name "
538                                 + linkedEndpoint.getServiceName()
539                                 + " and endpoint name "
540                                 + linkedEndpoint.getEndpointName() + " to "
541                                 + linkedEndpoint.getToEndpointName() + " ");
542             }
543         }
544         // }
545
log.end();
546     }
547
548     /**
549      * @see Registry#registerExternalEndpoint(ServiceEndpoint)
550      */

551     public void registerExternalEndpoint(ServiceEndpoint externalEndpoint)
552         throws RegistryException {
553         log.start();
554         registerEndpoint(externalEndpoint);
555         log.end();
556     }
557
558     /**
559      * @see Registry#registerInternalEndpoint(AbstractEndpoint)
560      */

561     public void registerInternalEndpoint(AbstractEndpoint endpoint)
562         throws RegistryException {
563         log.start();
564         registerEndpoint(endpoint);
565         log.end();
566     }
567
568     /**
569      * @see Registry#retrieveNewEndpoints()
570      */

571     public List JavaDoc<AbstractEndpoint> retrieveNewEndpoints()
572         throws RegistryException {
573         log.start();
574         List JavaDoc<AbstractEndpoint> endpoints = new ArrayList JavaDoc<AbstractEndpoint>();
575         try {
576             NamingEnumeration JavaDoc<Binding JavaDoc> bindings = rootContext
577                     .listBindings(NEW_ENDPOINTS_REF);
578
579             while (bindings.hasMoreElements()) {
580                 Binding JavaDoc binding = bindings.next();
581                 Object JavaDoc object = binding.getObject();
582                 if (object instanceof LinkRef JavaDoc) {
583                     AbstractEndpoint abstractEndpoint = (AbstractEndpoint) rootContext
584                             .lookup(((LinkRef JavaDoc) object).getLinkName());
585                     if (abstractEndpoint.getDescription() == null) {
586                         endpoints.add(abstractEndpoint);
587                     }
588                 }
589             }
590         } catch (Exception JavaDoc e) {
591             String JavaDoc msg = "Problem while retrieving all the new endpoints";
592             log.error(msg, e);
593             throw new RegistryException(msg, e);
594         }
595         log.end();
596         return endpoints;
597     }
598
599     /**
600      * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
601      */

602     @LifeCycle(on=LifeCycleType.START)
603     public void start() throws IllegalLifeCycleException {
604         log = new LoggingUtil(logger);
605         try {
606             initContexts();
607         } catch (JBIException e) {
608             throw new IllegalLifeCycleException(e.getMessage());
609         }
610     }
611
612     /**
613      * @see Registry#validateEndpoint(AbstractEndpoint)
614      */

615     public void validateEndpoint(AbstractEndpoint endpoint)
616         throws RegistryException {
617         log.start();
618         if (endpoint == null) {
619             throw new IllegalArgumentException JavaDoc("The endpoint must be non null");
620         }
621         try {
622             // Add the interfaces to the interfaces context
623
for (QName JavaDoc name : endpoint.getInterfaces()) {
624                 Name JavaDoc nameInterface = NamingHelper.qNameToName(name);
625
626                 Context JavaDoc interfaceCtx = retrieveInterfaceContext(nameInterface);
627                 // Check if the endpoint is already bound
628
if (JNDIUtil.isBound(interfacesContext, nameInterface, endpoint
629                         .getEndpointName())) {
630                     // It is already bound, so we update it
631
LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
632                             + endpoint.getEndpointName());
633                     interfaceCtx.rebind(endpoint.getEndpointName(), linkRef);
634                 } else {
635                     // It is not bound so we bind it
636
LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
637                             + endpoint.getEndpointName());
638                     interfaceCtx.bind(endpoint.getEndpointName(), linkRef);
639                 }
640             }
641             // Update the endpoint in the endpoint context
642
endpointsContext.rebind(endpoint.getEndpointName(), endpoint);
643
644             // Remove the endpoint from the new endpoints context
645
// newEndpointContext.unbind(endpoint.getEndpointName());
646
} catch (Exception JavaDoc e) {
647             log.error("Error while validating endpoint "
648                     + endpoint.getEndpointName(), e);
649             throw new RegistryException(e);
650         }
651         log.end();
652     }
653
654     /**
655      * Bind the endpoint in the endpointcontext
656      *
657      * @param endpoint
658      * endpoint to bind
659      * @throws NamingException
660      */

661     @SuppressWarnings JavaDoc("unchecked")
662     protected void bindInEndpointContext(AbstractEndpoint endpoint)
663         throws NamingException JavaDoc {
664         log.call();
665         if (JNDIUtil.isBound(rootContext, ENDPOINTS_REF, endpoint
666                 .getEndpointName())) {
667             throw new NamingException JavaDoc("Endpoint " + endpoint.getEndpointName()
668                     + " is already bound");
669         } else {
670             endpointsContext.bind(endpoint.getEndpointName(), endpoint);
671         }
672     }
673
674     /**
675      * Bind the endpoint in the newendpointcontext
676      *
677      * @param endpoint
678      * endpoint to bind
679      * @throws NamingException
680      */

681     protected void bindInNewEndpointContext(AbstractEndpoint endpoint)
682         throws NamingException JavaDoc {
683         log.call();
684         LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
685                 + endpoint.getEndpointName());
686         if (!JNDIUtil.isBound(rootContext, NEW_ENDPOINTS_REF, endpoint
687                 .getEndpointName())) {
688             newEndpointContext.bind(endpoint.getEndpointName(), linkRef);
689         }
690     }
691
692     /**
693      * Bind the endpoint in the servicecontext. If no serviceName is specified,
694      * do nothing.
695      *
696      * @param endpoint
697      * endpoint to bind
698      * @throws NamingException
699      */

700     protected void bindInServiceContext(AbstractEndpoint endpoint)
701         throws NamingException JavaDoc {
702         log.call();
703         Name JavaDoc serviceName = NamingHelper.qNameToName(endpoint.getServiceName());
704         if (!StringHelper.isEmpty(NamingHelper.qNameToString(endpoint
705                 .getServiceName()))) {
706             LinkRef JavaDoc linkRef = new LinkRef JavaDoc("/" + ENDPOINTS_REF + "/"
707                     + endpoint.getEndpointName());
708             Context JavaDoc servContext = retrieveServiceContext(serviceName);
709             if (!JNDIUtil.isBound(servicesContext, serviceName, endpoint
710                     .getEndpointName())) {
711                 servContext.bind(endpoint.getEndpointName(), linkRef);
712             }
713         }
714     }
715
716     /**
717      * Create and / or reference contexts "containers", "endpoints", "services"
718      * and "interfaces" in the global directory.
719      *
720      * @throws JBIException
721      * problems while creating or lookup contexts
722      */

723     protected void initContexts() throws RegistryException {
724         log.start();
725         try {
726             rootContext = new InitialContext JavaDoc(PropertyUtil
727                     .retrieveJNDIProperties());
728             if (JNDIUtil.isBound(rootContext, "/", ENDPOINTS_REF)) {
729                 endpointsContext = (Context JavaDoc) rootContext.lookup(ENDPOINTS_REF);
730             } else {
731                 endpointsContext = rootContext.createSubcontext(ENDPOINTS_REF);
732             }
733             if (JNDIUtil.isBound(rootContext, "/", SERVICES_REF)) {
734                 servicesContext = (Context JavaDoc) rootContext.lookup(SERVICES_REF);
735             } else {
736                 servicesContext = rootContext.createSubcontext(SERVICES_REF);
737             }
738             if (JNDIUtil.isBound(rootContext, "/", NEW_ENDPOINTS_REF)) {
739                 newEndpointContext = (Context JavaDoc) rootContext
740                         .lookup(NEW_ENDPOINTS_REF);
741             } else {
742                 newEndpointContext = rootContext
743                         .createSubcontext(NEW_ENDPOINTS_REF);
744             }
745             if (JNDIUtil.isBound(rootContext, "/", INTERFACES_REF)) {
746                 interfacesContext = (Context JavaDoc) rootContext
747                         .lookup(INTERFACES_REF);
748             } else {
749                 interfacesContext = rootContext
750                         .createSubcontext(INTERFACES_REF);
751             }
752             if (JNDIUtil.isBound(rootContext, "/", USERS_REF)) {
753                 usersContext = (Context JavaDoc) rootContext.lookup(USERS_REF);
754             } else {
755                 usersContext = rootContext.createSubcontext(USERS_REF);
756             }
757             if (endpointsContext == null || servicesContext == null
758                     || interfacesContext == null || usersContext == null) {
759                 throw new NotContextException JavaDoc();
760             }
761         } catch (Exception JavaDoc e) {
762             log.error("Problem initating the contexts", e);
763             throw new RegistryException(e);
764         }
765         log.end();
766     }
767
768     /**
769      * @see Registry#registerExternalEndpoint(ServiceEndpoint)
770      */

771     protected void registerEndpoint(ServiceEndpoint endpoint)
772         throws RegistryException {
773         log.start();
774         if (endpoint == null) {
775             throw new IllegalArgumentException JavaDoc("Endpoint must not be null");
776         }
777         if (JNDIUtil.isBound(rootContext, ENDPOINTS_REF, endpoint
778                 .getEndpointName())) {
779             // Retrieve a potential matching endpoint
780
AbstractEndpoint registeredEndpoint = retrieveEndpoint(endpoint
781                     .getEndpointName());
782
783             // registeredEndpoint == null ==> the bound object isn't a real
784
// endpoint (link or anything else)
785
if (registeredEndpoint == null) {
786                 String JavaDoc msg = "You are trying to register an endpoint with a name that "
787                         + "is already bound to an other object "
788                         + "(link or anything else) in the JNDI Registry.";
789                 log.error(msg);
790                 throw new RegistryException(msg);
791             }
792
793             // An endpoint can't be registered with the same endpoint name as a
794
// previously registered endpoint that have different container name
795
// or component name
796
if (!(((AbstractEndpoint) endpoint).getContainerName().equals(
797                     registeredEndpoint.getContainerName()) && ((AbstractEndpoint) endpoint)
798                     .getComponentName().equals(
799                             registeredEndpoint.getComponentName()))) {
800                 String JavaDoc msg = "The endpoint "
801                         + endpoint.getEndpointName()
802                         + " is already registered for an other component/container";
803                 log.error(msg);
804                 throw new RegistryException(msg);
805             }
806
807         } else {
808             try {
809                 bindInEndpointContext((AbstractEndpoint) endpoint);
810
811                 bindInNewEndpointContext((AbstractEndpoint) endpoint);
812
813                 bindInServiceContext((AbstractEndpoint) endpoint);
814             } catch (Exception JavaDoc e) {
815                 log.error("Problem while registering endpoint "
816                         + endpoint.getEndpointName(), e);
817                 throw new RegistryException(
818                         "Problem while registering endpoint "
819                                 + endpoint.getEndpointName());
820             }
821         }
822         log.end();
823     }
824
825     /**
826      * Get the internal/external service endpoint for the named activated
827      * endpoint, if any. If the Endpoint is a link, resolve it and return the
828      * real endpoint
829      *
830      * @param service
831      * the {@link QName} of the service
832      * @param name
833      * the endpoint name
834      * @param endpointClassName
835      * class name of the endpoint
836      * @return the matching endpoint
837      * @throws RegistryException
838      */

839     @SuppressWarnings JavaDoc("unchecked")
840     protected AbstractEndpoint retrieveEndpoint(QName JavaDoc service, String JavaDoc name,
841             String JavaDoc endpointClassName) throws RegistryException {
842         log.call();
843         AbstractEndpoint endpoint = retrieveEndpoint(service, name,
844                 endpointClassName, true);
845         return endpoint;
846     }
847
848     /**
849      * Get the internal/external service endpoint for the named activated
850      * endpoint, if any
851      *
852      * @param service
853      * qualified service name
854      * @param name
855      * endpoint name
856      * @param endpointClassName
857      * class name of the endpoint
858      * @param resolveLink
859      * if the Endpoint is a link, resolve it (true) or return the
860      * link (false)
861      * @return
862      * @throws RegistryException
863      */

864     @SuppressWarnings JavaDoc("unchecked")
865     protected AbstractEndpoint retrieveEndpoint(QName JavaDoc service, String JavaDoc name,
866             String JavaDoc endpointClassName, boolean resolveLink)
867         throws RegistryException {
868         log.start();
869         if (service == null) {
870             throw new IllegalArgumentException JavaDoc(
871                     "The service name must be non null");
872         }
873         if (name == null) {
874             throw new IllegalArgumentException JavaDoc(
875                     "The endpoint name must be non null");
876         }
877         AbstractEndpoint result = null;
878         try {
879             Object JavaDoc outObj = endpointsContext.lookup(name);
880
881             // real endpoint
882
if (outObj instanceof AbstractEndpoint) {
883                 AbstractEndpoint endpoint = (AbstractEndpoint) outObj;
884                 if (endpoint.getClass().getName().equals(endpointClassName)) {
885                     result = endpoint;
886                 }
887             }
888             // link endpoint
889
else if (outObj instanceof LinkRef JavaDoc) {
890                 Object JavaDoc obj = rootContext.lookup(((LinkRef JavaDoc) outObj)
891                         .getLinkName());
892
893                 if (obj instanceof AbstractEndpoint) {
894                     AbstractEndpoint endpoint = (AbstractEndpoint) obj;
895                     if (endpoint.getClass().getName().equals(endpointClassName)) {
896                         // return the real endpoint
897
if (resolveLink) {
898                             result = endpoint;
899                         }
900                         // return a link
901
else {
902                             result = new LinkedEndpoint(service, name, endpoint
903                                     .getServiceName(), endpoint
904                                     .getEndpointName(), null);
905                         }
906                     }
907                 }
908             }
909         } catch (NamingException JavaDoc e1) {
910             // Do nothing
911
}
912         log.end();
913         return result;
914     }
915
916     /**
917      * Retrieve an endpoint in the JNDI registry without making distinction
918      * between Internal and External one.
919      *
920      * @param name
921      * endpoint name
922      * @return an {@link AbstractEndpoint} if the matching endpoint is a real
923      * endpoint, null otherwise (for exemple, the matching endpoint is a
924      * link)
925      * @throws RegistryException
926      */

927     @SuppressWarnings JavaDoc("unchecked")
928     protected AbstractEndpoint retrieveEndpoint(String JavaDoc name)
929         throws RegistryException {
930         log.start();
931         if (name == null) {
932             throw new IllegalArgumentException JavaDoc(
933                     "The endpoint name must be non null");
934         }
935         AbstractEndpoint result = null;
936         try {
937             Object JavaDoc outObj = endpointsContext.lookup(name);
938
939             // real endpoint
940
if (outObj instanceof AbstractEndpoint) {
941                 result = (AbstractEndpoint) outObj;
942             }
943         } catch (NamingException JavaDoc e1) {
944             String JavaDoc msg = "Error while looking up an Endpoint in the JNDI Registry";
945             log.error(msg, e1);
946             throw new RegistryException(msg, e1);
947         }
948         log.end();
949         return result;
950     }
951
952     /**
953      * Retreive the list of endpoint that implements the interface name
954      *
955      * @param interfaceName
956      * the {@link QName} of the interface
957      * @param endpointType
958      * the type (class name) of the endpoint (InternalEndpoint or
959      * ExternalEndpoint)
960      * @return an Array of matching endpoints
961      * @throws RegistryException
962      */

963     @SuppressWarnings JavaDoc("unchecked")
964     protected AbstractEndpoint[] retrieveEndpointsForInterface(
965             QName JavaDoc interfaceName, String JavaDoc endpointType) throws RegistryException {
966         log.start();
967         List JavaDoc<AbstractEndpoint> result = new ArrayList JavaDoc<AbstractEndpoint>();
968         try {
969             if (interfaceName == null) {
970                 // We must return all the endpoints of the specified type
971
NamingEnumeration JavaDoc<Binding JavaDoc> bindings = rootContext
972                         .listBindings(ENDPOINTS_REF);
973
974                 while (bindings.hasMoreElements()) {
975                     Binding JavaDoc binding = (Binding JavaDoc) bindings.next();
976                     if (binding.getObject() instanceof AbstractEndpoint) {
977                         AbstractEndpoint endpoint = (AbstractEndpoint) binding
978                                 .getObject();
979                         if (endpoint.getClass().getName().equals(endpointType)) {
980                             result.add(endpoint);
981                         }
982                     }
983                 }
984             } else {
985                 // We must return only the endpoints for the specified
986
// interface
987
Name JavaDoc name = NamingHelper.qNameToName(interfaceName);
988                 if (JNDIUtil.isBound(rootContext, INTERFACES_REF, name)) {
989                     NamingEnumeration JavaDoc<Binding JavaDoc> bindings = interfacesContext
990                             .listBindings(name);
991
992                     while (bindings.hasMoreElements()) {
993                         Binding JavaDoc binding = bindings.next();
994                         Object JavaDoc object = binding.getObject();
995                         if (object instanceof LinkRef JavaDoc) {
996                             LinkRef JavaDoc linkRef = (LinkRef JavaDoc) object;
997                             Object JavaDoc obj = rootContext.lookup(linkRef
998                                     .getLinkName());
999                             if (obj instanceof AbstractEndpoint) {
1000                                AbstractEndpoint endpoint = (AbstractEndpoint) obj;
1001                                if (endpoint.getClass().getName().equals(
1002                                        endpointType)) {
1003                                    result.add(endpoint);
1004                                }
1005                            }
1006                        }
1007                    }
1008                }
1009            }
1010        } catch (Exception JavaDoc e) {
1011            log.error(e.getMessage(), e);
1012            throw new RegistryException(
1013                    "Problem while getting endpoints for the specified interface "
1014                            + interfaceName);
1015        }
1016        log.end();
1017        return result.toArray(new AbstractEndpoint[0]);
1018    }
1019
1020    /**
1021     * @see Registry#getExternalEndpointsForService(QName)
1022     */

1023    @SuppressWarnings JavaDoc("unchecked")
1024    protected AbstractEndpoint[] retrieveEndpointsForService(QName JavaDoc serviceName,
1025            String JavaDoc endpointType) {
1026        log.start();
1027        if (serviceName == null) {
1028            throw new IllegalArgumentException JavaDoc(
1029                    "The service name must be non null");
1030        }
1031        AbstractEndpoint[] result = new AbstractEndpoint[0];
1032        Name JavaDoc name = NamingHelper.qNameToName(serviceName);
1033        if (JNDIUtil.isBound(rootContext, SERVICES_REF, name)) {
1034            try {
1035                NamingEnumeration JavaDoc<Binding JavaDoc> bindings = servicesContext
1036                        .listBindings(NamingHelper.qNameToName(serviceName));
1037                List JavaDoc<AbstractEndpoint> endpoints = new ArrayList JavaDoc<AbstractEndpoint>();
1038                while (bindings.hasMoreElements()) {
1039                    Binding JavaDoc binding = bindings.next();
1040                    Object JavaDoc object = binding.getObject();
1041                    if (object instanceof LinkRef JavaDoc) {
1042                        LinkRef JavaDoc linkRef = (LinkRef JavaDoc) object;
1043                        Object JavaDoc obj = rootContext.lookup(linkRef.getLinkName());
1044                        if (obj instanceof AbstractEndpoint) {
1045                            AbstractEndpoint endpoint = (AbstractEndpoint) obj;
1046                            if (endpoint.getClass().getName().equals(
1047                                    endpointType)) {
1048                                endpoints.add(endpoint);
1049                            }
1050                        }
1051                    }
1052                }
1053                result = endpoints.toArray(new AbstractEndpoint[0]);
1054            } catch (NamingException JavaDoc e) {
1055                log.error(e.getMessage(), e);
1056            }
1057        }
1058        log.end();
1059        return result;
1060    }
1061
1062    /**
1063     * Return the interfaceContext associated with this interfaceName. If the
1064     * subcontext does not exists, it is created.
1065     *
1066     * @param serviceName
1067     * @throws NamingException
1068     */

1069    protected Context JavaDoc retrieveInterfaceContext(Name JavaDoc interfaceName)
1070        throws NamingException JavaDoc {
1071        log.call();
1072        Context JavaDoc interfContext = null;
1073        if (JNDIUtil.isBound(rootContext, INTERFACES_REF, interfaceName)) {
1074            interfContext = (Context JavaDoc) interfacesContext.lookup(interfaceName);
1075        } else {
1076            interfContext = interfacesContext.createSubcontext(interfaceName);
1077        }
1078        return interfContext;
1079    }
1080
1081    /**
1082     * Return the serviceContext associated with this serviceName. If the
1083     * subcontext does not exists, it is created.
1084     *
1085     * @param serviceName
1086     * @throws NamingException
1087     */

1088    protected Context JavaDoc retrieveServiceContext(Name JavaDoc serviceName)
1089        throws NamingException JavaDoc {
1090        log.call();
1091        Context JavaDoc serviceContext = null;
1092        if (JNDIUtil.isBound(rootContext, SERVICES_REF, serviceName)) {
1093            serviceContext = (Context JavaDoc) servicesContext.lookup(serviceName);
1094        } else {
1095            serviceContext = servicesContext.createSubcontext(serviceName);
1096        }
1097        return serviceContext;
1098    }
1099
1100}
1101
Popular Tags