View Javadoc

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: UnicastDelegate.java,v 1.2 2005/11/16 12:32:49 tanderson Exp $
44   */
45  package org.exolab.jms.net.orb;
46  
47  import java.io.IOException;
48  import java.io.ObjectInputStream;
49  import java.io.ObjectOutputStream;
50  import java.io.Serializable;
51  import java.lang.reflect.Method;
52  import java.rmi.server.ObjID;
53  
54  import org.apache.commons.logging.Log;
55  import org.apache.commons.logging.LogFactory;
56  
57  import org.exolab.jms.net.connector.Connection;
58  import org.exolab.jms.net.connector.ConnectionContext;
59  import org.exolab.jms.net.connector.ConnectionFactory;
60  import org.exolab.jms.net.connector.Request;
61  import org.exolab.jms.net.connector.ResourceException;
62  import org.exolab.jms.net.connector.Response;
63  import org.exolab.jms.net.proxy.Delegate;
64  import org.exolab.jms.net.uri.InvalidURIException;
65  import org.exolab.jms.net.uri.URIHelper;
66  
67  
68  /***
69   * <code>UnicastDelegate</code> supports the invocation of methods on a single
70   * remote object, over arbitrary transport protocols.
71   *
72   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
73   * @version $Revision: 1.2 $ $Date: 2005/11/16 12:32:49 $
74   */
75  public class UnicastDelegate implements Delegate, Serializable {
76  
77      /***
78       * Serialization version.
79       */
80      static final long serialVersionUID = 1;
81  
82      /***
83       * The target object identifier.
84       */
85      private ObjID _objID;
86  
87      /***
88       * The connection URI.
89       */
90      private String _uri;
91  
92      /***
93       * The connection used to make invocations.
94       */
95      private transient Connection _connection;
96  
97      /***
98       * The connection factory.
99       */
100     private transient ConnectionFactory _factory;
101 
102     /***
103      * The logger.
104      */
105     private static final Log _log = LogFactory.getLog(UnicastDelegate.class);
106 
107 
108     /***
109      * Default constructor for serialization support.
110      */
111     protected UnicastDelegate() {
112     }
113 
114     /***
115      * Construct a new <code>UnicastDelegate</code>.
116      * <p/>
117      * This is intended for use on the server side, to create delegates
118      * for serialization to clients.
119      *
120      * @param objID the identifier of the target object
121      * @param uri   the connection URI
122      */
123     public UnicastDelegate(ObjID objID, String uri) {
124         _objID = objID;
125         _uri = uri;
126     }
127 
128     /***
129      * Construct a new <code>UnicastDelegate</code>.
130      * <p/>
131      * This is intended for use on the client side.
132      *
133      * @param objID      the identifier of the target object
134      * @param connection the connection used to make invocations
135      */
136     public UnicastDelegate(ObjID objID, Connection connection) {
137         _objID = objID;
138         _connection = connection;
139     }
140 
141     /***
142      * Invoke a method.
143      *
144      * @param method   the method to invoke
145      * @param args     the arguments to pass
146      * @param methodID the unique identifier for the method
147      * @return the result of the invocation
148      * @throws Throwable for any error
149      */
150     public Object invoke(Method method, Object[] args, long methodID)
151             throws Throwable {
152         Request request = new Request(_objID, method, args, methodID);
153         Response response = getConnection().invoke(request);
154         if (response.isException()) {
155             throw response.getException();
156         }
157         return response.getObject();
158     }
159 
160     /***
161      * Dispose the delegate, releasing any resources.
162      * <p/>
163      * It is an error to invoke any method other than this, after the delegate
164      * has been disposed.
165      */
166     public synchronized void dispose() {
167         if (_connection != null) {
168             try {
169                 _connection.close();
170             } catch (ResourceException exception) {
171                 _log.warn("Failed to close connection", exception);
172             } finally {
173                 _connection = null;
174                 _factory = null;
175             }
176         }
177     }
178 
179     /***
180      * Returns the connection to perform invocations.
181      *
182      * @return the connection to perform invocations
183      * @throws InvalidURIException if the URI is invalid
184      * @throws ResourceException   if a connection cannot be created
185      */
186     protected synchronized Connection getConnection()
187             throws InvalidURIException, ResourceException {
188         if (_connection == null) {
189             _connection = _factory.getConnection(null, URIHelper.parse(_uri));
190         }
191         return _connection;
192     }
193 
194     /***
195      * Write this to a stream.
196      *
197      * @param out the stream to write to
198      * @throws IOException for any I/O error
199      */
200     private void writeObject(ObjectOutputStream out) throws IOException {
201         out.defaultWriteObject();
202     }
203 
204     /***
205      * Read the state of this from a stream.
206      *
207      * @param in the stream to read from
208      * @throws ClassNotFoundException if a class cannot be deserialized
209      * @throws IOException            for any I/O error
210      */
211     private void readObject(ObjectInputStream in)
212             throws ClassNotFoundException, IOException {
213         in.defaultReadObject();
214 
215         // try and get a reference to the connection
216         _connection = ConnectionContext.getConnection(URIHelper.parse(_uri));
217         if (_connection == null) {
218             // cache the factory, so the connection can be obtained when
219             // needed
220             _factory = ConnectionContext.getConnectionFactory();
221         }
222     }
223 
224 }