summaryrefslogtreecommitdiffstats
path: root/sandbox/rajith/binding.jms/src/main/java/org/apache/tuscany/binding/jms/JMSProxy.java
blob: d088e0f2885335558519fc4337a0318fe2da5307 (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
package org.apache.tuscany.binding.jms;

import java.lang.reflect.Method;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.NamingException;

public class JMSProxy implements MessageListener{
	
	protected Method operationMethod;
    protected SDODataBinding dataBinding;
    private Object entryPointProxy;
    private JMSBinding jmsBinding;
    private Context context;
	private JMSResourceFactory jmsResourceFactory; 
	private OperationSelector operationSelector;
    
	public JMSProxy(Object entryPointProxy,JMSResourceFactory jmsResourceFactory, JMSBinding jmsBinding,SDODataBinding dataBinding, OperationSelector operationSelector) throws NamingException{		
		
		this.entryPointProxy = entryPointProxy;
        this.dataBinding = dataBinding;
		this.jmsBinding = jmsBinding;	
		this.jmsResourceFactory = jmsResourceFactory;
		this.operationSelector = operationSelector;
	}   

	public void onMessage(Message msg){
		
		String operationName = operationSelector.getOperationName(msg);
				       
        try {            
		            	
		    Object[] args = dataBinding.fromXML(((TextMessage)msg).getText());
		    
		    operationMethod = getOperationMethod(entryPointProxy,operationName,args); 
		    
		    Object result = operationMethod.invoke(entryPointProxy,args);
		    
		    // if result is null then the method can be assumed as oneway
		    if (result != null){
		    	String response = dataBinding.toXML(new Object[] {result} );
		    	sendReply(operationName, response);
		    }
		} catch (Exception e) {
			// need to do proper error handling
			e.printStackTrace();
		}
	}
	
	private Method getOperationMethod(Object entryPointProxy2, String operationName, Object[] args) {
		// TODO Auto-generated method stub
		return null;
	}

	private void sendReply(String operationName,String payload) throws JMSException, NamingException{
        
		Session session = jmsResourceFactory.createSession();
    	
    	javax.jms.Message message = jmsResourceFactory.createMessage(session);
    	message.setJMSCorrelationID(operationName);  // I am using the existing header instead of adding a new JMS property.
    	
        ((javax.jms.TextMessage)message).setText((String)payload);
    	
    	Destination destination = null;
    	Object obj = context.lookup(jmsBinding.getDestinationName());
        destination = (Destination) obj;
        
        MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
        session.close();
	}
}