001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.framework;
018:
019: import java.util.MissingResourceException;
020: import java.util.logging.Logger;
021:
022: import javax.jbi.JBIException;
023: import javax.jbi.component.Component;
024: import javax.jbi.component.ComponentContext;
025: import javax.jbi.management.MBeanNames;
026: import javax.jbi.messaging.DeliveryChannel;
027: import javax.jbi.servicedesc.ServiceEndpoint;
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030: import javax.naming.InitialContext;
031: import javax.xml.namespace.QName;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.DocumentFragment;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.servicemix.jbi.container.ActivationSpec;
039: import org.apache.servicemix.jbi.container.ComponentEnvironment;
040: import org.apache.servicemix.jbi.container.JBIContainer;
041: import org.apache.servicemix.jbi.container.SubscriptionSpec;
042: import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
043:
044: /**
045: * This context provides access to data needed by all JBI components running in the JBI environment.
046: *
047: * @version $Revision: 564374 $
048: */
049: public class ComponentContextImpl implements ComponentContext,
050: MBeanNames {
051:
052: private static final Log LOG = LogFactory
053: .getLog(ComponentContextImpl.class);
054:
055: private ComponentNameSpace componentName;
056: private ComponentEnvironment environment;
057: private JBIContainer container;
058: private Component component;
059: private DeliveryChannel deliveryChannel;
060: private ActivationSpec activationSpec;
061: private boolean activated;
062:
063: /**
064: * Constructor
065: *
066: * @param container
067: * @param componentName
068: */
069: public ComponentContextImpl(JBIContainer container,
070: ComponentNameSpace componentName) {
071: this .componentName = componentName;
072: this .container = container;
073: }
074:
075: /**
076: * Activate the ComponentContext
077: *
078: * @param comp
079: * @param channel
080: * @param env
081: * @param spec
082: * @param installRoot
083: */
084: public void activate(Component comp, ComponentEnvironment env,
085: ActivationSpec spec) {
086: this .component = comp;
087: this .environment = env;
088: this .activationSpec = spec;
089: activated = true;
090: //activate and subscriptions
091: container.getRegistry().registerSubscriptions(this , spec);
092: }
093:
094: /**
095: * get the id of the ComponentConnector
096: *
097: * @return the id
098: */
099: public ComponentNameSpace getComponentNameSpace() {
100: return componentName;
101: }
102:
103: /**
104: * @return the unique component name
105: */
106: public String getComponentName() {
107: return componentName.getName();
108: }
109:
110: /**
111: * @return this component instance
112: */
113: public Component getComponent() {
114: return component;
115: }
116:
117: /**
118: * @param serviceName
119: * @param endpointName
120: * @return EndPointReference
121: * @throws JBIException
122: */
123: public ServiceEndpoint activateEndpoint(QName serviceName,
124: String endpointName) throws JBIException {
125: checkActivated();
126: if (LOG.isDebugEnabled()) {
127: LOG.debug("Component: " + componentName.getName()
128: + " activated endpoint: " + serviceName + " : "
129: + endpointName);
130: }
131: return container.getRegistry().activateEndpoint(this ,
132: serviceName, endpointName);
133: }
134:
135: /**
136: * @param serviceName
137: * @return endpoints registered against the service
138: * @throws JBIException
139: */
140: public ServiceEndpoint[] availableEndpoints(QName serviceName)
141: throws JBIException {
142: checkActivated();
143: return container.getRegistry().getEndpointsForService(
144: serviceName);
145: }
146:
147: /**
148: * Deregister the endpoint with the NMR
149: *
150: * @param endpoint
151: * @throws JBIException
152: */
153: public void deactivateEndpoint(ServiceEndpoint endpoint)
154: throws JBIException {
155: checkActivated();
156: container.getRegistry().deactivateEndpoint(this ,
157: (InternalEndpoint) endpoint);
158: }
159:
160: /**
161: * Register All subscriptions
162: * @param context
163: * @param as
164: */
165: public void registerSubscriptions(ComponentContextImpl context,
166: ActivationSpec as) {
167: checkActivated();
168: container.getRegistry().registerSubscriptions(context, as);
169: }
170:
171: /**
172: * Deregister All subscriptions
173: * @param context
174: * @param as
175: */
176: public void deregisterSubscriptions(ComponentContextImpl context,
177: ActivationSpec as) {
178: checkActivated();
179: container.getRegistry().deregisterSubscriptions(context, as);
180: }
181:
182: /**
183: * @param context
184: * @param subscription
185: * @param endpoint
186: */
187: public void registerSubscription(ComponentContextImpl context,
188: SubscriptionSpec subscription, ServiceEndpoint endpoint) {
189: checkActivated();
190: container.getRegistry().registerSubscription(context,
191: subscription, endpoint);
192: }
193:
194: /**
195: * @param context
196: * @param subscription
197: * @return the ServiceEndpoint
198: */
199: public InternalEndpoint deregisterSubscription(
200: ComponentContextImpl context, SubscriptionSpec subscription) {
201: checkActivated();
202: return container.getRegistry().deregisterSubscription(context,
203: subscription);
204: }
205:
206: /**
207: * @return the Delivery Channel
208: * @throws MessagingException
209: */
210: public DeliveryChannel getDeliveryChannel() {
211: return deliveryChannel;
212: }
213:
214: /**
215: * Retrieve the default JMX Domain Name for MBeans registered in this instance of the JBI implementation.
216: *
217: * @return the JMX domain name for this instance of the JBI implementation.
218: */
219: public String getJmxDomainName() {
220: return container.getManagementContext().getJmxDomainName();
221: }
222:
223: /**
224: * Formulate and return the MBean ObjectName of a custom control MBean for a JBI component.
225: *
226: * @param customName the name of the custom control.
227: * @return the JMX ObjectName of the MBean, or <code>null</code> if <code>customName</code> is invalid.
228: */
229: public ObjectName createCustomComponentMBeanName(String customName) {
230: return container.getManagementContext()
231: .createCustomComponentMBeanName(customName,
232: componentName.getName());
233: }
234:
235: /**
236: * @return the MBeanNames service
237: */
238: public MBeanNames getMBeanNames() {
239: return this ;
240: }
241:
242: /**
243: * @return theMBean server assocated with the JBI
244: */
245: public MBeanServer getMBeanServer() {
246: return container.getMBeanServer();
247: }
248:
249: /**
250: * @return the naming context
251: */
252: public InitialContext getNamingContext() {
253: return container.getNamingContext();
254: }
255:
256: /**
257: * Get the TransactionManager for this implementation. The instance returned is an implementation of the standard
258: * JTA interface. If none is available, this method returns <code>null</code>.
259: * <p>
260: * The object returned by this method is untyped, to allow this interface to be compiled in environments that do not
261: * support JTA. If not null, the object returned must be of type <code>javax.transaction.TransactionManager</code>.
262: * <p>
263: * This downcast is necessary because JBI is used in environments that do not support JTA (i.e., J2SE). Explicit use
264: * of JTA types would cause compilation failures in such environments.
265: *
266: * @return A TransactionManager instance, or <code>null</code> if none is available in the execution environment.
267: */
268: public Object getTransactionManager() {
269: return container.getTransactionManager();
270: }
271:
272: /**
273: * @return the root directory path
274: */
275: public String getWorkspaceRoot() {
276: if (environment.getWorkspaceRoot() != null) {
277: return environment.getWorkspaceRoot().getAbsolutePath();
278: }
279: return null;
280: }
281:
282: /**
283: * @return Returns the container.
284: */
285: public JBIContainer getContainer() {
286: return container;
287: }
288:
289: /**
290: * @return Returns the ComponentEnvironment
291: */
292: public ComponentEnvironment getEnvironment() {
293: return environment;
294: }
295:
296: /**
297: * Set the ComponentEnvironment
298: * @param ce
299: */
300: public void setEnvironment(ComponentEnvironment ce) {
301: this .environment = ce;
302: }
303:
304: /**
305: * @param container The container to set.
306: */
307: public void setContainer(JBIContainer container) {
308: this .container = container;
309: }
310:
311: /**
312: * @param deliveryChannel The deliveryChannel to set.
313: */
314: public void setDeliveryChannel(DeliveryChannel deliveryChannel) {
315: this .deliveryChannel = deliveryChannel;
316: }
317:
318: /**
319: * Registers the given external endpoint with the NMR. This indicates to the NMR that the given endpoint is used as
320: * a proxy for external service consumers to access an internal service of the same service name (but a different
321: * endpoint name).
322: *
323: * @param externalEndpoint the external endpoint to be registered, must be non-null.
324: * @exception JBIException if an external endpoint with the same name is already registered, by this or another
325: * component.
326: */
327: public void registerExternalEndpoint(
328: ServiceEndpoint externalEndpoint) throws JBIException {
329: checkActivated();
330: if (externalEndpoint == null) {
331: throw new IllegalArgumentException(
332: "externalEndpoint should be non null");
333: }
334: container.getRegistry().registerExternalEndpoint(
335: getComponentNameSpace(), externalEndpoint);
336: }
337:
338: /**
339: * Deregisters the given external endpoint with the NMR. This indicates to the NMR that the given external endpoint
340: * can no longer be used as a proxy for external service consumers to access an internal service of the same service
341: * name.
342: *
343: * @param externalEndpoint the external endpoint to be deregistered; must be non-null.
344: * @exception JBIException if the given external endpoint was not previously registered.
345: */
346: public void deregisterExternalEndpoint(
347: ServiceEndpoint externalEndpoint) throws JBIException {
348: checkActivated();
349: container.getRegistry().deregisterExternalEndpoint(
350: getComponentNameSpace(), externalEndpoint);
351: }
352:
353: /**
354: * Resolve the given endpoint reference into a service endpoint. This is called by the component when it has an EPR
355: * that it wants to resolve into a service endpoint.
356: * <p>
357: * Note that the service endpoint returned refers to a dynamic endpoint; the endpoint will exist only as long as
358: * this component retains a strong reference to the object returned by this method. The endpoint may not be included
359: * in the list of "activated" endpoints.
360: *
361: * @param epr endpoint reference as an XML fragment; must be non-null.
362: * @return the service endpoint corresponding to the given endpoint reference; <code>null</code> if the reference
363: * cannot be resolved.
364: */
365: public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
366: checkActivated();
367: return container.getRegistry().resolveEndpointReference(epr);
368: }
369:
370: /**
371: * Get the service endpoint for the named activated endpoint, if any.
372: *
373: * @param service qualified-name of the endpoint's service; must be non-null.
374: * @param name name of the endpoint; must be non-null.
375: * @return the named endpoint, or <code>null</code> if the named endpoint is not activated.
376: */
377: public ServiceEndpoint getEndpoint(QName service, String name) {
378: checkActivated();
379: return container.getRegistry().getEndpoint(service, name);
380: }
381:
382: /**
383: * Retrieve the service description metadata for the specified endpoint.
384: * <p>
385: * Note that the result can use either the WSDL 1.1 or WSDL 2.0 description language.
386: *
387: * @param endpoint endpoint reference; must be non-null.
388: * @return metadata describing endpoint, or <code>null</code> if metadata is unavailable.
389: * @exception JBIException invalid endpoint reference.
390: */
391: public Document getEndpointDescriptor(ServiceEndpoint endpoint)
392: throws JBIException {
393: checkActivated();
394: return container.getRegistry().getEndpointDescriptor(endpoint);
395: }
396:
397: /**
398: * Queries the NMR for active endpoints that implement the given interface. This will return the endpoints for all
399: * services and endpoints that implement the named interface (portType in WSDL 1.1). This method does NOT include
400: * external endpoints (those registered using {@link #registerExternalEndpoint(ServiceEndpoint)}.
401: *
402: * @param interfaceName qualified name of interface/portType that is implemented by the endpoint; if
403: * <code>null</code> then all activated endpoints in the JBI environment must be returned.
404: * @return an array of available endpoints for the specified interface name; must be non-null; may be empty.
405: */
406: public ServiceEndpoint[] getEndpoints(QName interfaceName) {
407: checkActivated();
408: return container.getRegistry().getEndpointsForInterface(
409: interfaceName);
410: }
411:
412: /**
413: * Queries the NMR for active endpoints belonging to the given service. This method does NOT include external
414: * endpoints (those registered using {@link #registerExternalEndpoint(ServiceEndpoint)}.
415: *
416: * @param serviceName qualified name of the service that the endpoints are part of; must be non-null.
417: * @return an array of available endpoints for the specified service name; must be non-null; may be empty.
418: */
419: public ServiceEndpoint[] getEndpointsForService(QName serviceName) {
420: checkActivated();
421: return container.getRegistry().getEndpointsForService(
422: serviceName);
423: }
424:
425: /**
426: * Queries the NMR for external endpoints that implement the given interface name. This methods returns only
427: * registered external endpoints (see {@link #registerExternalEndpoint(ServiceEndpoint)}.
428: *
429: * @param interfaceName qualified name of interface implemented by the endpoints; must be non-null.
430: * @return an array of available external endpoints for the specified interface name; must be non-null; may be
431: * empty.
432: */
433: public ServiceEndpoint[] getExternalEndpoints(QName interfaceName) {
434: checkActivated();
435: return container.getRegistry().getExternalEndpoints(
436: interfaceName);
437: }
438:
439: /**
440: * Queries the NMR for external endpoints that are part of the given service.
441: *
442: * @param serviceName qualified name of service that contains the endpoints; must be non-null.
443: * @return an array of available external endpoints for the specified service name; must be non-null; may be empty.
444: */
445: public ServiceEndpoint[] getExternalEndpointsForService(
446: QName serviceName) {
447: checkActivated();
448: return container.getRegistry().getExternalEndpointsForService(
449: serviceName);
450: }
451:
452: /**
453: * Get the installation root directory path for this component.
454: * <p>
455: * This method MUST return the file path formatted for the underlying platform.
456: *
457: * @return the installation root directory path, in platform-specific form; must be non-null and non-empty.
458: */
459: public String getInstallRoot() {
460: if (environment.getInstallRoot() != null) {
461: return environment.getInstallRoot().getAbsolutePath();
462: }
463: return null;
464: }
465:
466: /**
467: * Get a logger instance from JBI. Loggers supplied by JBI are guaranteed to have unique names such that they avoid
468: * name collisions with loggers from other components created using this method. The suffix parameter allows for the
469: * creation of subloggers as needed. The JBI specification says nothing about the exact names to be used, only that
470: * they must be unique across components and the JBI implementation itself.
471: *
472: * @param suffix for creating subloggers; use an empty string for the base component logger; must be non-null.
473: * @param resourceBundleName name of <code>ResourceBundle</code> to be used for localizing messages for the
474: * logger. May be <code>null</code> if none of the messages require localization. The resource, if non-null, must
475: * be loadable using the component's class loader as the initiating loader.
476: * @return a standard logger, named uniquely for this component (plus the given suffix, if applicable); must be
477: * non-null.
478: * @exception MissingResourceException if the ResourceBundleName is non-null and no corresponding resource can be
479: * found.
480: * @exception JBIException if the resourceBundleName has changed from a previous invocation by this component of
481: * this method with the same suffix.
482: */
483: public Logger getLogger(String suffix, String resourceBundleName)
484: throws MissingResourceException, JBIException {
485: String name = suffix != null ? suffix : "";
486: name += componentName.getName();
487: return container.getLogger(name, resourceBundleName);
488: }
489:
490: /**
491: * @return the ActivationSpec
492: */
493: public ActivationSpec getActivationSpec() {
494: return activationSpec;
495: }
496:
497: private void checkActivated() {
498: if (!activated) {
499: throw new IllegalStateException(
500: "ComponentContext not activated");
501: }
502: }
503: }
|