1 2 package SOFA.SOFAnode.Made.CodeGen; 3 4 import java.io.IOException ; 5 import java.rmi.RemoteException ; 6 7 import javax.xml.parsers.DocumentBuilder ; 8 import javax.xml.parsers.DocumentBuilderFactory ; 9 import javax.xml.parsers.ParserConfigurationException ; 10 import javax.xml.transform.Transformer ; 11 import javax.xml.transform.TransformerConfigurationException ; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.TransformerFactory ; 14 import javax.xml.transform.dom.DOMSource ; 15 import javax.xml.transform.stream.StreamResult ; 16 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Text ; 20 21 import SOFA.SOFAnode.Made.TIR.AbsoluteName; 22 import SOFA.SOFAnode.Made.TIR.ArchitectureDef; 23 import SOFA.SOFAnode.Made.TIR.ArrayDef; 24 import SOFA.SOFAnode.Made.TIR.AttrMode; 25 import SOFA.SOFAnode.Made.TIR.AttributeDef; 26 import SOFA.SOFAnode.Made.TIR.BindDef; 27 import SOFA.SOFAnode.Made.TIR.BindMode; 28 import SOFA.SOFAnode.Made.TIR.BindOperKind; 29 import SOFA.SOFAnode.Made.TIR.BindOperProvReq; 30 import SOFA.SOFAnode.Made.TIR.BindOperSub; 31 import SOFA.SOFAnode.Made.TIR.BindType; 32 import SOFA.SOFAnode.Made.TIR.BindTypeArray; 33 import SOFA.SOFAnode.Made.TIR.BindTypeKind; 34 import SOFA.SOFAnode.Made.TIR.BindTypeNormal; 35 import SOFA.SOFAnode.Made.TIR.CDLType; 36 import SOFA.SOFAnode.Made.TIR.ConstantDef; 37 import SOFA.SOFAnode.Made.TIR.Contained; 38 import SOFA.SOFAnode.Made.TIR.Container; 39 import SOFA.SOFAnode.Made.TIR.DefinitionKind; 40 import SOFA.SOFAnode.Made.TIR.EnumDef; 41 import SOFA.SOFAnode.Made.TIR.EnumMember; 42 import SOFA.SOFAnode.Made.TIR.ExceptionDef; 43 import SOFA.SOFAnode.Made.TIR.ExprBinOperationDef; 44 import SOFA.SOFAnode.Made.TIR.ExprBoolean; 45 import SOFA.SOFAnode.Made.TIR.ExprChar; 46 import SOFA.SOFAnode.Made.TIR.ExprConstant; 47 import SOFA.SOFAnode.Made.TIR.ExprDouble; 48 import SOFA.SOFAnode.Made.TIR.ExprFixed; 49 import SOFA.SOFAnode.Made.TIR.ExprFloat; 50 import SOFA.SOFAnode.Made.TIR.ExprLong; 51 import SOFA.SOFAnode.Made.TIR.ExprLongDouble; 52 import SOFA.SOFAnode.Made.TIR.ExprLongLong; 53 import SOFA.SOFAnode.Made.TIR.ExprOctet; 54 import SOFA.SOFAnode.Made.TIR.ExprOperDef; 55 import SOFA.SOFAnode.Made.TIR.ExprOperKind; 56 import SOFA.SOFAnode.Made.TIR.ExprProperty; 57 import SOFA.SOFAnode.Made.TIR.ExprShort; 58 import SOFA.SOFAnode.Made.TIR.ExprString; 59 import SOFA.SOFAnode.Made.TIR.ExprUnOperationDef; 60 import SOFA.SOFAnode.Made.TIR.ExprUnsigLong; 61 import SOFA.SOFAnode.Made.TIR.ExprUnsigLongLong; 62 import SOFA.SOFAnode.Made.TIR.ExprUnsigShort; 63 import SOFA.SOFAnode.Made.TIR.ExprWchar; 64 import SOFA.SOFAnode.Made.TIR.ExprWstring; 65 import SOFA.SOFAnode.Made.TIR.FrameDef; 66 import SOFA.SOFAnode.Made.TIR.Identification; 67 import SOFA.SOFAnode.Made.TIR.InstDef; 68 import SOFA.SOFAnode.Made.TIR.InterfaceDef; 69 import SOFA.SOFAnode.Made.TIR.OperationDef; 70 import SOFA.SOFAnode.Made.TIR.ParamDescr; 71 import SOFA.SOFAnode.Made.TIR.ParamMode; 72 import SOFA.SOFAnode.Made.TIR.PropertyDef; 73 import SOFA.SOFAnode.Made.TIR.ProvideDef; 74 import SOFA.SOFAnode.Made.TIR.Repository; 75 import SOFA.SOFAnode.Made.TIR.RequireDef; 76 import SOFA.SOFAnode.Made.TIR.SequenceDef; 77 import SOFA.SOFAnode.Made.TIR.StructDef; 78 import SOFA.SOFAnode.Made.TIR.StructMember; 79 import SOFA.SOFAnode.Made.TIR.TIRExceptCalculate; 80 import SOFA.SOFAnode.Made.TIR.TIRExceptLock; 81 import SOFA.SOFAnode.Made.TIR.TypedefDef; 82 83 87 public class JavaGenerator { 88 89 ObjectList allObjects; GenObjectList genObjects; CGFileAccess fs; GenObjectList pregenIfaces; ArchitectureDef[] archToGen; 95 GenFileList genFiles; 96 97 boolean typesonly; 98 99 100 public JavaGenerator(ObjectList allObjects, ArchitectureDef[] archToGen) throws IOException { 101 this.allObjects = allObjects; 102 this.genObjects = new GenObjectList(); 103 this.genFiles = new GenFileList(); 104 this.archToGen = archToGen; 105 106 PrimitiveGeneratedObject.createPrimGenerObjects(genObjects); 107 108 String curDir = System.getProperty("codegen.dir", null); 109 boolean rewrite = Boolean.valueOf(System.getProperty("codegen.rewrite", "true")).booleanValue(); 110 111 if (curDir == null) { fs = new CGFileAccess(".", rewrite); 113 } else { fs = new CGFileAccess(curDir, rewrite); 115 } 116 typesonly = false; 117 } 118 119 122 public JavaGenerator(ObjectList allObjects, boolean typesonly) throws IOException { 123 this.allObjects = allObjects; 124 this.genObjects = new GenObjectList(); 125 this.genFiles = new GenFileList(); 126 this.typesonly = typesonly; 127 128 PrimitiveGeneratedObject.createPrimGenerObjects(genObjects); 129 130 String curDir = System.getProperty("codegen.dir", null); 131 boolean rewrite = Boolean.valueOf(System.getProperty("codegen.rewrite", "true")).booleanValue(); 132 133 if (curDir == null) { fs = new CGFileAccess(".", rewrite); 135 } else { fs = new CGFileAccess(curDir, rewrite); 137 } 138 } 139 140 146 public void generate() throws RemoteException , GenerException, IOException { 147 pregenIfaces = pregenIfaceNames(); 148 Debug.println("---- begin of pregenIfaces ----"); 149 Debug.println(pregenIfaces); 150 Debug.println("---- end of pregenIfaces ----"); 151 for (int i=allObjects.size()-1; i>=0 ; i--) { 152 Contained current = allObjects.getObject(i); 153 CGFileWriter out = null; 154 boolean closeOut = true; 155 String name = current.get_identification().name(); 156 if (GeneratedObject.isReservedName(name)) 157 name = "_"+name; 158 AbsoluteName absN = current.get_identification().absolute_name(); 159 String pkg = CGFileAccess.absToPkg(absN); 160 Debug.println("Currently generated object: "+absN.name()); 161 162 switch (current.get_def_kind().value()) { 163 case DefinitionKind.dk_Constant: 164 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 165 generateConstant((ConstantDef) current, out, name, pkg); 166 break; 167 case DefinitionKind.dk_Enum: 168 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 169 generateEnum((EnumDef) current, out, name, CGFileAccess.absToPath(absN, name, false), pkg); 170 break; 171 case DefinitionKind.dk_Struct: 172 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 173 generateStruct((StructDef) current, out, name, CGFileAccess.absToPath(absN, name, false), pkg); 174 break; 175 case DefinitionKind.dk_Exception: 176 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 177 generateException((ExceptionDef) current, out, name, CGFileAccess.absToPath(absN, name, false), pkg); 178 break; 179 case DefinitionKind.dk_Typedef: 180 closeOut = false; 182 generateTypedef((TypedefDef) current, name, CGFileAccess.absToPath(absN, name, false), pkg); 183 break; 184 case DefinitionKind.dk_Interface: 185 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 186 generateInterface((InterfaceDef) current, out, name, CGFileAccess.absToPath(absN, name, false), pkg); 187 break; 188 default: 189 out = fs.newCGWriter(CGFileAccess.absToPath(absN, name, true)); 190 out.iwriteln("Currently, I don't know, how to generate this object."); 191 } 192 if (closeOut) 193 out.close(); 194 } 195 196 if (!typesonly) { 197 for (int i=0;i<archToGen.length; i++) { 199 boolean isPrimitive = false; 200 try { 201 if (archToGen[i].contents(null).length == 0) 202 isPrimitive = true; 203 } catch (TIRExceptLock e) { 204 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 205 } 206 CGFileWriter out = null; 207 String name = archToGen[i].get_identification().name(); 208 if (GeneratedObject.isReservedName(name)) 209 name = "_"+name; 210 AbsoluteName absN = archToGen[i].get_identification().absolute_name(); 211 String pkg = CGFileAccess.archAbsToPkg(absN); 212 String provider = absN.elementAt(0); 213 out = fs.newCGWriter(CGFileAccess.archAbsToPath(absN, name+"Builder", true)); 214 generateBuilder(archToGen[i], out, provider, name, pkg); 215 out.close(); 216 if (isPrimitive) { 217 out = fs.newCGWriter(CGFileAccess.archAbsToPath(absN, "_Base"+name+"Impl", true)); 218 generateBaseImpl(archToGen[i], out, provider, name, pkg); 219 out.close(); 220 String implName = CGFileAccess.archAbsToPath(absN, name+"Impl", true); 221 if (!fs.exists(implName)) { 222 out = fs.newCGWriter(implName); 223 generateRealImplSkel(archToGen[i], out, provider, name, pkg); 224 out.close(); 225 } 226 } 227 generateComponentInfoFile(archToGen[i], absN, name, pkg+"."+name+"Builder"); 228 } 229 } 230 231 } 232 233 235 private GenObjectList pregenIfaceNames() throws RemoteException { 236 JavaMapping jm = new JavaMapping(); 237 GenObjectList ret = new GenObjectList(); 238 for(int i=0; i<allObjects.size(); i++) { 240 Contained obj = allObjects.getObject(i); 241 if (obj instanceof CDLType) { 242 ret.add(new ContainedGeneratedObject(obj, jm.getImplTypeName((CDLType) obj))); 243 } 244 } 245 return ret; 246 } 247 248 253 private GeneratedObject searchObject(String key) { 254 GeneratedObject ret = null; 255 if ((ret = genObjects.searchObject(key)) == null) { 256 ret = pregenIfaces.searchObject(key); 257 } 258 return ret; 259 } 260 261 263 private String getNumber(ExprOperDef expr) throws GenerException, RemoteException , PropException { 264 GeneratedObject genObj; 265 String key; 266 switch (expr.get_eok_kind().value()) { 267 case ExprOperKind.eok_boolean: 268 if ( ((ExprBoolean) expr).value() ) 269 return ("true"); 270 else 271 return ("false"); 272 case ExprOperKind.eok_char: 273 return ( ((ExprChar) expr).toString() ); 274 case ExprOperKind.eok_constant: 275 ExprConstant exprc = (ExprConstant) expr; 276 key = GeneratedObject.createKey(exprc.ref_const()); 277 genObj = searchObject(key); 278 if (genObj == null) 279 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 280 return genObj.genReference(GeneratedObject.INREF); 281 case ExprOperKind.eok_double: 282 return (Double.toString( ((ExprDouble) expr).value() )); 283 case ExprOperKind.eok_fixed: 284 ExprFixed exprf = (ExprFixed) expr; 285 return ("new java.math.BigDecimal("+exprf.value().toString()+")"); 286 case ExprOperKind.eok_float: 287 return (Float.toString( ((ExprFloat) expr).value() )); 288 case ExprOperKind.eok_long: 289 return (Long.toString( ((ExprLong) expr).value() )); 290 case ExprOperKind.eok_longdouble: 291 return (Double.toString( ((ExprLongDouble) expr).value() )); 292 case ExprOperKind.eok_longlong: 293 return (Long.toString( ((ExprLongLong) expr).value() )); 294 case ExprOperKind.eok_octet: 295 return (Byte.toString( ((ExprOctet) expr).value() )); 296 case ExprOperKind.eok_short: 297 return (Short.toString( ((ExprShort) expr).value() )); 298 case ExprOperKind.eok_string: 299 return ( ((ExprString) expr).value() ); 300 case ExprOperKind.eok_unsiglong: 301 return (Long.toString( ((ExprUnsigLong) expr).value() )); 302 case ExprOperKind.eok_unsiglonglong: 303 return (Long.toString( ((ExprUnsigLongLong) expr).value() )); 304 case ExprOperKind.eok_unsigshort: 305 return (Short.toString( ((ExprUnsigShort) expr).value() )); 306 case ExprOperKind.eok_wchar: 307 return ( ((ExprWchar) expr).toString() ); 308 case ExprOperKind.eok_wstring: 309 return ( ((ExprWstring) expr).value() ); 310 case ExprOperKind.eok_property: 311 throw new PropException(Res.res.getString("MSG_PropInExpr")); 312 } 313 return ""; 314 } 315 316 318 private void writeNumber(ExprOperDef expr, CGFileWriter out) throws GenerException, IOException , RemoteException , PropException { 319 GeneratedObject genObj; 320 String key; 321 switch (expr.get_eok_kind().value()) { 322 case ExprOperKind.eok_boolean: 323 if ( ((ExprBoolean) expr).value() ) 324 out.iwrite("true"); 325 else 326 out.iwrite("false"); 327 break; 328 case ExprOperKind.eok_char: 329 out.iwrite( ((ExprChar) expr).value() ); 330 break; 331 case ExprOperKind.eok_constant: 332 ExprConstant exprc = (ExprConstant) expr; 333 key = GeneratedObject.createKey(exprc.ref_const()); 334 genObj = searchObject(key); 335 if (genObj == null) 336 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 337 genObj.genReference(out, GeneratedObject.INREF); 338 break; 339 case ExprOperKind.eok_double: 340 out.iwrite(Double.toString( ((ExprDouble) expr).value() )); 341 break; 342 case ExprOperKind.eok_fixed: 343 ExprFixed exprf = (ExprFixed) expr; 344 out.iwrite("new java.math.BigDecimal("+exprf.value().toString()+")"); 345 break; 346 case ExprOperKind.eok_float: 347 out.iwrite(Float.toString( ((ExprFloat) expr).value() )); 348 break; 349 case ExprOperKind.eok_long: 350 out.iwrite(Long.toString( ((ExprLong) expr).value() )); 351 break; 352 case ExprOperKind.eok_longdouble: 353 out.iwrite(Double.toString( ((ExprLongDouble) expr).value() )); 354 break; 355 case ExprOperKind.eok_longlong: 356 out.iwrite(Long.toString( ((ExprLongLong) expr).value() )); 357 break; 358 case ExprOperKind.eok_octet: 359 out.iwrite(Byte.toString( ((ExprOctet) expr).value() )); 360 break; 361 case ExprOperKind.eok_short: 362 out.iwrite(Short.toString( ((ExprShort) expr).value() )); 363 break; 364 case ExprOperKind.eok_string: 365 out.iwrite( ((ExprString) expr).value() ); 366 break; 367 case ExprOperKind.eok_unsiglong: 368 out.iwrite(Long.toString( ((ExprUnsigLong) expr).value() )); 369 break; 370 case ExprOperKind.eok_unsiglonglong: 371 out.iwrite(Long.toString( ((ExprUnsigLongLong) expr).value() )); 372 break; 373 case ExprOperKind.eok_unsigshort: 374 out.iwrite(Short.toString( ((ExprUnsigShort) expr).value() )); 375 break; 376 case ExprOperKind.eok_wchar: 377 out.iwrite( ((ExprWchar) expr).value() ); 378 break; 379 case ExprOperKind.eok_wstring: 380 out.iwrite( ((ExprWstring) expr).value() ); 381 break; 382 case ExprOperKind.eok_property: 383 throw new PropException(Res.res.getString("MSG_PropInExpr")); 384 } 385 } 386 387 389 private String expressionWithPropertyToString(ExprOperDef expr) throws GenerException, IOException , RemoteException , PropException { 390 GeneratedObject genObj; 391 String key; 392 StringBuffer ret; 393 switch (expr.get_eok_kind().value()) { 394 case ExprOperKind.eok_boolean: 395 if ( ((ExprBoolean) expr).value() ) 396 return "true"; 397 else 398 return "false"; 399 case ExprOperKind.eok_char: 400 return "" + ((ExprChar) expr).value() ; 401 case ExprOperKind.eok_constant: 402 ExprConstant exprc = (ExprConstant) expr; 403 key = GeneratedObject.createKey(exprc.ref_const()); 404 genObj = searchObject(key); 405 if (genObj == null) 406 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 407 return genObj.genReference(GeneratedObject.INREF); 408 case ExprOperKind.eok_double: 409 return (Double.toString( ((ExprDouble) expr).value() )); 410 case ExprOperKind.eok_fixed: 411 ExprFixed exprf = (ExprFixed) expr; 412 return ("new java.math.BigDecimal("+exprf.value().toString()+")"); 413 case ExprOperKind.eok_float: 414 return (Float.toString( ((ExprFloat) expr).value() )); 415 case ExprOperKind.eok_long: 416 return (Long.toString( ((ExprLong) expr).value() )); 417 case ExprOperKind.eok_longdouble: 418 return (Double.toString( ((ExprLongDouble) expr).value() )); 419 case ExprOperKind.eok_longlong: 420 return (Long.toString( ((ExprLongLong) expr).value() )); 421 case ExprOperKind.eok_octet: 422 return (Byte.toString( ((ExprOctet) expr).value() )); 423 case ExprOperKind.eok_short: 424 return (Short.toString( ((ExprShort) expr).value() )); 425 case ExprOperKind.eok_string: 426 return ( ((ExprString) expr).value() ); 427 case ExprOperKind.eok_unsiglong: 428 return (Long.toString( ((ExprUnsigLong) expr).value() )); 429 case ExprOperKind.eok_unsiglonglong: 430 return (Long.toString( ((ExprUnsigLongLong) expr).value() )); 431 case ExprOperKind.eok_unsigshort: 432 return (Short.toString( ((ExprUnsigShort) expr).value() )); 433 case ExprOperKind.eok_wchar: 434 return "" + ((ExprWchar) expr).value() ; 435 case ExprOperKind.eok_wstring: 436 return ( ((ExprWstring) expr).value() ); 437 case ExprOperKind.eok_property: 438 return ( "_prop_"+((ExprProperty) expr).ref_prop() ); 439 case ExprOperKind.eok_unminus: 440 ret = new StringBuffer ("(-"); 441 ret.append(expressionWithPropertyToString( ((ExprUnOperationDef) expr).operand() )); 442 ret.append(")"); 443 return ret.toString(); 444 case ExprOperKind.eok_unplus: 445 ret = new StringBuffer ("(+"); 446 ret.append(expressionWithPropertyToString( ((ExprUnOperationDef) expr).operand())); 447 return ret.append(")").toString(); 448 case ExprOperKind.eok_or: 449 ret = new StringBuffer ("("); 450 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 451 ret.append("||"); 452 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 453 return ret.append(")").toString(); 454 case ExprOperKind.eok_xor: 455 ret = new StringBuffer ("("); 456 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 457 ret.append("^"); 458 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 459 return ret.append(")").toString(); 460 case ExprOperKind.eok_and: 461 ret = new StringBuffer ("("); 462 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 463 ret.append("&&"); 464 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 465 return ret.append(")").toString(); 466 case ExprOperKind.eok_shr: 467 ret = new StringBuffer ("("); 468 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 469 ret.append(">>"); 470 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 471 return ret.append(")").toString(); 472 case ExprOperKind.eok_shl: 473 ret = new StringBuffer ("("); 474 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 475 ret.append(">>"); 476 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 477 return ret.append(")").toString(); 478 case ExprOperKind.eok_plus: 479 ret = new StringBuffer ("("); 480 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 481 ret.append("+"); 482 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 483 return ret.append(")").toString(); 484 case ExprOperKind.eok_minus: 485 ret = new StringBuffer ("("); 486 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 487 ret.append("-"); 488 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 489 return ret.append(")").toString(); 490 case ExprOperKind.eok_mul: 491 ret = new StringBuffer ("("); 492 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 493 ret.append("*"); 494 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 495 return ret.append(")").toString(); 496 case ExprOperKind.eok_div: 497 ret = new StringBuffer ("("); 498 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 499 ret.append("/"); 500 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 501 return ret.append(")").toString(); 502 case ExprOperKind.eok_mod: 503 ret = new StringBuffer ("("); 504 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand1())); 505 ret.append("%"); 506 ret.append(expressionWithPropertyToString( ((ExprBinOperationDef) expr).operand2())); 507 return ret.append(")").toString(); 508 } 509 return ""; 510 } 511 512 520 public void generateExpression(ExprOperDef expr, CGFileWriter out) throws GenerException, IOException , RemoteException { 521 try { 522 ExprOperDef res = expr.calculate(); 523 try { 525 writeNumber(res, out); 526 } catch (PropException pe) { 527 throw new GenerException(pe.getMessage()); 528 } 529 return; 530 } catch (TIRExceptCalculate e) { 531 ; } 533 } 534 535 545 public void generateConstant(ConstantDef obj, CGFileWriter out, String name, String pkg) throws GenerException, IOException , RemoteException { 546 Container parent = obj.get_defined_in(); 547 String key = GeneratedObject.createKey(obj.type()); 548 GeneratedObject type = searchObject(key); 549 if (type == null) { 550 throw new GenerException(Res.res.getString("MSG_ReferenceTo")+" "+key+" "+Res.res.getString("MSG_CantBeGenerated")); 551 } 552 553 if (parent instanceof InterfaceDef) { type.genReference(out, GeneratedObject.INREF); 555 out.iwritespace(1); 556 out.iwrite(name); 557 out.iwrite(" = "); 558 generateExpression(obj.value(), out); 559 out.iwriteln(" ;"); out.iwriteln(); 560 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+name)); } else { 562 out.iwriteln("package "+pkg+";"); 563 out.iwriteln(); 564 printHeadComment(contained2string(obj), out); 565 out.iwrite("public interface "); 566 out.iwrite(name); 567 out.iwriteln(" {"); 568 out.incIndent(); 569 out.iwrite("public static "); 570 type.genReference(out, GeneratedObject.INREF); 571 out.iwrite(" value = "); 572 generateExpression(obj.value(), out); 573 out.iwriteln(" ;"); 574 out.decIndent(); 575 out.iwriteln("};"); 576 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+name+".value")); 577 genFiles.addNewObject(pkg+"."+name,contained2string(obj)); 578 } 579 } 580 581 592 public void generateEnum(EnumDef obj, CGFileWriter out, String shortName, String absName, String pkg) throws GenerException, IOException , RemoteException { 593 try { 594 EnumMember[] membs = obj.members(); 595 out.iwriteln("package "+pkg+";"); 596 out.iwriteln(); 597 printHeadComment(contained2string(obj),out); 598 out.iwriteln("public class "+shortName+" implements "+GeneratedObject.stdPackage+"CDLEntity {"); 599 out.incIndent(); 600 printCDLIdField(obj, out, false); 601 out.iwriteln("private int __value;"); 602 out.iwriteln("private static int __size = "+Integer.toString(membs.length)+";"); 603 out.iwriteln("private static "+pkg+"."+shortName+"[] __array = new "+pkg+"."+shortName+"[__size];"); 604 out.iwriteln(); 605 for (int i=0;i<membs.length; i++) { 606 String mname = membs[i].name(); 607 out.iwriteln("public static final int _"+mname+" = "+Integer.toString(i)+";"); 608 out.iwriteln("public static final "+pkg+"."+shortName+" "+mname+" = new "+pkg+"."+shortName+"(_"+mname+");"); 609 } 610 out.iwriteln(); 611 out.iwriteln("public int value () {"); 612 out.iwriteln(" return __value;"); 613 out.iwriteln("}"); 614 out.iwriteln(); 615 out.iwriteln("public static "+pkg+"."+shortName+" from_int(int value) {"); 616 out.iwriteln(" if (value >= 0 && value < __size)"); 617 out.iwriteln(" return __array[value];"); 618 out.iwriteln(" else"); 619 out.iwriteln(" throw new "+GeneratedObject.stdPackage+"BAD_PARAM();"); 620 out.iwriteln("}"); 621 out.iwriteln(); 622 out.iwriteln("protected Barvy (int value) {"); 623 out.iwriteln(" __value = value;"); 624 out.iwriteln(" __array[__value] = this;"); 625 out.iwriteln("}"); 626 out.decIndent(); 627 out.iwriteln("}"); 629 createHolder( absName+"Holder.java", shortName,pkg); 631 632 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 634 outHelp.iwriteln("package "+pkg+";"); 635 outHelp.iwriteln(); 636 printHeadComment(null, outHelp); 637 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 638 outHelp.incIndent(); 639 outHelp.iwriteln("public static "+pkg+"."+shortName+" read("+GeneratedObject.stdPackage+"InputStream istream) throws java.io.IOException {"); 640 outHelp.iwriteln(" return "+pkg+"."+shortName+".from_int(istream.read_long ());"); 641 outHelp.iwriteln("}"); 642 outHelp.iwriteln(); 643 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+pkg+"."+shortName+" value) throws java.io.IOException {"); 644 outHelp.iwriteln(" ostream.write_long (value.value ());"); 645 outHelp.iwriteln("}"); 646 outHelp.iwriteln(); 647 648 outHelp.decIndent(); 649 outHelp.iwriteln("}"); 650 outHelp.close(); 651 653 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+shortName)); 654 genFiles.addNewObject(pkg+"."+shortName,contained2string(obj)); 655 genFiles.addNewObject(pkg+"."+shortName+"Holder",contained2string(obj)); 656 genFiles.addNewObject(pkg+"."+shortName+"Helper",contained2string(obj)); 657 } catch (TIRExceptLock e) { 658 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 659 } 660 } 661 662 private class StructMemberInfo { 663 String name; 664 GeneratedObject type; 665 public StructMemberInfo(String name, GeneratedObject type) { 666 this.name = name; 667 this.type = type; 668 } 669 } 670 671 682 public void generateStruct(StructDef obj, CGFileWriter out, String shortName, String absName, String pkg) throws GenerException, IOException , RemoteException { 683 try { 684 Contained[] cont = obj.contents(null); 685 out.iwriteln("package "+pkg+";"); 686 out.iwriteln(); 687 printHeadComment(contained2string(obj),out); 688 out.iwriteln("public final class "+shortName+" implements "+GeneratedObject.stdPackage+"CDLEntity {"); 689 out.incIndent(); 690 printCDLIdField(obj, out, false); 691 692 String key; 693 GeneratedObject type; 694 StructMemberInfo smi; 695 java.util.ArrayList genContent = new java.util.ArrayList (cont.length); 697 CGFileWriter outInner; 698 Identification idInner; 699 String nameInner; 700 for (int i=0;i<cont.length;i++) { switch (cont[i].get_def_kind().value()) { 702 case DefinitionKind.dk_StructMember: 703 StructMember sm = (StructMember) cont[i]; 704 key = GeneratedObject.createKey(sm.type()); 705 type = searchObject(key); 706 if (type == null) { 707 throw new GenerException(Res.res.getString("MSG_ReferenceTo")+" "+key+" "+Res.res.getString("MSG_CantBeGenerated")); 708 } 709 smi = new StructMemberInfo(sm.name(), type); 710 out.iwriteln("public "+smi.type.genReference(GeneratedObject.INREF)+" "+smi.name+" = "+smi.type.defaultValue()+";"); 711 genContent.add(smi); 712 break; 713 case DefinitionKind.dk_Struct: 714 StructDef strd = (StructDef) cont[i]; 715 idInner = strd.get_identification(); 716 nameInner = idInner.name(); 717 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 718 generateStruct(strd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 719 break; 720 case DefinitionKind.dk_Exception: 721 ExceptionDef excd = (ExceptionDef) cont[i]; 722 idInner = excd.get_identification(); 723 nameInner = idInner.name(); 724 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 725 generateException(excd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 726 break; 727 case DefinitionKind.dk_Enum: 728 EnumDef enmd = (EnumDef) cont[i]; 729 idInner = enmd.get_identification(); 730 nameInner = idInner.name(); 731 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 732 generateEnum(enmd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 733 break; 734 } 735 } 736 737 out.iwriteln(); 738 out.iwriteln("public "+shortName+"() {}"); 739 out.iwriteln(); 740 out.iwrite("public "+shortName+"("); 741 for (int i=0;i<genContent.size();i++) { 742 smi = (StructMemberInfo) genContent.get(i); 743 out.iwrite(smi.type.genReference(GeneratedObject.INREF)+" _"+smi.name); 744 if (i!=genContent.size() - 1) 745 out.iwrite(", "); 746 } 747 out.iwriteln(") {"); 748 out.incIndent(); 749 for (int i=0;i<genContent.size();i++) { 750 smi = (StructMemberInfo) genContent.get(i); 751 out.iwriteln(smi.name+" = _"+smi.name+";"); 752 } 753 out.decIndent(); 754 out.iwriteln("}"); 755 756 out.decIndent(); 757 out.iwriteln("}"); 759 createHolder( absName+"Holder.java", shortName,pkg); 761 762 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 764 outHelp.iwriteln("package "+pkg+";"); 765 outHelp.iwriteln(); 766 printHeadComment(null, outHelp); 767 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 768 outHelp.incIndent(); 769 outHelp.iwriteln("public static "+pkg+"."+shortName+" read("+GeneratedObject.stdPackage+"InputStream istream) throws java.io.IOException {"); 770 outHelp.iwriteln(" "+shortName+" value = new "+shortName+"();"); 771 for (int i=0;i<genContent.size();i++) { 772 smi = (StructMemberInfo) genContent.get(i); 773 outHelp.iwriteln(" "+smi.type.genReadType("value."+smi.name)+";"); 774 } 775 outHelp.iwriteln(" return value;"); 776 outHelp.iwriteln("}"); 777 outHelp.iwriteln(); 778 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+pkg+"."+shortName+" value) throws java.io.IOException {"); 779 for (int i=0;i<genContent.size();i++) { 780 smi = (StructMemberInfo) genContent.get(i); 781 outHelp.iwriteln(" "+ smi.type.genWriteType("value."+smi.name) +";"); 782 } 783 outHelp.iwriteln("}"); 784 outHelp.iwriteln(); 785 786 outHelp.decIndent(); 787 outHelp.iwriteln("}"); 788 outHelp.close(); 789 791 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+shortName)); 792 genFiles.addNewObject(pkg+"."+shortName,contained2string(obj)); 793 genFiles.addNewObject(pkg+"."+shortName+"Holder",contained2string(obj)); 794 genFiles.addNewObject(pkg+"."+shortName+"Helper",contained2string(obj)); 795 } catch (TIRExceptLock e) { 796 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 797 } 798 } 799 800 811 public void generateException(ExceptionDef obj, CGFileWriter out, String shortName, String absName, String pkg) throws GenerException, IOException , RemoteException { 812 try { 813 Contained[] cont = obj.contents(null); 814 out.iwriteln("package "+pkg+";"); 815 out.iwriteln(); 816 printHeadComment(contained2string(obj),out); 817 out.iwriteln("public final class "+shortName+" extends "+GeneratedObject.stdPackage+"UserException {"); 818 out.incIndent(); 819 printCDLIdField(obj, out, false); 820 821 String key; 822 GeneratedObject type; 823 StructMemberInfo smi; 824 java.util.ArrayList genContent = new java.util.ArrayList (cont.length); 826 CGFileWriter outInner; 827 Identification idInner; 828 String nameInner; 829 for (int i=0;i<cont.length;i++) { switch (cont[i].get_def_kind().value()) { 831 case DefinitionKind.dk_StructMember: 832 StructMember sm = (StructMember) cont[i]; 833 key = GeneratedObject.createKey(sm.type()); 834 type = searchObject(key); 835 if (type == null) { 836 throw new GenerException(Res.res.getString("MSG_ReferenceTo")+" "+key+" "+Res.res.getString("MSG_CantBeGenerated")); 837 } 838 smi = new StructMemberInfo(sm.name(), type); 839 out.iwriteln("public "+smi.type.genReference(GeneratedObject.INREF)+" "+smi.name+" = "+smi.type.defaultValue()+";"); 840 genContent.add(smi); 841 break; 842 case DefinitionKind.dk_Struct: 843 StructDef strd = (StructDef) cont[i]; 844 idInner = strd.get_identification(); 845 nameInner = idInner.name(); 846 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 847 generateStruct(strd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 848 break; 849 case DefinitionKind.dk_Exception: 850 ExceptionDef excd = (ExceptionDef) cont[i]; 851 idInner = excd.get_identification(); 852 nameInner = idInner.name(); 853 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 854 generateException(excd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 855 break; 856 case DefinitionKind.dk_Enum: 857 EnumDef enmd = (EnumDef) cont[i]; 858 idInner = enmd.get_identification(); 859 nameInner = idInner.name(); 860 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 861 generateEnum(enmd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 862 break; 863 } 864 } 865 866 out.iwriteln(); 867 out.iwriteln("public "+shortName+"() {"); out.iwriteln(" super();"); 869 out.iwriteln("}"); out.iwriteln(); 871 if (genContent.size()!=0) { 872 out.iwrite("public "+shortName+"("); for (int i=0;i<genContent.size();i++) { 874 smi = (StructMemberInfo) genContent.get(i); 875 out.iwrite(smi.type.genReference(GeneratedObject.INREF)+" _"+smi.name); 876 if (i!=genContent.size() - 1) 877 out.iwrite(", "); 878 } 879 out.iwriteln(") {"); 880 out.incIndent(); 881 out.iwriteln("super();"); 882 for (int i=0;i<genContent.size();i++) { 883 smi = (StructMemberInfo) genContent.get(i); 884 out.iwriteln(smi.name+" = _"+smi.name+";"); 885 } 886 out.decIndent(); 887 out.iwriteln("}"); out.iwriteln(); 889 } 890 out.iwrite("public "+shortName+"(String __reason"); for (int i=0;i<genContent.size();i++) { 892 out.iwrite(", "); 893 smi = (StructMemberInfo) genContent.get(i); 894 out.iwrite(smi.type.genReference(GeneratedObject.INREF)+" _"+smi.name); 895 } 896 out.iwriteln(") {"); 897 out.incIndent(); 898 out.iwriteln("super(__reason);"); 899 for (int i=0;i<genContent.size();i++) { 900 smi = (StructMemberInfo) genContent.get(i); 901 out.iwriteln(smi.name+" = _"+smi.name+";"); 902 } 903 out.decIndent(); 904 out.iwriteln("}\n"); 906 out.iwrite("public "+shortName+"(String __reason, Throwable __cause"); for (int i=0;i<genContent.size();i++) { 908 out.iwrite(", "); 909 smi = (StructMemberInfo) genContent.get(i); 910 out.iwrite(smi.type.genReference(GeneratedObject.INREF)+" _"+smi.name); 911 } 912 out.iwriteln(") {"); 913 out.incIndent(); 914 out.iwriteln("super(__reason, __cause);"); 915 for (int i=0;i<genContent.size();i++) { 916 smi = (StructMemberInfo) genContent.get(i); 917 out.iwriteln(smi.name+" = _"+smi.name+";"); 918 } 919 out.decIndent(); 920 out.iwriteln("}"); 922 out.decIndent(); 923 924 out.decIndent(); 925 out.iwriteln("}"); 927 createHolder( absName+"Holder.java", shortName,pkg); 929 930 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 932 outHelp.iwriteln("package "+pkg+";"); 933 outHelp.iwriteln(); 934 printHeadComment(null, outHelp); 935 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 936 outHelp.incIndent(); 937 outHelp.iwriteln("public static "+pkg+"."+shortName+" read("+GeneratedObject.stdPackage+"InputStream istream) throws java.io.IOException {"); 938 outHelp.iwriteln(" "+shortName+" value = new "+shortName+"();"); 939 for (int i=0;i<genContent.size();i++) { 940 smi = (StructMemberInfo) genContent.get(i); 941 outHelp.iwriteln(" "+smi.type.genReadType("value."+smi.name)+";"); 942 } 943 outHelp.iwriteln(" return value;"); 944 outHelp.iwriteln("}"); 945 outHelp.iwriteln(); 946 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+pkg+"."+shortName+" value) throws java.io.IOException {"); 947 for (int i=0;i<genContent.size();i++) { 948 smi = (StructMemberInfo) genContent.get(i); 949 outHelp.iwriteln(" "+ smi.type.genWriteType("value."+smi.name) +";"); 950 } 951 outHelp.iwriteln("}"); 952 outHelp.iwriteln(); 953 954 outHelp.decIndent(); 955 outHelp.iwriteln("}"); 956 outHelp.close(); 957 959 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+shortName)); 960 genFiles.addNewObject(pkg+"."+shortName,contained2string(obj)); 961 genFiles.addNewObject(pkg+"."+shortName+"Holder",contained2string(obj)); 962 genFiles.addNewObject(pkg+"."+shortName+"Helper",contained2string(obj)); 963 } catch (TIRExceptLock e) { 964 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 965 } 966 } 967 968 969 980 public void generateInterface(InterfaceDef obj, CGFileWriter out, String shortName, String absName, String pkg) throws GenerException, IOException , RemoteException { 981 try { 982 out.iwriteln("package "+pkg+";"); 983 out.iwriteln(); 984 printHeadComment(contained2string(obj),out); 985 InterfaceDef[] base = obj.base_interfaces(); 986 out.iwrite("public interface "+shortName+" extends "+shortName+"Operations, "); 988 for (int i=0; i< base.length; i++) { 989 GeneratedObject bitem = searchObject(GeneratedObject.createKey(base[i])); 990 out.iwrite(bitem.genReference(GeneratedObject.INREF)); 991 out.iwrite(", "); 992 } 993 out.iwriteln(GeneratedObject.stdPackage+"CDLEntity, org.objectweb.fractal.api.Interface {"); 994 printCDLIdField(obj, out, true); 996 out.iwriteln("}"); 997 998 Contained[] cont = obj.contents(null); 1000 CGFileWriter outOp = fs.newCGWriter(absName+"Operations.java"); 1001 outOp.iwriteln("package "+pkg+";"); 1002 outOp.iwriteln(); 1003 outOp.iwriteln(); 1004 printHeadComment(contained2string(obj), outOp); 1005 outOp.iwrite("public interface "+shortName+"Operations "); 1006 outOp.iwrite("extends "); 1007 if (base.length != 0) { 1008 for(int i=0; i<base.length;i++) { 1009 GeneratedObject bitem = searchObject(GeneratedObject.createKey(base[i])); 1010 outOp.iwrite(bitem.genReference(GeneratedObject.INREF)); 1011 outOp.iwrite("Operations"); 1012 outOp.iwrite(", "); 1014 } 1016 } 1017 outOp.iwriteln("org.objectweb.fractal.api.InterfaceOperations {"); 1018 outOp.incIndent(); 1019 CGFileWriter outInner; 1020 Identification idInner; 1021 String nameInner; 1022 GeneratedObject genObj; 1023 String key; 1024 for(int i=0; i< cont.length; i++) { 1025 switch (cont[i].get_def_kind().value()) { 1026 case DefinitionKind.dk_Struct: 1027 StructDef strd = (StructDef) cont[i]; 1028 idInner = strd.get_identification(); 1029 nameInner = idInner.name(); 1030 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 1031 generateStruct(strd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 1032 break; 1033 case DefinitionKind.dk_Exception: 1034 ExceptionDef excd = (ExceptionDef) cont[i]; 1035 idInner = excd.get_identification(); 1036 nameInner = idInner.name(); 1037 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 1038 generateException(excd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 1039 break; 1040 case DefinitionKind.dk_Typedef: 1041 TypedefDef typd = (TypedefDef) cont[i]; 1042 idInner = typd.get_identification(); 1043 nameInner = idInner.name(); 1044 generateTypedef(typd, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 1045 break; 1046 case DefinitionKind.dk_Constant: 1047 ConstantDef cnstd = (ConstantDef) cont[i]; 1048 idInner = cnstd.get_identification(); 1049 nameInner = idInner.name(); 1050 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 1051 generateConstant(cnstd, outInner, nameInner, pkg+"."+shortName+"Package"); 1052 break; 1053 case DefinitionKind.dk_Enum: 1054 EnumDef enmd = (EnumDef) cont[i]; 1055 idInner = enmd.get_identification(); 1056 nameInner = idInner.name(); 1057 outInner = fs.newCGWriter(absName+"Package"+java.io.File.separator+nameInner+".java"); 1058 generateEnum(enmd, outInner, nameInner, absName+"Package"+java.io.File.separator+nameInner, pkg+"."+shortName+"Package"); 1059 break; 1060 case DefinitionKind.dk_Attribute: 1061 AttributeDef attrd = (AttributeDef) cont[i]; 1062 idInner = attrd.get_identification(); 1063 nameInner = idInner.name(); 1064 if (GeneratedObject.isReservedName(nameInner)) { 1065 nameInner = "_"+nameInner; 1066 } 1067 key = GeneratedObject.createKey(attrd.type()); 1068 genObj = searchObject(key); 1069 if (genObj == null) { 1070 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1071 } 1072 String inRef = genObj.genReference(GeneratedObject.INREF); 1073 outOp.iwriteln(inRef+" "+nameInner+"();"); 1074 if (attrd.mode().value() == AttrMode.ATTR_NORMAL) { 1075 outOp.iwriteln("void "+nameInner+"("+inRef+" new"+nameInner+");"); 1076 } 1077 break; 1078 case DefinitionKind.dk_Operation: 1079 OperationDef opd = (OperationDef) cont[i]; 1080 idInner = opd.get_identification(); 1081 nameInner = idInner.name(); 1082 if (GeneratedObject.isReservedName(nameInner)) { 1083 nameInner = "_"+nameInner; 1084 } 1085 key = GeneratedObject.createKey(opd.result()); 1086 genObj = searchObject(key); 1087 if (genObj == null) { 1088 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1089 } 1090 outOp.iwrite(genObj.genReference(GeneratedObject.INREF)+" "+nameInner+"("); 1091 ParamDescr[] params = opd.params(); 1092 for (int j=0; j<params.length ;j++ ) { 1093 String pName = params[j].name(); 1094 if (GeneratedObject.isReservedName(pName)) { 1095 pName = "_"+pName; 1096 } 1097 key = GeneratedObject.createKey(params[j].type()); 1098 genObj = searchObject(key); 1099 if (genObj == null) { 1100 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1101 } 1102 if (params[j].mode().value() != ParamMode.PARAM_IN ) { 1103 outOp.iwrite(genObj.genReference(GeneratedObject.OUTREF)+" "); 1104 } else { 1105 outOp.iwrite(genObj.genReference(GeneratedObject.INREF)+" "); 1106 } 1107 outOp.iwrite(pName); 1108 if (j!=params.length-1) { 1109 outOp.iwrite(", "); 1110 } 1111 } 1112 outOp.iwrite(") "); 1113 ExceptionDef[] excs = opd.exceptions(); 1114 if (excs.length != 0) { 1115 outOp.iwrite("throws "); 1116 for (int j=0; j<excs.length; j++) { 1117 key = GeneratedObject.createKey(excs[j]); 1118 genObj = searchObject(key); 1119 if (genObj == null) { 1120 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1121 } 1122 outOp.iwrite(genObj.genReference(GeneratedObject.INREF)); 1123 if (j != excs.length-1 ) { 1124 outOp.iwrite(", "); 1125 } 1126 } 1127 } 1128 outOp.iwriteln(";"); 1129 break; 1130 } 1131 } 1132 outOp.decIndent(); 1133 outOp.iwriteln("}"); 1134 outOp.close(); 1135 1136 1138 createHolder( absName+"Holder.java", shortName,pkg); 1140 1141 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 1143 outHelp.iwriteln("package "+pkg+";"); 1144 outHelp.iwriteln(); 1145 printHeadComment(null, outHelp); 1146 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 1147 outHelp.incIndent(); 1148 outHelp.iwriteln("public static "+pkg+"."+shortName+" read("+GeneratedObject.stdPackage+"InputStream istream) throws java.io.IOException {"); 1149 outHelp.iwriteln(" return null;"); 1151 outHelp.iwriteln("}"); 1152 outHelp.iwriteln(); 1153 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+pkg+"."+shortName+" value) throws java.io.IOException {"); 1154 outHelp.iwriteln("}"); 1156 outHelp.iwriteln(); 1157 1158 outHelp.decIndent(); 1159 outHelp.iwriteln("}"); 1160 outHelp.close(); 1161 1163 genObjects.add(new ContainedGeneratedObject(obj, pkg+"."+shortName)); 1164 genFiles.addNewObject(pkg+"."+shortName,contained2string(obj)); 1165 genFiles.addNewObject(pkg+"."+shortName+"Operations",contained2string(obj)); 1166 genFiles.addNewObject(pkg+"."+shortName+"Holder",contained2string(obj)); 1167 genFiles.addNewObject(pkg+"."+shortName+"Helper",contained2string(obj)); 1168 } catch (TIRExceptLock e) { 1169 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 1170 } 1171 } 1172 1173 1174 1175 private class TypedefArrayInfo { 1176 String length; 1177 public TypedefArrayInfo(String l) { 1178 length = l; 1179 } 1180 } 1181 1182 1185 static String genBrackets(int number) { 1186 StringBuffer sb = new StringBuffer (); 1187 for (int i =0; i<number;i++) { 1188 sb.append("[]"); 1189 } 1190 return sb.toString(); 1191 } 1192 1193 1195 private String generateFor(String index, String max) { 1196 return "for (int "+index+" = 0; "+index+" < ("+max+"); "+index+"++) {"; 1197 } 1198 1199 1201 private String generateArray(String name, String indexPrefix, int number) { 1202 StringBuffer sb = new StringBuffer (name); 1203 for (int i=0; i<number; i++) { 1204 sb.append("["); 1205 sb.append(indexPrefix); 1206 sb.append(Integer.toString(i)); 1207 sb.append("]"); 1208 } 1209 return sb.toString(); 1210 } 1211 1212 1222 public void generateTypedef(TypedefDef obj, String shortName, String absName, String pkg) throws GenerException, IOException , RemoteException { 1223 1225 java.util.ArrayList arrInfo = new java.util.ArrayList (); 1226 java.util.ArrayList seqInfo = new java.util.ArrayList (); 1227 boolean ok = false; 1228 CDLType lastType = obj.original_type(); 1229 1230 TypedefArrayInfo tdi = null; 1231 while (!ok) { 1232 switch (lastType.get_def_kind().value()) { 1233 case DefinitionKind.dk_Array: 1234 String len; 1235 try { 1236 len = getNumber(((ArrayDef) lastType).length()); 1237 } catch (PropException pe) { 1238 len = null; 1239 } 1240 tdi = new TypedefArrayInfo(len); 1241 arrInfo.add(tdi); 1242 lastType = ((ArrayDef) lastType).element_type(); 1243 break; 1244 case DefinitionKind.dk_Sequence: 1245 try { 1246 tdi = new TypedefArrayInfo(getNumber(((SequenceDef) lastType).bound())); 1247 } catch (PropException pe) { 1248 ; } 1250 seqInfo.add(tdi); 1251 lastType = ((SequenceDef) lastType).element_type(); 1252 break; 1253 default: 1254 ok = true; 1255 break; 1256 } 1257 } 1258 1259 String origKey = GeneratedObject.createKey(lastType); 1260 GeneratedObject generObj = searchObject(origKey); 1261 String inRef = generObj.genReference(GeneratedObject.INREF); 1262 1263 if (arrInfo.size() == 0 && seqInfo.size()==0) { switch (lastType.get_def_kind().value()) { 1265 case DefinitionKind.dk_Primitive: 1266 case DefinitionKind.dk_String: 1267 case DefinitionKind.dk_Wstring: 1268 case DefinitionKind.dk_Typedef: genObjects.add(new ContainedGeneratedObject(obj, inRef, generObj.genReference(GeneratedObject.OUTREF))); 1270 return; 1271 default: genObjects.add(new ContainedGeneratedObject(obj, inRef, generObj.genReference(GeneratedObject.OUTREF))); 1273 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 1276 outHelp.iwriteln("package "+pkg+";"); 1277 outHelp.iwriteln(); 1278 printHeadComment(null, outHelp); 1279 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 1280 outHelp.incIndent(); 1281 outHelp.iwriteln("public static "+inRef+" read("+GeneratedObject.stdPackage+"InputStream istream) {"); 1282 outHelp.iwriteln(" "+inRef+" value = null;"); 1283 outHelp.iwriteln(" "+generObj.genReadType("value")+";"); 1284 outHelp.iwriteln(" return value;"); 1285 outHelp.iwriteln("}"); 1286 outHelp.iwriteln(); 1287 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+inRef+" value) {"); 1288 outHelp.iwriteln(" "+ generObj.genWriteType("value") +";"); 1289 outHelp.iwriteln("}"); 1290 outHelp.iwriteln(); 1291 1292 outHelp.decIndent(); 1293 outHelp.iwriteln("}"); 1294 outHelp.close(); 1295 return; 1297 } 1298 } 1299 1300 int bracketNum = arrInfo.size() + seqInfo.size(); 1301 createTypedefHolder( absName+"Holder.java", shortName,pkg, inRef+genBrackets(bracketNum)); 1303 CGFileWriter outHelp = fs.newCGWriter(absName+"Helper.java"); 1305 outHelp.iwriteln("package "+pkg+";"); 1306 outHelp.iwriteln(); 1307 printHeadComment(null, outHelp); 1308 outHelp.iwriteln("abstract public class "+shortName+"Helper {"); 1309 outHelp.incIndent(); 1310 outHelp.iwriteln("public static "+inRef+genBrackets(bracketNum)+" read("+GeneratedObject.stdPackage+"InputStream istream) throws java.io.IOException {"); 1311 outHelp.incIndent(); 1312 outHelp.iwriteln(inRef+" value"+genBrackets(bracketNum)+" = null;"); 1314 for(int i=0; i<arrInfo.size(); i++) { 1315 if (((TypedefArrayInfo)arrInfo.get(i)).length == null) { 1316 outHelp.iwriteln("int _len"+Integer.toString(i)+" = istream.read_long();"); 1317 outHelp.iwriteln(generateArray("value","_o",i)+" = new "+inRef+"[_len"+Integer.toString(i)+"]"+genBrackets(bracketNum-i-1)+";"); 1318 outHelp.iwriteln(generateFor("_o"+Integer.toString(i), generateArray("value","_o",i)+".length")); 1319 } else { 1320 outHelp.iwriteln(generateArray("value","_o",i)+" = new "+inRef+"["+((TypedefArrayInfo)arrInfo.get(i)).length+"]"+genBrackets(bracketNum-i-1)+";"); 1321 outHelp.iwriteln(generateFor("_o"+Integer.toString(i), ((TypedefArrayInfo)arrInfo.get(i)).length)); 1322 } 1323 outHelp.incIndent(); 1324 } 1325 for(int i=arrInfo.size(); i<bracketNum;i++) { 1326 outHelp.iwriteln("int _len"+Integer.toString(i)+" = istream.read_long();"); 1327 outHelp.iwriteln(generateArray("value","_o",i)+" = new "+inRef+"[_len"+Integer.toString(i)+"]"+genBrackets(bracketNum-i-1)+";"); 1328 outHelp.iwriteln(generateFor("_o"+Integer.toString(i), generateArray("value","_o",i)+".length")); 1329 outHelp.incIndent(); 1330 } 1331 outHelp.iwriteln(generObj.genReadType(generateArray("value", "_o", bracketNum))+";"); 1332 for (int i=0; i<bracketNum; i++) { 1333 outHelp.decIndent(); 1334 outHelp.iwriteln("}"); 1335 } 1336 outHelp.iwriteln("return value;"); 1337 outHelp.decIndent(); 1339 outHelp.iwriteln("}"); 1340 outHelp.iwriteln(); 1341 1342 outHelp.iwriteln("public static void write("+GeneratedObject.stdPackage+"OutputStream ostream, "+inRef+genBrackets(bracketNum)+" value) throws java.io.IOException {"); 1343 outHelp.incIndent(); 1344 for(int i=0; i<arrInfo.size(); i++) { 1346 if (((TypedefArrayInfo)arrInfo.get(i)).length == null) { 1347 outHelp.iwriteln("ostream.write_long("+generateArray("value","_i",i-arrInfo.size())+".length);"); 1348 outHelp.iwriteln(generateFor("_i"+Integer.toString(i), generateArray("value", "_i",i-arrInfo.size())+".length")); 1349 } else { 1350 outHelp.iwriteln(generateFor("_i"+Integer.toString(i), ((TypedefArrayInfo)arrInfo.get(i)).length)); 1351 } 1352 outHelp.incIndent(); 1353 } 1354 for(int i=arrInfo.size(); i<bracketNum;i++) { 1355 outHelp.iwriteln("ostream.write_long("+generateArray("value","_i",i-arrInfo.size())+".length);"); 1356 outHelp.iwriteln(generateFor("_i"+Integer.toString(i), generateArray("value", "_i",i-arrInfo.size())+".length")); 1357 outHelp.incIndent(); 1358 } 1359 outHelp.iwriteln(generObj.genWriteType(generateArray("value", "_i", bracketNum))+";"); 1360 for (int i=0; i<bracketNum; i++) { 1361 outHelp.decIndent(); 1362 outHelp.iwriteln("}"); 1363 } 1364 outHelp.decIndent(); 1366 outHelp.iwriteln("}"); 1367 outHelp.iwriteln(); 1368 1369 outHelp.decIndent(); 1370 outHelp.iwriteln("}"); 1371 outHelp.close(); 1372 1374 genObjects.add(new ContainedGeneratedObject(obj, inRef+genBrackets(bracketNum), pkg+"."+shortName+"Holder")); 1375 genFiles.addNewObject(pkg+"."+shortName+"Holder",contained2string(obj)); 1376 genFiles.addNewObject(pkg+"."+shortName+"Helper",contained2string(obj)); 1377 1378 } 1382 1383 1384 1385 1386 1387 1388 1393 private void printHeadComment(String tirID, CGFileWriter out) throws IOException { 1394 java.util.Date date = java.util.Calendar.getInstance().getTime(); 1395 java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.FULL); 1396 out.iwriteln("/**"); 1397 out.iwriteln(" * "+Res.res.getString("MSG_GeneratedBy")+" SOFA CodeGen"); 1398 if (tirID != null) { 1399 out.iwriteln(" * "+tirID); 1400 } 1401 out.iwriteln(" * "+df.format(date)); 1402 out.iwriteln(" */"); 1403 out.iwriteln(); 1404 } 1405 1406 1414 private void printCDLIdField(Contained contained, CGFileWriter out, boolean isInterface) throws IOException { 1415 out.iwriteln(); 1416 if (!isInterface) { 1417 out.iwriteln("public static final "); 1418 } 1419 out.iwriteln("String _CDLID = \""+contained2string(contained)+"\";"); 1420 out.iwriteln(); 1421 } 1422 1423 1430 private void createHolder(String fileName, String name, String pkg) throws IOException { 1431 CGFileWriter out = fs.newCGWriter(fileName); 1432 out.iwriteln("package "+pkg+";"); 1433 out.iwriteln(); 1434 printHeadComment(null,out); 1435 out.iwriteln("public class "+name+"Holder implements "+GeneratedObject.stdPackage+"Streamable, java.io.Serializable {"); 1436 out.incIndent(); 1437 out.iwriteln("public "+pkg+"."+name+" value = null;"); 1438 out.iwriteln(); 1439 out.iwriteln("public "+name+"Holder() {}"); 1440 out.iwriteln(); 1441 out.iwriteln("public "+name+"Holder("+pkg+"."+name+" initialValue) {"); 1442 out.iwriteln(" value = initialValue;"); 1443 out.iwriteln("}"); 1444 out.iwriteln(); 1445 out.iwriteln("public void _read("+GeneratedObject.stdPackage+"InputStream i) throws java.io.IOException {"); 1446 out.iwriteln(" "+pkg+"."+name+"Helper.read(i);"); 1447 out.iwriteln("}"); 1448 out.iwriteln(); 1449 out.iwriteln("public void _write("+GeneratedObject.stdPackage+"OutputStream o) throws java.io.IOException {"); 1450 out.iwriteln(" "+pkg+"."+name+"Helper.write(o, value);"); 1451 out.iwriteln("}"); 1452 out.decIndent(); 1453 out.iwriteln("}"); 1454 out.close(); 1455 } 1456 1457 1465 private void createTypedefHolder(String fileName, String name, String pkg, String type) throws IOException { 1466 CGFileWriter out = fs.newCGWriter(fileName); 1467 out.iwriteln("package "+pkg+";"); 1468 out.iwriteln(); 1469 printHeadComment(null,out); 1470 out.iwriteln("public class "+name+"Holder implements "+GeneratedObject.stdPackage+"Streamable, java.io.Serializable {"); 1471 out.incIndent(); 1472 out.iwriteln("public "+type+" value = null;"); 1473 out.iwriteln(); 1474 out.iwriteln("public "+name+"Holder() {}"); 1475 out.iwriteln(); 1476 out.iwriteln("public "+name+"Holder("+type+" initialValue) {"); 1477 out.iwriteln(" value = initialValue;"); 1478 out.iwriteln("}"); 1479 out.iwriteln(); 1480 out.iwriteln("public void _read("+GeneratedObject.stdPackage+"InputStream i) throws java.io.IOException {"); 1481 out.iwriteln(" "+pkg+"."+name+"Helper.read(i);"); 1482 out.iwriteln("}"); 1483 out.iwriteln(); 1484 out.iwriteln("public void _write("+GeneratedObject.stdPackage+"OutputStream o) throws java.io.IOException {"); 1485 out.iwriteln(" "+pkg+"."+name+"Helper.write(o, value);"); 1486 out.iwriteln("}"); 1487 out.decIndent(); 1488 out.iwriteln("}"); 1489 out.close(); 1490 } 1491 1492 private String contained2string(Contained obj) throws RemoteException { 1493 Identification id = obj.get_identification(); 1494 StringBuffer sb = new StringBuffer (); 1495 sb.append(id.absolute_name().name()); 1496 sb.append("?"); 1497 sb.append(id.version()); 1498 return sb.toString(); 1499 } 1500 1501 private void generateBuilder(ArchitectureDef arch, CGFileWriter out, String provider, String name, String pkg) throws IOException , RemoteException , GenerException { 1502 Contained[] insts = null; 1503 boolean isPrimitive = false; 1504 java.util.ArrayList instInfos = null; 1505 boolean isSystem = arch.is_system(); 1506 try { 1507 insts = arch.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Inst)); 1508 if (insts.length == 0) 1509 isPrimitive = true; 1510 } catch (TIRExceptLock e) { 1511 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 1512 } 1513 1514 1515 out.iwriteln("package "+pkg+";"); 1516 out.iwriteln(); 1517 printHeadComment("Component builder "+Res.res.getString("MSG_For")+" "+contained2string(arch),out); 1518 out.iwriteln("public class "+name+"Builder extends SOFA.Component.DCUP.ComponentBuilderImpl {"); 1519 out.incIndent(); 1520 out.iwriteln("// "+Res.res.getString("MSG_DefinedInAncestor")); 1521 out.iwriteln("// protected SOFA.Component.ComponentManager cm;"); 1522 out.iwriteln("// protected SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd;"); 1523 out.iwriteln(); 1524 if (isPrimitive) { 1525 out.iwriteln(name+"Impl impl;"); 1526 out.iwriteln(); 1527 } else { 1528 instInfos = new java.util.ArrayList (); 1529 for (int i=0; i<insts.length; i++) { 1531 InstInfo iInfo = InstInfo.getInstInfo(insts[i]); 1532 instInfos.add(iInfo); 1533 out.iwriteln("SOFA.Component.DCUP.DCUPComponentManager "+iInfo.name+"cm"+genBrackets(iInfo.arrays.size())+" = null;"); 1534 } 1535 } 1536 generateProperties(arch, out); 1537 out.iwriteln(); 1538 out.iwriteln("public "+name+"Builder() {"); 1539 out.iwriteln("}"); 1540 out.iwriteln(); 1541 out.iwriteln("public void initialize(SOFA.Component.ComponentManager cm, SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd) {"); 1542 out.incIndent(); 1543 out.iwriteln("super.initialize(cm, dd);"); 1544 try { 1545 generateBuilderInitialize(arch, insts, instInfos, out, name ,isPrimitive, isSystem); 1546 } catch (PropException e) { 1547 throw new GenerException(e.getMessage()); 1548 } 1549 out.decIndent(); 1550 out.iwriteln("}"); 1551 out.iwriteln(); 1552 out.iwriteln("public void onLeaving(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {"); 1553 out.iwriteln(" if (!stopped) {"); 1554 if (isPrimitive) { 1556 out.iwriteln(" impl.bStop();"); 1557 } else { 1558 out.iwriteln(""); 1559 } 1560 out.iwriteln(" } else {"); 1561 out.iwriteln(" SOFA.SOFAnode.Run.Deployment.DeplDock dock = cm.getLocalDeplDock();"); 1562 out.iwriteln(" try {"); 1563 out.iwriteln(" dock.removeComponent(fullInstanceName);"); 1564 out.iwriteln(" } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {"); 1565 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_ComponentCantBeRemoved")+"\", e);"); 1566 out.iwriteln(" }"); 1567 out.iwriteln(" }"); 1568 out.iwriteln("}"); 1569 out.iwriteln(); 1570 out.iwriteln("public void onArrival(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {"); 1571 try { 1572 generateBuilderOnArrival(arch, insts, instInfos, out, name ,isPrimitive, isSystem); 1573 } catch (PropException e) { 1574 throw new GenerException(e.getMessage()); 1575 } 1576 out.iwriteln("}"); 1577 out.iwriteln(); 1578 out.iwriteln("public void store(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {"); 1579 if (isPrimitive) { 1580 out.iwriteln(" impl.bStore();"); 1581 } else { 1582 out.iwriteln(""); 1583 } 1584 out.iwriteln("}"); 1585 out.iwriteln(); 1586 out.iwriteln("public void restore(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {"); 1587 if (isPrimitive) { 1588 out.iwriteln(" impl.bRestore();"); 1589 } else { 1590 out.iwriteln(""); 1591 } 1592 out.iwriteln("}"); 1593 out.decIndent(); 1594 out.iwriteln("}"); 1595 } 1596 1597 1605 private void generateProperties(ArchitectureDef arch, CGFileWriter out) throws IOException , RemoteException , GenerException { 1606 try { 1607 java.util.Hashtable generatedNames = new java.util.Hashtable (); 1608 Contained[] archProps = arch.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Property)); 1609 Contained[] frameProps = arch.frame().contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Property)); 1610 for (int i=0; i<frameProps.length; i++) { 1611 PropertyDef prop = (PropertyDef) frameProps[i]; 1612 String name = prop.get_identification().name(); 1613 String key = GeneratedObject.createKey(prop.type()); 1614 GeneratedObject generObj = searchObject(key); 1615 if (generObj == null) { 1616 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1617 } 1618 String inRef = generObj.genReference(GeneratedObject.INREF); 1619 generatedNames.put(name, inRef); 1620 out.iwriteln("public "+inRef+" _prop_"+name+";"); 1621 } 1622 out.iwriteln(""); 1623 for (int i=0; i<archProps.length; i++) { 1624 PropertyDef prop = (PropertyDef) archProps[i]; 1625 String name = prop.get_identification().name(); 1626 if (generatedNames.containsKey(name)) 1627 break; 1628 String key = GeneratedObject.createKey(prop.type()); 1629 GeneratedObject generObj = searchObject(key); 1630 if (generObj == null) { 1631 throw new GenerException(Res.res.getString("MSG_TIRError")+"\n"+key+" "+Res.res.getString("MSG_CantBeGenerated")); 1632 } 1633 String inRef = generObj.genReference(GeneratedObject.INREF); 1634 generatedNames.put(name, inRef); 1635 out.iwriteln("public "+inRef+" _prop_"+name+";"); 1636 } 1637 out.iwriteln(""); 1638 out.iwriteln("private void initProperties() throws NumberFormatException {"); 1639 out.incIndent(); 1640 for (java.util.Enumeration e = generatedNames.keys(); e.hasMoreElements(); ) { 1641 String name = (String ) e.nextElement(); 1642 out.iwrite((String ) generatedNames.get(name)); 1643 out.iwriteln(" _prop_"+name+" = "+baseTypeToParseMethod((String )generatedNames.get(name), "dd.getProperty(\""+name+"\")")+";"); 1644 } 1645 out.decIndent(); 1646 out.iwriteln("}\n"); 1647 } catch (TIRExceptLock e) { 1648 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 1649 } 1650 } 1651 1652 private void generateBuilderInitialize(ArchitectureDef arch, Contained[] insts, java.util.ArrayList instInfos, CGFileWriter out, String name, boolean isPrimitive, boolean isSystem) throws RemoteException , IOException , GenerException, PropException { 1653 out.iwriteln("initProperties();"); 1654 out.iwriteln(""); 1655 if (isPrimitive) { 1656 out.iwriteln("impl = new "+name+"Impl ((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm);"); 1657 out.iwriteln(""); 1659 generateBuilderInitializeBinds(arch, insts, instInfos, out, name ,isPrimitive, isSystem); 1660 out.iwriteln(""); 1661 out.iwriteln("updatable = impl.isUpdatable();"); 1663 } else { 1664 out.iwriteln("SOFA.SOFAnode.Run.Deployment.DeplDock dock = cm.getLocalDeplDock();"); 1665 for (int i=0; i<insts.length; i++) { 1667 InstInfo iInfo = (InstInfo) instInfos.get(i); 1668 out.iwriteln("SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor "+iInfo.name+"dd = dd.getSubComponentDeploymentDescriptor(\""+iInfo.name+"\");"); 1669 } 1670 out.iwriteln(""); 1671 1672 out.iwriteln("String location = null;"); 1674 int bracketNum; 1675 for(int i=0; i<instInfos.size(); i++) { 1676 InstInfo iInfo = (InstInfo) instInfos.get(i); 1677 out.iwriteln("SOFA.Connector.Reference "+iInfo.name+"ref"+genBrackets(iInfo.arrays.size())+" = null;"); 1678 out.iwriteln("location = dd.getSubComponentLocation(\""+iInfo.name+"\");"); 1680 out.iwriteln("if (SOFA.Util.SOFAURI.isLocal(location)) {"); 1681 out.incIndent(); 1682 out.iwriteln("try {"); 1683 out.incIndent(); 1684 bracketNum = iInfo.arrays.size(); 1685 for (int j=0; j<bracketNum; j++) { 1686 out.iwriteln(generateArray(iInfo.name+"ref","_o",j)+" = new SOFA.Connector.Reference["+expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j))+"]"+genBrackets(bracketNum-j-1)+";"); 1687 out.iwriteln(generateArray(iInfo.name+"cm","_o",j)+" = new SOFA.Component.DCUP.DCUPComponentManager["+expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j))+"]"+genBrackets(bracketNum-j-1)+";"); 1688 out.iwriteln(generateFor("_o"+Integer.toString(j), expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j)))); 1689 out.incIndent(); 1690 } 1691 out.iwriteln("String instName = "+generateInstName(iInfo.name,"_o",bracketNum)+";"); 1692 out.iwriteln( generateArray(iInfo.name+"ref","_o",bracketNum)+" = dock.instantiate("+iInfo.name+"dd, instName);"); 1693 out.iwriteln( generateArray(iInfo.name+"cm","_o",bracketNum)+" = getCMConnectorFromRef("+generateArray(iInfo.name+"ref","_o",bracketNum)+");"); 1694 out.iwriteln("cm.registerSubcomponent(instName, "+generateArray(iInfo.name+"ref","_o",bracketNum)+");"); 1695 for (int j=0; j<bracketNum; j++) { 1696 out.decIndent(); 1697 out.iwriteln("}"); 1698 } 1699 out.decIndent(); 1700 out.iwriteln("} catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {"); 1701 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateSubcomponentCM")+"\", e);"); 1702 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 1703 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantObtainConnectorSubcomponentCM")+"\", e);"); 1704 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1705 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantRegisterSubcomponent")+"\", e);"); 1706 out.iwriteln("}"); 1707 out.decIndent(); 1708 out.iwriteln("} else {"); 1709 out.incIndent(); 1710 out.iwriteln("try {"); 1711 out.incIndent(); 1712 out.iwriteln("SOFA.Connector.Reference subDDockRef = SOFA.SOFAnode.Run.Deployment.DeplDockImpl.bytesToSofaReference(SOFA.Util.SOFAURI.getReferenceToDeplDockFromSOFAURI(location));"); 1713 out.iwriteln("SOFA.SOFAnode.Run.Deployment.DeplDock subDDock = (SOFA.SOFAnode.Run.Deployment.DeplDock) SOFA.Connector.Boot.DeplDockConnector.createClt(subDDockRef);"); 1714 bracketNum = iInfo.arrays.size(); 1715 for (int j=0; j<bracketNum; j++) { 1716 out.iwriteln(generateArray(iInfo.name+"ref","_o",j)+" = new SOFA.Connector.Reference["+expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j))+"]"+genBrackets(bracketNum-j-1)+";"); 1717 out.iwriteln(generateArray(iInfo.name+"cm","_o",j)+" = new SOFA.Component.DCUP.DCUPComponentManager["+expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j))+"]"+genBrackets(bracketNum-j-1)+";"); 1718 out.iwriteln(generateFor("_o"+Integer.toString(j), expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j)))); 1719 out.incIndent(); 1720 } 1721 out.iwriteln("String instName = "+generateInstName(iInfo.name,"_o",bracketNum)+";"); 1722 out.iwriteln( generateArray(iInfo.name+"ref","_o",bracketNum)+" = subDDock.instantiate("+iInfo.name+"dd, instName);"); 1723 out.iwriteln( generateArray(iInfo.name+"cm","_o",bracketNum)+" = getCMConnectorFromRef("+generateArray(iInfo.name+"ref","_o",bracketNum)+");"); 1724 out.iwriteln("cm.registerSubcomponent(instName, "+generateArray(iInfo.name+"ref","_o",bracketNum)+");"); 1725 for (int j=0; j<bracketNum; j++) { 1726 out.decIndent(); 1727 out.iwriteln("}"); 1728 } 1729 out.decIndent(); 1730 out.iwriteln("} catch (SOFA.Connector.ConnectorException e) {"); 1731 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateSubcomponentCM")+"\", e);"); 1732 out.iwriteln("} catch (SOFA.Util.SOFAURI.BadSOFAURIException e) {"); 1733 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateSubcomponentCM")+"\", e);"); 1734 out.iwriteln("} catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {"); 1735 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateSubcomponentCM")+"\", e);"); 1736 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 1737 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantObtainConnectorSubcomponentCM")+"\", e);"); 1738 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1739 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantRegisterSubcomponent")+"\", e);"); 1740 out.iwriteln("}"); 1741 out.decIndent(); 1742 out.iwriteln("}"); 1743 out.iwriteln(""); 1744 } 1745 out.iwriteln(""); 1746 out.iwriteln(""); 1748 generateBuilderInitializeBinds(arch, insts, instInfos, out, name ,isPrimitive, isSystem); 1749 out.iwriteln(""); 1750 1751 out.iwriteln("try {"); 1752 out.incIndent(); 1753 for(int i=0; i<instInfos.size(); i++) { 1754 InstInfo iInfo = (InstInfo) instInfos.get(i); 1755 out.iwriteln(iInfo.name+"cm.connectSubsumable();"); 1756 } 1757 out.decIndent(); 1758 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1759 out.incIndent(); 1760 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantSubsumeSubcomponents")+"\", e);"); 1761 out.decIndent(); 1762 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 1763 out.incIndent(); 1764 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantSubsumeSubcomponents")+"\", e);"); 1765 out.decIndent(); 1766 out.iwriteln("}"); 1767 out.iwriteln(""); 1768 1769 out.iwriteln("updatable = false;"); 1771 } 1772 } 1773 1774 1776 private void generateBuilderOnArrival(ArchitectureDef arch, Contained[] insts, java.util.ArrayList instInfos, CGFileWriter out, String name, boolean isPrimitive, boolean isSystem) throws RemoteException , IOException , GenerException, PropException { 1777 if (isPrimitive) { 1778 out.iwriteln(" impl.bStart();"); 1779 } else { 1780 int bracketNum; 1781 out.incIndent(); 1782 for(int i=0; i<instInfos.size(); i++) { 1783 InstInfo iInfo = (InstInfo) instInfos.get(i); 1784 bracketNum = iInfo.arrays.size(); 1785 if (bracketNum == 0) { out.iwriteln(iInfo.name+"cm.createComponent(stateStore);"); 1787 } else { for (int j=0; j<bracketNum; j++) { 1789 out.iwriteln(generateFor("_i"+Integer.toString(j), expressionWithPropertyToString((ExprOperDef)iInfo.arrays.get(j)))); 1790 out.incIndent(); 1791 } 1792 out.iwriteln(generateArray(iInfo.name+"cm","_i",bracketNum)+".createComponent(stateStore);"); 1793 for (int j=0; j<bracketNum; j++) { 1794 out.decIndent(); 1795 out.iwriteln("}"); 1796 } 1797 } 1798 } 1799 out.decIndent(); 1800 } 1801 } 1802 1803 1805 private void generateBuilderInitializeBinds(ArchitectureDef arch, Contained[] insts, java.util.ArrayList instInfos, CGFileWriter out, String name, boolean isPrimitive, boolean isSystem) throws RemoteException , IOException , GenerException, PropException { 1806 1807 Contained[] binds; 1808 java.util.ArrayList delegs = new java.util.ArrayList (); 1809 java.util.ArrayList subsums = new java.util.ArrayList (); 1810 java.util.ArrayList bindspr = new java.util.ArrayList (); 1811 try { 1812 binds = arch.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Bind)); 1813 } catch (TIRExceptLock e) { 1814 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 1815 } 1816 1817 if (binds != null && binds.length != 0) { 1819 out.iwriteln("try {"); 1820 out.incIndent(); 1821 out.iwriteln("SOFA.Connector.Reference pref;"); 1822 for (int i=0; i<binds.length; i++) { 1823 BindDef bind = (BindDef) binds[i]; 1824 switch (bind.mode().value()) { 1825 case BindMode.DELEGATE: 1826 delegs.add(bind); 1827 break; 1828 case BindMode.SUBSUME: 1829 subsums.add(bind); 1830 break; 1831 case BindMode.BIND: 1832 if (bind.lhsop().get_bo_kind().value() == BindOperKind.bok_provreq) { 1833 bindspr.add(bind); 1834 } else { 1835 BindOperSub lhs = (BindOperSub) bind.lhsop(); 1836 BindOperSub rhs = (BindOperSub) bind.rhsop(); 1837 BindType[] lelems = lhs.elements(); 1838 BindType[] lsubs = lhs.subs(); 1839 BindType[] relems = rhs.elements(); 1840 BindType[] rsubs = rhs.subs(); 1841 String lcompname; 1842 String rcompname; 1843 boolean containsArray = false; 1844 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1845 lcompname = ((BindTypeNormal) lsubs[0]).name(); 1846 } else { 1847 lcompname = ((BindTypeArray) lsubs[0]).name(); 1848 containsArray = true; 1849 } 1850 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1851 rcompname = ((BindTypeNormal) rsubs[0]).name(); 1852 } else { 1853 rcompname = ((BindTypeArray) rsubs[0]).name(); 1854 containsArray = true; 1855 } 1856 String lelemname; 1857 String relemname; 1858 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1859 lelemname = ((BindTypeNormal) lelems[0]).name(); 1860 } else { 1861 lelemname = ((BindTypeArray) lelems[0]).name(); 1862 containsArray = true; 1863 } 1864 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1865 relemname = ((BindTypeNormal) relems[0]).name(); 1866 } else { 1867 relemname = ((BindTypeArray) relems[0]).name(); 1868 containsArray = true; 1869 } 1870 if (containsArray) { 1871 out.iwriteln(Res.res.getString("MSG_ProvideArray")); 1872 } 1873 out.iwriteln("pref = "+rcompname+"cm.getProvisionReference(\""+relemname+"\");"); 1874 out.iwriteln(lcompname+"cm.setRequirement(\""+lelemname+"\", pref);"); 1875 } 1876 break; 1877 } 1878 } 1879 out.decIndent(); 1880 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1881 out.incIndent(); 1882 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantBindSubcomponents")+"\", e);"); 1883 out.decIndent(); 1884 out.iwriteln("}"); 1885 } 1886 1887 if (delegs.size() != 0) { 1889 out.iwriteln("try {"); 1890 out.incIndent(); 1891 out.iwriteln("SOFA.Connector.Reference pref;"); 1892 for (int i=0; i<delegs.size(); i++) { 1893 BindDef bind = (BindDef) delegs.get(i); 1894 BindOperProvReq lhs = (BindOperProvReq) bind.lhsop(); 1895 BindOperSub rhs = (BindOperSub) bind.rhsop(); 1896 BindType[] lelems = lhs.elements(); 1897 BindType[] relems = rhs.elements(); 1898 BindType[] rsubs = rhs.subs(); 1899 String rcompname; 1900 boolean containsArray = false; 1901 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1902 rcompname = ((BindTypeNormal) rsubs[0]).name(); 1903 } else { 1904 rcompname = ((BindTypeArray) rsubs[0]).name(); 1905 containsArray = true; 1906 } 1907 String lelemname; 1908 String relemname; 1909 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1910 lelemname = ((BindTypeNormal) lelems[0]).name(); 1911 } else { 1912 lelemname = ((BindTypeArray) lelems[0]).name(); 1913 containsArray = true; 1914 } 1915 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1916 relemname = ((BindTypeNormal) relems[0]).name(); 1917 } else { 1918 relemname = ((BindTypeArray) relems[0]).name(); 1919 containsArray = true; 1920 } 1921 if (containsArray) { 1922 out.iwriteln(Res.res.getString("MSG_ProvideArray")); 1923 } 1924 out.iwriteln("pref = "+rcompname+"cm.getProvisionReference(\""+relemname+"\");"); 1925 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).delegateProvision(\""+lelemname+"\", pref);"); 1926 } 1927 out.decIndent(); 1928 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1929 out.incIndent(); 1930 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDelegateSubcomponents")+"\", e);"); 1931 out.decIndent(); 1932 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 1933 out.incIndent(); 1934 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDelegateSubcomponents")+"\", e);"); 1935 out.decIndent(); 1936 out.iwriteln("}"); 1937 } 1938 1939 if (subsums.size() != 0) { 1941 1943 out.iwriteln("try {"); 1944 out.incIndent(); 1945 out.iwriteln("SOFA.Connector.Reference pref;"); 1946 for (int i=0; i<subsums.size(); i++) { 1947 BindDef bind = (BindDef) subsums.get(i); 1948 BindOperSub lhs = (BindOperSub) bind.lhsop(); 1949 BindOperProvReq rhs = (BindOperProvReq) bind.rhsop(); 1950 BindType[] lelems = lhs.elements(); 1951 BindType[] lsubs = lhs.subs(); 1952 BindType[] relems = rhs.elements(); 1953 String lcompname; 1954 boolean containsArray = false; 1955 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1956 lcompname = ((BindTypeNormal) lsubs[0]).name(); 1957 } else { 1958 lcompname = ((BindTypeArray) lsubs[0]).name(); 1959 containsArray = true; 1960 } 1961 String lelemname; 1962 String relemname; 1963 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1964 lelemname = ((BindTypeNormal) lelems[0]).name(); 1965 } else { 1966 lelemname = ((BindTypeArray) lelems[0]).name(); 1967 containsArray = true; 1968 } 1969 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 1970 relemname = ((BindTypeNormal) relems[0]).name(); 1971 } else { 1972 relemname = ((BindTypeArray) relems[0]).name(); 1973 containsArray = true; 1974 } 1975 if (containsArray) { 1976 out.iwriteln(Res.res.getString("MSG_ProvideArray")); 1977 } 1978 out.iwriteln("pref = ((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).createSubsumableRequirement(\""+relemname+"\");"); 1979 out.iwriteln(lcompname+"cm.setRequirement(\""+lelemname+"\", pref);"); 1980 1981 } 1982 out.iwriteln(""); 1983 1990 out.decIndent(); 1991 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 1992 out.incIndent(); 1993 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantSubsumeSubcomponents")+"\", e);"); 1994 out.decIndent(); 1995 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 1996 out.incIndent(); 1997 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantSubsumeSubcomponents")+"\", e);"); 1998 out.decIndent(); 1999 out.iwriteln("}"); 2000 } 2001 2002 if (bindspr.size() != 0) { 2004 out.iwriteln("try {"); 2005 out.incIndent(); 2006 out.iwriteln("SOFA.Connector.Reference pref;"); 2007 for (int i=0; i<bindspr.size(); i++) { 2008 BindDef bind = (BindDef) bindspr.get(i); 2009 boolean containsArray = false; 2010 BindOperProvReq lhs = (BindOperProvReq) bind.lhsop(); 2011 BindOperProvReq rhs = (BindOperProvReq) bind.rhsop(); 2012 BindType[] lelems = lhs.elements(); 2013 BindType[] relems = rhs.elements(); 2014 String lelemname; 2015 String relemname; 2016 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 2017 lelemname = ((BindTypeNormal) lelems[0]).name(); 2018 } else { 2019 lelemname = ((BindTypeArray) lelems[0]).name(); 2020 containsArray = true; 2021 } 2022 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 2023 relemname = ((BindTypeNormal) relems[0]).name(); 2024 } else { 2025 relemname = ((BindTypeArray) relems[0]).name(); 2026 containsArray = true; 2027 } 2028 if (containsArray) { 2029 out.iwriteln(Res.res.getString("MSG_ProvideArray")); 2030 } 2031 out.iwriteln("pref = ((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).createSubsumableRequirement(\""+relemname+"\");"); 2032 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).delegateProvision(\""+lelemname+"\", pref);"); 2033 } 2034 out.decIndent(); 2035 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 2036 out.incIndent(); 2037 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDirectBindSubcomponents")+"\", e);"); 2038 out.decIndent(); 2039 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 2040 out.incIndent(); 2041 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDirectbindSubcomponents")+"\", e);"); 2042 out.decIndent(); 2043 out.iwriteln("}"); 2044 } 2045 2046 Contained[] provs = null; 2048 try { 2049 provs = arch.frame().contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Provides)); 2050 } catch (TIRExceptLock e) { 2051 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 2052 } 2053 for(int i=0; i<provs.length; i++) { 2054 String prName = provs[i].get_identification().name(); 2055 out.iwriteln("// "+Res.res.getString("MSG_BeginOfProvision")+" "+prName); 2056 if (isPrimitive) { 2057 out.iwriteln("try {"); 2058 out.incIndent(); 2059 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).reSetProvisionImpl(\""+prName+"\", impl);"); 2060 out.decIndent(); 2061 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 2062 out.incIndent(); 2063 out.iwriteln("try {"); 2064 out.incIndent(); 2065 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).setProvisionImpl(\""+prName+"\", impl);"); 2066 out.decIndent(); 2067 out.iwriteln("} catch (SOFA.Component.NamingException e1) {"); 2068 out.incIndent(); 2069 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantRegisterProvision")+"\", e1);"); 2070 out.decIndent(); 2071 out.iwriteln("} catch (SOFA.Component.InstantiationException e1) {"); 2072 out.incIndent(); 2073 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateProvision")+"\", e1);"); 2074 out.decIndent(); 2075 out.iwriteln("}"); 2076 out.decIndent(); 2077 out.iwriteln("}"); 2078 } else { out.iwriteln("try {"); 2080 out.incIndent(); 2081 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).createProvision(\""+prName+"\");"); 2082 out.decIndent(); 2083 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 2084 out.incIndent(); 2085 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantRegisterProvision")+"\", e);"); 2086 out.decIndent(); 2087 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 2088 out.incIndent(); 2089 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantCreateProvision")+"\", e);"); 2090 out.decIndent(); 2091 out.iwriteln("}"); 2092 } 2093 out.iwriteln("// "+Res.res.getString("MSG_EndOfProvision")+" "+prName); 2094 } 2095 2096 if ((delegs.size() + bindspr.size()) != 0) { 2097 out.iwriteln("try {"); 2098 out.incIndent(); 2099 out.iwriteln("((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).connectDelegated();"); 2100 out.decIndent(); 2101 out.iwriteln("} catch (SOFA.Component.NamingException e) {"); 2102 out.incIndent(); 2103 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDelegateSubcomponents")+"\", e);"); 2104 out.decIndent(); 2105 out.iwriteln("} catch (SOFA.Component.InstantiationException e) {"); 2106 out.incIndent(); 2107 out.iwriteln("throw new SOFA.Component.ComponentLifecycleException(\""+Res.res.getString("MSG_CantDelegateSubcomponents")+"\", e);"); 2108 out.decIndent(); 2109 out.iwriteln("}"); 2110 } 2111 2112 2113 } 2114 2115 private void generateBaseImpl(ArchitectureDef arch, CGFileWriter out, String provider, String name, String pkg) throws IOException , RemoteException , GenerException { 2116 Contained[] insts = null; 2117 boolean isPrimitive = false; 2118 FrameDef frame = arch.frame(); 2119 Contained[] provs = null; 2120 Contained[] reqs = null; 2121 String [] reqTypes = null; 2122 String [] reqNames = null; 2123 boolean isSystem = arch.is_system(); 2124 try { 2125 insts = arch.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Inst)); 2126 if (insts.length == 0) 2127 isPrimitive = true; 2128 provs = frame.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Provides)); 2129 reqs = frame.contents(((Repository) arch.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Requires)); 2130 2131 2132 2133 out.iwriteln("package "+pkg+";"); 2134 out.iwriteln(); 2135 printHeadComment(Res.res.getString("MSG_BaseComponentImplementation")+" "+Res.res.getString("MSG_For")+" "+contained2string(arch),out); 2136 out.iwrite("abstract public class _Base"+name+"Impl "); 2137 if (provs.length != 0) { 2138 out.iwrite("implements "); 2139 for (int i=0; i<provs.length; i++) { 2140 InterfaceDef iface = SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.getIfaceOfProvides((ProvideDef) provs[i]); 2141 GeneratedObject go = searchObject(GeneratedObject.createKey(iface)); 2142 out.iwrite(go.genReference(GeneratedObject.INREF)); 2143 if (i<provs.length-1) 2144 out.iwrite(", "); 2145 } 2146 } 2147 out.iwriteln("{"); 2148 out.incIndent(); 2149 out.iwriteln("private SOFA.Component.DCUP.DCUPComponentManagerImpl cm;"); 2150 if (reqs.length!=0) { 2151 reqTypes = new String [reqs.length]; 2152 reqNames = new String [reqs.length]; 2153 for (int i=0; i<reqs.length; i++) { 2154 InterfaceDef iface = SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.getIfaceOfRequires((RequireDef) reqs[i]); 2155 GeneratedObject go = searchObject(GeneratedObject.createKey(iface)); 2156 reqTypes[i] = go.genReference(GeneratedObject.INREF); 2157 reqNames[i] = ((RequireDef)reqs[i]).get_identification().name(); 2158 out.iwriteln("protected "+reqTypes[i]+" "+reqNames[i]+"Requirement;"); 2159 } 2160 } 2161 out.iwriteln(); 2162 out.iwriteln("public _Base"+name+"Impl(SOFA.Component.DCUP.DCUPComponentManagerImpl _cm) {"); 2163 out.iwriteln(" cm = _cm;"); 2164 out.iwriteln("}"); 2165 out.iwriteln(); 2166 out.iwriteln("public boolean isUpdatable() {"); 2167 out.iwriteln(" return false;"); 2168 out.iwriteln("}"); 2169 out.iwriteln(); 2170 if (reqs.length != 0) { 2171 out.iwriteln("public void setRequirements() throws SOFA.Component.NamingException {"); 2172 out.incIndent(); 2173 for (int i=0; i<reqs.length; i++) { 2174 out.iwriteln(reqNames[i]+"Requirement = ("+reqTypes[i]+") cm.getRequirement(\""+reqNames[i]+"\");"); 2175 } 2176 out.decIndent(); 2177 out.iwriteln("}"); 2178 } 2179 out.iwriteln(); 2180 out.iwriteln("final public void bStart() throws SOFA.Component.ComponentLifecycleException {"); 2181 if (reqs.length != 0) { 2182 out.iwriteln(" try {"); 2183 out.iwriteln(" setRequirements();"); 2184 out.iwriteln(" } catch (SOFA.Component.NamingException e) {"); 2185 out.iwriteln(" throw new SOFA.Component.ComponentLifecycleException(e.getMessage(), e);"); 2186 out.iwriteln(" }"); 2187 } 2188 out.iwriteln(" start();"); 2189 out.iwriteln("}"); 2190 out.iwriteln(); 2191 out.iwriteln("final public void bStop() throws SOFA.Component.ComponentLifecycleException {"); 2192 out.iwriteln(" stop();"); 2193 out.iwriteln("}"); 2194 out.iwriteln(); 2195 out.iwriteln("final public void bStore() throws SOFA.Component.ComponentLifecycleException {"); 2196 out.iwriteln(" store();"); 2197 out.iwriteln("}"); 2198 out.iwriteln(); 2199 out.iwriteln("final public void bRestore() throws SOFA.Component.ComponentLifecycleException {"); 2200 out.iwriteln(" restore();"); 2201 out.iwriteln("}"); 2202 out.iwriteln(); 2203 out.iwriteln("abstract public void start() throws SOFA.Component.ComponentLifecycleException;"); 2204 out.iwriteln("abstract public void stop() throws SOFA.Component.ComponentLifecycleException;"); 2205 out.iwriteln("abstract public void store() throws SOFA.Component.ComponentLifecycleException;"); 2206 out.iwriteln("abstract public void restore() throws SOFA.Component.ComponentLifecycleException;"); 2207 out.iwriteln(); 2208 if (provs.length != 0) { 2209 out.iwriteln("final public java.lang.String getFcItfName() { return \"\"; }"); 2210 out.iwriteln(); 2211 out.iwriteln("final public org.objectweb.fractal.api.Type getFcItfType() { return new SOFA.Util.FcTypeImpl(); }"); 2212 out.iwriteln(); 2213 out.iwriteln("final public boolean isFcInternalItf() { return false; }"); 2214 out.iwriteln(); 2215 out.iwriteln("final public org.objectweb.fractal.api.Component getFcItfOwner() { return cm; }"); 2216 out.iwriteln(); 2217 } 2218 out.decIndent(); 2219 out.iwriteln("}"); 2220 } catch (TIRExceptLock e) { 2221 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 2222 } 2223 } 2224 2225 private void generateRealImplSkel(ArchitectureDef arch, CGFileWriter out, String provider, String name, String pkg) throws IOException , RemoteException , GenerException { 2226 out.iwriteln("package "+pkg+";"); 2227 out.iwriteln(); 2228 printHeadComment(Res.res.getString("MSG_Implementation")+" "+Res.res.getString("MSG_For")+" "+contained2string(arch),out); 2229 out.iwriteln("public class "+name+"Impl extends _Base"+name+"Impl {"); 2230 out.incIndent(); 2231 out.iwriteln("public "+name+"Impl(SOFA.Component.DCUP.DCUPComponentManagerImpl cm) {"); 2232 out.iwriteln(" super(cm);"); 2233 out.iwriteln("}"); 2234 out.iwriteln(); 2235 out.iwriteln("public void start() throws SOFA.Component.ComponentLifecycleException {"); 2236 out.iwriteln(""); 2237 out.iwriteln("}"); 2238 out.iwriteln(); 2239 out.iwriteln("public void stop() throws SOFA.Component.ComponentLifecycleException {"); 2240 out.iwriteln(""); 2241 out.iwriteln("}"); 2242 out.iwriteln(); 2243 out.iwriteln("public void store() throws SOFA.Component.ComponentLifecycleException {"); 2244 out.iwriteln(""); 2245 out.iwriteln("}"); 2246 out.iwriteln(); 2247 out.iwriteln("public void restore() throws SOFA.Component.ComponentLifecycleException {"); 2248 out.iwriteln(""); 2249 out.iwriteln("}"); 2250 out.iwriteln(); 2251 out.decIndent(); 2252 out.iwriteln("}"); 2253 } 2254 2255 static class InstInfo { 2256 String name; 2257 FrameDef frame; 2258 java.util.ArrayList arrays; 2259 public InstInfo(String n, FrameDef f, java.util.ArrayList a) { 2260 name = n; 2261 frame = f; 2262 arrays = a; 2263 } 2264 2265 public static InstInfo getInstInfo(Contained inst) throws RemoteException { 2266 int num = 0; 2267 java.util.ArrayList arrays = new java.util.ArrayList (); 2268 CDLType obj = ((InstDef) inst).type(); 2269 int defKind = obj.get_def_kind().value(); 2270 while (defKind == DefinitionKind.dk_Typedef || defKind == DefinitionKind.dk_Array) { 2271 if (defKind == DefinitionKind.dk_Typedef) 2272 obj = ((TypedefDef) obj).original_type(); 2273 else { 2274 arrays.add(((ArrayDef) obj).length()); 2275 obj = ((ArrayDef) obj).element_type(); 2276 } 2277 defKind = obj.get_def_kind().value(); 2278 } 2279 FrameDef realType = (FrameDef) obj; 2280 return new InstInfo(inst.get_identification().name(), realType, arrays); 2281 } 2282 2283 public static InstInfo findInstInfo(java.util.ArrayList infos, String name) { 2284 for (int i=0; i<infos.size(); i++) { 2285 InstInfo f = (InstInfo) infos.get(i); 2286 if ( f.name.compareTo(name)==0) 2287 return f; 2288 } 2289 return null; 2290 } 2291 } 2292 2293 private static String baseTypeToParseMethod(String type, String from) throws GenerException { 2294 if (type.compareTo("int")==0) 2295 return "Integer.parseInt("+from+")"; 2296 if (type.compareTo("long")==0) 2297 return "Long.parseLong("+from+")"; 2298 if (type.compareTo("byte")==0) 2299 return "Byte.parseByte("+from+")"; 2300 if (type.compareTo("double")==0) 2301 return "Double.parseDouble("+from+")"; 2302 if (type.compareTo("boolean")==0) 2303 return "Boolean.valueOf("+from+")"; 2304 if (type.compareTo("short")==0) 2305 return "Short.parseShort("+from+")"; 2306 if (type.compareTo("String")==0) 2307 return from; 2308 throw new GenerException(Res.res.getString("MSG_UnsupportedPropertyType")+type); 2309 } 2310 2311 2312 private static String generateInstName(String name, String indexPrefix, int number) { 2313 StringBuffer sb = new StringBuffer ("\""+name+"\""); 2314 for (int i=0; i<number; i++) { 2315 sb.append("+\"_\"+"); 2316 sb.append("Integer.toString("+indexPrefix+Integer.toString(i)+")"); 2317 } 2318 return sb.toString(); 2319 } 2320 2321 private void generateComponentInfoFile(ArchitectureDef obj, SOFA.SOFAnode.Made.TIR.AbsoluteName absN, String name, String builderName) throws RemoteException , GenerException, IOException { 2322 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 2323 Document document = null; 2324 try { 2325 DocumentBuilder builder = factory.newDocumentBuilder(); 2326 document = builder.newDocument(); 2327 Element root = null; 2328 if (obj.is_system()) 2329 root = (Element ) document.createElement("sofa_system"); 2330 else 2331 root = (Element ) document.createElement("sofa_component"); 2332 document.appendChild(root); 2333 Element el; 2334 Text tx; 2335 2336 el = document.createElement("architecture_ref"); 2337 tx = document.createTextNode(contained2string(obj)); 2338 el.appendChild(tx); 2339 root.appendChild(el); 2340 2341 el = document.createElement("frame_ref"); 2342 tx = document.createTextNode(contained2string(obj.frame())); 2343 el.appendChild(tx); 2344 root.appendChild(el); 2345 2346 el = document.createElement("version"); 2347 tx = document.createTextNode("!set.version.here!"); 2348 el.appendChild(tx); 2349 root.appendChild(el); 2350 2351 Element entit = (Element ) document.createElement("cdl_entities"); 2352 for (int i=0; i<genFiles.size(); i++) { 2353 GenFile go = genFiles.getObject(i); 2354 el = document.createElement("entity"); 2355 el.setAttribute("cdlname", go.cdlName); 2356 el.setAttribute("javaname", go.genName); 2357 entit.appendChild(el); 2358 } 2359 root.appendChild(entit); 2360 2361 el = document.createElement("implementation"); 2362 el.setAttribute("type", "builder"); 2363 tx = document.createTextNode(builderName); 2364 el.appendChild(tx); 2365 root.appendChild(el); 2366 2367 try { 2368 Contained[] cont = obj.contents(((Repository)obj.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Property)); 2369 if (cont!=null && cont.length!=0) { 2370 for (int i=0; i<cont.length; i++) { 2371 el = document.createElement("property"); 2372 el.setAttribute("name",cont[i].get_identification().name()); 2373 el.setAttribute("value","!set.property.value.here!"); 2374 root.appendChild(el); 2375 } 2376 } 2377 2378 cont = obj.contents(((Repository)obj.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Inst)); 2379 boolean isPrimitive = true; 2380 if (cont!=null && cont.length!=0) { 2381 isPrimitive = false; 2382 for (int i=0; i<cont.length; i++) { 2383 el = document.createElement("component_ref"); 2384 el.setAttribute("inst",cont[i].get_identification().name()); 2385 el.setAttribute("arch","!set.architecture.name.here!"); 2386 el.setAttribute("version","!set.implementation.version.here!"); 2387 root.appendChild(el); 2388 } 2389 } 2390 2391 cont = obj.frame().contents(((Repository)obj.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Provides)); 2392 if (cont!=null && cont.length!=0) { 2393 for (int i=0; i<cont.length;i++) { 2394 InterfaceDef iface = SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.getIfaceOfProvides((ProvideDef) cont[i]); 2395 String connName = cont[i].get_identification().name(); 2396 if (SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.containProvArrays((ProvideDef) cont[i])) 2397 connName = connName+"*"; 2398 el = document.createElement("connector"); 2399 el.setAttribute("name", connName); 2400 2402 el.setAttribute("requires", contained2string(iface)); 2403 root.appendChild(el); 2404 2412 2413 if (!isPrimitive) { 2414 el = document.createElement("connector"); 2415 el.setAttribute("name", "_delegate_"+connName); 2416 2418 el.setAttribute("provides", contained2string(iface)); 2419 root.appendChild(el); 2420 2428 } 2429 } 2430 } 2431 2432 cont = obj.frame().contents(((Repository)obj.get_containing_repository()).get_spec_def_kind(DefinitionKind.dk_Requires)); 2433 if (cont!=null && cont.length!=0) { 2434 for (int i=0; i<cont.length;i++) { 2435 InterfaceDef iface = SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.getIfaceOfRequires((RequireDef) cont[i]); 2436 String connName = cont[i].get_identification().name(); 2437 if (SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods.containReqArrays((RequireDef) cont[i])) 2438 connName = connName+"*"; 2439 el = document.createElement("connector"); 2440 el.setAttribute("name", connName); 2441 2443 el.setAttribute("provides", contained2string(iface)); 2444 root.appendChild(el); 2445 2453 2454 if (!isPrimitive) { 2455 el = document.createElement("connector"); 2456 el.setAttribute("name", "_subsume_"+connName); 2457 2459 el.setAttribute("requires", contained2string(iface)); 2460 root.appendChild(el); 2461 2469 } 2470 } 2471 } 2472 } catch (TIRExceptLock e) { 2473 throw new GenerException(Res.res.getString("MSG_LockedTIR")); 2474 } 2475 2476 TransformerFactory tFactory = TransformerFactory.newInstance(); 2477 Transformer transformer = tFactory.newTransformer(); 2478 transformer.setOutputProperty("indent", "yes"); 2480 DOMSource source = new DOMSource (document); 2481 CGFileWriter out = fs.newCGWriter(name+"CDD.xml"); 2482 StreamResult result = new StreamResult (out); 2483 transformer.transform(source, result); 2484 out.close(); 2485 } catch (ParserConfigurationException e) { 2486 throw new GenerException(e.getMessage(), e); 2487 } catch (TransformerConfigurationException e) { 2488 throw new GenerException(e.getMessage(), e); 2489 } catch (TransformerException e) { 2490 throw new GenerException(e.getMessage(), e); 2491 } 2492 } 2493} 2494 | Popular Tags |