KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > basic > binding > AbstractBindingComponent


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AbstractBindingComponent.java 601 2006-06-13 12:53:50Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.basic.binding;
23
24 import java.util.logging.Logger JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.management.DeploymentException;
28 import javax.jbi.messaging.DeliveryChannel;
29 import javax.jbi.messaging.ExchangeStatus;
30 import javax.jbi.messaging.MessageExchange;
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.jbi.servicedesc.ServiceEndpoint;
34
35 import org.objectweb.petals.component.common.MEPConstants;
36 import org.objectweb.petals.component.common.basic.AbstractBasicComponent;
37 import org.objectweb.petals.component.common.basic.AbstractServiceUnitManager;
38
39 /**
40  * This class provides a basic binding component, allowing developper to create
41  * simply a new JBI binding component
42  *
43  * @author wjoseph - eBMWebSourcing
44  *
45  */

46 public abstract class AbstractBindingComponent extends AbstractBasicComponent {
47
48     protected JBIListener jbiListener;
49
50     protected ExternalListenerManager externalListenerManager;
51
52     /**
53      * Default contructor
54      */

55     public AbstractBindingComponent() {
56         super();
57     }
58
59     /**
60      * Create a new binding component, setting its logger and channel
61      *
62      * @param channel
63      * the component's DeliveryChannel
64      * @param log
65      * the component's logger
66      */

67     public AbstractBindingComponent(DeliveryChannel channel, Logger JavaDoc log) {
68         super(channel, log);
69     }
70
71     /**
72      * If this binding component must handle jbi messages, you must implement
73      * this method to return your custom {@link JBIListener}.
74      *
75      * @return
76      */

77     public abstract JBIListener getJBIListener();
78
79     /**
80      * If this binding component must handle messages from external service to
81      * internal jbi components, you must implement this method to return your
82      * custom {@link ExternalListenerManager}.
83      *
84      * @return
85      */

86     public abstract ExternalListenerManager getExternalListenerManager();
87
88     @Override JavaDoc
89     protected AbstractServiceUnitManager createServiceUnitManager()
90         throws DeploymentException {
91         AbstractServiceUnitManager serviceUnitManager = null;
92         try {
93             serviceUnitManager = new SimpleBindingComponentServiceUnitManager(
94                 getComponentContext(), log);
95         } catch (JBIException e) {
96             throw new DeploymentException(
97                 "Error when creating the Service Unit Manager", e);
98         }
99
100         return serviceUnitManager;
101     }
102
103     @Override JavaDoc
104     public void start() throws JBIException {
105         jbiListener = getJBIListener();
106         externalListenerManager = getExternalListenerManager();
107         ((SimpleBindingComponentServiceUnitManager) serviceUnitManager)
108             .setExternalListenerManager(externalListenerManager);
109         super.start();
110
111     }
112
113     /**
114      * Process incoming messages for the component. Ignore DONE,ERROR status.
115      * Ignore "consumer" role.
116      */

117     public boolean process(MessageExchange exchange) throws Exception JavaDoc {
118
119         if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())
120             && MessageExchange.Role.PROVIDER.equals(exchange.getRole())) {
121
122             String JavaDoc address = ((SimpleBindingComponentServiceUnitManager) serviceUnitManager)
123                 .getAddressForEndpoint(exchange.getEndpoint());
124
125             if (MEPConstants.IN_ONLY_PATTERN.equals(exchange.getPattern())) {
126                 processInOnly(exchange, address);
127                 
128             } else if (MEPConstants.ROBUST_IN_ONLY_PATTERN.equals(exchange
129                 .getPattern())) {
130                 processRobustInOnly(exchange, address);
131                 
132             } else if (MEPConstants.IN_OUT_PATTERN
133                 .equals(exchange.getPattern())
134                 || MEPConstants.IN_OPTIONAL_OUT_PATTERN.equals(exchange
135                     .getPattern())) {
136                 processInOut(exchange, address);
137                 
138             } else {
139                 Exception JavaDoc e = new Exception JavaDoc(
140                     "MessageExchangePattern not recognized :"
141                         + exchange.getPattern());
142                 exchange.setError(e);
143                 getDeliveryChannel().send(exchange);
144                 throw e;
145             }
146             getDeliveryChannel().send(exchange);
147         }
148         return true;
149     }
150
151     /**
152      * Call listener's 'onJBIMessage()' for an InOnly exchange.
153      * The Exchange status is set to done.
154      * @param exchange
155      * @param address
156      * @throws MessagingException
157      */

158     protected void processInOnly(MessageExchange exchange, String JavaDoc address)
159         throws MessagingException {
160         NormalizedMessage in = exchange.getMessage("in");
161         try{
162         jbiListener.onJBIMessage(address, in, null, exchange.getOperation(),
163             exchange.getPattern());
164         }catch(MessagingException e){
165             // even if there was a problem, the exchange is set to done
166
}
167         exchange.setStatus(ExchangeStatus.DONE);
168     }
169
170     /**
171      * Call listener's 'onJBIMessage()' for an RobustInOnly exchange.
172      * The Exchange status is set to done or a fault is returned on the exchange.
173      * @param exchange
174      * @param address
175      * @throws MessagingException
176      */

177     protected void processRobustInOnly(MessageExchange exchange,String JavaDoc address) throws MessagingException{
178         NormalizedMessage in = exchange.getMessage("in");
179         try {
180             jbiListener.onJBIMessage(address, in, null, exchange.getOperation(), exchange
181                 .getPattern());
182             // exchange is done
183
exchange.setStatus(ExchangeStatus.DONE);
184         } catch (MessagingException e) {
185             // a fault has to be returned
186
exchange.setFault(createFault(new Exception JavaDoc(
187                     "Error sending the message", e), exchange));
188         }
189     }
190
191     /**
192      * Call listener's 'onJBIMessage()' for an InOut or InOptionalOut exchange.
193      * An out or a fault is returned on the exchange
194      * @param exchange
195      * @param address
196      * @throws MessagingException
197      */

198     protected void processInOut(MessageExchange exchange, String JavaDoc address)
199         throws MessagingException {
200         
201         NormalizedMessage out = exchange.createMessage();
202         NormalizedMessage in = exchange.getMessage("in");
203         
204         try {
205             boolean responseSet = jbiListener.onJBIMessage(address, in, out, exchange
206                 .getOperation(), exchange.getPattern());
207             
208             if (responseSet || MEPConstants.IN_OUT_PATTERN.equals(exchange
209                     .getPattern())) {
210                 // response has to be returned
211
exchange.setMessage(out, "out");
212             } else {
213                 // exchange is done
214
exchange.setStatus(ExchangeStatus.DONE);
215             }
216         } catch (MessagingException e) {
217             // a fault has to be returned
218
exchange.setFault(createFault(new Exception JavaDoc(
219                 "Error sending the message", e), exchange));
220         }
221
222     }
223
224     /**
225      * Send a message to the endpoint associated to the distant address. This
226      * method is used by a listener, that listen to messages from external
227      * services (linked to the given address), in order to send these messages
228      * to internal JBI Components.
229      *
230      * @param address
231      * the distant address of the external consumer service
232      * @param messageExchange
233      * the message exchange to be sent to the internal JBI Component
234      * @throws MessagingException
235      * if the address has no endpoint associated to it, or if an
236      * error occurs while sending the message on the container
237      */

238     public void sendMessage(String JavaDoc address, MessageExchange messageExchange)
239         throws MessagingException {
240         ServiceEndpoint ep = ((SimpleBindingComponentServiceUnitManager) serviceUnitManager)
241             .getEndpointForAddress(address);
242         if (ep != null) {
243             messageExchange.setEndpoint(ep);
244             messageExchange.setService(ep.getServiceName());
245             try {
246                 getDeliveryChannel().send(messageExchange);
247             } catch (JBIException e) {
248                 throw new MessagingException(
249                     "Failed to send the mapped message to the delivery channel.");
250             }
251         } else {
252             throw new MessagingException(
253                 "No endpoint associated with this address");
254         }
255     }
256 }
257
Popular Tags