KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > vm > VMManagedConnection


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: VMManagedConnection.java,v 1.4 2005/06/04 14:28:52 tanderson Exp $
44  */

45 package org.exolab.jms.net.vm;
46
47 import java.io.IOException JavaDoc;
48 import java.rmi.MarshalException JavaDoc;
49 import java.rmi.MarshalledObject JavaDoc;
50 import java.security.Principal JavaDoc;
51
52 import org.exolab.jms.common.uuid.UUIDGenerator;
53 import org.exolab.jms.net.connector.AbstractManagedConnection;
54 import org.exolab.jms.net.connector.Caller;
55 import org.exolab.jms.net.connector.CallerImpl;
56 import org.exolab.jms.net.connector.Connection;
57 import org.exolab.jms.net.connector.IllegalStateException;
58 import org.exolab.jms.net.connector.InvocationHandler;
59 import org.exolab.jms.net.connector.MarshalledInvocation;
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.connector.URIRequestInfo;
64 import org.exolab.jms.net.uri.InvalidURIException;
65 import org.exolab.jms.net.uri.URI;
66 import org.exolab.jms.net.uri.URIHelper;
67
68
69 /**
70  * <code>VMManagedConnection</code> manages multiple <code>VMConnection</code>
71  * instances.
72  *
73  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
74  * @version $Revision: 1.4 $ $Date: 2005/06/04 14:28:52 $
75  */

76 class VMManagedConnection extends AbstractManagedConnection {
77
78     /**
79      * The invoker for delegating invocations to the remote managed connection.
80      */

81     private VMInvoker _remoteInvoker;
82
83     /**
84      * The invocation handler.
85      */

86     private InvocationHandler _invoker;
87
88     /**
89      * The remote address to which this is connected.
90      */

91     private URI _remoteURI;
92
93     /**
94      * The the local address that this connection is bound to.
95      */

96     private URI _localURI;
97
98     /**
99      * The security principal.
100      */

101     private Principal JavaDoc _principal;
102
103     /**
104      * Cached caller instance. Non-null if this is a server-side instance.
105      */

106     private Caller _caller;
107
108
109     /**
110      * Construct a new client <code>VMManagedConnection</code>.
111      *
112      * @param principal the security principal
113      * @param info the connection request info
114      * @throws ResourceException for any error
115      */

116     protected VMManagedConnection(Principal JavaDoc principal, URIRequestInfo info)
117             throws ResourceException {
118         _remoteURI = info.getURI();
119         try {
120             _localURI = URIHelper.create("vm", null, -1,
121                                          UUIDGenerator.create());
122         } catch (InvalidURIException exception) {
123             throw new ResourceException("Failed to generate local URI",
124                                         exception);
125         }
126         VMInvoker invoker = new VMInvoker(this);
127         _remoteInvoker = VMManagedConnectionAcceptor.connect(principal, info,
128                                                              invoker,
129                                                              _localURI);
130         _principal = principal;
131     }
132
133     /**
134      * Construct a new server <code>VMManagedConnection</code>.
135      *
136      * @param principal the security principal
137      * @param info the connection request info
138      * @param client the invoker which delegates invocations to the client
139      * managed connection
140      * @param uri the URI representing the client
141      */

142     protected VMManagedConnection(Principal JavaDoc principal, URIRequestInfo info,
143                                   VMInvoker client, URI uri) {
144         _localURI = info.getURI();
145         _remoteInvoker = client;
146         _remoteURI = uri;
147         _caller = new CallerImpl(_remoteURI, _localURI);
148         _principal = principal;
149     }
150
151     /**
152      * Creates a new connection handle for the underlying physical connection.
153      *
154      * @return a new connection handle
155      * @throws IllegalStateException if an invocation handler hasn't been
156      * registered
157      */

158     public synchronized Connection getConnection()
159             throws IllegalStateException JavaDoc {
160         if (_invoker == null) {
161             throw new IllegalStateException JavaDoc("No InvocationHandler registered");
162         }
163         return new VMConnection(this);
164     }
165
166     /**
167      * Registers a handler for handling invocations on objects exported via this
168      * connection.
169      *
170      * @param handler the invocation handler
171      * @throws IllegalStateException if a handler is already registered
172      * @throws ResourceException for any error
173      */

174     public synchronized void setInvocationHandler(InvocationHandler handler)
175             throws ResourceException {
176         if (_invoker != null) {
177             throw new IllegalStateException JavaDoc(
178                     "An invocation handler is already registered");
179         }
180         _invoker = handler;
181     }
182
183     /**
184      * Determines if the underlying physical connection is alive.
185      *
186      * @return <code>true</code> if the connection is alive
187      */

188     public boolean isAlive() {
189         boolean alive = false;
190         VMInvoker invoker = null;
191         synchronized (this) {
192             invoker = _remoteInvoker;
193         }
194         if (invoker != null) {
195             alive = invoker.isAlive();
196         }
197         return alive;
198     }
199
200     /**
201      * Destroys the physical connection.
202      *
203      * @throws ResourceException for any error
204      */

205     public void destroy() throws ResourceException {
206         synchronized (this) {
207             _remoteInvoker = null;
208         }
209     }
210
211     /**
212      * Returns the remote address to which this is connected.
213      *
214      * @return the remote address to which this is connected
215      */

216     public URI getRemoteURI() {
217         return _remoteURI;
218     }
219
220     /**
221      * Returns the local address that this connection is bound to.
222      *
223      * @return the local address that this connection is bound to
224      */

225     public URI getLocalURI() {
226         return _localURI;
227     }
228
229     /**
230      * Determines if the security principal that owns this connection is the
231      * same as that supplied.
232      *
233      * @param principal the principal to compare. May be <code>null</code>.
234      * @return <code>true</code> if the principal that owns this connection is
235      * the same as <code>principal</code>
236      */

237     public boolean hasPrincipal(Principal JavaDoc principal) {
238         boolean result = false;
239         if ((_principal != null && _principal.equals(principal))
240                 || (_principal == null && principal == null)) {
241             result = true;
242         }
243         return result;
244     }
245
246     /**
247      * Invoke a method on a remote object.
248      *
249      * @param connection the connection performing the invocation
250      * @param request the request
251      * @return the result of the invocation
252      */

253     protected Response invoke(Connection connection, Request request) {
254         Response response;
255         try {
256             MarshalledObject JavaDoc wrappedRequest = new MarshalledObject JavaDoc(request);
257             MarshalledObject JavaDoc wrappedResponse =
258                     _remoteInvoker.invoke(wrappedRequest);
259             response = (Response) wrappedResponse.get();
260         } catch (ClassNotFoundException JavaDoc exception) {
261             response = new Response(exception);
262         } catch (IOException JavaDoc exception) {
263             response = new Response(exception);
264         }
265         return response;
266     }
267
268     /**
269      * Invoke a method on a local object.
270      *
271      * @param request the wrapped <code>Request</code>
272      * @return the wrapped <code>Response</code>
273      * @throws MarshalException if the request can't be unmarshalled or the
274      * response can't be marshalled
275      */

276     protected MarshalledObject JavaDoc invokeLocal(MarshalledObject JavaDoc request)
277             throws MarshalException JavaDoc {
278         MarshalledInvocation invocation
279                 = new MarshalledInvocation(request, _caller);
280
281         _invoker.invoke(invocation);
282         MarshalledObject JavaDoc response = null;
283         try {
284             response = invocation.getMarshalledResponse();
285         } catch (IOException JavaDoc exception) {
286             throw new MarshalException JavaDoc("Failed to marshal response",
287                                        exception);
288         }
289         return response;
290     }
291
292     /**
293      * Determines if the local end of the connection is alive.
294      *
295      * @return <code>true</code> if the connection is alive
296      */

297     protected boolean isAliveLocal() {
298         boolean alive = false;
299         synchronized (this) {
300             alive = (_remoteInvoker != null);
301         }
302         return alive;
303     }
304
305 }
306
Popular Tags