1 22 package org.objectweb.petals.jbi.management.service; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import javax.jbi.JBIException; 28 import javax.jbi.messaging.MessageExchange; 29 import javax.jbi.servicedesc.ServiceEndpoint; 30 import javax.xml.namespace.QName ; 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 ; 55 56 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 69 protected String containerName; 70 71 74 protected LoggingUtil log; 75 76 79 @Monolog(name="logger") 80 protected Logger logger; 81 82 85 @Requires(name="registry",signature=org.objectweb.petals.jbi.registry.Registry.class) 86 protected Registry registry; 87 88 91 @Requires(name="dist-jmx-server",signature=org.objectweb.petals.kernel.admin.DistributedJMXServerFactory.class) 92 protected DistributedJMXServerFactory serverManager; 93 94 97 public InternalEndpoint activateEndpoint(QName serviceName, 98 String endpointName, ConsumerEndpoint address) throws JBIException { 99 log.start(); 100 if (serviceName == null) { 101 throw new IllegalArgumentException ("Service name must be non null"); 102 } 103 if (endpointName == null) { 104 throw new IllegalArgumentException ("Endpoint name must be non null"); 105 } 106 if (address == null) { 107 throw new IllegalArgumentException ( 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 121 public void createConnection(QName consInterface, QName provService, 122 String provEndpoint) throws JBIException { 123 log.start(); 124 if (consInterface == null) { 125 throw new IllegalArgumentException ( 126 "Interface name must be non null"); 127 } 128 if (provService == null) { 129 throw new IllegalArgumentException ( 130 "Provider service name must be non null"); 131 } 132 if (provEndpoint == null) { 133 throw new IllegalArgumentException ( 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 145 public void createConnection(QName consService, String consEndpoint, 146 QName provService, String provEndpoint) throws JBIException { 147 log.start(); 148 if (consService == null) { 149 throw new IllegalArgumentException ( 150 "Consumer service name must be non null"); 151 } 152 if (consEndpoint == null) { 153 throw new IllegalArgumentException ( 154 "Consumer endpoint name must be non null"); 155 } 156 if (provService == null) { 157 throw new IllegalArgumentException ( 158 "Provider service name must be non null"); 159 } 160 if (provEndpoint == null) { 161 throw new IllegalArgumentException ( 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 173 public void deactivateEndpoint(ServiceEndpoint endpoint) 174 throws JBIException { 175 log.start(); 176 if (endpoint == null) { 177 throw new IllegalArgumentException ("Endpoint must be non null"); 178 } 179 registry.deregisterInternalEndpoint((AbstractEndpoint) endpoint); 180 log.end(); 181 } 182 183 186 public void deleteConnection(QName consInterface, QName provService, 187 String provEndpoint) throws JBIException { 188 log.start(); 189 if (consInterface == null) { 190 throw new IllegalArgumentException ( 191 "Consumer interface name must be non null"); 192 } 193 if (provService == null) { 194 throw new IllegalArgumentException ( 195 "Provider service name must be non null"); 196 } 197 if (provEndpoint == null) { 198 throw new IllegalArgumentException ( 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 210 public void deleteConnection(QName consService, String consEndpoint, 211 QName provService, String provEndpoint) throws JBIException { 212 log.start(); 213 if (consService == null) { 214 throw new IllegalArgumentException ( 215 "Consumer service name must be non null"); 216 } 217 if (consEndpoint == null) { 218 throw new IllegalArgumentException ( 219 "Consumer endpoint name must be non null"); 220 } 221 if (provService == null) { 222 throw new IllegalArgumentException ( 223 "Provider service name must be non null"); 224 } 225 if (provEndpoint == null) { 226 throw new IllegalArgumentException ( 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 238 public void deregisterExternalEndpoint(ServiceEndpoint endpoint) 239 throws JBIException { 240 log.start(); 241 if (endpoint == null) { 242 throw new IllegalArgumentException ("Endpoint must be non null"); 243 } 244 registry.deregisterExternalEndpoint((AbstractEndpoint) endpoint); 245 log.end(); 246 } 247 248 257 public ServiceEndpoint getEndpoint(QName service, String name) { 258 log.call(); 259 return getEndpoint(service, name, true); 260 } 261 262 271 public ServiceEndpoint getEndpoint(QName service, String name, 272 boolean resolveLink) { 273 log.start(); 274 if (service == null) { 275 throw new IllegalArgumentException ("Service name must be non null"); 276 } 277 if (name == null) { 278 throw new IllegalArgumentException ("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 } 287 if (abstractEndpoint != null 288 && abstractEndpoint.getEndpointService() == null) { 289 abstractEndpoint.setEndpointService(this); 290 } 291 log.end(); 292 return abstractEndpoint; 293 } 294 295 298 public Document getEndpointDescriptorForEndpoint(ServiceEndpoint endpoint) 299 throws JBIException { 300 log.start(); 301 if (endpoint == null) { 302 throw new IllegalArgumentException ("Endpoint must be non null"); 303 } 304 Document document = null; 305 try { 306 updateNewEndpoints(); 308 309 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 324 public ServiceEndpoint[] getExternalEndpointsForInterface( 325 QName interfaceName) { 326 log.start(); 327 if (interfaceName == null) { 328 throw new IllegalArgumentException ( 329 "Interface name must be non null"); 330 } 331 ServiceEndpoint[] endpoints = null; 332 try { 333 endpoints = registry 334 .getExternalEndpointsForInterface(interfaceName); 335 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 354 public ServiceEndpoint[] getExternalEndpointsForService(QName serviceName) { 355 log.start(); 356 if (serviceName == null) { 357 throw new IllegalArgumentException ("Service name must be non null"); 358 } 359 ServiceEndpoint[] endpoints = null; 360 endpoints = registry.getExternalEndpointsForService(serviceName); 361 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 381 public QName [] getInterfacesForEndpoint(AbstractEndpoint endpoint) { 382 log.start(); 383 if (endpoint == null) { 384 throw new IllegalArgumentException ("Endpoint must be non null"); 385 } 386 QName [] interfs = new QName [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 e) { 394 log.error("Error while getting interfaces for endpoint " 395 + endpoint.getEndpointName(), e); 396 } 397 log.end(); 398 return interfs; 399 } 400 401 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 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 427 public ServiceEndpoint[] getInternalEndpointsForInterface( 428 QName interfaceName) { 429 log.start(); 430 ServiceEndpoint[] endpoints = new ServiceEndpoint[0]; 431 try { 432 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 458 public ServiceEndpoint[] getInternalEndpointsForService(QName serviceName) { 459 log.start(); 460 if (serviceName == null) { 461 throw new IllegalArgumentException ("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 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 DistributedJMXServer server = serverManager 490 .createDistributedJMXServer(internalEndpoint 491 .getContainerName()); 492 Boolean bool = (Boolean ) server.invoke(server 494 .getAdminServiceMBeanName(), 495 DistributedJMXServer.IS_OK_WITH_CONS, new Object [] { 496 internalEndpoint.getComponentName(), 497 (ServiceEndpoint) internalEndpoint, exchange}, 498 new String [] {String .class.getName(), 499 ServiceEndpoint.class.getName(), 500 MessageExchange.class.getName()}); 501 test = bool.booleanValue(); 502 } catch (Exception e) { 503 log.error("Problem in isExchangeWithConsumerOkayForComponent", e); 504 } 505 log.end(); 506 return test; 507 } 508 509 513 public boolean isExchangeWithProviderOkayForComponent( 514 InternalEndpoint internalEndpoint, MessageExchange exchange) { 515 log.start(); 516 if (internalEndpoint == null) { 517 throw new IllegalArgumentException ("Endpoint must be non null"); 518 } 519 boolean test = true; 520 try { 521 DistributedJMXServer server = serverManager 524 .createDistributedJMXServer(internalEndpoint 525 .getContainerName()); 526 Boolean bool = (Boolean ) server.invoke(server 528 .getAdminServiceMBeanName(), 529 DistributedJMXServer.IS_OK_WITH_PROV, new Object [] { 530 internalEndpoint.getComponentName(), 531 (ServiceEndpoint) internalEndpoint, exchange}, 532 new String [] {String .class.getName(), 533 ServiceEndpoint.class.getName(), 534 MessageExchange.class.getName()}); 535 test = bool.booleanValue(); 536 } catch (Exception e) { 537 log.error("Problem in isExchangeWithProviderOkayForComponent", e); 538 } 539 log.end(); 540 return test; 541 } 542 543 546 public void registerExternalEndpoint(ServiceEndpoint externalEndpoint) 547 throws JBIException { 548 log.start(); 549 if (externalEndpoint == null) { 550 throw new IllegalArgumentException ("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 579 private AbstractEndpoint updateEndpointDescriptionAndInterfaces( 580 AbstractEndpoint endpoint) throws JBIException { 581 log.start(); 582 try { 583 DistributedJMXServer server = serverManager 586 .createDistributedJMXServer(endpoint.getContainerName()); 587 Document document = (Document ) server.invoke(server 589 .getAdminServiceMBeanName(), 590 DistributedJMXServer.GET_SERVICE_DESCRITION, 591 new Object [] {endpoint.getComponentName(), 592 (ServiceEndpoint) endpoint}, new String [] { 593 "java.lang.String", 594 "javax.jbi.servicedesc.ServiceEndpoint"}); 595 server.closeConnector(); 596 List <QName > interfaces = new ArrayList <QName >(); 597 if (document != null) { 598 String description = XMLUtil 600 .createStringFromDOMDocument(document); 601 endpoint.setDescription(description); 602 interfaces = WSDLUtil.getInterfacesForService(document, 604 endpoint.getServiceName()); 605 } 606 607 if (interfaces.size() == 0) { 608 QName unresolved = new QName ( 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 [0])); 618 } catch (Exception e) { 619 log.error(e.getMessage(), e); 620 throw new JBIException(e); 621 } 622 log.end(); 623 return endpoint; 624 } 625 626 629 public boolean isContainerStarted(AbstractEndpoint endpoint) { 630 log.start(); 631 if (endpoint == null) { 632 throw new IllegalArgumentException ("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 |