KickJava   Java API By Example, From Geeks To Geeks.

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


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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: UnicastDelegate.java,v 1.1 2004/11/26 01:51:05 tanderson Exp $
44  */

45 package org.exolab.jms.net.orb;
46
47 import java.io.IOException JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50 import java.io.Serializable JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.rmi.server.ObjID JavaDoc;
53
54 import org.exolab.jms.net.connector.Connection;
55 import org.exolab.jms.net.connector.ConnectionContext;
56 import org.exolab.jms.net.connector.ConnectionFactory;
57 import org.exolab.jms.net.connector.Request;
58 import org.exolab.jms.net.connector.ResourceException;
59 import org.exolab.jms.net.connector.Response;
60 import org.exolab.jms.net.proxy.Delegate;
61 import org.exolab.jms.net.uri.InvalidURIException;
62 import org.exolab.jms.net.uri.URIHelper;
63
64
65 /**
66  * <code>UnicastDelegate</code> supports the invocation of methods on a single
67  * remote object, over arbitrary transport protocols
68  *
69  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
70  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:05 $
71  */

72 public class UnicastDelegate implements Delegate, Serializable JavaDoc {
73
74     /**
75      * The target object identifier
76      */

77     private ObjID JavaDoc _objID;
78
79     /**
80      * The connection URI
81      */

82     private String JavaDoc _uri;
83
84     /**
85      * The connection used to make invocations
86      */

87     private transient Connection _connection;
88
89     /**
90      * The connection factory
91      */

92     private transient ConnectionFactory _factory;
93
94
95     /**
96      * Default constructor for serialization support
97      */

98     protected UnicastDelegate() {
99     }
100
101     /**
102      * Construct a new <code>UnicastDelegate</code>
103      *
104      * @param objID the identifier of the target object
105      * @param uri the connection URI
106      * @param factory the connector factory
107      */

108     public UnicastDelegate(ObjID JavaDoc objID, String JavaDoc uri,
109                            ConnectionFactory factory) {
110         _objID = objID;
111         _uri = uri;
112         _factory = factory;
113     }
114
115     /**
116      * Construct a new <code>UnicastDelegate</code>
117      *
118      * @param objID the identifier of the target object
119      * @param connection the connection used to make invocations
120      */

121     public UnicastDelegate(ObjID JavaDoc objID, Connection connection) {
122         _objID = objID;
123         _connection = connection;
124     }
125
126     /**
127      * Invoke a method
128      *
129      * @param method the method to invoke
130      * @param args the arguments to pass
131      * @param methodID the unique identifier for the method
132      * @return the result of the invocation
133      * @throws Throwable for any error
134      */

135     public Object JavaDoc invoke(Method JavaDoc method, Object JavaDoc[] args, long methodID)
136             throws Throwable JavaDoc {
137         Request request = new Request(_objID, method, args, methodID);
138         Response response = getConnection().invoke(request);
139         if (response.isException()) {
140             throw response.getException();
141         }
142         return response.getObject();
143     }
144
145     /**
146      * Returns the connection to perform invocations
147      *
148      * @return the connection to perform invocations
149      * @throws InvalidURIException if the URI is invalid
150      * @throws ResourceException if a connection cannot be created
151      */

152     protected synchronized Connection getConnection()
153             throws InvalidURIException, ResourceException {
154         if (_connection == null) {
155             _connection = _factory.getConnection(null, URIHelper.parse(_uri));
156         }
157         return _connection;
158     }
159
160     /**
161      * Write this to a stream
162      *
163      * @param out the stream to write to
164      * @throws IOException for any I/O error
165      */

166     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
167         out.defaultWriteObject();
168     }
169
170     /**
171      * Read the state of this from a stream
172      *
173      * @param in the stream to read from
174      * @throws ClassNotFoundException if a class cannot be deserialized
175      * @throws IOException for any I/O error
176      */

177     private void readObject(ObjectInputStream JavaDoc in)
178             throws ClassNotFoundException JavaDoc, IOException JavaDoc {
179         in.defaultReadObject();
180         _factory = ConnectionContext.getConnectionFactory();
181         _connection = ConnectionContext.getConnection(URIHelper.parse(_uri));
182     }
183
184 }
185
Popular Tags