1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.util.MissingResourceException ; 20 import java.util.logging.Logger ; 21 22 import javax.jbi.JBIException; 23 import javax.jbi.component.Component; 24 import javax.jbi.component.ComponentContext; 25 import javax.jbi.management.MBeanNames; 26 import javax.jbi.messaging.DeliveryChannel; 27 import javax.jbi.messaging.MessagingException; 28 import javax.jbi.servicedesc.ServiceEndpoint; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import javax.naming.InitialContext ; 32 import javax.resource.spi.work.WorkManager ; 33 import javax.xml.namespace.QName ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.apache.servicemix.jbi.container.ActivationSpec; 38 import org.apache.servicemix.jbi.container.ComponentEnvironment; 39 import org.apache.servicemix.jbi.container.JBIContainer; 40 import org.apache.servicemix.jbi.container.SubscriptionSpec; 41 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.DocumentFragment ; 44 45 50 public class ComponentContextImpl implements ComponentContext, MBeanNames { 51 52 private static final Log log = LogFactory.getLog(ComponentContextImpl.class); 53 54 private ComponentNameSpace componentName; 55 private ComponentEnvironment environment; 56 private JBIContainer container; 57 private Component component; 58 private DeliveryChannel deliveryChannel; 59 private ActivationSpec activationSpec; 60 private boolean activated; 61 62 68 public ComponentContextImpl(JBIContainer container, ComponentNameSpace componentName) { 69 this.componentName = componentName; 70 this.container = container; 71 } 72 73 82 public void activate(Component component, 83 ComponentEnvironment env, 84 ActivationSpec spec) { 85 this.component = component; 86 this.environment = env; 87 this.activationSpec = spec; 88 activated = true; 89 container.getRegistry().registerSubscriptions(this, spec); 91 } 92 93 98 public ComponentNameSpace getComponentNameSpace() { 99 return componentName; 100 } 101 102 105 public String getComponentName() { 106 return componentName.getName(); 107 } 108 109 112 public Component getComponent() { 113 return component; 114 } 115 116 121 public WorkManager getWorkManager() { 122 return container.getWorkManager(); 123 } 124 125 131 public ServiceEndpoint activateEndpoint(QName serviceName, String endpointName) throws JBIException { 132 checkActivated(); 133 if (log.isDebugEnabled()) { 134 log.debug("Component: " + componentName.getName() + " activated endpoint: " + serviceName + " : " + endpointName); 135 } 136 return container.getRegistry().activateEndpoint(this, serviceName, endpointName); 137 } 138 139 144 public ServiceEndpoint[] availableEndpoints(QName serviceName) throws JBIException { 145 checkActivated(); 146 return container.getRegistry().getEndpointsForService(serviceName); 147 } 148 149 155 public void deactivateEndpoint(ServiceEndpoint endpoint) throws JBIException { 156 checkActivated(); 157 container.getRegistry().deactivateEndpoint(this, (InternalEndpoint) endpoint); 158 } 159 160 165 public void registerSubscriptions(ComponentContextImpl context, ActivationSpec as) { 166 checkActivated(); 167 container.getRegistry().registerSubscriptions(context, as); 168 } 169 170 175 public void deregisterSubscriptions(ComponentContextImpl context, ActivationSpec as) { 176 checkActivated(); 177 container.getRegistry().deregisterSubscriptions(context, as); 178 } 179 180 185 public void registerSubscription(ComponentContextImpl context, SubscriptionSpec subscription, ServiceEndpoint endpoint) { 186 checkActivated(); 187 container.getRegistry().registerSubscription(context, subscription, endpoint); 188 } 189 190 195 public InternalEndpoint deregisterSubscription(ComponentContextImpl context, SubscriptionSpec subscription) { 196 checkActivated(); 197 return container.getRegistry().deregisterSubscription(context, subscription); 198 } 199 200 201 205 public DeliveryChannel getDeliveryChannel() { 206 return deliveryChannel; 207 } 208 209 214 public String getJmxDomainName() { 215 return container.getManagementContext().getJmxDomainName(); 216 } 217 218 224 public ObjectName createCustomComponentMBeanName(String customName) { 225 return container.getManagementContext().createCustomComponentMBeanName(customName, componentName.getName()); 226 } 227 228 231 public MBeanNames getMBeanNames() { 232 return this; 233 } 234 235 238 public MBeanServer getMBeanServer() { 239 return container.getMBeanServer(); 240 } 241 242 245 public InitialContext getNamingContext() { 246 return container.getNamingContext(); 247 } 248 249 261 public Object getTransactionManager() { 262 return container.getTransactionManager(); 263 } 264 265 268 public String getWorkspaceRoot() { 269 if (environment.getWorkspaceRoot() != null) { 270 return environment.getWorkspaceRoot().getAbsolutePath(); 271 } 272 return null; 273 } 274 275 278 public JBIContainer getContainer() { 279 return container; 280 } 281 282 285 public ComponentEnvironment getEnvironment() { 286 return environment; 287 } 288 289 293 public void setEnvironment(ComponentEnvironment ce){ 294 this.environment = ce; 295 } 296 297 300 public void setContainer(JBIContainer container) { 301 this.container = container; 302 } 303 304 307 public void setDeliveryChannel(DeliveryChannel deliveryChannel) { 308 this.deliveryChannel = deliveryChannel; 309 } 310 311 320 public void registerExternalEndpoint(ServiceEndpoint externalEndpoint) throws JBIException { 321 checkActivated(); 322 if (externalEndpoint == null) { 323 throw new IllegalArgumentException ("externalEndpoint should be non null"); 324 } 325 container.getRegistry().registerExternalEndpoint(getComponentNameSpace(), externalEndpoint); 326 } 327 328 336 public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint) throws JBIException { 337 checkActivated(); 338 container.getRegistry().deregisterExternalEndpoint(getComponentNameSpace(), externalEndpoint); 339 } 340 341 353 public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) { 354 checkActivated(); 355 return container.getRegistry().resolveEndpointReference(epr); 356 } 357 358 365 public ServiceEndpoint getEndpoint(QName service, String name) { 366 checkActivated(); 367 return container.getRegistry().getEndpoint(service, name); 368 } 369 370 379 public Document getEndpointDescriptor(ServiceEndpoint endpoint) throws JBIException { 380 checkActivated(); 381 return container.getRegistry().getEndpointDescriptor(endpoint); 382 } 383 384 393 public ServiceEndpoint[] getEndpoints(QName interfaceName) { 394 checkActivated(); 395 return container.getRegistry().getEndpointsForInterface(interfaceName); 396 } 397 398 405 public ServiceEndpoint[] getEndpointsForService(QName serviceName) { 406 checkActivated(); 407 return container.getRegistry().getEndpointsForService(serviceName); 408 } 409 410 418 public ServiceEndpoint[] getExternalEndpoints(QName interfaceName) { 419 checkActivated(); 420 return container.getRegistry().getExternalEndpoints(interfaceName); 421 } 422 423 429 public ServiceEndpoint[] getExternalEndpointsForService(QName serviceName) { 430 checkActivated(); 431 return container.getRegistry().getExternalEndpointsForService(serviceName); 432 } 433 434 441 public String getInstallRoot() { 442 if (environment.getInstallRoot() != null) { 443 return environment.getInstallRoot().getAbsolutePath(); 444 } 445 return null; 446 } 447 448 465 public Logger getLogger(String suffix, String resourceBundleName) throws MissingResourceException , JBIException { 466 String name = suffix != null ? suffix : ""; 467 name += componentName.getName(); 468 return container.getLogger(name, resourceBundleName); 469 } 470 471 474 public ActivationSpec getActivationSpec() { 475 return activationSpec; 476 } 477 478 private void checkActivated() { 479 if (!activated) { 480 throw new IllegalStateException ("ComponentContext not activated"); 481 } 482 } 483 } | Popular Tags |