1 20 21 package org.jacorb.orb.rmi; 22 23 27 28 import javax.rmi.CORBA.Tie ; 29 import javax.rmi.CORBA.Stub ; 30 import javax.rmi.CORBA.Util ; 31 32 import org.omg.CORBA.ORB ; 33 34 public class PortableRemoteObjectDelegateImpl implements javax.rmi.CORBA.PortableRemoteObjectDelegate 35 { 36 private static ORB _orb = null; 37 38 42 public static synchronized ORB getORB() 43 { 44 if ( _orb == null ) 45 { 46 System.out.println("Unknwon ORB"); 47 _orb = ORB.init( new String [0], null ); 48 } 49 return _orb; 50 } 51 52 56 public static synchronized void setORB( ORB orb ) 57 { 58 if ( _orb != null ) 59 { 60 throw new IllegalStateException ( "RMI orb has already been initialized" ); 61 } 62 _orb = orb; 63 } 64 65 69 public void exportObject( java.rmi.Remote obj ) throws java.rmi.RemoteException 70 { 71 if (obj == null) throw new NullPointerException (); 72 if ( obj instanceof Stub ) 73 { 74 throw new java.rmi.server.ExportException ( "Attempted to export a stub class" ); 75 } 76 Tie tie = Util.getTie( obj ); 77 if ( tie != null ) 78 { 79 throw new java.rmi.server.ExportException ( "Object already exported" ); 80 } 81 tie = toTie( obj ); 82 tie.orb( getORB() ); 83 Util.registerTarget( tie, obj ); 84 } 85 86 92 public java.rmi.Remote toStub( java.rmi.Remote obj ) throws java.rmi.NoSuchObjectException 93 { 94 if ( obj instanceof Stub ) 95 { 96 return obj; 97 } 98 99 Tie tie = null; 100 if ( obj instanceof Tie ) 101 { 102 tie = ( Tie ) obj; 103 obj = tie.getTarget(); 104 } 105 else 106 { 107 tie = Util.getTie( obj ); 108 } 109 if ( tie == null ) 110 { 111 throw new java.rmi.NoSuchObjectException ( "Object not exported" ); 112 } 113 114 org.omg.CORBA.Object thisObject = tie.thisObject(); 115 if ( thisObject instanceof java.rmi.Remote ) 116 { 117 return ( java.rmi.Remote ) thisObject; 118 } 119 throw new java.rmi.NoSuchObjectException ( "Object not exported" ); 120 } 121 122 127 public void unexportObject( java.rmi.Remote obj ) throws java.rmi.NoSuchObjectException 128 { 129 Tie tie = Util.getTie( obj ); 130 if ( tie == null ) 131 { 132 throw new java.rmi.NoSuchObjectException ( "Object not exported" ); 133 } 134 Util.unexportObject( obj ); 135 } 136 137 144 public Object narrow( Object obj, Class newClass ) throws ClassCastException 145 { 146 if (newClass == null) 147 throw new ClassCastException ("Can't narrow to null class"); 148 if (obj == null) 149 return null; 150 151 Class fromClass = obj.getClass(); 152 Object result = null; 153 154 try 155 { 156 if (newClass.isAssignableFrom(fromClass)) 157 result = obj; 158 else 159 { 160 Class [] cs = fromClass.getInterfaces(); 161 Exception e1 = new Exception (); 162 try 163 { 164 throw e1; 165 } 166 catch(Exception ee) 167 { 168 ee.printStackTrace(); 169 } 170 System.exit(2); 171 } 172 } 173 catch(Exception e) 174 { 175 result = null; 176 } 177 178 if (result == null) 179 throw new ClassCastException ("Can't narrow from " + fromClass + " to " + newClass); 180 181 return result; 182 } 183 184 187 public void connect( java.rmi.Remote target, java.rmi.Remote source ) throws java.rmi.RemoteException 188 { 189 throw new Error ("Not implemented for PortableRemoteObjectDelegateImpl"); 190 } 191 192 198 static Tie toTie( java.rmi.Remote obj ) throws java.rmi.server.ExportException 199 { 200 for (Class clz = obj.getClass(); clz != null; clz = clz.getSuperclass()) { 201 try 202 { 203 String clzName = clz.getName(); 204 String [] clzParts = clzName.split("\\."); 205 clzParts[clzParts.length - 1] = "_" + clzParts[clzParts.length - 1] + "_Tie"; 206 StringBuffer tieClzName = new StringBuffer ("org.omg.stub"); 207 for (int i = 0; i < clzParts.length; i++) tieClzName.append("." + clzParts[i]); 208 Class tieClass = Util.loadClass(tieClzName.toString(), Util.getCodebase( clz ), clz.getClassLoader() ); 209 return ( javax.rmi.CORBA.Tie ) tieClass.newInstance(); 210 } 211 catch ( ClassNotFoundException ex ) 212 { 213 } 215 catch (InstantiationException e) 216 { 217 throw new java.rmi.server.ExportException ("InstantiationException: " + e, e ); 218 } 219 catch (IllegalAccessException e) 220 { 221 throw new java.rmi.server.ExportException ("IllegalAccessException: " + e, e ); 222 } 223 } 224 throw new java.rmi.server.ExportException ("Tie class not found "); 225 } 226 } 227 | Popular Tags |