1 26 27 package org.objectweb.corba.generator.cif.lib; 28 29 30 import org.objectweb.openccm.ast.api.*; 32 import org.objectweb.openccm.generator.java.core.lib.*; 33 import org.objectweb.openccm.generator.translator.idl2java.api.PSDL_JavaTranslator; 34 import org.objectweb.openccm.generator.java.ast.api.*; 35 import org.objectweb.openccm.generator.java.ast.lib.*; 36 import org.objectweb.openccm.ast.utils.api.ExecutorInfo; 37 import org.objectweb.openccm.generator.common.lib.GenerationException; 38 import java.util.List ; 39 import java.util.ArrayList ; 40 41 42 48 49 public class CIF_JavaGenerator 50 extends org.objectweb.openccm.generator.common.lib.GeneratorBase 51 implements org.objectweb.corba.generator.cif.api.CIF_JavaGenerator 52 { 53 59 60 private Repository repository_; 61 62 63 public PSDL_JavaTranslator translator_; 64 65 71 76 public CIF_JavaGenerator(AST ast) 77 { 78 super(ast); 79 repository_ = null; 81 translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.PSDL_JavaTranslator(); 82 } 83 84 90 97 private void 98 mapHomeExecutor( HomeExecutorDecl home_executor, 99 String package_name, 100 String component_executor_name ) 101 { 102 ClassObject clazz = null; 103 ConstructorObject ct = null; 104 MethodObject method = null; 105 106 clazz = new ClassObjectImpl(home_executor.getName()); 107 clazz.addComment("Home Executor for " + component_executor_name); 108 clazz.setAbstract(true); 109 clazz.setModifier(ModifierKindImpl.mk_public); 110 clazz.setPackage(package_name); 111 clazz.addInheritedObject("org.objectweb.ccm.runtime.cif.lib.HomeBase"); 112 clazz.addImplementedObject("CIF_" + home_executor.getName()); 113 114 ct = new ConstructorObjectImpl(); 116 clazz.addConstructor(ct); 117 118 method = new MethodObjectImpl(); 120 method.addComment("Create a component executor."); 121 method.addComment(" "); 122 method.addComment("@return The EnterpriseComponent created executor."); 123 method.setName("create"); 124 method.setReturnType("org.omg.Components.EnterpriseComponent"); 125 method.getImpl().setMacro("HE_CREATE"); 126 clazz.addMethod(method); 127 128 repository_.addObject(clazz); 129 } 130 131 140 private void 141 mapComponentExecutor( ExecutorDecl comp_exec, 142 String package_name, 143 ComponentDecl comp_decl, 144 Declaration[] segments, 145 InterfaceDecl context ) 146 { 147 ClassObject clazz = null; 148 ConstructorObject ct = null; 149 MethodObject method = null; 150 AttributeObject att = null; 151 ParameterObject param = null; 152 CategoryKind kind = null; 153 ExecutorInfo exec_info = null; 154 155 clazz = new ClassObjectImpl(comp_exec.getName()); 156 clazz.addComment("Component Executor for "+comp_decl.getName()); 157 clazz.setAbstract(true); 158 clazz.setModifier(ModifierKindImpl.mk_public); 159 clazz.setPackage(package_name); 160 kind = comp_exec.getCompositionKind(); 161 if ((kind==CategoryKind.ck_entity) || (kind==CategoryKind.ck_process)) 162 clazz.addInheritedObject("org.objectweb.ccm.runtime.cif.lib.EntityComponentSegmentBase"); 163 else 164 clazz.addInheritedObject("org.objectweb.ccm.runtime.cif.lib.SessionComponentSegmentBase"); 165 166 clazz.addImplementedObject("CIF_" + comp_exec.getName()); 167 168 att = new AttributeObjectImpl(); 170 att.addComment("The segmentation table."); 171 att.setName("the_segmentation_table_"); 172 att.setType("java.util.HashMap"); 173 att.setModifier( ModifierKindImpl.mk_private ); 174 att.setStatic(true); 175 clazz.addAttribute(att); 176 177 att = new AttributeObjectImpl(); 179 att.addComment("The segment id."); 180 att.setName("_segment_id_value"); 181 att.setType("int"); 182 att.setModifier( ModifierKindImpl.mk_public ); 183 att.setFinal(true); 184 att.setStatic(true); 185 att.setInitialValue("0"); 186 clazz.addAttribute(att); 187 188 att = new AttributeObjectImpl(); 190 att.addComment(" To refer to the component context."); 191 att.setName("the_context_"); 192 att.setType(translator_.getAbsoluteName(context)); 193 att.setModifier( ModifierKindImpl.mk_private ); 194 clazz.addAttribute(att); 195 196 ct = new ConstructorObjectImpl(); 198 ct.getImpl().setMacro("CE_CONSTRUCTOR"); 199 ct.getImpl().addContextValue("nb", new Integer (segments.length+1)); 200 clazz.addConstructor(ct); 201 202 method = new MethodObjectImpl(); 204 method.setName("StaticStatement"); 205 method.getImpl().setMacro("CE_STATIC_STATEMENT"); 206 exec_info = new org.objectweb.openccm.ast.utils.lib.ExecutorInfo(comp_exec); 207 method.getImpl().addContextValue("comp_facet_list", exec_info.getMainSegmentFacets()); 208 method.getImpl().addContextValue("segment_list", segments); 209 method.getImpl().addContextValue("component_name", comp_decl.getName()); 210 clazz.addMethod(method); 211 212 method = new MethodObjectImpl(); 214 method.addComment("Obtain the segmentation table."); 215 method.addComment(" "); 216 method.addComment("@return A HashMap containing <facet_name, segid> associations."); 217 method.setName("get_segmentation_table"); 218 method.setReturnType("java.util.HashMap"); 219 method.getImpl().setMacro("GET_METHOD"); 220 method.getImpl().addContextValue("var", "the_segmentation_table_"); 221 clazz.addMethod(method); 222 223 if ((kind==CategoryKind.ck_entity) || (kind==CategoryKind.ck_process)) 224 { 225 method = new MethodObjectImpl(); 227 method.addComment("Set the entity component context."); 228 method.addComment("Must be implemented in subclasses."); 229 method.addComment(" "); 230 method.addComment("@param context The entity component context."); 231 method.addComment(" "); 232 method.addComment("@throw org.omg.Components.CCMException For any problems."); 233 method.setName("set_entity_context"); 234 method.setReturnType("void"); 235 param = new ParameterObjectImpl(); 236 param.setName("context"); 237 param.setType("org.omg.Components.EntityContext"); 238 method.addParameter(param); 239 method.addException("org.omg.Components.CCMException"); 240 method.getImpl().setMacro("SET_METHOD"); 241 method.getImpl().addContextValue("var", "the_context_"); 242 method.getImpl().addContextValue("value", "(" + translator_.getAbsoluteName(context) + ")context"); 243 clazz.addMethod(method); 244 245 method = new MethodObjectImpl(); 247 method.addComment("Unset the entity component context."); 248 method.addComment(" "); 249 method.addComment("@throw org.omg.Components.CCMException For any problems."); 250 method.setName("unset_entity_context"); 251 method.setReturnType("void"); 252 method.addException("org.omg.Components.CCMException"); 253 method.getImpl().setMacro("SET_METHOD"); 254 method.getImpl().addContextValue("var", "the_context_"); 255 method.getImpl().addContextValue("value", "null"); 256 clazz.addMethod(method); 257 } 258 else 259 { 260 method = new MethodObjectImpl(); 262 method.addComment("Set the session component context."); 263 method.addComment("Must be implemented in subclasses."); 264 method.addComment(" "); 265 method.addComment("@param context The session component context."); 266 method.addComment(" "); 267 method.addComment("@throw org.omg.Components.CCMException For any problems."); 268 method.setName("set_session_context"); 269 method.setReturnType("void"); 270 param = new ParameterObjectImpl(); 271 param.setName("context"); 272 param.setType("org.omg.Components.SessionContext"); 273 method.addParameter(param); 274 method.addException("org.omg.Components.CCMException"); 275 method.getImpl().setMacro("SET_METHOD"); 276 method.getImpl().addContextValue("var", "the_context_"); 277 method.getImpl().addContextValue("value", "(" + translator_.getAbsoluteName(context) + ")context"); 278 clazz.addMethod(method); 279 } 280 281 method = new MethodObjectImpl(); 283 method.addComment("Obtain the context."); 284 method.addComment(" "); 285 method.addComment("@return The associated context."); 286 method.setName("get_context"); 287 method.setReturnType(translator_.getAbsoluteName(context)); 288 method.getImpl().setMacro("GET_METHOD"); 289 method.getImpl().addContextValue("var", "the_context_"); 290 clazz.addMethod(method); 291 292 method = new MethodObjectImpl(); 294 method.addComment("Obtain the main executor segment."); 295 method.addComment(" "); 296 method.addComment("@return The main segment (with id = 0)."); 297 method.setName("get_main_segment"); 298 method.setReturnType("CIF_" + comp_exec.getName()); 299 method.getImpl().setMacro("GET_METHOD"); 300 method.getImpl().addContextValue("var", "this"); 301 clazz.addMethod(method); 302 303 304 if ((kind==CategoryKind.ck_entity) || (kind==CategoryKind.ck_process)) 305 { 306 } 308 309 repository_.addObject(clazz); 310 } 311 312 321 private void 322 mapSegment( SegmentDecl segment, 323 String package_name, 324 int seg_id, 325 ExecutorDecl comp_exec, 326 InterfaceDecl context ) 327 { 328 ClassObject clazz = null; 329 ConstructorObject ct = null; 330 MethodObject method = null; 331 AttributeObject att = null; 332 ParameterObject param = null; 333 334 clazz = new ClassObjectImpl(segment.getName()); 335 clazz.addComment("Segment"); 336 clazz.setAbstract(true); 337 clazz.setModifier(ModifierKindImpl.mk_public); 338 clazz.setPackage(package_name); 339 clazz.addInheritedObject("org.objectweb.corba.util.LocalObjectBase"); 340 clazz.addImplementedObject("CIF_" + segment.getName()); 341 clazz.addImplementedObject("org.objectweb.ccm.runtime.cif.api.Segment"); 342 343 att = new AttributeObjectImpl(); 345 att.addComment("The segment id."); 346 att.setName("_segment_id_value"); 347 att.setType("int"); 348 att.setModifier( ModifierKindImpl.mk_public ); 349 att.setFinal(true); 350 att.setStatic(true); 351 att.setInitialValue(new Integer (seg_id).toString()); 352 clazz.addAttribute(att); 353 354 att = new AttributeObjectImpl(); 356 att.addComment("To refer to the main segment."); 357 att.setName("main_segment_"); 358 att.setType("CIF_" + comp_exec.getName()); 359 att.setModifier( ModifierKindImpl.mk_private ); 360 clazz.addAttribute(att); 361 362 ct = new ConstructorObjectImpl(); 364 clazz.addConstructor(ct); 365 366 method = new MethodObjectImpl(); 368 method.addComment("Set the main segment executor (segid = 0)."); 369 method.addComment(" "); 370 method.addComment("@param seg - The main segment."); 371 method.setName("set_main_segment"); 372 method.setReturnType("void"); 373 param = new ParameterObjectImpl(); 374 param.setName("seg"); 375 param.setType("org.objectweb.ccm.runtime.cif.api.ComponentSegment"); 376 method.addParameter(param); 377 method.getImpl().setMacro("SET_METHOD"); 378 method.getImpl().addContextValue("var", "main_segment_"); 379 method.getImpl().addContextValue("value", "(CIF_" + comp_exec.getName() + ")seg"); 380 clazz.addMethod(method); 381 382 method = new MethodObjectImpl(); 384 method.addComment("Obtain the context."); 385 method.addComment(" "); 386 method.addComment("@return The associated context."); 387 method.setName("get_context"); 388 method.setReturnType(translator_.getAbsoluteName(context)); 389 method.getImpl().setMacro("GET_METHOD"); 390 method.getImpl().addContextValue("var", " get_main_segment().get_context()"); 391 clazz.addMethod(method); 392 393 method = new MethodObjectImpl(); 395 method.addComment("Obtain the main executor segment."); 396 method.addComment(" "); 397 method.addComment("@return The main segment (with id = 0)."); 398 method.setName("get_main_segment"); 399 method.setReturnType("CIF_" + comp_exec.getName()); 400 method.getImpl().setMacro("GET_METHOD"); 401 method.getImpl().addContextValue("var", "main_segment_"); 402 clazz.addMethod(method); 403 404 repository_.addObject(clazz); 405 } 406 407 413 419 public void 420 cif_to_java(String base_dir, Scope scope) 421 throws GenerationException 422 { 423 java.io.File out_dir = null; 424 repository_ = new RepositoryImpl(); 425 Declaration[] compositions = null; 426 List list = null; 427 428 try{ 430 out_dir = org.objectweb.openccm.generator.common.lib.FileManager.mkdir(base_dir); 431 }catch(GenerationException ex){ 432 System.err.println(ex.getMessage()); 433 return; 434 } 435 436 437 list = getAllDeclarations(scope, DeclarationKind.dk_composition); 438 compositions = (Declaration[]) list.toArray(new Declaration[0]); 439 440 for (int i=0; i<compositions.length; i++) 442 { 443 CompositionDecl composition = ((CompositionDecl)compositions[i]); 444 ComponentDecl component = null; 445 Declaration[] decls = null, segments = null; 446 HomeExecutorDecl home_exec = null; 447 ExecutorDecl comp_exec = null; 448 InterfaceDecl context = null; 449 String package_name = null; 450 ArrayList facet_list = null; 451 452 453 decls = composition.getContents(true, DeclarationKind.dk_home_executor); 454 home_exec = (org.objectweb.openccm.ast.lib.HomeExecutorDeclImpl)decls[0]; 456 package_name = translator_.getPackage(home_exec); 457 458 459 decls = home_exec.getContents(true, DeclarationKind.dk_executor); 460 comp_exec = (org.objectweb.openccm.ast.lib.ExecutorDeclImpl)decls[0]; 462 463 464 component = home_exec.getHomeType().getManagedComponent(); 465 context = component.getLocalContextMapping(); 466 467 468 segments = comp_exec.getContents(true, DeclarationKind.dk_segment); 469 470 mapHomeExecutor( home_exec, 472 package_name, 473 comp_exec.getName() ); 474 mapComponentExecutor( comp_exec, 476 package_name, 477 component, 478 segments, 479 context ); 480 for (int j=0; j<segments.length; j++) 482 { 483 mapSegment( (SegmentDecl)segments[j], 484 package_name, 485 j+1, comp_exec, 487 context ); 488 } 489 } 490 491 org.objectweb.openccm.generator.java.core.lib.JavaGenerator java_gen = new JavaGenerator(); 493 java.util.Vector v = new java.util.Vector (); 494 v.add("org/objectweb/corba/generator/cif/cif.vm"); 495 java_gen.initialize("CIF", v); 496 java_gen.generate(repository_, base_dir); 497 } 498 } 499 | Popular Tags |