KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > client > HessianJMSProxy


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Emil Ong
47  */

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 JavaDoc;
56 import javax.naming.InitialContext JavaDoc;
57 import javax.naming.NamingException JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.lang.reflect.InvocationHandler JavaDoc;
60 import java.lang.reflect.Method JavaDoc;
61 import java.lang.reflect.Proxy JavaDoc;
62 import java.util.logging.Logger JavaDoc;
63
64 /**
65  * Proxy implementation for Hessian clients using JMS. Applications will
66  * generally use HessianProxyFactory to create proxy clients.
67  */

68 public class HessianJMSProxy implements InvocationHandler JavaDoc {
69   protected static Logger JavaDoc 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 JavaDoc _outboundName;
78  
79   HessianJMSProxy(HessianProxyFactory factory,
80                   String JavaDoc outboundName, String JavaDoc connectionFactoryName)
81     throws NamingException JavaDoc, JMSException
82   {
83     _factory = factory;
84     _outboundName = outboundName;
85
86     Context JavaDoc context = (Context JavaDoc) new InitialContext JavaDoc().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 JavaDoc getOutboundName()
102   {
103     return _outboundName;
104   }
105
106   /**
107    * Handles the object invocation.
108    *
109    * @param proxy the proxy object to invoke
110    * @param method the method to call
111    * @param args the arguments to the proxy object
112    */

113   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc []args)
114     throws Throwable JavaDoc
115   {
116     String JavaDoc methodName = method.getName();
117     Class JavaDoc []params = method.getParameterTypes();
118
119     // equals and hashCode are special cased
120
if (methodName.equals("equals") &&
121         params.length == 1 && params[0].equals(Object JavaDoc.class)) {
122       Object JavaDoc value = args[0];
123       if (value == null || ! Proxy.isProxyClass(value.getClass()))
124         return new Boolean JavaDoc(false);
125
126       InvocationHandler JavaDoc handler = Proxy.getInvocationHandler(value);
127
128       if (! (handler instanceof HessianJMSProxy))
129         return new Boolean JavaDoc(false);
130
131       String JavaDoc otherOutboundName = ((HessianJMSProxy) handler).getOutboundName();
132
133       return new Boolean JavaDoc(_outboundName.equals(otherOutboundName));
134     }
135     else if (methodName.equals("hashCode") && params.length == 0)
136       return new Integer JavaDoc(_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 JavaDoc e) {
156       throw new HessianRuntimeException(e);
157     }
158   }
159
160   private void sendRequest(String JavaDoc methodName, Object JavaDoc []args)
161     throws JMSException, IOException JavaDoc
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