KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > ComponentSupport


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.components.util;
18
19 import org.apache.servicemix.jbi.NoInMessageAvailableException;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.DocumentFragment JavaDoc;
22
23 import javax.jbi.component.Component;
24 import javax.jbi.component.ComponentLifeCycle;
25 import javax.jbi.component.ServiceUnitManager;
26 import javax.jbi.messaging.DeliveryChannel;
27 import javax.jbi.messaging.InOnly;
28 import javax.jbi.messaging.InOut;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessageExchangeFactory;
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.jbi.servicedesc.ServiceEndpoint;
34 import javax.xml.namespace.QName JavaDoc;
35
36 /**
37  * A useful base class for developers wishing to implement a JBI Component.
38  *
39  * @version $Revision: 432921 $
40  */

41 public abstract class ComponentSupport extends PojoSupport implements Component {
42
43     private ComponentLifeCycle lifeCycle;
44     private ServiceUnitManager serviceManager;
45     private MessageTransformer messageTransformer = CopyTransformer.getInstance();
46
47     protected ComponentSupport() {
48     }
49
50
51     protected ComponentSupport(QName JavaDoc service, String JavaDoc endpoint) {
52         super(service, endpoint);
53     }
54
55     /**
56      * @return the lifecycel control implementation
57      */

58     public ComponentLifeCycle getLifeCycle() {
59         synchronized (this) {
60             if (lifeCycle == null) {
61                 lifeCycle = createComponentLifeCycle();
62             }
63         }
64         return lifeCycle;
65     }
66
67     /**
68      * @return the ServiceUnitManager or null if there isn't one
69      */

70     public ServiceUnitManager getServiceUnitManager() {
71         initializeServiceUnitManager();
72         return serviceManager;
73     }
74
75     /**
76      * @param fragment
77      * @return the description of the specified reference
78      */

79     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc fragment) {
80         return null;
81     }
82
83     /**
84      * Retrieves a DOM representation containing metadata which describes the
85      * service provided by this component, through the given endpoint. The
86      * result can use WSDL 1.1 or WSDL 2.0.
87      *
88      * @param endpoint the service endpoint.
89      * @return the description for the specified service endpoint.
90      */

91
92     public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
93         return null;
94     }
95
96     /**
97      * This method is called by JBI to check if this component, in the role of
98      * provider of the service indicated by the given exchange, can actually
99      * perform the operation desired.
100      *
101      * @param endpoint the endpoint to be used by the consumer; must be
102      * non-null.
103      * @param exchange the proposed message exchange to be performed; must be
104      * non-null.
105      * @return <code>true</code> if this provider component can perform the
106      * given exchange with the described consumer.
107      */

108     public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint,
109                                               MessageExchange exchange) {
110         return true;
111     }
112
113     /**
114      * This method is called by JBI to check if this component, in the role of
115      * consumer of the service indicated by the given exchange, can actually
116      * interact with the provider properly. The provider is described by the
117      * given endpoint and the service description supplied by that endpoint.
118      *
119      * @param endpoint the endpoint to be used by the provider; must be
120      * non-null.
121      * @param exchange the proposed message exchange to be performed; must be
122      * non-null.
123      * @return <code>true</code> if this consumer component can interact with
124      * the described provider to perform the given exchange.
125      */

126     public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint,
127                                               MessageExchange exchange) {
128         return true;
129     }
130
131
132     // Implementation methods
133
//-------------------------------------------------------------------------
134

135     protected synchronized void initializeServiceUnitManager() {
136         if (this.serviceManager == null) {
137             this.serviceManager = createServiceUnitManager();
138         }
139     }
140
141     protected ServiceUnitManager createServiceUnitManager() {
142         return new ServiceUnitManagerSupport();
143     }
144
145     protected ComponentLifeCycle createComponentLifeCycle() {
146         return this;
147     }
148
149
150     /**
151      * Returns the in message or throws an exception if there is no in message.
152      */

153     protected NormalizedMessage getInMessage(MessageExchange exchange) throws NoInMessageAvailableException {
154         NormalizedMessage message = exchange.getMessage("in");
155         if (message == null) {
156             throw new NoInMessageAvailableException(exchange);
157         }
158         return message;
159     }
160
161     public MessageTransformer getMessageTransformer() {
162         return messageTransformer;
163     }
164
165     public void setMessageTransformer(MessageTransformer transformer) {
166         this.messageTransformer = transformer;
167     }
168
169     /**
170      * Performs an invocation where the service, operation or interface name could be specified
171      *
172      * @param exchange
173      * @param in
174      * @param service
175      * @param interfaceName
176      * @param operation
177      */

178     public void invoke(MessageExchange exchange, NormalizedMessage in, QName JavaDoc service, QName JavaDoc interfaceName, QName JavaDoc operation) throws MessagingException {
179         InOnly outExchange = createInOnlyExchange(service, interfaceName, operation);
180         forwardToExchange(exchange, outExchange, in, operation);
181     }
182
183     /**
184      * Creates a new InOnly exchange for the given service, interface and/or operation (any of which can be null).
185      */

186     public InOnly createInOnlyExchange(QName JavaDoc service, QName JavaDoc interfaceName, QName JavaDoc operation) throws MessagingException {
187         DeliveryChannel channel = getDeliveryChannel();
188         MessageExchangeFactory factory = null;
189         if (service != null) {
190             factory = channel.createExchangeFactoryForService(service);
191         }
192         else if (interfaceName != null) {
193             factory = channel.createExchangeFactory(interfaceName);
194         }
195         else {
196             factory = getExchangeFactory();
197         }
198         InOnly outExchange = factory.createInOnlyExchange();
199         if (service != null) {
200             outExchange.setService(service);
201         }
202         if (interfaceName != null) {
203             outExchange.setInterfaceName(interfaceName);
204         }
205         if (operation != null) {
206             outExchange.setOperation(operation);
207         }
208         return outExchange;
209     }
210
211
212     /**
213      * Creates a new InOut exchange for the given service, interface and/or operation (any of which can be null).
214      */

215     public InOut createInOutExchange(QName JavaDoc service, QName JavaDoc interfaceName, QName JavaDoc operation) throws MessagingException {
216         DeliveryChannel channel = getDeliveryChannel();
217         MessageExchangeFactory factory = null;
218         if (service != null) {
219             factory = channel.createExchangeFactoryForService(service);
220         }
221         else if (interfaceName != null) {
222             factory = channel.createExchangeFactory(interfaceName);
223         }
224         else {
225             factory = getExchangeFactory();
226         }
227         InOut outExchange = factory.createInOutExchange();
228         if (service != null) {
229             outExchange.setService(service);
230         }
231         if (interfaceName != null) {
232             outExchange.setInterfaceName(interfaceName);
233         }
234         if (operation != null) {
235             outExchange.setOperation(operation);
236         }
237         return outExchange;
238     }
239
240     protected void forwardToExchange(MessageExchange exchange, InOnly outExchange, NormalizedMessage in, QName JavaDoc operationName) throws MessagingException {
241         if (operationName != null) {
242             exchange.setOperation(operationName);
243         }
244         forwardToExchange(exchange, outExchange, in);
245     }
246
247     protected void forwardToExchange(MessageExchange exchange, InOnly outExchange, NormalizedMessage in) throws MessagingException {
248         NormalizedMessage out = outExchange.createMessage();
249         outExchange.setInMessage(out);
250         getMessageTransformer().transform(exchange, in, out);
251         getDeliveryChannel().send(outExchange);
252     }
253
254 }
255
Popular Tags