1 22 package org.objectweb.petals.jbi.component.context; 23 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.MissingResourceException ; 28 import java.util.Set ; 29 import java.util.logging.Logger ; 30 31 import javax.jbi.JBIException; 32 import javax.jbi.component.Component; 33 import javax.jbi.management.MBeanNames; 34 import javax.jbi.messaging.DeliveryChannel; 35 import javax.jbi.messaging.MessagingException; 36 import javax.jbi.servicedesc.ServiceEndpoint; 37 import javax.management.MBeanServer ; 38 import javax.naming.InitialContext ; 39 import javax.xml.namespace.QName ; 40 41 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 42 import org.objectweb.petals.jbi.management.service.EndpointService; 43 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 44 import org.objectweb.petals.jbi.messaging.DeliveryChannelImpl; 45 import org.objectweb.petals.jbi.registry.AbstractEndpoint; 46 import org.objectweb.petals.jbi.registry.ConsumerEndpoint; 47 import org.objectweb.petals.jbi.registry.InternalEndpoint; 48 import org.objectweb.petals.jbi.routing.Router; 49 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 50 import org.objectweb.petals.util.PetalsRuntimeException; 51 import org.objectweb.petals.util.StringHelper; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.DocumentFragment ; 54 55 62 public class ComponentContextImpl implements 63 javax.jbi.component.ComponentContext { 64 65 protected ConsumerEndpoint address; 66 67 protected MBeanNames beanNames; 68 69 protected MBeanServer beanServer; 70 71 protected DeliveryChannelImpl deliveryChannel; 72 73 protected JBIDescriptor descriptor; 74 75 79 protected EndpointService endpointService; 80 81 84 protected Set <ServiceEndpoint> externalEndpoints; 85 86 protected String installationRoot; 87 88 91 protected Set <AbstractEndpoint> internalEndpoints; 92 93 protected Component jbiComponent; 94 95 98 protected org.objectweb.util.monolog.api.Logger logger; 99 100 protected LifeCycleManagerService manager; 101 102 protected Logger rootLogger; 103 104 protected String workspaceRoot; 105 106 protected InitialContext namingContext; 107 108 111 private Router router; 112 113 public ComponentContextImpl() { 114 } 115 116 137 public ComponentContextImpl(JBIDescriptor descriptor, 138 String installationRoot, String workspaceRoot, 139 MBeanNames beanNames, MBeanServer beanServer, 140 LifeCycleManagerService manager, ConsumerEndpoint address, 141 EndpointService endpointService, 142 org.objectweb.util.monolog.api.Logger logger, Router router, 143 InitialContext namingContext) { 144 super(); 145 if (installationRoot == null) { 146 throw new IllegalArgumentException ( 147 "installationRoot must not be null"); 148 } else { 149 if (installationRoot.length() == 0) { 150 throw new IllegalArgumentException ( 151 "installationRoot must not be empty String"); 152 } 153 } 154 if (beanNames == null) { 155 throw new IllegalArgumentException ("beanNames must not be null"); 156 } 157 if (beanServer == null) { 158 throw new IllegalArgumentException ("beanServer must not be null"); 159 } 160 if (workspaceRoot == null) { 161 throw new IllegalArgumentException ("workspaceRoot must not be null"); 162 } else { 163 if (workspaceRoot.length() == 0) { 164 throw new IllegalArgumentException ( 165 "workspaceRoot must not be empty String"); 166 } 167 } 168 this.descriptor = descriptor; 169 this.installationRoot = installationRoot; 170 this.beanNames = beanNames; 171 this.beanServer = beanServer; 172 this.manager = manager; 173 this.address = address; 174 this.internalEndpoints = new HashSet <AbstractEndpoint>(); 175 this.externalEndpoints = new HashSet <ServiceEndpoint>(); 176 this.endpointService = endpointService; 177 this.logger = logger; 178 this.router = router; 179 this.namingContext = namingContext; 180 } 181 182 186 public ServiceEndpoint activateEndpoint(QName serviceName, 187 String endpointName) throws JBIException { 188 if (serviceName == null) { 189 throw new IllegalArgumentException ("serviceName must be non-null"); 190 } 191 if (endpointName == null) { 192 throw new IllegalArgumentException ("endpointName must be non-null"); 193 } 194 InternalEndpoint se = endpointService.activateEndpoint(serviceName, 195 endpointName, address); 196 internalEndpoints.add(se); 197 return se; 198 } 199 200 203 public void deactivateEndpoint(ServiceEndpoint endpoint) 204 throws JBIException { 205 if (endpoint == null) { 206 throw new IllegalArgumentException ("endpoint must be non-null"); 207 } 208 endpointService.deactivateEndpoint(endpoint); 209 internalEndpoints.remove(endpoint); 210 } 211 212 216 public void deregisterAllEndpoints() throws JBIException { 217 for (AbstractEndpoint se : internalEndpoints) { 218 endpointService.deactivateEndpoint(se); 219 internalEndpoints.remove(se); 220 } 221 for (ServiceEndpoint se : externalEndpoints) { 222 endpointService.deregisterExternalEndpoint(se); 223 externalEndpoints.remove(se); 224 } 225 } 226 227 230 public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint) 231 throws JBIException { 232 endpointService.deregisterExternalEndpoint(externalEndpoint); 233 externalEndpoints.remove(externalEndpoint); 234 } 235 236 public ConsumerEndpoint getAddress() { 237 return address; 238 } 239 240 public Component getComponent() { 241 return jbiComponent; 242 } 243 244 247 public String getComponentName() { 248 return descriptor.getComponent().getIdentification().getName(); 249 } 250 251 254 public DeliveryChannel getDeliveryChannel() throws MessagingException { 255 if (deliveryChannel == null) { 256 deliveryChannel = createDeliveryChannel(); 257 } else { 258 if (deliveryChannel.isOpened()) { 259 throw new MessagingException( 260 "The previous DeliveryChannel is still opened"); 261 } else { 262 deliveryChannel = createDeliveryChannel(); 263 } 264 } 265 return deliveryChannel; 266 } 267 268 public DeliveryChannelImpl getDeliveryChannelImpl() { 269 return deliveryChannel; 270 } 271 272 276 public ServiceEndpoint getEndpoint(QName service, String name) { 277 if (service == null) { 278 throw new IllegalArgumentException ("service must not be null"); 279 } 280 if (name == null) { 281 throw new IllegalArgumentException ("name must not be null"); 282 } 283 return endpointService.getEndpoint(service, name, false); 284 } 285 286 289 public Document getEndpointDescriptor(ServiceEndpoint endpoint) 290 throws JBIException { 291 Document result = null; 292 if (endpoint == null) { 293 throw new IllegalArgumentException ("endpoint must not be null"); 294 } 295 if (getEndpoint(endpoint.getServiceName(), endpoint.getEndpointName()) != null) { 296 299 AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoint; 300 if (abstractEndpoint.getEndpointService() == null) { 301 abstractEndpoint.setEndpointService(endpointService); 302 } 303 result = abstractEndpoint.getEndpointDescriptor(); 304 } 305 return result; 306 } 307 308 311 public ServiceEndpoint[] getEndpoints(QName interfaceName) { 312 return endpointService.getInternalEndpointsForInterface(interfaceName); 313 } 314 315 318 public ServiceEndpoint[] getEndpointsForService(QName serviceName) { 319 if (serviceName == null) { 320 throw new IllegalArgumentException ("Service name must be non null"); 321 } 322 return endpointService.getInternalEndpointsForService(serviceName); 323 } 324 325 328 public ServiceEndpoint[] getExternalEndpoints(QName interfaceName) { 329 if (interfaceName == null) { 330 throw new IllegalArgumentException ( 331 "Interface name must be non null"); 332 } 333 return endpointService.getExternalEndpointsForInterface(interfaceName); 334 } 335 336 339 public ServiceEndpoint[] getExternalEndpointsForService(QName serviceName) { 340 if (serviceName == null) { 341 throw new IllegalArgumentException ("Service name must be non null"); 342 } 343 return endpointService.getExternalEndpointsForService(serviceName); 344 } 345 346 349 public String getInstallRoot() { 350 if (StringHelper.isEmpty(installationRoot)) { 351 throw new PetalsRuntimeException( 352 "Installation root must be non-null and non empty " 353 + "for a correctly installed component. Check component installation !"); 354 } 355 return installationRoot; 356 } 357 358 362 public Logger getLogger(String suffix, String resourceBundleName) 363 throws MissingResourceException , JBIException { 364 String loggerName = rootLogger.getName(); 365 if (suffix == null) { 366 throw new IllegalArgumentException ("suffix must not be null"); 367 } else { 368 if (suffix.length() > 0) { 369 loggerName += "." + suffix; 370 } 371 } 372 Logger l = Logger.getLogger(loggerName, resourceBundleName); 373 if (l == null) { 374 l = Logger.getAnonymousLogger(); 375 } 376 if (l == null) { 377 throw new JBIException("No logger can be created"); 378 } 379 return l; 380 381 } 382 383 386 public MBeanNames getMBeanNames() { 387 return beanNames; 388 } 389 390 393 public MBeanServer getMBeanServer() { 394 return beanServer; 395 } 396 397 400 public InitialContext getNamingContext() { 401 return namingContext; 402 } 403 404 409 public Object getTransactionManager() { 410 throw new NoSuchMethodError ("not yet implemented in Petals"); 411 } 412 413 416 public String getWorkspaceRoot() { 417 return workspaceRoot; 418 } 419 420 423 public void registerExternalEndpoint(ServiceEndpoint externalEndpoint) 424 throws JBIException { 425 if (externalEndpoint == null) { 426 throw new IllegalArgumentException ( 427 "externalEndpoint must not be null"); 428 } 429 endpointService.registerExternalEndpoint(externalEndpoint); 430 externalEndpoints.add(externalEndpoint); 431 } 432 433 437 442 public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) { 443 if (epr == null) { 444 throw new IllegalArgumentException ("epr must not be null"); 445 } 446 ServiceEndpoint result = null; 447 Map engines = manager.getEngineCompoLifeCycles(); 449 450 synchronized (engines) { 451 for (Iterator iter = engines.values().iterator(); iter.hasNext() 452 && result == null;) { 453 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next(); 454 455 result = lifeCycle.getComponent().resolveEndpointReference(epr); 456 } 457 } 458 459 if (result == null) { 460 Map bindings = manager.getBindingCompoLifeCycles(); 462 463 synchronized (bindings) { 464 for (Iterator iter = bindings.values().iterator(); iter 465 .hasNext() 466 && result == null;) { 467 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter 468 .next(); 469 470 result = lifeCycle.getComponent().resolveEndpointReference( 471 epr); 472 } 473 } 474 } 475 return result; 476 477 } 478 479 public void setComponent(Component component) { 480 this.jbiComponent = component; 481 } 482 483 public void setDeliveryChannel(DeliveryChannelImpl deliveryChannel) { 484 this.deliveryChannel = deliveryChannel; 485 } 486 487 public void setRootLogger(Logger logger) { 488 rootLogger = logger; 489 } 490 491 protected DeliveryChannelImpl createDeliveryChannel() { 492 return new DeliveryChannelImpl(this, router, logger); 493 } 494 495 public JBIDescriptor getJBIDescriptor() { 496 return this.descriptor; 497 } 498 499 } 500 | Popular Tags |