001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Emil Ong
047: */
048:
049: package com.caucho.hessian.client;
050:
051: import com.caucho.hessian.io.AbstractHessianOutput;
052: import com.caucho.jms.util.BytesMessageOutputStream;
053:
054: import javax.jms.*;
055: import javax.naming.Context;
056: import javax.naming.InitialContext;
057: import javax.naming.NamingException;
058: import java.io.IOException;
059: import java.lang.reflect.InvocationHandler;
060: import java.lang.reflect.Method;
061: import java.lang.reflect.Proxy;
062: import java.util.logging.Logger;
063:
064: /**
065: * Proxy implementation for Hessian clients using JMS. Applications will
066: * generally use HessianProxyFactory to create proxy clients.
067: */
068: public class HessianJMSProxy implements InvocationHandler {
069: protected static Logger log = Logger
070: .getLogger(HessianJMSProxy.class.getName());
071:
072: private HessianProxyFactory _factory;
073:
074: private MessageProducer _producer;
075: private Session _jmsSession;
076: private Connection _jmsConnection;
077: private String _outboundName;
078:
079: HessianJMSProxy(HessianProxyFactory factory, String outboundName,
080: String connectionFactoryName) throws NamingException,
081: JMSException {
082: _factory = factory;
083: _outboundName = outboundName;
084:
085: Context context = (Context) new InitialContext()
086: .lookup("java:comp/env");
087:
088: ConnectionFactory connectionFactory = (ConnectionFactory) context
089: .lookup(connectionFactoryName);
090:
091: Destination outboundDestination = (Destination) context
092: .lookup(outboundName);
093:
094: _jmsConnection = connectionFactory.createConnection();
095: _jmsSession = _jmsConnection.createSession(false,
096: Session.AUTO_ACKNOWLEDGE);
097:
098: _producer = _jmsSession.createProducer(outboundDestination);
099: }
100:
101: public String getOutboundName() {
102: return _outboundName;
103: }
104:
105: /**
106: * Handles the object invocation.
107: *
108: * @param proxy the proxy object to invoke
109: * @param method the method to call
110: * @param args the arguments to the proxy object
111: */
112: public Object invoke(Object proxy, Method method, Object[] args)
113: throws Throwable {
114: String methodName = method.getName();
115: Class[] params = method.getParameterTypes();
116:
117: // equals and hashCode are special cased
118: if (methodName.equals("equals") && params.length == 1
119: && params[0].equals(Object.class)) {
120: Object value = args[0];
121: if (value == null || !Proxy.isProxyClass(value.getClass()))
122: return new Boolean(false);
123:
124: InvocationHandler handler = Proxy
125: .getInvocationHandler(value);
126:
127: if (!(handler instanceof HessianJMSProxy))
128: return new Boolean(false);
129:
130: String otherOutboundName = ((HessianJMSProxy) handler)
131: .getOutboundName();
132:
133: return new Boolean(_outboundName.equals(otherOutboundName));
134: } else if (methodName.equals("hashCode") && params.length == 0)
135: return new Integer(_outboundName.hashCode());
136: else if (methodName.equals("getHessianType"))
137: return proxy.getClass().getInterfaces()[0].getName();
138: else if (methodName.equals("getHessianURL"))
139: return _outboundName;
140: else if (methodName.equals("toString") && params.length == 0)
141: return "[HessianJMSProxy " + _outboundName + "]";
142:
143: try {
144: if (!_factory.isOverloadEnabled()) {
145: } else if (args != null)
146: methodName = methodName + "__" + args.length;
147: else
148: methodName = methodName + "__0";
149:
150: sendRequest(methodName, args);
151:
152: return null;
153: } catch (Exception e) {
154: throw new HessianRuntimeException(e);
155: }
156: }
157:
158: private void sendRequest(String methodName, Object[] args)
159: throws JMSException, IOException {
160: BytesMessage message = _jmsSession.createBytesMessage();
161:
162: BytesMessageOutputStream os = new BytesMessageOutputStream(
163: message);
164:
165: AbstractHessianOutput out = _factory.getHessianOutput(os);
166:
167: out.call(methodName, args);
168: out.flush();
169:
170: _producer.send(message);
171: }
172: }
|