1 48 49 package com.caucho.hessian.client; 50 51 import com.caucho.hessian.io.AbstractHessianOutput; 52 import com.caucho.jms.util.BytesMessageOutputStream; 53 54 import javax.jms.*; 55 import javax.naming.Context ; 56 import javax.naming.InitialContext ; 57 import javax.naming.NamingException ; 58 import java.io.IOException ; 59 import java.lang.reflect.InvocationHandler ; 60 import java.lang.reflect.Method ; 61 import java.lang.reflect.Proxy ; 62 import java.util.logging.Logger ; 63 64 68 public class HessianJMSProxy implements InvocationHandler { 69 protected static Logger log 70 = Logger.getLogger(HessianJMSProxy.class.getName()); 71 72 private HessianProxyFactory _factory; 73 74 private MessageProducer _producer; 75 private Session _jmsSession; 76 private Connection _jmsConnection; 77 private String _outboundName; 78 79 HessianJMSProxy(HessianProxyFactory factory, 80 String outboundName, String connectionFactoryName) 81 throws NamingException , JMSException 82 { 83 _factory = factory; 84 _outboundName = outboundName; 85 86 Context context = (Context ) new InitialContext ().lookup("java:comp/env"); 87 88 ConnectionFactory connectionFactory = 89 (ConnectionFactory) context.lookup(connectionFactoryName); 90 91 Destination outboundDestination = 92 (Destination) context.lookup(outboundName); 93 94 _jmsConnection = connectionFactory.createConnection(); 95 _jmsSession = 96 _jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 97 98 _producer = _jmsSession.createProducer(outboundDestination); 99 } 100 101 public String getOutboundName() 102 { 103 return _outboundName; 104 } 105 106 113 public Object invoke(Object proxy, Method method, Object []args) 114 throws Throwable 115 { 116 String methodName = method.getName(); 117 Class []params = method.getParameterTypes(); 118 119 if (methodName.equals("equals") && 121 params.length == 1 && params[0].equals(Object .class)) { 122 Object value = args[0]; 123 if (value == null || ! Proxy.isProxyClass(value.getClass())) 124 return new Boolean (false); 125 126 InvocationHandler handler = Proxy.getInvocationHandler(value); 127 128 if (! (handler instanceof HessianJMSProxy)) 129 return new Boolean (false); 130 131 String otherOutboundName = ((HessianJMSProxy) handler).getOutboundName(); 132 133 return new Boolean (_outboundName.equals(otherOutboundName)); 134 } 135 else if (methodName.equals("hashCode") && params.length == 0) 136 return new Integer (_outboundName.hashCode()); 137 else if (methodName.equals("getHessianType")) 138 return proxy.getClass().getInterfaces()[0].getName(); 139 else if (methodName.equals("getHessianURL")) 140 return _outboundName; 141 else if (methodName.equals("toString") && params.length == 0) 142 return "[HessianJMSProxy " + _outboundName + "]"; 143 144 try { 145 if (! _factory.isOverloadEnabled()) { 146 } 147 else if (args != null) 148 methodName = methodName + "__" + args.length; 149 else 150 methodName = methodName + "__0"; 151 152 sendRequest(methodName, args); 153 154 return null; 155 } catch (Exception e) { 156 throw new HessianRuntimeException(e); 157 } 158 } 159 160 private void sendRequest(String methodName, Object []args) 161 throws JMSException, IOException 162 { 163 BytesMessage message = _jmsSession.createBytesMessage(); 164 165 BytesMessageOutputStream os = new BytesMessageOutputStream(message); 166 167 AbstractHessianOutput out = _factory.getHessianOutput(os); 168 169 out.call(methodName, args); 170 out.flush(); 171 172 _producer.send(message); 173 } 174 } 175 | Popular Tags |