KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > orb > Locator


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 JavaDoc;
48 import java.lang.reflect.InvocationTargetException JavaDoc;
49 import java.rmi.ConnectIOException JavaDoc;
50 import java.rmi.RemoteException JavaDoc;
51 import java.rmi.StubNotFoundException JavaDoc;
52 import java.rmi.AccessException JavaDoc;
53 import java.rmi.server.ObjID JavaDoc;
54 import java.security.Principal JavaDoc;
55 import java.util.Map JavaDoc;
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 JavaDoc principal, String JavaDoc uri,
96                                        ConnectionFactory factory,
97                                        ClassLoader JavaDoc loader, Map JavaDoc properties)
98             throws InvalidURIException, RemoteException JavaDoc {
99
100         ObjID JavaDoc objId = new ObjID JavaDoc(ObjID.REGISTRY_ID);
101         String JavaDoc 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 JavaDoc objId, Principal JavaDoc principal, String JavaDoc uri,
122                                  ConnectionFactory factory,
123                                  String JavaDoc className, ClassLoader JavaDoc loader,
124                                  Map JavaDoc properties)
125             throws InvalidURIException, RemoteException JavaDoc {
126
127         Proxy proxy;
128
129         Connection connection;
130         try {
131             connection = factory.getConnection(principal,
132                                                URIHelper.parse(uri),
133                                                properties);
134         } catch (SecurityException JavaDoc exception) {
135             throw new AccessException JavaDoc(exception.getMessage(), exception);
136         } catch (ResourceException exception) {
137             throw new ConnectIOException JavaDoc("Failed to create connection",
138                                          exception);
139         }
140
141         UnicastDelegate delegate = new UnicastDelegate(objId, connection);
142
143         try {
144             Class JavaDoc proxyClass = loader.loadClass(className);
145             Constructor JavaDoc constructor = proxyClass.getConstructor(
146                     new Class JavaDoc[]{Delegate.class});
147             proxy = (Proxy) constructor.newInstance(new Object JavaDoc[]{delegate});
148         } catch (ClassNotFoundException JavaDoc exception) {
149             throw new StubNotFoundException JavaDoc(exception.getMessage(), exception);
150         } catch (IllegalAccessException JavaDoc exception) {
151             throw new RemoteException JavaDoc(exception.getMessage(), exception);
152         } catch (InstantiationException JavaDoc exception) {
153             throw new RemoteException JavaDoc(exception.getMessage(), exception);
154         } catch (InvocationTargetException JavaDoc exception) {
155             // unwrap the target exception, if non-null
156
Throwable JavaDoc target = exception.getTargetException();
157             if (target != null) {
158                 throw new RemoteException JavaDoc(exception.getMessage(), target);
159             } else {
160                 throw new RemoteException JavaDoc(exception.getMessage(), exception);
161             }
162         } catch (NoSuchMethodException JavaDoc exception) {
163             throw new RemoteException JavaDoc(exception.getMessage(), exception);
164         }
165         return proxy;
166     }
167
168 }
169
Popular Tags