KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > ComponentContextImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.framework;
18
19 import java.util.MissingResourceException JavaDoc;
20 import java.util.logging.Logger JavaDoc;
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 JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.resource.spi.work.WorkManager JavaDoc;
33 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
43 import org.w3c.dom.DocumentFragment JavaDoc;
44
45 /**
46  * This context provides access to data needed by all JBI components running in the JBI environment.
47  *
48  * @version $Revision: 426415 $
49  */

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     /**
63      * Constructor
64      *
65      * @param container
66      * @param componentName
67      */

68     public ComponentContextImpl(JBIContainer container, ComponentNameSpace componentName) {
69         this.componentName = componentName;
70         this.container = container;
71     }
72
73     /**
74      * Activate the ComponentContext
75      *
76      * @param component
77      * @param channel
78      * @param env
79      * @param spec
80      * @param installRoot
81      */

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         //activate and subscriptions
90
container.getRegistry().registerSubscriptions(this, spec);
91     }
92
93     /**
94      * get the id of the ComponentConnector
95      *
96      * @return the id
97      */

98     public ComponentNameSpace getComponentNameSpace() {
99         return componentName;
100     }
101
102     /**
103      * @return the unique component name
104      */

105     public String JavaDoc getComponentName() {
106         return componentName.getName();
107     }
108
109     /**
110      * @return this component instance
111      */

112     public Component getComponent() {
113         return component;
114     }
115
116     /**
117      * Get WorkManager
118      *
119      * @return the Container's WorkManager
120      */

121     public WorkManager JavaDoc getWorkManager() {
122         return container.getWorkManager();
123     }
124
125     /**
126      * @param serviceName
127      * @param endpointName
128      * @return EndPointReference
129      * @throws JBIException
130      */

131     public ServiceEndpoint activateEndpoint(QName JavaDoc serviceName, String JavaDoc 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     /**
140      * @param serviceName
141      * @return endpoints registered against the service
142      * @throws JBIException
143      */

144     public ServiceEndpoint[] availableEndpoints(QName JavaDoc serviceName) throws JBIException {
145         checkActivated();
146         return container.getRegistry().getEndpointsForService(serviceName);
147     }
148
149     /**
150      * Deregister the endpoint with the NMR
151      *
152      * @param endpoint
153      * @throws JBIException
154      */

155     public void deactivateEndpoint(ServiceEndpoint endpoint) throws JBIException {
156         checkActivated();
157         container.getRegistry().deactivateEndpoint(this, (InternalEndpoint) endpoint);
158     }
159     
160     /**
161      * Register All subscriptions
162      * @param context
163      * @param as
164      */

165     public void registerSubscriptions(ComponentContextImpl context, ActivationSpec as) {
166         checkActivated();
167         container.getRegistry().registerSubscriptions(context, as);
168     }
169     
170     /**
171      * Deregister All subscriptions
172      * @param context
173      * @param as
174      */

175     public void deregisterSubscriptions(ComponentContextImpl context, ActivationSpec as) {
176         checkActivated();
177         container.getRegistry().deregisterSubscriptions(context, as);
178     }
179     
180     /**
181      * @param context
182      * @param subscription
183      * @param endpoint
184      */

185     public void registerSubscription(ComponentContextImpl context, SubscriptionSpec subscription, ServiceEndpoint endpoint) {
186         checkActivated();
187         container.getRegistry().registerSubscription(context, subscription, endpoint);
188     }
189
190     /**
191      * @param context
192      * @param subscription
193      * @return the ServiceEndpoint
194      */

195     public InternalEndpoint deregisterSubscription(ComponentContextImpl context, SubscriptionSpec subscription) {
196         checkActivated();
197         return container.getRegistry().deregisterSubscription(context, subscription);
198     }
199     
200
201     /**
202      * @return the Delivery Channel
203      * @throws MessagingException
204      */

205     public DeliveryChannel getDeliveryChannel() {
206         return deliveryChannel;
207     }
208
209     /**
210      * Retrieve the default JMX Domain Name for MBeans registered in this instance of the JBI implementation.
211      *
212      * @return the JMX domain name for this instance of the JBI implementation.
213      */

214     public String JavaDoc getJmxDomainName() {
215         return container.getManagementContext().getJmxDomainName();
216     }
217
218     /**
219      * Formulate and return the MBean ObjectName of a custom control MBean for a JBI component.
220      *
221      * @param customName the name of the custom control.
222      * @return the JMX ObjectName of the MBean, or <code>null</code> if <code>customName</code> is invalid.
223      */

224     public ObjectName JavaDoc createCustomComponentMBeanName(String JavaDoc customName) {
225         return container.getManagementContext().createCustomComponentMBeanName(customName, componentName.getName());
226     }
227
228     /**
229      * @return the MBeanNames service
230      */

231     public MBeanNames getMBeanNames() {
232         return this;
233     }
234
235     /**
236      * @return theMBean server assocated with the JBI
237      */

238     public MBeanServer JavaDoc getMBeanServer() {
239         return container.getMBeanServer();
240     }
241
242     /**
243      * @return the naming context
244      */

245     public InitialContext JavaDoc getNamingContext() {
246         return container.getNamingContext();
247     }
248
249     /**
250      * Get the TransactionManager for this implementation. The instance returned is an implementation of the standard
251      * JTA interface. If none is available, this method returns <code>null</code>.
252      * <p>
253      * The object returned by this method is untyped, to allow this interface to be compiled in environments that do not
254      * support JTA. If not null, the object returned must be of type <code>javax.transaction.TransactionManager</code>.
255      * <p>
256      * This downcast is necessary because JBI is used in environments that do not support JTA (i.e., J2SE). Explicit use
257      * of JTA types would cause compilation failures in such environments.
258      *
259      * @return A TransactionManager instance, or <code>null</code> if none is available in the execution environment.
260      */

261     public Object JavaDoc getTransactionManager() {
262         return container.getTransactionManager();
263     }
264
265     /**
266      * @return the root directory path
267      */

268     public String JavaDoc getWorkspaceRoot() {
269         if (environment.getWorkspaceRoot() != null) {
270             return environment.getWorkspaceRoot().getAbsolutePath();
271         }
272         return null;
273     }
274
275     /**
276      * @return Returns the container.
277      */

278     public JBIContainer getContainer() {
279         return container;
280     }
281
282     /**
283      * @return Returns the ComponentEnvironment
284      */

285     public ComponentEnvironment getEnvironment() {
286         return environment;
287     }
288     
289     /**
290      * Set the ComponentEnvironment
291      * @param ce
292      */

293     public void setEnvironment(ComponentEnvironment ce){
294           this.environment = ce;
295     }
296
297     /**
298      * @param container The container to set.
299      */

300     public void setContainer(JBIContainer container) {
301         this.container = container;
302     }
303
304     /**
305      * @param deliveryChannel The deliveryChannel to set.
306      */

307     public void setDeliveryChannel(DeliveryChannel deliveryChannel) {
308         this.deliveryChannel = deliveryChannel;
309     }
310
311     /**
312      * Registers the given external endpoint with the NMR. This indicates to the NMR that the given endpoint is used as
313      * a proxy for external service consumers to access an internal service of the same service name (but a different
314      * endpoint name).
315      *
316      * @param externalEndpoint the external endpoint to be registered, must be non-null.
317      * @exception JBIException if an external endpoint with the same name is already registered, by this or another
318      * component.
319      */

320     public void registerExternalEndpoint(ServiceEndpoint externalEndpoint) throws JBIException {
321         checkActivated();
322         if (externalEndpoint == null) {
323             throw new IllegalArgumentException JavaDoc("externalEndpoint should be non null");
324         }
325         container.getRegistry().registerExternalEndpoint(getComponentNameSpace(), externalEndpoint);
326     }
327
328     /**
329      * Deregisters the given external endpoint with the NMR. This indicates to the NMR that the given external endpoint
330      * can no longer be used as a proxy for external service consumers to access an internal service of the same service
331      * name.
332      *
333      * @param externalEndpoint the external endpoint to be deregistered; must be non-null.
334      * @exception JBIException if the given external endpoint was not previously registered.
335      */

336     public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint) throws JBIException {
337         checkActivated();
338         container.getRegistry().deregisterExternalEndpoint(getComponentNameSpace(), externalEndpoint);
339     }
340
341     /**
342      * Resolve the given endpoint reference into a service endpoint. This is called by the component when it has an EPR
343      * that it wants to resolve into a service endpoint.
344      * <p>
345      * Note that the service endpoint returned refers to a dynamic endpoint; the endpoint will exist only as long as
346      * this component retains a strong reference to the object returned by this method. The endpoint may not be included
347      * in the list of "activated" endpoints.
348      *
349      * @param epr endpoint reference as an XML fragment; must be non-null.
350      * @return the service endpoint corresponding to the given endpoint reference; <code>null</code> if the reference
351      * cannot be resolved.
352      */

353     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc epr) {
354         checkActivated();
355         return container.getRegistry().resolveEndpointReference(epr);
356     }
357
358     /**
359      * Get the service endpoint for the named activated endpoint, if any.
360      *
361      * @param service qualified-name of the endpoint's service; must be non-null.
362      * @param name name of the endpoint; must be non-null.
363      * @return the named endpoint, or <code>null</code> if the named endpoint is not activated.
364      */

365     public ServiceEndpoint getEndpoint(QName JavaDoc service, String JavaDoc name) {
366         checkActivated();
367         return container.getRegistry().getEndpoint(service, name);
368     }
369
370     /**
371      * Retrieve the service description metadata for the specified endpoint.
372      * <p>
373      * Note that the result can use either the WSDL 1.1 or WSDL 2.0 description language.
374      *
375      * @param endpoint endpoint reference; must be non-null.
376      * @return metadata describing endpoint, or <code>null</code> if metadata is unavailable.
377      * @exception JBIException invalid endpoint reference.
378      */

379     public Document JavaDoc getEndpointDescriptor(ServiceEndpoint endpoint) throws JBIException {
380         checkActivated();
381         return container.getRegistry().getEndpointDescriptor(endpoint);
382     }
383
384     /**
385      * Queries the NMR for active endpoints that implement the given interface. This will return the endpoints for all
386      * services and endpoints that implement the named interface (portType in WSDL 1.1). This method does NOT include
387      * external endpoints (those registered using {@link #registerExternalEndpoint(ServiceEndpoint)}.
388      *
389      * @param interfaceName qualified name of interface/portType that is implemented by the endpoint; if
390      * <code>null</code> then all activated endpoints in the JBI environment must be returned.
391      * @return an array of available endpoints for the specified interface name; must be non-null; may be empty.
392      */

393     public ServiceEndpoint[] getEndpoints(QName JavaDoc interfaceName) {
394         checkActivated();
395         return container.getRegistry().getEndpointsForInterface(interfaceName);
396     }
397
398     /**
399      * Queries the NMR for active endpoints belonging to the given service. This method does NOT include external
400      * endpoints (those registered using {@link #registerExternalEndpoint(ServiceEndpoint)}.
401      *
402      * @param serviceName qualified name of the service that the endpoints are part of; must be non-null.
403      * @return an array of available endpoints for the specified service name; must be non-null; may be empty.
404      */

405     public ServiceEndpoint[] getEndpointsForService(QName JavaDoc serviceName) {
406         checkActivated();
407         return container.getRegistry().getEndpointsForService(serviceName);
408     }
409
410     /**
411      * Queries the NMR for external endpoints that implement the given interface name. This methods returns only
412      * registered external endpoints (see {@link #registerExternalEndpoint(ServiceEndpoint)}.
413      *
414      * @param interfaceName qualified name of interface implemented by the endpoints; must be non-null.
415      * @return an array of available external endpoints for the specified interface name; must be non-null; may be
416      * empty.
417      */

418     public ServiceEndpoint[] getExternalEndpoints(QName JavaDoc interfaceName) {
419         checkActivated();
420         return container.getRegistry().getExternalEndpoints(interfaceName);
421     }
422
423     /**
424      * Queries the NMR for external endpoints that are part of the given service.
425      *
426      * @param serviceName qualified name of service that contains the endpoints; must be non-null.
427      * @return an array of available external endpoints for the specified service name; must be non-null; may be empty.
428      */

429     public ServiceEndpoint[] getExternalEndpointsForService(QName JavaDoc serviceName) {
430         checkActivated();
431         return container.getRegistry().getExternalEndpointsForService(serviceName);
432     }
433
434     /**
435      * Get the installation root directory path for this component.
436      * <p>
437      * This method MUST return the file path formatted for the underlying platform.
438      *
439      * @return the installation root directory path, in platform-specific form; must be non-null and non-empty.
440      */

441     public String JavaDoc getInstallRoot() {
442         if (environment.getInstallRoot() != null) {
443             return environment.getInstallRoot().getAbsolutePath();
444         }
445         return null;
446     }
447
448     /**
449      * Get a logger instance from JBI. Loggers supplied by JBI are guaranteed to have unique names such that they avoid
450      * name collisions with loggers from other components created using this method. The suffix parameter allows for the
451      * creation of subloggers as needed. The JBI specification says nothing about the exact names to be used, only that
452      * they must be unique across components and the JBI implementation itself.
453      *
454      * @param suffix for creating subloggers; use an empty string for the base component logger; must be non-null.
455      * @param resourceBundleName name of <code>ResourceBundle</code> to be used for localizing messages for the
456      * logger. May be <code>null</code> if none of the messages require localization. The resource, if non-null, must
457      * be loadable using the component's class loader as the initiating loader.
458      * @return a standard logger, named uniquely for this component (plus the given suffix, if applicable); must be
459      * non-null.
460      * @exception MissingResourceException if the ResourceBundleName is non-null and no corresponding resource can be
461      * found.
462      * @exception JBIException if the resourceBundleName has changed from a previous invocation by this component of
463      * this method with the same suffix.
464      */

465     public Logger JavaDoc getLogger(String JavaDoc suffix, String JavaDoc resourceBundleName) throws MissingResourceException JavaDoc, JBIException {
466         String JavaDoc name = suffix != null ? suffix : "";
467         name += componentName.getName();
468         return container.getLogger(name, resourceBundleName);
469     }
470
471     /**
472      * @return the ActivationSpec
473      */

474     public ActivationSpec getActivationSpec() {
475         return activationSpec;
476     }
477
478     private void checkActivated() {
479         if (!activated) {
480             throw new IllegalStateException JavaDoc("ComponentContext not activated");
481         }
482     }
483 }
Popular Tags