KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > presentation > rmi > InvocationHandlerFactoryImpl


1 /*
2  * @(#)InvocationHandlerFactoryImpl.java 1.8 04/07/27
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.presentation.rmi ;
9
10 import java.lang.reflect.InvocationHandler JavaDoc ;
11 import java.lang.reflect.Proxy JavaDoc ;
12 import java.lang.reflect.Method JavaDoc ;
13
14 import org.omg.CORBA.portable.ObjectImpl JavaDoc ;
15
16 import java.io.ObjectStreamException JavaDoc ;
17 import java.io.Serializable JavaDoc ;
18
19 import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
20 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
21 import com.sun.corba.se.spi.presentation.rmi.DynamicStub ;
22
23 import com.sun.corba.se.spi.orbutil.proxy.LinkedInvocationHandler ;
24 import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
25 import com.sun.corba.se.spi.orbutil.proxy.DelegateInvocationHandlerImpl ;
26 import com.sun.corba.se.spi.orbutil.proxy.CompositeInvocationHandler ;
27 import com.sun.corba.se.spi.orbutil.proxy.CompositeInvocationHandlerImpl ;
28
29 public class InvocationHandlerFactoryImpl implements InvocationHandlerFactory
30 {
31     private final PresentationManager.ClassData classData ;
32     private final PresentationManager pm ;
33     private Class JavaDoc[] proxyInterfaces ;
34
35     public InvocationHandlerFactoryImpl( PresentationManager pm,
36     PresentationManager.ClassData classData )
37     {
38     this.classData = classData ;
39     this.pm = pm ;
40
41         Class JavaDoc[] remoteInterfaces =
42         classData.getIDLNameTranslator().getInterfaces() ;
43     proxyInterfaces = new Class JavaDoc[ remoteInterfaces.length + 1 ] ;
44     for (int ctr=0; ctr<remoteInterfaces.length; ctr++)
45         proxyInterfaces[ctr] = remoteInterfaces[ctr] ;
46
47     proxyInterfaces[remoteInterfaces.length] = DynamicStub.class ;
48     }
49
50     private class CustomCompositeInvocationHandlerImpl extends
51     CompositeInvocationHandlerImpl implements LinkedInvocationHandler,
52     Serializable JavaDoc
53     {
54     private transient DynamicStub stub ;
55
56     public void setProxy( Proxy JavaDoc proxy )
57     {
58         ((DynamicStubImpl)stub).setSelf( (DynamicStub)proxy ) ;
59     }
60
61     public Proxy JavaDoc getProxy()
62     {
63         return (Proxy JavaDoc)((DynamicStubImpl)stub).getSelf() ;
64     }
65
66     public CustomCompositeInvocationHandlerImpl( DynamicStub stub )
67     {
68         this.stub = stub ;
69     }
70
71     /** Return the stub, which will actually be written to the stream.
72      * It will be custom marshalled, with the actual writing done in
73      * StubIORImpl. There is a corresponding readResolve method on
74      * DynamicStubImpl which will re-create the full invocation
75      * handler on read, and return the invocation handler on the
76      * readResolve method.
77      */

78     public Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
79     {
80         return stub ;
81     }
82     }
83
84     public InvocationHandler JavaDoc getInvocationHandler()
85     {
86     final DynamicStub stub = new DynamicStubImpl(
87         classData.getTypeIds() ) ;
88
89     return getInvocationHandler( stub ) ;
90     }
91
92     // This is also used in DynamicStubImpl to implement readResolve.
93
InvocationHandler JavaDoc getInvocationHandler( DynamicStub stub )
94     {
95     // Create an invocation handler for the methods defined on DynamicStub,
96
// which extends org.omg.CORBA.Object. This handler delegates all
97
// calls directly to a DynamicStubImpl, which extends
98
// org.omg.CORBA.portable.ObjectImpl.
99
InvocationHandler JavaDoc dynamicStubHandler =
100         DelegateInvocationHandlerImpl.create( stub ) ;
101
102     // Create an invocation handler that handles any remote interface
103
// methods.
104
InvocationHandler JavaDoc stubMethodHandler = new StubInvocationHandlerImpl(
105         pm, classData, stub ) ;
106
107     // Create a composite handler that handles the DynamicStub interface
108
// as well as the remote interfaces.
109
final CompositeInvocationHandler handler =
110         new CustomCompositeInvocationHandlerImpl( stub ) ;
111     handler.addInvocationHandler( DynamicStub.class,
112         dynamicStubHandler ) ;
113     handler.addInvocationHandler( org.omg.CORBA.Object JavaDoc.class,
114         dynamicStubHandler ) ;
115     handler.addInvocationHandler( Object JavaDoc.class,
116         dynamicStubHandler ) ;
117
118     // If the method passed to invoke is not from DynamicStub or its superclasses,
119
// it must be from an implemented interface, so we just handle
120
// all of these with the stubMethodHandler. This used to be
121
// done be adding explicit entries for stubMethodHandler for
122
// each remote interface, but that does not work correctly
123
// for abstract interfaces, since the graph analysis ignores
124
// abstract interfaces in order to compute the type ids
125
// correctly (see PresentationManagerImpl.NodeImpl.getChildren).
126
// Rather than produce more graph traversal code to handle this
127
// problem, we simply use a default.
128
// This also points to a possible optimization: just use explict
129
// checks for the three special classes, rather than a general
130
// table lookup that usually fails.
131
handler.setDefaultHandler( stubMethodHandler ) ;
132
133     return handler ;
134     }
135
136     public Class JavaDoc[] getProxyInterfaces()
137     {
138     return proxyInterfaces ;
139     }
140 }
141
Popular Tags