KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > connection > ConnectionHandler


1 /*
2  * CoadunationUtil: The coadunation util library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ConnectionHandler.java
20  */

21
22 // package path
23
package com.rift.coad.util.connection;
24
25 // java imports
26
import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29
30 /**
31  * This object is responsible for controlling the Proxy wrapping of any RMI
32  * classes in the connection manager.
33  *
34  * @author Brett Chaldecott
35  */

36 public class ConnectionHandler implements InvocationHandler JavaDoc {
37     
38     // private member variables
39
private RMIConnection rmiConnection = null;
40     private Object JavaDoc rmiRef = null;
41     
42     
43     /**
44      * Creates a new instance of ConnectionHandler.
45      *
46      * @param rmiConnection The reference to the connection object.
47      * @param rmiRef The reference to the sub object to make the call onto.
48      */

49     public ConnectionHandler(RMIConnection rmiConnection, Object JavaDoc rmiRef) {
50         this.rmiConnection = rmiConnection;
51         this.rmiRef = rmiRef;
52     }
53     
54     
55     /**
56      * This method performs the call on the rmi reference.
57      *
58      * @param proxy The proxy that is making the call on this handler.
59      * @param method The method that is being called.
60      * @param args The arguments that are being called.
61      * @exception Throwable.
62      */

63     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws
64             Throwable JavaDoc {
65         try {
66             // retrieve the method information
67
Method JavaDoc rmiMethod = rmiRef.getClass().getMethod(method.getName(),
68                     method.getParameterTypes());
69             
70             // make the call
71
return rmiMethod.invoke(rmiRef,args);
72         } catch (InvocationTargetException JavaDoc ex) {
73             Throwable JavaDoc ex2 = ex.getTargetException();
74             if ((ex2 instanceof java.rmi.RemoteException JavaDoc) ||
75                     (ex2 instanceof java.lang.RuntimeException JavaDoc))
76             {
77                 rmiConnection.invalidateConnection();
78             }
79             throw ex2;
80         } catch (Throwable JavaDoc ex) {
81             if ((ex instanceof java.rmi.RemoteException JavaDoc) ||
82                     (ex instanceof java.lang.RuntimeException JavaDoc))
83             {
84                 rmiConnection.invalidateConnection();
85             }
86             throw ex;
87         }
88     }
89     
90 }
91
Popular Tags