KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > RemoteBusinessIntfInvocationHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.containers;
24
25 import java.io.Serializable JavaDoc;
26 import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32
33 import com.sun.ejb.containers.util.InvocationHandlerUtil;
34
35 /**
36  * This is an invocation handler for a remote EJB 3.x business
37  * interface. It is used to convert invocations made on the
38  * EJB 3.0 business interface to its corresponding RMI-IIOP
39  * object.
40  *
41  * @@@ Need to handle serialization/deserialization
42  *
43  * @author Kenneth Saks
44  */

45
46 public final class RemoteBusinessIntfInvocationHandler
47     implements InvocationHandler JavaDoc, Serializable JavaDoc {
48
49     private Class JavaDoc businessInterface;
50     private java.rmi.Remote JavaDoc delegate;
51
52     public RemoteBusinessIntfInvocationHandler(Class JavaDoc businessIntf,
53                                                java.rmi.Remote JavaDoc stub) {
54         businessInterface = businessIntf;
55         delegate = stub;
56     }
57
58     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
59         throws Throwable JavaDoc {
60
61         // NOTE : be careful with "args" parameter. It is null
62
// if method signature has 0 arguments.
63

64         Class JavaDoc methodClass = method.getDeclaringClass();
65         if( methodClass == java.lang.Object JavaDoc.class ) {
66             return InvocationHandlerUtil.
67                 invokeJavaObjectMethod(this, method, args);
68         }
69
70         Object JavaDoc returnValue = null;
71         Throwable JavaDoc t = null;
72         try {
73             Method JavaDoc m = delegate.getClass().getMethod
74                 (method.getName(), method.getParameterTypes());
75                                                      
76             returnValue = m.invoke(delegate, args);
77         } catch(NoSuchMethodException JavaDoc nsme) {
78             t = nsme;
79         } catch(InvocationTargetException JavaDoc ite) {
80             t = ite.getCause();
81         } catch(Throwable JavaDoc c) {
82             t = c;
83         }
84
85         if( t != null ) {
86             if( t instanceof java.rmi.RemoteException JavaDoc ) {
87                 EJBException JavaDoc ejbEx = new EJBException JavaDoc();
88                 ejbEx.initCause(t);
89                 throw ejbEx;
90             } else {
91                 throw t;
92             }
93         }
94
95         return returnValue;
96     }
97     
98 }
99
Popular Tags