001: /*
002: * MessageQueueClient: The message queue client library
003: * Copyright (C) 2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * RPCMessageClient.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice.rpc;
024:
025: // java imports
026: import java.lang.reflect.Method;
027: import java.lang.reflect.Proxy;
028: import java.util.List;
029: import javax.naming.InitialContext;
030:
031: /**
032: * This object is responsible for establishing a connection to the Message
033: * Service and for setting the appropriate RPC proxy and handler so that
034: * messages can be constructed appropriatly.
035: *
036: * @author Brett Chaldecott
037: */
038: public class RPCMessageClient {
039:
040: /**
041: * Creates a new instance of RPCMessageClient
042: */
043: private RPCMessageClient() {
044: }
045:
046: /**
047: * This method is called to setup a async interface so that messages can
048: * be sent properly.
049: *
050: * @return The reference that was created.
051: * @param from The address all the requests will come from.
052: * @param targetInterface The target interface.
053: * @param asyncInterface The asynchronis interface.
054: * @param targetURL The target url for the message.
055: * @exception RPCMessageClientException
056: */
057: public static Object create(String from, Class targetInterface,
058: Class asyncInterface, String targetURL)
059: throws RPCMessageClientException {
060: verify(targetInterface, asyncInterface);
061: RPCMessageHandler handler = new RPCMessageHandler(from,
062: targetInterface, targetURL, true);
063: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
064: new Class[] { asyncInterface }, handler);
065: }
066:
067: /**
068: * This method is called to setup a async interface so that messages can
069: * be sent properly.
070: *
071: * @return The reference that was created.
072: * @param from The address all the requests will come from.
073: * @param targetInterface The target interface.
074: * @param asyncInterface The asynchronis interface.
075: * @param services The list of services for this rpc message
076: * @param broadcast If true this message will be sent to all daemons suppling
077: * the given service.
078: * @exception RPCMessageClientException
079: */
080: public static Object create(String from, Class targetInterface,
081: Class asyncInterface, List services, boolean broadcast)
082: throws RPCMessageClientException {
083: verify(targetInterface, asyncInterface);
084: RPCMessageHandler handler = new RPCMessageHandler(from,
085: targetInterface, services, broadcast, true);
086: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
087: new Class[] { asyncInterface }, handler);
088: }
089:
090: /**
091: * This method is called to setup a async interface so that messages can
092: * be sent properly. The message will only go one way no result will be
093: * received.
094: *
095: * @return The reference that was created.
096: * @param from The address all the requests will come from.
097: * @param targetInterface The target interface.
098: * @param asyncInterface The asynchronis interface.
099: * @param targetURL The target url for the message.
100: * @exception RPCMessageClientException
101: */
102: public static Object createOneWay(String from,
103: Class targetInterface, Class asyncInterface,
104: String targetURL) throws RPCMessageClientException {
105: verify(targetInterface, asyncInterface);
106: RPCMessageHandler handler = new RPCMessageHandler(from,
107: targetInterface, targetURL, false);
108: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
109: new Class[] { asyncInterface }, handler);
110: }
111:
112: /**
113: * This method is called to setup a async interface so that messages can
114: * be sent properly. The message will only go one way no result will be
115: * received.
116: *
117: * @return The reference that was created.
118: * @param from The address all the requests will come from.
119: * @param targetInterface The target interface.
120: * @param asyncInterface The asynchronis interface.
121: * @param services The list of services for this rpc message
122: * @param broadcast If true this message will be sent to all daemons suppling
123: * the given service.
124: * @exception RPCMessageClientException
125: */
126: public static Object createOneWay(String from,
127: Class targetInterface, Class asyncInterface, List services,
128: boolean broadcast) throws RPCMessageClientException {
129: verify(targetInterface, asyncInterface);
130: RPCMessageHandler handler = new RPCMessageHandler(from,
131: targetInterface, services, broadcast, false);
132: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
133: new Class[] { asyncInterface }, handler);
134: }
135:
136: /**
137: * This method is called to setup a async interface so that messages can
138: * be sent properly.
139: *
140: * @return The reference that was created.
141: * @param from The address all the requests will come from.
142: * @param targetInterface The target interface.
143: * @param asyncInterface The asynchronis interface.
144: * @param targetURL The target url for the message.
145: * @param correlationId The correlation id for this message.
146: * @exception RPCMessageClientException
147: */
148: public static Object create(String from, Class targetInterface,
149: Class asyncInterface, String targetURL, String correlationId)
150: throws RPCMessageClientException {
151: verify(targetInterface, asyncInterface);
152: RPCMessageHandler handler = new RPCMessageHandler(from,
153: targetInterface, targetURL, correlationId);
154: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
155: new Class[] { asyncInterface }, handler);
156: }
157:
158: /**
159: * This method is called to setup a async interface so that messages can
160: * be sent properly.
161: *
162: * @return The reference that was created.
163: * @param from The address all the requests will come from.
164: * @param targetInterface The target interface.
165: * @param asyncInterface The asynchronis interface.
166: * @param services The list of services for this rpc message
167: * @param broadcast If true this message will be sent to all daemons suppling
168: * the given service.
169: * @param correlationId The correlation id for this message.
170: * @exception RPCMessageClientException
171: */
172: public static Object create(String from, Class targetInterface,
173: Class asyncInterface, List services, boolean broadcast,
174: String correlationId) throws RPCMessageClientException {
175: verify(targetInterface, asyncInterface);
176: RPCMessageHandler handler = new RPCMessageHandler(from,
177: targetInterface, services, broadcast, correlationId);
178: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
179: new Class[] { asyncInterface }, handler);
180: }
181:
182: /**
183: * This method is called to setup a async interface so that messages can
184: * be sent properly.
185: *
186: * @return The reference that was created.
187: * @param jndiURL The url of the message producer.
188: * @param context The context of the message service.
189: * @param from The address all the requests will come from.
190: * @param targetInterface The target interface.
191: * @param asyncInterface The asynchronis interface.
192: * @param targetURL The target url for the message.
193: * @exception RPCMessageClientException
194: */
195: public static Object create(InitialContext context, String jndiURL,
196: String from, Class targetInterface, Class asyncInterface,
197: String targetURL) throws RPCMessageClientException {
198: verify(targetInterface, asyncInterface);
199: RPCMessageHandler handler = new RPCMessageHandler(context,
200: jndiURL, from, targetInterface, targetURL);
201: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
202: new Class[] { asyncInterface }, handler);
203: }
204:
205: /**
206: * This method is called to setup a async interface so that messages can
207: * be sent properly.
208: *
209: * @return The reference that was created.
210: * @param context The context of the message service.
211: * @param jndiURL The url of the message producer.
212: * @param from The address all the requests will come from.
213: * @param targetInterface The target interface.
214: * @param asyncInterface The asynchronis interface.
215: * @param services The list of services for this rpc message
216: * @param broadcast If true this message will be sent to all daemons suppling
217: * the given service.
218: * @exception RPCMessageClientException
219: */
220: public static Object create(InitialContext context, String jndiURL,
221: String from, Class targetInterface, Class asyncInterface,
222: List services, boolean broadcast)
223: throws RPCMessageClientException {
224: verify(targetInterface, asyncInterface);
225: RPCMessageHandler handler = new RPCMessageHandler(context,
226: jndiURL, from, targetInterface, services, broadcast);
227: return Proxy.newProxyInstance(asyncInterface.getClassLoader(),
228: new Class[] { asyncInterface }, handler);
229: }
230:
231: /**
232: * This method is responsible for verifying the target interface and async
233: * interface in sync.
234: *
235: * @param targetInterface The interface the call are made onto.
236: * @param asyncInterface The async interface the calls are made with.
237: * @exception RPCMessageClientException
238: */
239: private static void verify(Class targetInterface,
240: Class asyncInterface) throws RPCMessageClientException {
241: Method[] methods = asyncInterface.getMethods();
242: for (int index = 0; index < methods.length; index++) {
243: Method method = methods[index];
244: try {
245: targetInterface.getMethod(method.getName(), method
246: .getParameterTypes());
247: } catch (Exception ex) {
248: throw new RPCMessageClientException(
249: "Could not validate the methods : "
250: + ex.getMessage(), ex);
251: }
252: if (method.getReturnType() != java.lang.String.class) {
253: throw new RPCMessageClientException(
254: "The return type of the async interface must be a "
255: + "String. As it will contain the new Message Id");
256: }
257: }
258: }
259: }
|