1 26 27 package org.objectweb.openccm.corba; 28 29 import java.net.InetAddress ; 31 import java.net.DatagramPacket ; 32 import java.net.DatagramSocket ; 33 import java.net.MulticastSocket ; 34 import java.net.SocketException ; 35 import java.net.UnknownHostException ; 36 37 import java.io.IOException ; 38 39 46 47 public abstract class TheORBSingleton 48 { 49 55 58 private static org.omg.CORBA.ORB orb_ = null; 59 60 63 private static java.util.Hashtable initialReferences_ = 64 new java.util.Hashtable (); 65 69 75 78 protected TheORBSingleton() 79 { 80 } 81 82 88 95 protected static void initialize( 96 String [] args, 97 String orbClass, 98 String orbSingletonClass) 99 { 100 101 java.util.Properties properties = System.getProperties(); 102 properties.put("org.omg.CORBA.ORBClass", orbClass); 103 properties.put("org.omg.CORBA.ORBSingletonClass", orbSingletonClass); 104 105 setORB(org.omg.CORBA.ORB.init(args, properties)); 107 } 108 109 116 public static String 117 request_ior(String location) 118 { 119 120 122 String file = 123 location.substring( 124 location.indexOf("//") + 2).substring( 125 location.substring( 126 location.indexOf("//") + 2).indexOf( 127 "/") 128 + 1); 129 String addr = 130 location.substring( 131 location.indexOf("//") + 2).substring( 132 0, 133 location.substring( 134 location.indexOf("//") + 2).indexOf( 135 "/")); 136 137 int s_port = 8888; 139 int ms_port = 8880; 140 141 143 InetAddress groupAddress = null; 144 DatagramSocket socket; 145 MulticastSocket msocket; 146 147 try { 148 groupAddress = InetAddress.getByName(addr); 149 } catch (UnknownHostException ex) { 150 System.out.println("Unknown host" + ex.getMessage()); 151 } 152 153 String message = "NameService.IOR"; 154 155 message = file; 158 159 System.out.println("Sending multicast request to get IOR..."); 160 DatagramPacket send_request = 161 new DatagramPacket ( 162 message.getBytes(), 163 message.getBytes().length, 164 groupAddress, 165 ms_port); 166 try { 167 msocket = new MulticastSocket (); 168 msocket.send(send_request); 169 msocket.disconnect(); 170 msocket.close(); 171 msocket.close(); 172 173 } catch (IOException e) { 174 System.err.println(e.getMessage()); 175 } 176 177 byte[] mem = new byte[1000]; 179 String ior_recv = null; 180 try { 181 socket = new DatagramSocket (s_port + 1); 182 DatagramPacket recv = new DatagramPacket (mem, mem.length); 183 socket.receive(recv); 184 ior_recv = new String (mem, 0, recv.getLength() - 2); 185 System.out.println( 186 "Receiving IOR from server " 187 + recv.getAddress().getHostName() 188 + ":" 189 + recv.getPort()); 190 socket.disconnect(); 191 socket.close(); 192 } catch (SocketException e) { 193 } catch (IOException e) { 194 } 195 196 return ior_recv; 197 198 } 199 200 207 protected static String [] 208 remove_ORB_arguments(String [] args) { 209 int nb = 0; 210 211 String [] tmp = new String [args.length]; 213 for (int i = 0; i < args.length; i++) { 214 if (args[i].startsWith("-ORBInitRef")) { 215 i++; 216 } else if (!args[i].startsWith("-ORB")) { 217 tmp[nb] = args[i]; 218 nb++; 219 } 220 } 221 222 args = new String [nb]; 224 for (int i = 0; i < nb; i++) { 225 args[i] = tmp[i]; 226 } 227 228 return args; 230 } 231 232 238 243 public static org.omg.CORBA.ORB 244 getORB() 245 { 246 if (orb_ == null) { 247 orb_ = org.omg.CORBA.ORB.init(); 248 } 249 return orb_; 250 } 251 252 257 public static void 258 setORB(org.omg.CORBA.ORB orb) 259 { 260 orb_ = orb; 261 } 262 263 270 public static org.omg.CORBA.Object 271 resolve_initial_reference(String name) 272 { 273 Object object = initialReferences_.get(name); 278 if (object != null) { 279 return (org.omg.CORBA.Object ) object; 280 } 281 282 try { 283 return getORB().resolve_initial_references(name); 284 } catch (org.omg.CORBA.ORBPackage.InvalidName exc) { 285 throw new UserExceptionWrapper(exc); 287 } 288 } 289 290 296 public static void 297 register_initial_reference( String name, 298 org.omg.CORBA.Object object) 299 { 300 initialReferences_.put(name, object); 305 306 } 313 314 317 public static void 318 run() 319 { 320 getORB().run(); 321 } 322 323 329 public static void 330 shutdown(boolean wait_for_completion) 331 { 332 getORB().shutdown(wait_for_completion); 333 } 334 335 340 public static org.omg.CORBA.Any 341 create_any() 342 { 343 return getORB().create_any(); 344 } 345 346 352 public static void register_value_factory( 353 String id, 354 org.omg.CORBA.portable.ValueFactory factory) 355 { 356 ((org.omg.CORBA_2_3.ORB ) getORB()).register_value_factory(id, factory); 357 } 358 359 365 public static void 366 save_IOR(org.omg.CORBA.Object object, String fileName) 367 { 368 try { 369 String ior = getORB().object_to_string(object); 371 372 java.io.FileOutputStream file = 374 new java.io.FileOutputStream (fileName); 375 java.io.PrintWriter out = new java.io.PrintWriter (file); 376 377 out.println(ior); 379 380 out.flush(); 382 file.close(); 383 } catch (java.io.IOException exc) { 384 throw new java.lang.Error (exc.getMessage()); 386 } 387 } 388 389 396 public static org.omg.CORBA.Object 397 string_to_object(String string) 398 { 399 return getORB().string_to_object(string); 400 } 401 402 405 public static org.omg.CORBA.TypeCode 406 create_valuebase_tc() 407 { 408 return getORB().create_value_tc( 409 "IDL:omg.org/CORBA/ValueBase:1.0", 410 "ValueBase", 411 org.omg.CORBA.VM_ABSTRACT.value, 412 null, 413 new org.omg.CORBA.ValueMember [0]); 414 } 415 416 419 public static org.omg.CORBA.TypeCode 420 get_null_valuebase_tc() 421 { 422 return null; 423 } 424 } 425 | Popular Tags |