1 package org.jacorb.ir; 2 3 22 23 import java.lang.reflect.*; 24 import java.util.*; 25 import java.io.*; 26 27 import org.omg.CORBA.INTF_REPOS ; 28 import org.omg.CORBA.TypeCode ; 29 import org.omg.CORBA.Any ; 30 import org.omg.PortableServer.POA ; 31 32 import org.apache.avalon.framework.logger.Logger; 33 34 public class UnionDef 35 extends TypedefDef 36 implements org.omg.CORBA.UnionDefOperations , ContainerType 37 { 38 protected static char fileSeparator = 39 System.getProperty("file.separator").charAt(0); 40 41 private org.omg.CORBA.UnionMember [] members; 42 private org.omg.CORBA.TypeCode discriminator_type; 43 private org.omg.CORBA.IDLType discriminator_type_def; 44 45 private Method memberMethods[]; 46 private int member_size; 47 48 49 private org.omg.CORBA.Contained myContainer; 50 51 private Hashtable containedLocals = new Hashtable(); 52 53 private Hashtable contained = new Hashtable(); 54 55 private File my_dir; 56 private String path; 57 private Logger logger; 58 private ClassLoader loader; 59 private POA poa; 60 61 public UnionDef( Class c, 62 String path, 63 org.omg.CORBA.Container _defined_in, 64 org.omg.CORBA.Repository ir, 65 ClassLoader loader, 66 Logger logger, 67 POA poa ) 68 { 69 this.loader = loader; 70 this.logger = logger; 71 this.poa = poa; 72 73 def_kind = org.omg.CORBA.DefinitionKind.dk_Union; 74 containing_repository = ir; 75 defined_in = _defined_in; 76 version = "1.0"; 77 String classId = c.getName(); 78 myContainer = org.omg.CORBA.ContainedHelper.narrow( defined_in ); 79 80 if( classId.indexOf('.') > 0 ) 81 { 82 name( classId.substring( classId.lastIndexOf('.')+1)); 83 absolute_name = myContainer.absolute_name() + "::" + name; 84 } 85 else 86 { 87 name( classId ); 88 defined_in = containing_repository; 89 absolute_name = "::" + name; 90 } 91 92 Class helperClass; 93 try 94 { 95 helperClass = this.loader.loadClass(classId + "Helper"); 96 id( (String )helperClass.getDeclaredMethod("id", (Class []) null).invoke( null, (Object []) null )); 97 type = TypeCodeUtil.getTypeCode( c, this.loader, null, classId, this.logger ); 98 members = new org.omg.CORBA.UnionMember [ type.member_count() ]; 99 for( int i = 0; i < members.length; i++ ) 100 { 101 members[i] = new org.omg.CORBA.UnionMember ( type.member_name(i), 102 type.member_label(i), 103 type.member_type(i), 104 null ); 105 } 106 discriminator_type = type.discriminator_type(); 107 } 108 catch( Exception e ) 109 { 110 this.logger.error("Caught Exception", e); 111 } 112 } 113 114 public void loadContents() 115 { 116 if (getReference() == null) 118 { 119 throw new INTF_REPOS ("getReference returns null"); 120 } 121 122 org.omg.CORBA.UnionDef myReference = 123 org.omg.CORBA.UnionDefHelper.narrow( getReference()); 124 125 if (myReference == null) 126 { 127 throw new INTF_REPOS ("narrow failed for " + getReference() ); 128 } 129 130 131 132 String [] classes = null; 133 if( my_dir != null ) 134 { 135 classes = my_dir.list( new IRFilenameFilter(".class") ); 136 137 if( classes != null) 139 { 140 for( int j = 0; j < classes.length; j++ ) 141 { 142 try 143 { 144 if (this.logger.isDebugEnabled()) 145 { 146 this.logger.debug("Union " +name+ " tries " + 147 full_name.replace('.', fileSeparator) + 148 "Package" + fileSeparator + 149 classes[j].substring( 0, classes[j].indexOf(".class"))); 150 } 151 152 ClassLoader loader = getClass().getClassLoader(); 153 if( loader == null ) 154 { 155 loader = this.loader; 156 } 157 158 Class cl = 159 loader.loadClass( 160 ( full_name.replace('.', fileSeparator) + "Package" + fileSeparator + 161 classes[j].substring( 0, classes[j].indexOf(".class")) 162 ).replace( fileSeparator, '/') ); 163 164 165 Contained containedObject = 166 Contained.createContained( cl, 167 path, 168 myReference, 169 containing_repository, 170 this.logger, 171 this.loader, 172 this.poa); 173 if( containedObject == null ) 174 continue; 175 176 org.omg.CORBA.Contained containedRef = 177 Contained.createContainedReference(containedObject, 178 this.logger, 179 this.poa); 180 181 if( containedObject instanceof ContainerType ) 182 ((ContainerType)containedObject).loadContents(); 183 184 containedRef.move( myReference, containedRef.name(), containedRef.version() ); 185 186 if (this.logger.isDebugEnabled()) 187 { 188 this.logger.debug("Union " + full_name + 189 " loads "+ containedRef.name()); 190 } 191 192 contained.put( containedRef.name() , containedRef ); 193 containedLocals.put( containedRef.name(), containedObject ); 194 } 195 catch ( Exception e ) 196 { 197 this.logger.error("Caught Exception", e); 198 } 199 } 200 } 201 } 202 } 203 204 205 public void define() 206 { 207 if (this.logger.isDebugEnabled()) 208 { 209 this.logger.debug("Union " + name + " defining..."); 210 } 211 212 discriminator_type_def = 213 IDLType.create( discriminator_type, containing_repository, 214 this.logger, this.poa); 215 216 for( Enumeration e = containedLocals.elements(); 217 e.hasMoreElements(); 218 ((IRObject)e.nextElement()).define()) 219 ; 220 221 try 222 { 223 for( int i = 0; i < members.length; i++ ) 224 { 225 members[i].type_def = 226 IDLType.create( members[i].type, containing_repository, 227 this.logger, this.poa ); 228 } 229 } 230 catch ( Exception e ) 231 { 232 this.logger.error("Caught Exception", e); 233 } 234 235 if (this.logger.isDebugEnabled()) 236 { 237 this.logger.debug("UnionDef " + name + " defined"); 238 } 239 } 240 241 public org.omg.CORBA.UnionMember [] members() 242 { 243 return members; 244 } 245 246 public void members(org.omg.CORBA.UnionMember [] m) 247 { 248 members = m; 249 } 250 251 public org.omg.CORBA.TypeCode discriminator_type() 252 { 253 return discriminator_type; 254 } 255 256 public org.omg.CORBA.IDLType discriminator_type_def() 257 { 258 return discriminator_type_def; 259 } 260 261 public void discriminator_type_def(org.omg.CORBA.IDLType dt) 262 { 263 discriminator_type_def = dt; 264 } 265 266 267 269 public org.omg.CORBA.ContainedPackage.Description describe() 270 { 271 org.omg.CORBA.Any a = orb.create_any(); 272 String def_in_name; 273 274 if( myContainer != null ) 275 def_in_name = myContainer.id(); 276 else 277 def_in_name = "IDL:/:1.0"; 278 279 org.omg.CORBA.TypeDescriptionHelper.insert( a, 280 new org.omg.CORBA.TypeDescription ( name(), 281 id(), 282 def_in_name, 283 version(), 284 type() 285 ) ); 286 return new org.omg.CORBA.ContainedPackage.Description( org.omg.CORBA.DefinitionKind.dk_Union, a); 287 } 288 289 291 public void destroy(){} 292 293 294 public org.omg.CORBA.Contained [] contents(org.omg.CORBA.DefinitionKind limit_type, 295 boolean exclude_inherited) 296 { 297 Hashtable filtered = new Hashtable(); 298 299 if( limit_type == org.omg.CORBA.DefinitionKind.dk_all ) 300 { 301 filtered = contained; 302 } 303 else 304 { 305 Enumeration f = contained.keys(); 306 while( f.hasMoreElements() ) 307 { 308 Object k = f.nextElement(); 309 org.omg.CORBA.Contained c = (org.omg.CORBA.Contained )contained.get( k ); 310 if( c.def_kind() == limit_type ) 311 filtered.put( k, c ); 312 } 313 } 314 315 Enumeration e = filtered.elements(); 316 org.omg.CORBA.Contained [] result = new org.omg.CORBA.Contained [ filtered.size() ]; 317 318 for( int i = 0; i < filtered.size(); i++ ) 319 result[i] = (org.omg.CORBA.Contained )e.nextElement(); 320 321 return result; 322 } 323 324 327 328 public org.omg.CORBA.Contained lookup( String scopedname ) 329 { 330 String top_level_name; 331 String rest_of_name; 332 String name; 333 334 if( scopedname.startsWith("::") ) 335 { 336 name = scopedname.substring(2); 337 } 338 else 339 name = scopedname; 340 341 if( name.indexOf("::") > 0 ) 342 { 343 top_level_name = name.substring( 0, name.indexOf("::") ); 344 rest_of_name = name.substring( name.indexOf("::") + 2); 345 } 346 else 347 { 348 top_level_name = name; 349 rest_of_name = null; 350 } 351 352 org.omg.CORBA.Contained top = (org.omg.CORBA.Contained )contained.get( top_level_name ); 353 if( top == null ) 354 { 355 if (this.logger.isDebugEnabled()) 356 { 357 this.logger.debug("Container " + this.name + " top " + 358 top_level_name + " not found "); 359 } 360 361 return null; 362 } 363 364 if( rest_of_name == null ) 365 { 366 return top; 367 } 368 else 369 { 370 org.omg.CORBA.Container topContainer = org.omg.CORBA.ContainerHelper.narrow( top ); 371 if( topContainer != null ) 372 { 373 return topContainer.lookup( rest_of_name ); 374 } 375 else 376 { 377 if (this.logger.isDebugEnabled()) 378 { 379 this.logger.debug("Container " + this.name +" " + 380 scopedname + " not found, top " + 381 top.getClass().getName()); 382 } 383 return null; 384 } 385 } 386 } 387 388 public org.omg.CORBA.Contained [] lookup_name( String search_name, 389 int levels_to_search, 390 org.omg.CORBA.DefinitionKind limit_type, 391 boolean exclude_inherited) 392 { 393 if( levels_to_search == 0 ) 394 return null; 395 396 org.omg.CORBA.Contained [] c = contents( limit_type, exclude_inherited ); 397 Hashtable found = new Hashtable(); 398 399 for( int i = 0; i < c.length; i++) 400 if( c[i].name().equals( search_name ) ) 401 found.put( c[i], "" ); 402 403 if( levels_to_search > 1 || levels_to_search < 0 ) 404 { 405 for( int i = 0; i < c.length; i++) 407 { 408 if( c[i] instanceof org.omg.CORBA.Container ) 409 { 410 org.omg.CORBA.Contained [] tmp_seq = 411 ((org.omg.CORBA.Container )c[i]).lookup_name( 412 search_name, levels_to_search-1, limit_type, exclude_inherited); 413 if( tmp_seq != null ) 414 for( int j = 0; j < tmp_seq.length; j++) 415 found.put( tmp_seq[j], "" ); 416 } 417 } 418 } 419 420 421 org.omg.CORBA.Contained [] result = new org.omg.CORBA.Contained [ found.size() ]; 422 int idx = 0; 423 424 for( Enumeration e = found.keys(); e.hasMoreElements(); ) 425 result[ idx++] = (org.omg.CORBA.Contained )e.nextElement(); 426 427 return result; 428 } 429 430 public org.omg.CORBA.ContainerPackage.Description[] describe_contents(org.omg.CORBA.DefinitionKind limit_type, boolean exclude_inherited, int max_returned_objs) 431 { 432 return null; 433 } 434 435 public org.omg.CORBA.ModuleDef create_module( String id, String name, String version){ 436 return null; 437 } 438 439 public org.omg.CORBA.ConstantDef create_constant( String id, String name, String version, org.omg.CORBA.IDLType type, org.omg.CORBA.Any value){ 440 return null; 441 } 442 443 public org.omg.CORBA.StructDef create_struct( String id, String name, String version, org.omg.CORBA.StructMember [] members){ 444 return null; 445 } 446 447 public org.omg.CORBA.UnionDef create_union( String id, String name, String version, org.omg.CORBA.IDLType discriminator_type, org.omg.CORBA.UnionMember [] members){ 448 return null; 449 } 450 451 public org.omg.CORBA.EnumDef create_enum( String id, String name, String version, String [] members){ 452 return null; 453 } 454 455 public org.omg.CORBA.AliasDef create_alias( String id, String name, String version, org.omg.CORBA.IDLType original_type){ 456 return null; 457 } 458 459 462 463 public org.omg.CORBA.ExceptionDef create_exception(java.lang.String id, java.lang.String name , java.lang.String version, org.omg.CORBA.StructMember [] member ) 464 { 465 return null; 466 } 467 468 471 472 public org.omg.CORBA.InterfaceDef create_interface( 473 String id, 474 String name, 475 String version, 476 org.omg.CORBA.InterfaceDef [] base_interfaces, 477 boolean is_abstract ) 478 { 479 return null; 480 } 481 482 485 486 public org.omg.CORBA.ValueBoxDef create_value_box(java.lang.String id, 487 java.lang.String name, 488 java.lang.String version, 489 org.omg.CORBA.IDLType type) 490 { 491 return null; 492 } 493 494 495 498 499 public org.omg.CORBA.ValueDef create_value( 500 java.lang.String id, 501 java.lang.String name, 502 java.lang.String version, 503 boolean is_custom, 504 boolean is_abstract, 505 org.omg.CORBA.ValueDef base_value, 506 boolean is_truncatable, 507 org.omg.CORBA.ValueDef [] abstract_base_values, 508 org.omg.CORBA.InterfaceDef [] supported_interfaces, 509 org.omg.CORBA.Initializer [] initializers) 510 { 511 return null; 512 } 513 514 515 518 519 public org.omg.CORBA.NativeDef create_native(java.lang.String id, 520 java.lang.String name, 521 java.lang.String version) 522 { 523 return null; 524 } 525 526 527 528 } 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | Popular Tags |