1 19 package gcc.adapter; 20 21 import gcc.rmi.iiop.RemoteInterface; 22 23 import java.util.HashMap; 24 import java.util.Vector; 25 import java.lang.reflect.Method; 26 import java.lang.reflect.InvocationTargetException; 27 28 public class Adapter 29 { 30 protected String _bindName; 34 protected String _remoteClassName; 35 protected String _remoteInterfaceName; 36 protected Vector _idVector; 37 protected boolean _shared; 38 protected ClassLoader _cl; 39 protected RemoteInterface _ri; 40 41 45 protected Object _sharedObject; 46 protected HashMap _objects; 47 protected Class _remoteClassClass; 48 protected Class _remoteInterfaceClass; 49 50 public Adapter() 51 { 52 _objects = new HashMap(); 53 _idVector = new Vector(); 54 } 55 56 59 public String getBindName() 60 { 61 return _bindName; 62 } 63 64 public void setBindName( String bindName ) 65 { 66 _bindName = bindName; 67 } 68 69 73 public boolean isShared() 74 { 75 return _shared; 76 } 77 78 public void setShared( boolean shared ) 79 { 80 _shared = shared; 81 } 82 83 87 public ClassLoader getClassLoader() 88 { 89 return _cl; 90 } 91 92 public void setClassLoader( ClassLoader cl ) 93 { 94 _cl = cl; 95 } 96 97 103 public String getRemoteClassName() 104 { 105 return _remoteClassName; 106 } 107 108 public void setRemoteClassName( String rcName ) 109 { 110 _remoteClassName = rcName; 111 } 112 113 119 public String getRemoteInterfaceName() 120 { 121 return _remoteInterfaceName; 122 } 123 124 public void setRemoteInterfaceName( String riName ) 125 { 126 _remoteInterfaceName = riName; 127 } 128 129 135 public Vector getIds() 136 { 137 return _idVector; 138 } 139 140 public void addId( String id ) 141 { 142 _idVector.add(id); 143 } 144 145 public void removeId( String id ) 146 { 147 _idVector.remove( id ); 148 } 149 150 154 public RemoteInterface getRemoteInterface() 155 { 156 if (_ri == null) 157 { 158 synchronized( this ) 159 { 160 String riName = _remoteInterfaceName + "_Skeleton"; 161 _remoteInterfaceClass = loadClass( riName ); 162 163 try 164 { 165 _ri = (RemoteInterface)_remoteInterfaceClass.newInstance(); 166 } 167 catch (InstantiationException e) 168 { 169 e.printStackTrace(); } 171 catch (IllegalAccessException e) 172 { 173 e.printStackTrace(); } 175 } 176 } 177 178 return _ri; 179 } 180 181 187 public Object getInstance( byte[] objectKey ) 188 { 189 String key = new String( objectKey ); 190 return getInstance( key ); 191 } 192 193 public Object getInstance( String key ) 194 { 195 Object o = _objects.get( key ); 196 197 if (o == null) 198 { 199 o = newInstance( key ); 200 } 201 202 return o; 203 } 204 205 public Object newInstance( byte[] objectKey ) 206 { 207 String key = new String( objectKey ); 208 return newInstance( key ); 209 } 210 211 public Object newInstance( String key ) 212 { 213 Object o = null; 214 215 if (_remoteClassClass == null) 216 { 217 synchronized( this ) 218 { 219 _remoteClassClass = loadClass( _remoteClassName ); 220 } 221 222 try 223 { 224 if (_shared) 225 { 226 synchronized( this ) 227 { 228 Method m = _remoteClassClass.getMethod( "getInstance", (Class[]) null ); 229 o = m.invoke( _remoteClassClass, (Object[]) null ); 230 231 if (o != null) 232 { 233 _objects.put( key, o ); 234 } 235 } 236 } 237 else 238 { 239 o = _remoteClassClass.newInstance(); 240 _objects.put( key, o ); 241 } 242 } 243 catch (InstantiationException e) 244 { 245 e.printStackTrace(); } 247 catch (IllegalAccessException e) 248 { 249 e.printStackTrace(); } 251 catch( NoSuchMethodException e ) 252 { 253 e.printStackTrace(); 254 } 255 catch( InvocationTargetException e ) 256 { 257 e.printStackTrace(); 258 } 259 } 260 261 return o; 262 } 263 264 271 public void invoke( java.lang.String methodName, byte[] objectKey, gcc.rmi.iiop.ObjectInputStream input, gcc.rmi.iiop.ObjectOutputStream output ) 272 { 273 RemoteInterface skeleton = getRemoteInterface(); 274 Object instance = getInstance( objectKey ); 275 276 if (instance != null) 277 { 278 skeleton.$invoke( methodName, objectKey, instance, input, output ); 279 } 280 else 281 { 282 throw new org.omg.CORBA.OBJECT_NOT_EXIST(new String(objectKey)); 283 } 284 } 285 286 289 protected Class loadClass( String name ) 290 { 291 Class c = null; 292 293 try 294 { 295 c = _cl.loadClass( name ); 296 } 297 catch( Exception e ) 298 { 299 e.printStackTrace(); 300 } 301 302 return c; 303 } 304 } 305 | Popular Tags |