1 // 2 // SwitchRMI Framework 3 // Copyright (c) 2000-2002 by Michael J. Henderson & Associates. 4 // 5 // Michael Henderson 6 // http://switchrmi.sf.net 7 // mailto:mikehenderson@dunelm.org.uk 8 // 9 // This library is free software. 10 // 11 // You may redistribute it and/or modify it under the terms of the GNU 12 // Lesser General Public License as published by the Free Software Foundation. 13 // 14 // Version 2.1 of the license should be included with this distribution in 15 // the file LICENSE, as well as License.html. If the license is not 16 // included with this distribution, you may find a copy at the FSF web 17 // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the 18 // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. 19 // 20 // This library is distributed in the hope that it will be useful, 21 // but WITHOUT ANY WARRANTY; without even the implied waranty of 22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 // Lesser General Public License for more details. 24 // 25 // $Id: RmiClient.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $ 26 package com.mjh.switchrmi; 27 28 import java.lang.reflect.*; 29 30 import java.util.*; 31 32 import javax.naming.*; 33 34 import org.apache.log4j.Logger; 35 36 /** 37 * RmiClient provides the client-side interface to connect to remote objects served by any of the remote 38 * method invocation transports. 39 * 40 * 41 * @author Mike Henderson 42 */ 43 public class RmiClient 44 { 45 private static final Logger log = 46 Logger.getLogger(RmiClient.class.getName()); 47 private Context ctx; 48 49 /** 50 * Initialize a client for connecting to remote objects via HTTP. 51 */ 52 public RmiClient() throws Exception 53 { 54 this(null); 55 } 56 57 /** 58 * Creates a new RmiClient object. 59 * 60 * @param resourceName DOCUMENT ME! 61 * @throws Exception DOCUMENT ME! 62 */ 63 public RmiClient(String resourceName) 64 throws Exception 65 { 66 Hashtable env = new Hashtable(); 67 68 if (resourceName != null) 69 { 70 env.put(Context.PROVIDER_URL, resourceName); 71 } 72 73 env.put(Context.INITIAL_CONTEXT_FACTORY, 74 "com.mjh.switchrmi.jndi.JndiContextImplFactory"); 75 ctx = new InitialContext(env); 76 log.debug("ctx = " + ctx); 77 } 78 79 /** 80 * DOCUMENT ME! 81 * 82 * @return DOCUMENT ME! 83 */ 84 public Object getJndiContext() 85 { 86 return ctx; 87 } 88 89 /** 90 * Obtain a proxy for the remote obejct with the given URL to invoke methods in the specified interface. 91 * 92 * @param url a valid remote object URL, valid URLS end in /name.protocol 93 * @param interface a Java interface implemented by the remote object. 94 * 95 * @return a Proxy to the remote object. 96 * 97 */ 98 public Object connect(String url, Class interfaze) 99 throws Exception 100 { 101 return connect(url, new Class[] { interfaze }); 102 } 103 104 /** 105 * Obtain a proxy for the remote obejct with the given URL to invoke methods in the specified interfaces. 106 * 107 * @param url a valid remote object URL, valid URLS end in /name.protocol 108 * @param interfaces an array of Java interfaces implemented by the remote object. 109 * 110 * @return a Proxy to the remote object. 111 * 112 */ 113 public Object connect(String url, Class[] interfaces) 114 throws Exception 115 { 116 RmiUrlInfo info = new RmiUrlInfo(url); 117 118 return connect(url, interfaces, info.getProtocolName()); 119 } 120 121 /** 122 * Obtain a proxy for the remote obejct with the given URL to invoke methods in the specified interfaces. 123 * 124 * @param url a valid URL,for the remote transport method 125 * @param interface a Java interface implemented by the remote object. 126 * @param protocolName a valid RMI invocation protocol. Valid names are specified in RmiProtocol. 127 * 128 * @return a Proxy to the remote object. 129 * @see RmiProtocol 130 */ 131 public Object connect(String url, Class interfaze, String protocolName) 132 throws Exception 133 { 134 return connect(url, new Class[] { interfaze }, protocolName); 135 } 136 137 /** 138 * Obtain a proxy for the remote obejct with the given URL to invoke methods in the specified interfaces. 139 * 140 * @param url a valid URL,for the remote transport method 141 * @param interfaces an array of Java interfaces implemented by the remote object. 142 * @param protocolName a valid RMI invocation protocol. Valid names are specified in RmiProtocol. 143 * 144 * @return a Proxy to the remote object. 145 * @see RmiProtocol. 146 */ 147 public Object connect(String url, Class[] interfaces, String protocolName) 148 throws Exception 149 { 150 RmiClientInvocationHandler handler = 151 new RmiClientInvocationHandler(url, interfaces, protocolName, 152 ctx); 153 154 return Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, 155 handler); 156 } 157 }