KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > MessageExchangeFactoryImpl


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.messaging;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
20
21 import org.apache.activemq.util.IdGenerator;
22 import org.apache.servicemix.JbiConstants;
23 import org.apache.servicemix.jbi.framework.ComponentContextImpl;
24
25 import javax.jbi.messaging.InOnly;
26 import javax.jbi.messaging.InOptionalOut;
27 import javax.jbi.messaging.InOut;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.messaging.MessageExchangeFactory;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.RobustInOnly;
32 import javax.jbi.servicedesc.ServiceEndpoint;
33 import javax.xml.namespace.QName JavaDoc;
34
35 import java.net.URI JavaDoc;
36 import java.util.Calendar JavaDoc;
37
38 /**
39  * Resolver for URI patterns
40  *
41  * @version $Revision: 429375 $
42  */

43 public class MessageExchangeFactoryImpl implements MessageExchangeFactory {
44
45     private QName JavaDoc interfaceName;
46     private QName JavaDoc serviceName;
47     private QName JavaDoc operationName;
48     private ServiceEndpoint endpoint;
49     private IdGenerator idGenerator;
50     private ComponentContextImpl context;
51     private AtomicBoolean closed;
52
53     /**
54      * Constructor for a factory
55      * @param idGen
56      */

57     public MessageExchangeFactoryImpl(IdGenerator idGen, AtomicBoolean closed){
58         this.idGenerator = idGen;
59         this.closed = closed;
60     }
61     
62     protected void checkNotClosed() throws MessagingException {
63         if (closed.get()) {
64             throw new MessagingException("DeliveryChannel has been closed.");
65         }
66     }
67
68     /**
69      * Create an exchange from the specified pattern
70      *
71      * @param pattern
72      * @return MessageExchange
73      * @throws MessagingException
74      */

75     public MessageExchange createExchange(URI JavaDoc pattern) throws MessagingException {
76         checkNotClosed();
77         MessageExchange result = null;
78         if (pattern != null) {
79             if (pattern.equals(MessageExchangeSupport.IN_ONLY)) {
80                 result = createInOnlyExchange();
81             }
82             else if (pattern.equals(MessageExchangeSupport.IN_OUT)) {
83                 result = createInOutExchange();
84             }
85             else if (pattern.equals(MessageExchangeSupport.IN_OPTIONAL_OUT)) {
86                 result = createInOptionalOutExchange();
87             }
88             else if (pattern.equals(MessageExchangeSupport.ROBUST_IN_ONLY)) {
89                 result = createRobustInOnlyExchange();
90             }
91         }
92         if (result == null) {
93             throw new MessagingException("Do not understand pattern: " + pattern);
94         }
95         return result;
96     }
97
98     /**
99      * create InOnly exchange
100      *
101      * @return InOnly exchange
102      * @throws MessagingException
103      */

104     public InOnly createInOnlyExchange() throws MessagingException {
105         checkNotClosed();
106         InOnlyImpl result = new InOnlyImpl(getExchangeId());
107         setDefaults(result);
108         return result;
109     }
110
111     /**
112      * create RobustInOnly exchange
113      *
114      * @return RobsutInOnly exchange
115      * @throws MessagingException
116      */

117     public RobustInOnly createRobustInOnlyExchange() throws MessagingException {
118         checkNotClosed();
119         RobustInOnlyImpl result = new RobustInOnlyImpl(getExchangeId());
120         setDefaults(result);
121         return result;
122     }
123
124     /**
125      * create InOut Exchange
126      *
127      * @return InOut exchange
128      * @throws MessagingException
129      */

130     public InOut createInOutExchange() throws MessagingException {
131         checkNotClosed();
132         InOutImpl result = new InOutImpl(getExchangeId());
133         setDefaults(result);
134         return result;
135     }
136
137     /**
138      * create InOptionalOut exchange
139      *
140      * @return InOptionalOut exchange
141      * @throws MessagingException
142      */

143     public InOptionalOut createInOptionalOutExchange() throws MessagingException {
144         checkNotClosed();
145         InOptionalOutImpl result = new InOptionalOutImpl(getExchangeId());
146         setDefaults(result);
147         return result;
148     }
149
150     /**
151      * Create an exchange that points at an endpoint that conforms to the declared capabilities, requirements, and
152      * policies of both the consumer and the provider.
153      *
154      * @param serviceName
155      * @param operationName the WSDL name of the operation to be performed
156      * @return a message exchange that is initialized with given interfaceName, operationName, and the endpoint decided
157      * upon by JBI.
158      * @throws MessagingException
159      */

160     public MessageExchange createExchange(QName JavaDoc serviceName, QName JavaDoc operationName) throws MessagingException {
161         // TODO: look for the operation in the wsdl and infer the MEP
162
checkNotClosed();
163         InOptionalOutImpl me = new InOptionalOutImpl(getExchangeId());
164         setDefaults(me);
165         me.setService(serviceName);
166         me.setOperation(operationName);
167         return me;
168     }
169
170     protected String JavaDoc getExchangeId() {
171         return idGenerator.generateId();
172     }
173     
174     /**
175      * @return endpoint
176      */

177     public ServiceEndpoint getEndpoint() {
178         return endpoint;
179     }
180     
181     /**
182      * set endpoint
183      * @param endpoint
184      */

185     public void setEndpoint(ServiceEndpoint endpoint) {
186         this.endpoint = endpoint;
187     }
188     
189     /**
190      * @return interface name
191      */

192     public QName JavaDoc getInterfaceName() {
193         return interfaceName;
194     }
195     
196     /**
197      * set interface name
198      * @param interfaceName
199      */

200     public void setInterfaceName(QName JavaDoc interfaceName) {
201         this.interfaceName = interfaceName;
202     }
203     
204     /**
205      * @return service name
206      */

207     public QName JavaDoc getServiceName() {
208         return serviceName;
209     }
210     
211     /**
212      * set service name
213      * @param serviceName
214      */

215     public void setServiceName(QName JavaDoc serviceName) {
216         this.serviceName = serviceName;
217     }
218     
219     /**
220      * @return Returns the operationName.
221      */

222     public QName JavaDoc getOperationName() {
223         return operationName;
224     }
225
226
227     /**
228      * @param operationName The operationName to set.
229      */

230     public void setOperationName(QName JavaDoc operationName) {
231         this.operationName = operationName;
232     }
233
234     /**
235      * Get the Context
236      * @return the context
237      */

238     public ComponentContextImpl getContext() {
239         return context;
240     }
241
242     /**
243      * Set the Context
244      * @param context
245      */

246     public void setContext(ComponentContextImpl context) {
247         this.context = context;
248     }
249
250     protected void setDefaults(MessageExchangeImpl exchange) {
251         exchange.setOperation(getOperationName());
252         if (endpoint != null) {
253             exchange.setEndpoint(getEndpoint());
254         } else {
255             exchange.setService(serviceName);
256             exchange.setInterfaceName(interfaceName);
257         }
258
259         if (getContext() != null) {
260             exchange.setSourceContext(getContext());
261             PojoMarshaler marshaler = getContext().getActivationSpec().getMarshaler();
262             if (marshaler != null) {
263                 exchange.setMarshaler(marshaler);
264             }
265         }
266         exchange.setProperty(JbiConstants.DATESTAMP_PROPERTY_NAME, Calendar.getInstance());
267     }
268 }
Popular Tags