KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > messaging > MessageExchangeFactoryImpl


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: MessageExchangeFactoryImpl.java,v 1.3 2005/07/25 07:44:35 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.messaging;
27
28 import java.net.URI JavaDoc;
29
30 import javax.jbi.messaging.InOnly;
31 import javax.jbi.messaging.InOptionalOut;
32 import javax.jbi.messaging.InOut;
33 import javax.jbi.messaging.MessageExchange;
34 import javax.jbi.messaging.MessageExchangeFactory;
35 import javax.jbi.messaging.MessagingException;
36 import javax.jbi.messaging.RobustInOnly;
37 import javax.jbi.messaging.MessageExchange.Role;
38 import javax.jbi.servicedesc.ServiceEndpoint;
39 import javax.xml.namespace.QName JavaDoc;
40
41 import org.objectweb.petals.jbi.messaging.types.InOnlyImpl;
42 import org.objectweb.petals.jbi.messaging.types.InOptionalOutImpl;
43 import org.objectweb.petals.jbi.messaging.types.InOutImpl;
44 import org.objectweb.petals.jbi.messaging.types.RobustInOnlyImpl;
45 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
46 import org.objectweb.petals.util.LoggingUtil;
47 import org.objectweb.util.monolog.api.Logger;
48
49 /**
50  * @author Adrien LOUIS - EBM WebSourcing
51  *
52  */

53 public class MessageExchangeFactoryImpl implements MessageExchangeFactory {
54
55     protected DeliveryChannelImpl channel;
56
57     protected ConsumerEndpoint consumerEndpoint;
58
59     protected QName JavaDoc defaultInterfaceName;
60
61     protected ServiceEndpoint defaultServiceEndpoint;
62
63     protected QName JavaDoc defaultServiceName;
64
65     /**
66      * Logger wrapper.
67      */

68     protected LoggingUtil log;
69
70     public MessageExchangeFactoryImpl(DeliveryChannelImpl dc,
71         ConsumerEndpoint ep, Logger logger) {
72         log = new LoggingUtil(logger);
73         this.channel = dc;
74         this.consumerEndpoint = ep;
75     }
76
77     /**
78      * TODO find an endpoint for this parameteters. Return a
79      * <code>MessageExchangeDecorator</code> for Consumer
80      *
81      * @see javax.jbi.messaging.MessageExchangeFactory#createExchange(javax.xml.namespace.QName,
82      * javax.xml.namespace.QName)
83      */

84     public MessageExchange createExchange(QName JavaDoc serviceName, QName JavaDoc operationName)
85         throws MessagingException {
86         log.call(serviceName + "," + operationName);
87
88         MessageExchangeImpl msg = createExchange();
89
90         setDefaultMessageExchangeProperties(msg);
91
92         msg.setService(serviceName);
93
94         msg.setOperation(operationName);
95
96         return new MessageExchangeDecorator(msg, Role.CONSUMER);
97     }
98
99     /**
100      * Return a <code>MessageExchangeDecorator</code> for Consumer, as
101      * <code>InOnlyExchange, InOutExchange,...</code>
102      *
103      * @see javax.jbi.messaging.MessageExchangeFactory#createExchange(java.net.URI)
104      */

105     public MessageExchange createExchange(URI JavaDoc pattern)
106         throws MessagingException {
107         log.call(pattern);
108
109         if (!MessageExchangeImpl.IN_ONLY_PATTERN.equals(pattern)
110             && !MessageExchangeImpl.ROBUST_IN_ONLY_PATTERN.equals(pattern)
111             && !MessageExchangeImpl.IN_OUT_PATTERN.equals(pattern)
112             && !MessageExchangeImpl.IN_OPTIONAL_OUT_PATTERN.equals(pattern)) {
113             throw new MessagingException(
114                 "This Message Exchange Pattern is not recognized by JBI.");
115         }
116
117         return createExchangeDecorator(createExchange(), pattern);
118     }
119
120     /**
121      * Return a <code>MessageExchangeDecorator</code>, as
122      * <code>InOnlyExchange, InOutExchange,...</code>, or
123      * <code>MessageExchangeDecorator</code> if pattern is unknown. The
124      * 'observer' role is set with the current MessageExchangeImpl.getRole()
125      * property.
126      *
127      * @see javax.jbi.messaging.MessageExchangeFactory#createExchange(java.net.URI)
128      */

129     public MessageExchange createExchangeDecorator(
130         MessageExchangeImpl exchange, URI JavaDoc pattern) throws MessagingException {
131         log.call(pattern);
132
133         if (MessageExchangeImpl.IN_ONLY_PATTERN.equals(pattern)) {
134             return new InOnlyImpl(exchange, exchange.getRole());
135         } else if (MessageExchangeImpl.ROBUST_IN_ONLY_PATTERN.equals(pattern)) {
136             return new RobustInOnlyImpl(exchange, exchange.getRole());
137         } else if (MessageExchangeImpl.IN_OUT_PATTERN.equals(pattern)) {
138             return new InOutImpl(exchange, exchange.getRole());
139         } else if (MessageExchangeImpl.IN_OPTIONAL_OUT_PATTERN.equals(pattern)) {
140             return new InOptionalOutImpl(exchange, exchange.getRole());
141         } else {
142             return new MessageExchangeDecorator(exchange, exchange.getRole());
143         }
144     }
145
146     /**
147      * Return a <code>MessageExchangeDecorator</code> for Consumer, as
148      * <code>InOnly</code>.
149      *
150      * @see javax.jbi.messaging.MessageExchangeFactory#createInOnlyExchange()
151      */

152     public InOnly createInOnlyExchange() throws MessagingException {
153         log.call();
154
155         MessageExchangeImpl msg = createExchange();
156
157         return new InOnlyImpl(msg, Role.CONSUMER);
158     }
159
160     /**
161      * Return a <code>MessageExchangeDecorator</code> for Consumer, as
162      * <code>InOptionalOut</code>.
163      *
164      * @see javax.jbi.messaging.MessageExchangeFactory#createInOptionalOutExchange()
165      */

166     public InOptionalOut createInOptionalOutExchange()
167         throws MessagingException {
168         log.call();
169
170         MessageExchangeImpl msg = createExchange();
171
172         return new InOptionalOutImpl(msg, Role.CONSUMER);
173     }
174
175     /**
176      * Return a <code>MessageExchangeDecorator</code> for Consumer, as
177      * <code>InOut</code>.
178      *
179      * @see javax.jbi.messaging.MessageExchangeFactory#createInOutExchange()
180      */

181     public InOut createInOutExchange() throws MessagingException {
182         log.call();
183
184         MessageExchangeImpl msg = createExchange();
185
186         return new InOutImpl(msg, Role.CONSUMER);
187     }
188
189     /**
190      * Return a <code>MessageExchangeDecorator</code> for Consumer, as
191      * <code>RobustInOnly</code>.
192      *
193      * @see javax.jbi.messaging.MessageExchangeFactory#createRobustInOnlyExchange()
194      */

195     public RobustInOnly createRobustInOnlyExchange() throws MessagingException {
196         log.call();
197
198         MessageExchangeImpl msg = createExchange();
199
200         return new RobustInOnlyImpl(msg, Role.CONSUMER);
201     }
202
203     public void setDefaultInterfaceName(QName JavaDoc defaultInterfaceName) {
204         this.defaultInterfaceName = defaultInterfaceName;
205     }
206
207     public void setDefaultServiceEndpoint(ServiceEndpoint defaultServiceEndpoint) {
208         this.defaultServiceEndpoint = defaultServiceEndpoint;
209     }
210
211     public void setDefaultServiceName(QName JavaDoc defaultServiceName) {
212         this.defaultServiceName = defaultServiceName;
213     }
214
215     /**
216      * Create a MessageExchangeImpl, which is the real object that is exchange
217      * between the components
218      *
219      * @return
220      * @throws MessagingException
221      */

222     protected MessageExchangeImpl createExchange() throws MessagingException {
223         log.call();
224
225         channel.checkDeliveryChannelIsOpened();
226
227         MessageExchangeImpl msg = new MessageExchangeImpl(consumerEndpoint);
228
229         setDefaultMessageExchangeProperties(msg);
230
231         msg.setExchangeId(createId());
232
233         return msg;
234     }
235
236     // //////////////////////////////////////////////////////
237
// specific methods
238
// //////////////////////////////////////////////////////
239

240     /**
241      * set to the MessageExchangeImpl the defaultInterfaceName,
242      * defaultServiceName and defaultServiceEndpoint, if they were specified
243      * during the creation of this factory.
244      *
245      * @param msg
246      */

247     protected void setDefaultMessageExchangeProperties(MessageExchangeImpl msg) {
248         log.call();
249
250         if (defaultInterfaceName != null) {
251             msg.setInterfaceName(defaultInterfaceName);
252         }
253         if (defaultServiceName != null) {
254             msg.setService(defaultServiceName);
255         }
256         if (defaultServiceEndpoint != null) {
257             msg.setEndpoint(defaultServiceEndpoint);
258         }
259
260     }
261
262     private String JavaDoc createId() {
263         return "Petals.message.exchange.id." + System.currentTimeMillis();
264     }
265 }
266
Popular Tags