KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > presentation > rmi > StubAdapter


1 /*
2  * @(#)StubAdapter.java 1.4 04/06/21
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.spi.presentation.rmi ;
9
10 import javax.rmi.CORBA.Tie JavaDoc ;
11
12 import org.omg.CORBA.portable.Delegate JavaDoc ;
13 import org.omg.CORBA.portable.ObjectImpl JavaDoc ;
14 import org.omg.CORBA.portable.OutputStream JavaDoc ;
15
16 import org.omg.PortableServer.POA JavaDoc ;
17 import org.omg.PortableServer.POAManager JavaDoc ;
18 import org.omg.PortableServer.Servant JavaDoc ;
19
20 import org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc ;
21 import org.omg.PortableServer.POAPackage.ServantNotActive JavaDoc ;
22 import org.omg.PortableServer.POAManagerPackage.AdapterInactive JavaDoc ;
23
24 import org.omg.CORBA.ORB JavaDoc ;
25
26 import com.sun.corba.se.spi.logging.CORBALogDomains ;
27 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
28
29 // XXX Getting rid of this requires introducing an ObjectAdapterManager abstraction
30
// as an interface into the OA framework.
31
import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
32
33 /** Provide access to stub delegate and type id information
34  * independent of the stub type. This class exists because
35  * ObjectImpl does not have an interface for the 3 delegate and
36  * type id methods, so a DynamicStub has a different type.
37  * We cannot simply change ObjectImpl as it is a standard API.
38  * We also cannot change the code generation of Stubs, as that
39  * is also standard. Hence I am left with this ugly class.
40  */

41 public abstract class StubAdapter
42 {
43     private StubAdapter() {}
44
45     private static ORBUtilSystemException wrapper =
46     ORBUtilSystemException.get( CORBALogDomains.RPC_PRESENTATION ) ;
47
48     public static boolean isStubClass( Class JavaDoc cls )
49     {
50     return (ObjectImpl JavaDoc.class.isAssignableFrom( cls )) ||
51         (DynamicStub.class.isAssignableFrom( cls )) ;
52     }
53
54     public static boolean isStub( Object JavaDoc stub )
55     {
56     return (stub instanceof DynamicStub) ||
57         (stub instanceof ObjectImpl JavaDoc) ;
58     }
59
60     public static void setDelegate( Object JavaDoc stub, Delegate JavaDoc delegate )
61     {
62     if (stub instanceof DynamicStub)
63         ((DynamicStub)stub).setDelegate( delegate ) ;
64     else if (stub instanceof ObjectImpl JavaDoc)
65         ((ObjectImpl JavaDoc)stub)._set_delegate( delegate ) ;
66     else
67         throw wrapper.setDelegateRequiresStub() ;
68     }
69
70     /** Use implicit activation to get an object reference for the servant.
71      */

72     public static org.omg.CORBA.Object JavaDoc activateServant( Servant JavaDoc servant )
73     {
74     POA JavaDoc poa = servant._default_POA() ;
75     org.omg.CORBA.Object JavaDoc ref = null ;
76
77     try {
78         ref = poa.servant_to_reference( servant ) ;
79     } catch (ServantNotActive JavaDoc sna) {
80         throw wrapper.getDelegateServantNotActive( sna ) ;
81     } catch (WrongPolicy JavaDoc wp) {
82         throw wrapper.getDelegateWrongPolicy( wp ) ;
83     }
84
85     // Make sure that the POAManager is activated if no other
86
// POAManager state management has taken place.
87
POAManager JavaDoc mgr = poa.the_POAManager() ;
88     if (mgr instanceof POAManagerImpl) {
89         POAManagerImpl mgrImpl = (POAManagerImpl)mgr ;
90         mgrImpl.implicitActivation() ;
91     }
92
93     return ref ;
94     }
95
96     /** Given any Tie, return the corresponding object refernce, activating
97      * the Servant if necessary.
98      */

99     public static org.omg.CORBA.Object JavaDoc activateTie( Tie JavaDoc tie )
100     {
101     /** Any implementation of Tie should be either a Servant or an ObjectImpl,
102      * depending on which style of code generation is used. rmic -iiop by
103      * default results in an ObjectImpl-based Tie, while rmic -iiop -poa
104      * results in a Servant-based Tie. Dynamic RMI-IIOP also uses Servant-based
105      * Ties (see impl.presentation.rmi.ReflectiveTie).
106      */

107     if (tie instanceof ObjectImpl JavaDoc) {
108         return tie.thisObject() ;
109     } else if (tie instanceof Servant JavaDoc) {
110         Servant JavaDoc servant = (Servant JavaDoc)tie ;
111         return activateServant( servant ) ;
112     } else {
113         throw wrapper.badActivateTieCall() ;
114     }
115     }
116
117
118     /** This also gets the delegate from a Servant by
119      * using Servant._this_object()
120      */

121     public static Delegate JavaDoc getDelegate( Object JavaDoc stub )
122     {
123     if (stub instanceof DynamicStub)
124         return ((DynamicStub)stub).getDelegate() ;
125     else if (stub instanceof ObjectImpl JavaDoc)
126         return ((ObjectImpl JavaDoc)stub)._get_delegate() ;
127     else if (stub instanceof Tie JavaDoc) {
128         Tie JavaDoc tie = (Tie JavaDoc)stub ;
129         org.omg.CORBA.Object JavaDoc ref = activateTie( tie ) ;
130         return getDelegate( ref ) ;
131     } else
132         throw wrapper.getDelegateRequiresStub() ;
133     }
134     
135     public static ORB JavaDoc getORB( Object JavaDoc stub )
136     {
137     if (stub instanceof DynamicStub)
138         return ((DynamicStub)stub).getORB() ;
139     else if (stub instanceof ObjectImpl JavaDoc)
140         return (ORB JavaDoc)((ObjectImpl JavaDoc)stub)._orb() ;
141     else
142         throw wrapper.getOrbRequiresStub() ;
143     }
144
145     public static String JavaDoc[] getTypeIds( Object JavaDoc stub )
146     {
147     if (stub instanceof DynamicStub)
148         return ((DynamicStub)stub).getTypeIds() ;
149     else if (stub instanceof ObjectImpl JavaDoc)
150         return ((ObjectImpl JavaDoc)stub)._ids() ;
151     else
152         throw wrapper.getTypeIdsRequiresStub() ;
153     }
154
155     public static void connect( Object JavaDoc stub,
156     ORB JavaDoc orb ) throws java.rmi.RemoteException JavaDoc
157     {
158     if (stub instanceof DynamicStub)
159         ((DynamicStub)stub).connect(
160         (com.sun.corba.se.spi.orb.ORB)orb ) ;
161     else if (stub instanceof javax.rmi.CORBA.Stub JavaDoc)
162         ((javax.rmi.CORBA.Stub JavaDoc)stub).connect( orb ) ;
163     else if (stub instanceof ObjectImpl JavaDoc)
164         orb.connect( (org.omg.CORBA.Object JavaDoc)stub ) ;
165     else
166         throw wrapper.connectRequiresStub() ;
167     }
168
169     public static boolean isLocal( Object JavaDoc stub )
170     {
171     if (stub instanceof DynamicStub)
172         return ((DynamicStub)stub).isLocal() ;
173     else if (stub instanceof ObjectImpl JavaDoc)
174         return ((ObjectImpl JavaDoc)stub)._is_local() ;
175     else
176         throw wrapper.isLocalRequiresStub() ;
177     }
178
179     public static OutputStream JavaDoc request( Object JavaDoc stub,
180     String JavaDoc operation, boolean responseExpected )
181     {
182     if (stub instanceof DynamicStub)
183         return ((DynamicStub)stub).request( operation,
184         responseExpected ) ;
185     else if (stub instanceof ObjectImpl JavaDoc)
186         return ((ObjectImpl JavaDoc)stub)._request( operation,
187         responseExpected ) ;
188     else
189         throw wrapper.requestRequiresStub() ;
190     }
191 }
192
Popular Tags