1 28 29 30 package org.objectweb.ccm.runtime; 31 32 import org.objectweb.corba.runtime.*; 33 import org.objectweb.corba.ast.api.*; 35 import org.objectweb.corba.ast.lib.*; 36 import org.objectweb.corba.generator.java.lib.*; 37 import org.objectweb.corba.generator.java.ast.api.*; 38 import org.objectweb.corba.generator.java.ast.lib.*; 39 import org.objectweb.corba.generator.common.lib.GenerationException; 40 41 44 public class CIDLJImplGenerator 45 extends org.objectweb.corba.generator.common.lib.GeneratorBase 46 { 47 private ExtendedJavaTranslator _translator; 49 private RepositoryImpl _repository; 50 51 public 53 CIDLJImplGenerator(AST ast) 54 { 55 super(ast); 56 57 _translator = new ExtendedJavaTranslator(); 58 _repository = null; 59 } 60 61 65 private ExtendedJavaTranslator 66 getTranslator() 67 { 68 return _translator; 69 } 70 71 private RepositoryImpl 72 getRepository() 73 { 74 return _repository; 75 } 76 77 private void 78 generateForDeclaration(Declaration decl) 79 throws GenerationException 80 { 81 if (decl.getDeclKind()==DeclarationKind.dk_module) { 87 generateForModule((ModuleDecl)decl); 88 } else if (decl.getDeclKind()==DeclarationKind.dk_component) { 89 generateForComponent((ComponentDecl)decl); 90 } else if (decl.getDeclKind()==DeclarationKind.dk_home) { 91 generateForHome((HomeDecl)decl); 92 } else if (decl.getDeclKind()==DeclarationKind.dk_composition) { 93 generateForComposition((CompositionDecl)decl); 94 } 95 } 96 97 private void 98 generateForModule(ModuleDecl module) 99 throws GenerationException 100 { 101 Declaration[] decls = module.getContents(false, DeclarationKind.dk_all); 102 for (int i=0;i<decls.length;i++) { 103 generateForDeclaration(decls[i]); 104 } 105 } 106 107 private void 108 generateForComposition(CompositionDecl composition) 109 throws GenerationException 110 { 111 } 113 114 private void 115 generateForHome(HomeDecl home) 116 throws GenerationException 117 { 118 String classname = home.getName()+"_Base"; 119 ClassObject ski = new ClassObjectImpl(classname); 120 121 ski.setPackage(getTranslator().getPackage(home)); 123 ski.addImport("org.objectweb.corba.runtime.*"); 124 ski.addImport("org.objectweb.ccm.runtime.*"); 125 126 ski.addComment(" "); 131 ski.addComment("Java servant implementation for home "+home.getName()); 132 ski.addComment(" "); 133 ski.addComment("NOTE: generated class; DO NOT EDIT !"); 134 ski.addComment(" "); 135 ski.setAbstract(true); 136 ski.setModifier(ModifierKindImpl.mk_public); 137 ski.addInheritedObject("KeylessCCMHomeBase"); 138 ski.addImplementedObject("org.omg.Components.Servant"); 139 ski.addImplementedObject(home.getName()+"Operations"); 140 141 AttributeObject attr = null; 143 attr = new AttributeObjectImpl(); 145 attr.addComment("Class name of this class (used for debugging)."); 146 attr.setStatic(true); 147 attr.setModifier(ModifierKindImpl.mk_private); 148 attr.setType("String"); 149 attr.setName("_class_name"); 150 attr.setInitialValue("\""+classname+"\""); 151 ski.addAttribute(attr); 152 153 ConstructorObject constructor = new ConstructorObjectImpl(); 156 constructor.getImpl().setMacro("EMPTY_BODY"); 158 ski.addConstructor(constructor); 159 160 MethodObject operation = null; 164 operation = new MethodObjectImpl(); 165 operation.setFinal(true); 166 operation.setModifier(ModifierKindImpl.mk_protected); 167 operation.setReturnType("String"); 168 operation.setName("getComponentRepid"); 169 operation.getImpl().setMacro("HOMEBASE_GETCOMPREPID_BODY"); 170 operation.getImpl().addContextValue("component", home.getManagedComponent()); 171 ski.addMethod(operation); 172 173 operation = new MethodObjectImpl(); 176 operation.setFinal(true); 177 operation.setModifier(ModifierKindImpl.mk_public); 178 operation.setReturnType("org.omg.PortableServer.Servant"); 179 operation.setName("as_native_servant"); 180 operation.getImpl().setMacro("HOMEBASE_ASNATIVESERV_BODY"); 181 operation.getImpl().addContextValue("home", home); 182 ski.addMethod(operation); 183 184 operation = new MethodObjectImpl(); 188 operation.setFinal(true); 189 operation.setModifier(ModifierKindImpl.mk_public); 190 operation.setReturnType(home.getManagedComponent().getName()); 191 operation.setName("create"); 192 operation.getImpl().setMacro("HOMEBASE_CREATE_BODY"); 193 operation.getImpl().addContextValue("component", home.getManagedComponent()); 194 operation.addException("org.omg.Components.CreateFailure"); 195 ski.addMethod(operation); 196 197 200 getRepository().addObject(ski); 202 } 203 204 private void 205 generateForComponent(ComponentDecl comp) 206 throws GenerationException 207 { 208 210 String classname = comp.getName()+"_MainSegBase"; 211 ClassObject ski = new ClassObjectImpl(classname); 212 213 ski.setPackage(getTranslator().getPackage(comp)); 215 ski.addImport("org.objectweb.corba.runtime.*"); 216 ski.addImport("org.objectweb.ccm.runtime.*"); 217 218 ski.addComment(" "); 223 ski.addComment("Java servant implementation for component "+comp.getName()); 224 ski.addComment(" "); 225 ski.addComment("NOTE: generated class; DO NOT EDIT !"); 226 ski.addComment(" "); 227 ski.setAbstract(true); 228 ski.setModifier(ModifierKindImpl.mk_public); 229 ski.addInheritedObject("CCMObjectBase"); 230 ski.addImplementedObject("org.omg.Components.Servant"); 231 ski.addImplementedObject(comp.getName()+"_MainSegOperations"); 232 233 AttributeObject attr = null; 235 attr = new AttributeObjectImpl(); 237 attr.addComment("Class name of this class (used for debugging)."); 238 attr.setStatic(true); 239 attr.setModifier(ModifierKindImpl.mk_private); 240 attr.setType("String"); 241 attr.setName("_class_name"); 242 attr.setInitialValue("\""+classname+"\""); 243 ski.addAttribute(attr); 244 245 ConstructorObject constructor = new ConstructorObjectImpl(); 248 constructor.getImpl().setMacro("EMPTY_BODY"); 250 ski.addConstructor(constructor); 251 252 MethodObject operation = new MethodObjectImpl(); 256 operation.setFinal(true); 257 operation.setModifier(ModifierKindImpl.mk_protected); 258 operation.setReturnType("void"); 259 operation.setName("initializePorts"); 260 operation.getImpl().setMacro("MAINSEGBASE_INITPORTS_BODY"); 261 Declaration[] recs = comp.getContents(false, DeclarationKind.dk_uses); 262 Declaration[] facets = comp.getContents(false, DeclarationKind.dk_provides); 263 Declaration[] sinks = comp.getContents(false, DeclarationKind.dk_consumes); 264 Declaration[] emitters = comp.getContents(false, DeclarationKind.dk_emits); 265 Declaration[] publishers = comp.getContents(false, DeclarationKind.dk_publishes); 266 if (facets.length!=0) { 267 operation.getImpl().addContextValue("has_facets", new Boolean (true)); 268 operation.getImpl().addContextValue("facets", facets); 269 operation.getImpl().addContextValue("facetslength", Integer.toString(facets.length)); 270 } 271 else { 272 operation.getImpl().addContextValue("has_facets", new Boolean (false)); 273 } 274 if (recs.length!=0) { 275 operation.getImpl().addContextValue("has_recs", new Boolean (true)); 276 operation.getImpl().addContextValue("recs", recs); 277 operation.getImpl().addContextValue("recslength", Integer.toString(recs.length)); 278 } 279 else { 280 operation.getImpl().addContextValue("has_recs", new Boolean (false)); 281 } 282 if (sinks.length!=0) { 283 operation.getImpl().addContextValue("has_sinks", new Boolean (true)); 284 operation.getImpl().addContextValue("sinks", sinks); 285 operation.getImpl().addContextValue("sinkslength", Integer.toString(sinks.length)); 286 } 287 else { 288 operation.getImpl().addContextValue("has_sinks", new Boolean (false)); 289 } 290 if (emitters.length!=0) { 291 operation.getImpl().addContextValue("has_emitters", new Boolean (true)); 292 operation.getImpl().addContextValue("emitters", emitters); 293 operation.getImpl().addContextValue("emitterslength", Integer.toString(emitters.length)); 294 } 295 else { 296 operation.getImpl().addContextValue("has_emitters", new Boolean (false)); 297 } 298 if (publishers.length!=0) { 299 operation.getImpl().addContextValue("has_publishers", new Boolean (true)); 300 operation.getImpl().addContextValue("publishers", publishers); 301 operation.getImpl().addContextValue("publisherslength", Integer.toString(publishers.length)); 302 } 303 else { 304 operation.getImpl().addContextValue("has_publishers", new Boolean (false)); 305 } 306 307 ski.addMethod(operation); 308 309 operation = new MethodObjectImpl(); 311 operation.setFinal(true); 312 operation.setModifier(ModifierKindImpl.mk_public); 313 operation.setReturnType("org.omg.PortableServer.Servant"); 314 operation.setName("as_native_servant"); 315 operation.getImpl().setMacro("MAINSEGBASE_ASNATIVESERV_BODY"); 316 operation.getImpl().addContextValue("component", comp); 317 ski.addMethod(operation); 318 319 for (int i=0;i<facets.length;i++) { 322 ProvidesDecl facet = (ProvidesDecl)facets[i]; 325 OperationDecl opprovide = facet.getClientMapping()[0]; 326 327 addJavaOperation(ski, opprovide, opprovide.getType(), 328 opprovide.getParameterList().getParameters(), 329 opprovide.getExceptionList().getExceptions(), 330 "MAINSEGBASE_PROVIDEFACET_OPERATION_BODY", "facet", facet); 331 } 332 333 for (int i=0;i<recs.length;i++) { 335 UsesDecl receptacle = (UsesDecl)recs[i]; 336 String macrobody = "SIMPLE"; 338 if (receptacle.isMultiple()) { 339 macrobody = "MULTIPLE"; 340 } 341 342 OperationDecl opconnect = null; 344 OperationDecl opdisconnect = null; 345 OperationDecl opgetconnection = null; 346 OperationDecl[] opeqs = receptacle.getClientMapping(); 347 for (int j=0;j<opeqs.length;j++) { 348 if (opeqs[j].getName().charAt(0)=='c') { 349 opconnect = opeqs[j]; 350 } 351 else if (opeqs[j].getName().charAt(0)=='d') { 352 opdisconnect = opeqs[j]; 353 } 354 else if (opeqs[j].getName().charAt(0)=='g') { 355 opgetconnection = opeqs[j]; 356 } 357 } 358 359 addJavaOperation(ski, opconnect, opconnect.getType(), 367 opconnect.getParameterList().getParameters(), 368 opconnect.getExceptionList().getExceptions(), 369 "MAINSEGBASE_"+macrobody+"_CONNECT_OPERATION_BODY", 370 "receptacle", receptacle); 371 372 addJavaOperation(ski, opdisconnect, opdisconnect.getType(), 380 opdisconnect.getParameterList().getParameters(), 381 opdisconnect.getExceptionList().getExceptions(), 382 "MAINSEGBASE_"+macrobody+"_DISCONNECT_OPERATION_BODY", 383 "receptacle", receptacle); 384 385 if (receptacle.isMultiple()) { 391 String [] ctxkeys = { "receptacle", "comp_absname" }; 392 Object [] ctxvals = { receptacle, getTranslator().getAbsoluteName(comp) }; 393 394 addJavaOperation(ski, opgetconnection, opgetconnection.getType(), 395 opgetconnection.getParameterList().getParameters(), 396 opgetconnection.getExceptionList().getExceptions(), 397 "MAINSEGBASE_"+macrobody+"_GETCONNECTION_OPERATION_BODY", 398 ctxkeys, ctxvals); 399 } else { 400 addJavaOperation(ski, opgetconnection, opgetconnection.getType(), 401 opgetconnection.getParameterList().getParameters(), 402 opgetconnection.getExceptionList().getExceptions(), 403 "MAINSEGBASE_"+macrobody+"_GETCONNECTION_OPERATION_BODY", 404 "receptacle", receptacle); 405 } 406 } 407 408 for (int i=0;i<sinks.length;i++) { 411 ConsumesDecl sink = (ConsumesDecl)sinks[i]; 414 OperationDecl opsink = sink.getClientMapping()[0]; 415 416 addJavaOperation(ski, opsink, opsink.getType(), 417 opsink.getParameterList().getParameters(), 418 opsink.getExceptionList().getExceptions(), 419 "MAINSEGBASE_GETCONSUMER_OPERATION_BODY", "sink", sink); 420 } 421 422 for (int i=0;i<emitters.length;i++) { 424 EmitsDecl emitter = (EmitsDecl)emitters[i]; 425 OperationDecl opconnect = null; 427 OperationDecl opdisconnect = null; 428 OperationDecl[] opeqs = emitter.getClientMapping(); 429 for (int j=0;j<opeqs.length;j++) { 430 if (opeqs[j].getName().charAt(0)=='c') { 431 opconnect = opeqs[j]; 432 } 433 else if (opeqs[j].getName().charAt(0)=='d') { 434 opdisconnect = opeqs[j]; 435 } 436 } 437 438 addJavaOperation(ski, opconnect, opconnect.getType(), 442 opconnect.getParameterList().getParameters(), 443 opconnect.getExceptionList().getExceptions(), 444 "MAINSEGBASE_EMITTER_CONNECT_OPERATION_BODY", 445 "emitter", emitter); 446 447 addJavaOperation(ski, opdisconnect, opdisconnect.getType(), 451 opdisconnect.getParameterList().getParameters(), 452 opdisconnect.getExceptionList().getExceptions(), 453 "MAINSEGBASE_EMITTER_DISCONNECT_OPERATION_BODY", 454 "emitter", emitter); 455 } 456 457 for (int i=0;i<publishers.length;i++) { 459 PublishesDecl publisher = (PublishesDecl)publishers[i]; 460 OperationDecl opsubscribe = null; 462 OperationDecl opunsubscribe = null; 463 OperationDecl[] opeqs = publisher.getClientMapping(); 464 for (int j=0;j<opeqs.length;j++) { 465 if (opeqs[j].getName().charAt(0)=='s') { 466 opsubscribe = opeqs[j]; 467 } 468 else if (opeqs[j].getName().charAt(0)=='u') { 469 opunsubscribe = opeqs[j]; 470 } 471 } 472 473 addJavaOperation(ski, opsubscribe, opsubscribe.getType(), 477 opsubscribe.getParameterList().getParameters(), 478 opsubscribe.getExceptionList().getExceptions(), 479 "MAINSEGBASE_PUBLISHER_SUBSCRIBE_OPERATION_BODY", 480 "publisher", publisher); 481 482 addJavaOperation(ski, opunsubscribe, opunsubscribe.getType(), 486 opunsubscribe.getParameterList().getParameters(), 487 opunsubscribe.getExceptionList().getExceptions(), 488 "MAINSEGBASE_PUBLISHER_UNSUBSCRIBE_OPERATION_BODY", 489 "publisher", publisher); 490 } 491 492 getRepository().addObject(ski); 494 } 495 496 private void 497 addJavaOperation(ClassObject ski, Declaration decl, TypeRef rtype, 498 Parameter[] params, ExceptionDecl[] exceptions, 499 String macroname) 500 { 501 addJavaOperation(ski, decl, rtype, params, exceptions, macroname, new String [0], new Object [0]); 502 } 503 504 private void 505 addJavaOperation(ClassObject ski, Declaration decl, TypeRef rtype, 506 Parameter[] params, ExceptionDecl[] exceptions, 507 String macroname, String ctxkey, Object ctxval) 508 { 509 String [] ctxkeys = { ctxkey }; 510 Object [] ctxvals = { ctxval }; 511 512 addJavaOperation(ski, decl, rtype, params, exceptions, macroname, ctxkeys, ctxvals); 513 } 514 515 private void 516 addJavaOperation(ClassObject ski, Declaration decl, TypeRef rtype, 517 Parameter[] params, ExceptionDecl[] exceptions, String macroname, 518 String [] ctxkeys, Object [] ctxvals) 519 { 520 MethodObject operation = new MethodObjectImpl(); 521 operation.setReturnType(getTranslator().toJava(rtype)); 523 524 operation.setName(decl.getName()); 526 527 ParameterObject parameter = null; 529 for (int j=0;j<params.length;j++) { 530 parameter = new ParameterObjectImpl(); 531 parameter.setName(params[j].getName()); 532 parameter.setType(getTranslator().toJava(params[j].getType(), params[j].getMode())); 533 operation.addParameter(parameter); 534 } 535 String param_names = ""; 537 if (params.length!=0) { 539 for (int j=0;j<params.length-1;j++) { 540 param_names = param_names + params[j].getName() + ", "; 541 } 542 543 param_names = param_names + params[params.length-1].getName(); 544 } 545 operation.getImpl().addContextValue("parameter_list", param_names); 546 547 if (exceptions.length!=0) { 549 operation.getImpl().addContextValue("has_exceptions", new Boolean (true)); 550 String [] exc_absnames = new String [exceptions.length]; 551 for (int j=0;j<exceptions.length;j++) { 552 exc_absnames[j] = getTranslator().getAbsoluteName(exceptions[j]); 553 operation.addException(exc_absnames[j]); 554 } 555 operation.getImpl().addContextValue("exception_absnames", exc_absnames); 556 } 557 else { 558 operation.getImpl().addContextValue("has_exceptions", new Boolean (false)); 559 } 560 561 operation.getImpl().setMacro(macroname); 564 operation.getImpl().addContextValue("declaration", decl); 565 operation.getImpl().addContextValue("return_type", getTranslator().toJava(rtype)); 566 567 for (int i=0;i<ctxkeys.length;i++) { 569 operation.getImpl().addContextValue(ctxkeys[i], ctxvals[i]); 570 } 571 572 if (rtype.getTypeKind()!=TypeKind.tk_void) { 573 operation.getImpl().addContextValue("has_return_type", new Boolean (true)); 574 } 575 else { 576 operation.getImpl().addContextValue("has_return_type", new Boolean (false)); 577 } 578 579 ski.addMethod(operation); 581 } 582 583 587 final public void 588 generate(String basedir, Scope scope) 589 throws GenerationException 590 { 591 _repository = new RepositoryImpl(); 592 593 generateForDeclaration(scope); 594 595 602 String tpath = System.getProperty("ecm.templates"); 603 java.io.File tdir = new java.io.File (tpath); 604 605 JavaGenerator javagen = new JavaGenerator(); 607 java.util.Vector v = new java.util.Vector (); 608 v.add("cidl_jimpl.vm"); 609 javagen.initialize("Component Implementation", v); 610 javagen.generate(getRepository(), basedir); 611 } 612 613 } 614 615 616 | Popular Tags |