KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)JNDIStateFactoryImpl.java 1.6 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.Field JavaDoc ;
11
12 import java.util.Hashtable JavaDoc;
13
14 import javax.naming.*;
15 import javax.naming.spi.StateFactory JavaDoc;
16
17 import java.security.AccessController JavaDoc ;
18 import java.security.PrivilegedAction JavaDoc ;
19
20 import javax.rmi.PortableRemoteObject JavaDoc ;
21
22 import com.sun.corba.se.spi.orb.ORB;
23
24 import java.rmi.Remote JavaDoc;
25 import java.rmi.server.ExportException JavaDoc;
26
27 // XXX This creates a dependendcy on the implementation
28
// of the CosNaming service provider.
29
import com.sun.jndi.cosnaming.CNCtx ;
30
31 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
32
33 /**
34   * StateFactory that turns java.rmi.Remote objects to org.omg.CORBA.Object.
35   * This version works either with standard RMI-IIOP or Dynamic RMI-IIOP.
36   * Based on the original com.sun.jndi.cosnaming.RemoteToCorba and
37   * com.sun.jndi.toolkit.corba.CorbaUtils.
38   *
39   * @author Ken Cavanaugh
40   */

41
42 public class JNDIStateFactoryImpl implements StateFactory JavaDoc
43 {
44     private static final Field JavaDoc orbField ;
45
46     static {
47     orbField = (Field JavaDoc) AccessController.doPrivileged(
48         new PrivilegedAction JavaDoc() {
49         public Object JavaDoc run() {
50             Field JavaDoc fld = null ;
51             try {
52             Class JavaDoc cls = CNCtx.class ;
53             fld = cls.getDeclaredField( "_orb" ) ;
54             fld.setAccessible( true ) ;
55             } catch (Exception JavaDoc exc) {
56             // XXX log exception at FINE
57
}
58             return fld ;
59         }
60         }
61     ) ;
62     }
63
64     public JNDIStateFactoryImpl()
65     {
66     }
67
68     /**
69      * Returns the CORBA object for a Remote object.
70      * If input is not a Remote object, or if Remote object uses JRMP, return null.
71      * If the RMI-IIOP library is not available, throw ConfigurationException.
72      *
73      * @param orig The object to turn into a CORBA object. If not Remote,
74      * or if is a JRMP stub or impl, return null.
75      * @param name Ignored
76      * @param ctx The non-null CNCtx whose ORB to use.
77      * @param env Ignored
78      * @return The CORBA object for <tt>orig</tt> or null.
79      * @exception ConfigurationException If the CORBA object cannot be obtained
80      * due to configuration problems
81      * @exception NamingException If some other problem prevented a CORBA
82      * object from being obtained from the Remote object.
83      */

84     public Object JavaDoc getStateToBind(Object JavaDoc orig, Name name, Context ctx,
85     Hashtable JavaDoc<?,?> env) throws NamingException
86     {
87     if (orig instanceof org.omg.CORBA.Object JavaDoc)
88         return orig ;
89
90         if (!(orig instanceof Remote JavaDoc))
91         // Not for this StateFactory
92
return null ;
93
94     ORB orb = getORB( ctx ) ;
95     if (orb == null)
96         // Wrong kind of context, so just give up and let another StateFactory
97
// try to satisfy getStateToBind.
98
return null ;
99
100     Remote JavaDoc stub = null;
101
102     try {
103         stub = PortableRemoteObject.toStub( (Remote JavaDoc)orig ) ;
104     } catch (Exception JavaDoc exc) {
105         // XXX log at FINE level?
106
// Wrong sort of object: just return null to allow another StateFactory
107
// to handle this. This can happen easily because this StateFactory
108
// is specified for the application, not the service context provider.
109
return null ;
110     }
111
112     if (StubAdapter.isStub( stub )) {
113         try {
114         StubAdapter.connect( stub, orb ) ;
115         } catch (Exception JavaDoc exc) {
116         if (!(exc instanceof java.rmi.RemoteException JavaDoc)) {
117             // XXX log at FINE level?
118
// Wrong sort of object: just return null to allow another StateFactory
119
// to handle this call.
120
return null ;
121         }
122
123         // ignore RemoteException because stub might have already
124
// been connected
125
}
126     }
127
128     return stub ;
129     }
130
131     // This is necessary because the _orb field is package private in
132
// com.sun.jndi.cosnaming.CNCtx. This is not an ideal solution.
133
// The best solution for our ORB is to change the CosNaming provider
134
// to use the StubAdapter. But this has problems as well, because
135
// other vendors may use the CosNaming provider with a different ORB
136
// entirely.
137
private ORB getORB( Context ctx )
138     {
139     ORB orb = null ;
140
141     try {
142         orb = (ORB)orbField.get( ctx ) ;
143     } catch (Exception JavaDoc exc) {
144         // XXX log this exception at FINE level
145
// ignore the exception and return null.
146
// Note that the exception may be because ctx
147
// is not a CosNaming context.
148
}
149
150     return orb ;
151     }
152 }
153
Popular Tags