summaryrefslogtreecommitdiffstats
path: root/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSTargetInvoker.java
blob: 97b2dbef83a1a773358116d55ed1c379e807df5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.apache.tuscany.binding.jms;


import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.tuscany.spi.wire.InvocationRuntimeException;
import org.apache.tuscany.spi.wire.Message;
import org.apache.tuscany.spi.wire.TargetInvoker;

/**
 * Invoke a JMS reference.
 *
 * @version $Rev$ $Date$
 */
public class JMSTargetInvoker implements TargetInvoker {
    private JMSBinding jmsBinding;
    private Context context;
    private String operationName;
	private JMSResourceFactory jmsResourceFactory;
    private OperationSelector operationSelector;
    
    public JMSTargetInvoker(JMSResourceFactory jmsResourceFactory,JMSBinding jmsBinding, String operationName, OperationSelector operationSelector) throws NamingException {
        this.jmsBinding = jmsBinding;
        this.jmsResourceFactory = jmsResourceFactory;
        this.operationName = operationName;
        this.operationSelector = operationSelector;
    }

    public Message invoke(Message msg) throws InvocationRuntimeException {
        try {
            Object resp = invokeTarget(msg.getBody());
            msg.setBody(resp);
        } catch (InvocationTargetException e) {
            msg.setBody(e.getCause());
        }
        return msg;
    }

    public Object invokeTarget(Object payload) throws InvocationTargetException {
        try {
            
        	return sendReceiveMessage(payload);
        	
        } catch (Exception e) { // catch JMS specific error
            
        	throw new AssertionError(e);
        }

    }

    public Object clone() throws CloneNotSupportedException {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(e);
        }
    }

    public boolean isOptimizable() {
        return false;
    }

    public boolean isCacheable() {
        return false;
    }

    public void setCacheable(boolean cacheable) {
    }
    
    private Object sendReceiveMessage(Object payload) throws JMSException, NamingException{
    	
    	Session session = jmsResourceFactory.createSession();
    	
    	javax.jms.Message message = jmsResourceFactory.createMessage(session);
    	operationSelector.setOperationName(operationName,message);  
    	
        ((javax.jms.TextMessage)message).setText((String)payload);
    	
    	Destination destination = (Destination) context.lookup(jmsBinding.getDestinationName());
        
        MessageProducer producer = session.createProducer(destination);
        
        // create a temporary queue and listen to the response
        Destination replyDest = session.createTemporaryQueue();
        message.setJMSReplyTo(replyDest);
        
        producer.send(message);
        producer.close();
        
        MessageConsumer consumer = session.createConsumer(replyDest);
        jmsResourceFactory.startConnection();
        javax.jms.Message reply = consumer.receive(jmsBinding.getTimeToLive());
        consumer.close();
        
        session.close();
        
        return ((javax.jms.TextMessage)reply).getText();
    }
}