KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jms > jca > JcaProviderProcessor


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.jms.jca;
18
19 import javax.jbi.messaging.DeliveryChannel;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOnly;
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.jbi.messaging.RobustInOnly;
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Session JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32
33 import org.apache.servicemix.jms.AbstractJmsProcessor;
34 import org.apache.servicemix.jms.JmsEndpoint;
35
36 /**
37  *
38  * @author <a HREF="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
39  */

40 public class JcaProviderProcessor extends AbstractJmsProcessor {
41
42     protected Destination JavaDoc destination;
43     protected Destination JavaDoc replyToDestination;
44     protected DeliveryChannel channel;
45     protected ConnectionFactory JavaDoc connectionFactory;
46     
47     public JcaProviderProcessor(JmsEndpoint endpoint) {
48         super(endpoint);
49     }
50
51     public void start() throws Exception JavaDoc {
52         connectionFactory = getConnectionFactory();
53         channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
54         destination = endpoint.getDestination();
55         if (destination == null) {
56             if (endpoint.getJndiDestinationName() != null) {
57                 InitialContext JavaDoc ctx = getInitialContext();
58                 destination = (Destination JavaDoc) ctx.lookup(endpoint.getJndiDestinationName());
59             } else if (endpoint.getJmsProviderDestinationName() == null) {
60                 throw new IllegalStateException JavaDoc("No destination provided");
61             }
62         }
63     }
64
65
66     public void stop() throws Exception JavaDoc {
67         destination = null;
68         replyToDestination = null;
69     }
70
71     public void process(MessageExchange exchange) throws Exception JavaDoc {
72         if (exchange.getStatus() == ExchangeStatus.DONE) {
73             return;
74         } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
75             return;
76         }
77         if (exchange instanceof InOnly == false &&
78             exchange instanceof RobustInOnly == false) {
79             exchange.setError(new UnsupportedOperationException JavaDoc("Use an InOnly or RobustInOnly MEP"));
80             channel.send(exchange);
81             return;
82         }
83         Connection JavaDoc connection = null;
84         try {
85             connection = connectionFactory.createConnection();
86             Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
87             if (destination == null) {
88                 if (STYLE_QUEUE.equals(endpoint.getDestinationStyle())) {
89                     destination = session.createQueue(endpoint.getJmsProviderDestinationName());
90                 } else {
91                     destination = session.createTopic(endpoint.getJmsProviderDestinationName());
92                 }
93             }
94             MessageProducer JavaDoc producer = session.createProducer(destination);
95             
96             TextMessage JavaDoc msg = session.createTextMessage();
97             NormalizedMessage nm = exchange.getMessage("in");
98             fromNMS(nm, msg);
99             producer.send(msg);
100             exchange.setStatus(ExchangeStatus.DONE);
101             channel.send(exchange);
102         } finally {
103             if (connection != null) {
104                 connection.close();
105             }
106         }
107     }
108
109 }
110
Popular Tags