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