1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: Locator.java,v 1.3 2005/05/24 05:55:18 tanderson Exp $
44 */
45 package org.exolab.jms.net.orb;
46
47 import java.lang.reflect.Constructor;
48 import java.lang.reflect.InvocationTargetException;
49 import java.rmi.ConnectIOException;
50 import java.rmi.RemoteException;
51 import java.rmi.StubNotFoundException;
52 import java.rmi.AccessException;
53 import java.rmi.server.ObjID;
54 import java.security.Principal;
55 import java.util.Map;
56
57 import org.exolab.jms.net.connector.Connection;
58 import org.exolab.jms.net.connector.ConnectionFactory;
59 import org.exolab.jms.net.connector.ResourceException;
60 import org.exolab.jms.net.connector.SecurityException;
61 import org.exolab.jms.net.proxy.Delegate;
62 import org.exolab.jms.net.proxy.Proxy;
63 import org.exolab.jms.net.registry.Registry;
64 import org.exolab.jms.net.uri.InvalidURIException;
65 import org.exolab.jms.net.uri.URIHelper;
66
67
68 /***
69 * Helper class for constructing proxies for exported objects with a known
70 * identifier.
71 *
72 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
73 * @version $Revision: 1.3 $ $Date: 2005/05/24 05:55:18 $
74 */
75 final class Locator {
76
77 /***
78 * Prevent construction of helper class.
79 */
80 private Locator() {
81 }
82
83 /***
84 * Returns a proxy for a remote {@link Registry}.
85 *
86 * @param principal the security principal. May be <code>null</code>
87 * @param uri the connection URI
88 * @param factory the connection factory
89 * @param loader the loader for the proxy class
90 * @param properties the connection properties. May be <code>null</code>.
91 * @return a proxy for the registry exported at <code>uri</code>
92 * @throws InvalidURIException if <code>uri</code> is invalid
93 * @throws RemoteException if the object cannot be located
94 */
95 public static Registry getRegistry(Principal principal, String uri,
96 ConnectionFactory factory,
97 ClassLoader loader, Map properties)
98 throws InvalidURIException, RemoteException {
99
100 ObjID objId = new ObjID(ObjID.REGISTRY_ID);
101 String className = RegistryImpl.PROXY;
102
103 return (Registry) getProxy(objId, principal, uri, factory, className,
104 loader, properties);
105 }
106
107 /***
108 * Returns a proxy for the specified object.
109 *
110 * @param objId the object's identifier
111 * @param principal the security principal
112 * @param uri the connection URI
113 * @param factory the connection factory
114 * @param className the proxy class name
115 * @param loader the loader for the proxy class
116 * @param properties the connection properties. May be <code>null</code>.
117 * @return a proxy for <code>objId</code> exported at <code>uri</code>
118 * @throws InvalidURIException if <code>uri</code> is invalid
119 * @throws RemoteException if the object cannot be located
120 */
121 public static Proxy getProxy(ObjID objId, Principal principal, String uri,
122 ConnectionFactory factory,
123 String className, ClassLoader loader,
124 Map properties)
125 throws InvalidURIException, RemoteException {
126
127 Proxy proxy;
128
129 Connection connection;
130 try {
131 connection = factory.getConnection(principal,
132 URIHelper.parse(uri),
133 properties);
134 } catch (SecurityException exception) {
135 throw new AccessException(exception.getMessage(), exception);
136 } catch (ResourceException exception) {
137 throw new ConnectIOException("Failed to create connection",
138 exception);
139 }
140
141 UnicastDelegate delegate = new UnicastDelegate(objId, connection);
142
143 try {
144 Class proxyClass = loader.loadClass(className);
145 Constructor constructor = proxyClass.getConstructor(
146 new Class[]{Delegate.class});
147 proxy = (Proxy) constructor.newInstance(new Object[]{delegate});
148 } catch (ClassNotFoundException exception) {
149 throw new StubNotFoundException(exception.getMessage(), exception);
150 } catch (IllegalAccessException exception) {
151 throw new RemoteException(exception.getMessage(), exception);
152 } catch (InstantiationException exception) {
153 throw new RemoteException(exception.getMessage(), exception);
154 } catch (InvocationTargetException exception) {
155
156 Throwable target = exception.getTargetException();
157 if (target != null) {
158 throw new RemoteException(exception.getMessage(), target);
159 } else {
160 throw new RemoteException(exception.getMessage(), exception);
161 }
162 } catch (NoSuchMethodException exception) {
163 throw new RemoteException(exception.getMessage(), exception);
164 }
165 return proxy;
166 }
167
168 }