1 7 15 16 package javax.rmi; 17 18 import java.lang.reflect.Method ; 19 20 import org.omg.CORBA.INITIALIZE ; 21 import javax.rmi.CORBA.Util ; 22 23 import java.rmi.RemoteException ; 24 import java.rmi.NoSuchObjectException ; 25 import java.rmi.Remote ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.util.Properties ; 29 import java.net.MalformedURLException ; 30 import java.security.AccessController ; 31 import java.security.PrivilegedAction ; 32 import java.rmi.server.RMIClassLoader ; 33 34 import com.sun.corba.se.impl.orbutil.GetPropertyAction; 35 36 50 public class PortableRemoteObject { 51 52 private static javax.rmi.CORBA.PortableRemoteObjectDelegate proDelegate = null; 53 54 private static final String PortableRemoteObjectClassKey = 55 "javax.rmi.CORBA.PortableRemoteObjectClass"; 56 57 private static final String defaultPortableRemoteObjectImplName = 58 "com.sun.corba.se.impl.javax.rmi.PortableRemoteObject"; 59 60 static { 61 proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate ) 62 createDelegateIfSpecified(PortableRemoteObjectClassKey); 63 } 64 65 69 protected PortableRemoteObject() throws RemoteException { 70 if (proDelegate != null) { 71 PortableRemoteObject.exportObject((Remote )this); 72 } 73 } 74 75 82 public static void exportObject(Remote obj) 83 throws RemoteException { 84 85 if (proDelegate != null) { 87 proDelegate.exportObject(obj); 88 } 89 } 90 91 99 public static Remote toStub (Remote obj) 100 throws NoSuchObjectException { 101 102 if (proDelegate != null) { 103 return proDelegate.toStub(obj); 104 } 105 return null; 106 } 107 108 115 public static void unexportObject(Remote obj) 116 throws NoSuchObjectException { 117 118 if (proDelegate != null) { 119 proDelegate.unexportObject(obj); 120 } 121 122 } 123 124 132 public static java.lang.Object narrow ( java.lang.Object narrowFrom, 133 java.lang.Class narrowTo) 134 throws ClassCastException { 135 136 if (proDelegate != null) { 137 return proDelegate.narrow(narrowFrom, narrowTo); 138 } 139 return null; 140 141 } 142 143 155 public static void connect (Remote target, Remote source) 156 throws RemoteException { 157 158 if (proDelegate != null) { 159 proDelegate.connect(target, source); 160 } 161 162 } 163 164 private static Object createDelegateIfSpecified(String classKey) { 169 String className = (String ) 170 AccessController.doPrivileged(new GetPropertyAction(classKey)); 171 if (className == null) { 172 Properties props = getORBPropertiesFile(); 173 if (props != null) { 174 className = props.getProperty(classKey); 175 } 176 } 177 if (className == null) { 178 className = defaultPortableRemoteObjectImplName; 179 } 180 181 try { 182 return (Object ) loadDelegateClass(className).newInstance(); 183 } catch (ClassNotFoundException ex) { 184 INITIALIZE exc = new INITIALIZE ( "Cannot instantiate " + className); 185 exc.initCause( ex ) ; 186 throw exc ; 187 } catch (Exception ex) { 188 INITIALIZE exc = new INITIALIZE ( "Error while instantiating" + className); 189 exc.initCause( ex ) ; 190 throw exc ; 191 } 192 193 } 194 195 private static Class loadDelegateClass( String className ) throws ClassNotFoundException 196 { 197 try { 198 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 199 return Class.forName(className, false, loader); 200 } catch (ClassNotFoundException e) { 201 } 203 204 try { 205 return RMIClassLoader.loadClass(className); 206 } catch (MalformedURLException e) { 207 String msg = "Could not load " + className + ": " + e.toString(); 208 ClassNotFoundException exc = new ClassNotFoundException ( msg ) ; 209 throw exc ; 210 } 211 } 212 213 216 private static Properties getORBPropertiesFile () { 217 return (Properties ) AccessController.doPrivileged(new GetORBPropertiesFileAction()); 218 } 219 } 220 221 class GetORBPropertiesFileAction implements PrivilegedAction { 222 private boolean debug = false ; 223 224 public GetORBPropertiesFileAction () { 225 } 226 227 private String getSystemProperty(final String name) { 228 String propValue = (String ) AccessController.doPrivileged( 231 new PrivilegedAction () { 232 public java.lang.Object run() { 233 return System.getProperty(name); 234 } 235 } 236 ); 237 238 return propValue; 239 } 240 241 private void getPropertiesFromFile( Properties props, String fileName ) 242 { 243 try { 244 File file = new File ( fileName ) ; 245 if (!file.exists()) 246 return ; 247 248 FileInputStream in = new FileInputStream ( file ) ; 249 250 try { 251 props.load( in ) ; 252 } finally { 253 in.close() ; 254 } 255 } catch (Exception exc) { 256 if (debug) 257 System.out.println( "ORB properties file " + fileName + 258 " not found: " + exc) ; 259 } 260 } 261 262 public Object run() 263 { 264 Properties defaults = new Properties () ; 265 266 String javaHome = getSystemProperty( "java.home" ) ; 267 String fileName = javaHome + File.separator + "lib" + File.separator + 268 "orb.properties" ; 269 270 getPropertiesFromFile( defaults, fileName ) ; 271 272 Properties results = new Properties ( defaults ) ; 273 274 String userHome = getSystemProperty( "user.home" ) ; 275 fileName = userHome + File.separator + "orb.properties" ; 276 277 getPropertiesFromFile( results, fileName ) ; 278 return results ; 279 } 280 } 281 282 283 | Popular Tags |