KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > generator > metainformation > lib > IDL_JavaMIGenerator


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Hameau Fabien.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.corba.generator.metainformation.lib;
27
28
29 //Package dependencies.
30

31 import org.objectweb.openccm.ast.api.*;
32 import org.objectweb.openccm.generator.java.core.lib.*;
33 import org.objectweb.openccm.generator.java.ast.api.*;
34 import org.objectweb.openccm.generator.java.ast.lib.*;
35 import org.objectweb.openccm.generator.translator.idl2java.lib.CommonTranslator;
36 import org.objectweb.openccm.generator.common.lib.FileManager;
37 import org.objectweb.openccm.generator.common.lib.GenerationException;
38
39 /**
40  * This generator takes a IDL3 declaration from
41  * the repository and generate appriopriate files
42  * to support MetaInformation implementation.
43  *
44  * MI is an implementation of D2.1 specification
45  * (COACH) about static and dynamic CCM concept
46  * information handling.
47  *
48  * @author <a HREF="mailto:hameau@lifl.fr">Hameau</a>
49  *
50  * @version 0.1
51  */

52
53 public class IDL_JavaMIGenerator
54 extends org.objectweb.openccm.generator.common.lib.GeneratorBase
55 implements org.objectweb.corba.generator.metainformation.api.IDL_JavaMIGenerator
56 {
57   // ==================================================================
58
//
59
// Internal states.
60
//
61
// ==================================================================
62

63   /** Java Repository */
64   private Repository _repository;
65   private Scope CCMObj_scope;
66   /** utility to convert types in Java */
67   public CommonTranslator _translator;
68   private AST _ast;
69   private String JavaDoc _scope_name;
70
71   /**
72    *
73    */

74   
75   // ==================================================================
76
//
77
// Constructors.
78
//
79
// ==================================================================
80

81   /**
82    * This constructor initialize the IDL3->MI compiler
83    * with
84    * - the concerned AST
85    * - the creation of a new 'JavaTranslator' to generate
86    * java source code, and retrieve the scope of
87    * - CCMObject to add some IDL3-IDL2 artefact mapping ( such
88    * as equivalent interface for home and component)...
89    *
90    * @param ast : the AST associated to the IDL3 declaration
91    */

92
93   public IDL_JavaMIGenerator(AST ast)
94   {
95     super(ast);
96     _repository = null;
97     _translator = new CommonTranslator();
98     CCMObj_scope = null;
99     try
100     {
101       CCMObj_scope = (Scope) ast.lookup("::Components::CCMObject");
102     }
103     catch (ClassCastException JavaDoc e)
104     {
105       System.err.println("Declaration of CCMObject not found!");
106     }
107     
108     _ast = ast;
109   }
110
111  
112   // ==================================================================
113
//
114
// Internal methods.
115
//
116
// ==================================================================
117

118   /**
119    * mapHomeMI
120    *
121    * generates the appropriate type MI class for a given home
122    * declaration. Map the home type MI in a class
123    *
124    * @param _home_decl - the home declaration
125    * @param pkg_name - the home package name
126    *
127    */

128   
129   private void mapHomeMI(HomeDecl _home_decl, String JavaDoc pkg_name)
130   {
131     int i;
132     
133     ClassObject clazz = null;
134     ConstructorObject _cstr = null;
135     
136     // naming the class
137

138     String JavaDoc home_mi_class_name = _home_decl.getName() + "_homeMI"; // name of the home MI class
139
clazz = new ClassObjectImpl(home_mi_class_name); // create its representation
140

141     // filling the class representation
142

143     // some comments on generated class
144
clazz.addComment("This class represents the home type MetaInformation for the given " + _home_decl.getName());
145     // class is public
146
clazz.setModifier(ModifierKindImpl.mk_public);
147     // set the package name for home MI class
148
clazz.setPackage(pkg_name);
149     // this class inherits from the home type MI implementation
150
clazz.addInheritedObject("org.objectweb.openccm.Containers.MetaInformation.HomeTypeImpl");
151     
152     clazz.addImport("org.objectweb.openccm.Containers.MetaInformation.*");
153     // add specific home declaration elements
154
// in the constructor
155

156     _cstr = new ConstructorObjectImpl();
157     _cstr.getImpl().setMacro("HOME_MI_CONSTRUCTOR"); // set velocity macro
158

159     // set type MI properties for the given home
160

161     _cstr.getImpl().addContextValue("name", _home_decl.getName()); // type name
162
_cstr.getImpl().addContextValue("rep_id", _home_decl.getId()); // type repository ID
163
_cstr.getImpl().addContextValue("type_instance_factory_entrypt",
164                                     pkg_name + "."+_home_decl.getName() + "CCM");
165                                      
166     //////////////////////
167
// managed components
168
//////////////////////
169

170     _cstr.getImpl().addContextValue("component_class_name",
171                                     _home_decl.getManagedComponent().getName());
172     // managed attributes
173

174     Declaration[] _home_atts = _home_decl.getContents(true,DeclarationKind.dk_attribute);
175     if(_home_atts.length != 0)
176     {
177       _cstr.getImpl().addContextValue("att_list", _home_atts);
178     }
179     
180     ///////////////////////
181
// supported interfaces
182
///////////////////////
183

184     // get the supported interface list
185
InterfaceDecl[] intfs = _home_decl.getSupportedInterfaceList().getInterfaces();
186     // put a macro to add the supported intf to the home
187
if (intfs.length != 0)
188     {
189       _cstr.getImpl().addContextValue("interface_list", intfs);
190       // set value for given
191
// check for imported interfaces
192
for (int j = 0; j < intfs.length; j++)
193       {
194         // declaration from another scope ?
195
if (!(intfs[j].getAbsoluteName().startsWith(_scope_name)))
196         {
197           // generate the associated file
198
Declaration _imported = _ast.lookup(intfs[j].getAbsoluteName());
199           mapInterfaceMI((InterfaceDecl) _imported, pkg_name);
200         }
201       }
202     }
203  
204     // The contructor is now well created, and can be added to the class
205
// definition
206

207     clazz.addConstructor(_cstr);
208     
209     // static entrypt for call creation
210

211     MethodObjectImpl _met = new MethodObjectImpl();
212     
213     _met.setName("create_type_MI");
214     _met.setReturnType("HomeType");
215     _met.setModifier(ModifierKindImpl.mk_public);
216     _met.setStatic(true);
217     _met.addComment("static entrypt for container and installation");
218  
219     _met.getImpl().setMacro("STATIC_CALL_HOME_MI");
220     _met.getImpl().addContextValue("classname",_home_decl.getName() + "_homeMI");
221
222     clazz.addMethod(_met);
223     // No more properties are set for the home type MI class, it can be
224
// added to the java repository for code generation
225

226     _repository.addObject(clazz);
227     
228     // The Home also has an equivalent interface !
229
// As a component can offer a receptacle that reference a home
230
// the creation of the home equivalent interface is delegated
231
// to an other class.
232

233     // naming the class
234

235     String JavaDoc home_equv_itf_mi_class_name = _home_decl.getName() + "_interfaceMI"; // name of the home MI class
236
clazz = new ClassObjectImpl(home_equv_itf_mi_class_name); // create its representation
237

238     // filling the class representation
239

240     // some comments on generated class
241
clazz.addComment("This class represents the home equivalent interface type MetaInformation ");
242     clazz.addComment("for the given " + _home_decl.getName());
243     // class is public
244
clazz.setModifier(ModifierKindImpl.mk_public);
245     // set the package name for home MI class
246
clazz.setPackage(pkg_name);
247     // this class inherits from the home type MI implementation
248
clazz.addInheritedObject("org.objectweb.openccm.Containers.MetaInformation.InterfaceTypeImpl");
249
250     clazz.addImport("org.objectweb.openccm.Containers.MetaInformation.*");
251     // add specific home declaration elements
252
// in the constructor
253

254     _cstr = new ConstructorObjectImpl();
255     _cstr.getImpl().setMacro("HOME_EQUIV_ITF"); // set velocity macro the given home equivalent interface
256
AttributeObject param1 = new AttributeObjectImpl();
257     param1.setName("type_ref");
258     param1.setType("TypeImpl");
259     _cstr.addParameter(param1);
260     AttributeObject param2 = new AttributeObjectImpl();
261     param2.setName("type_ki");
262     param2.setType("type_kind");
263     _cstr.addParameter(param2);
264   
265     // Fill the specific Home equivalent interface Type MI
266

267     _cstr.getImpl().addContextValue("name", "???"); // TODO : find the good name
268
_cstr.getImpl().addContextValue("rep_id", "???"); // TODO : find the good repository ID
269
_cstr.getImpl().addContextValue("type_instance_factory_entrypt",
270                                     "???"); // TODO : find the good entrypt
271

272     // add the specific operations to the interface
273

274     Declaration[] _equiv_itf_decl = _home_decl
275                                        .getClientMapping()
276                                        .getContents(false, DeclarationKind.dk_operation);
277         
278     _cstr.getImpl().addContextValue("opt_list",_equiv_itf_decl);
279       
280     clazz.addConstructor(_cstr);
281
282     // the class is finished and ready to be generated
283

284     _repository.addObject(clazz);
285   };
286   
287   /**
288    * mapComponentMI
289    *
290    * generates the appropriate type MI class for a given
291    * component declaration. Map the componnet type MI
292    * in a class
293    *
294    * @param _component_decl - the home declaration
295    * @param pkg_name - the home package name
296    *
297    */

298     
299   private void mapComponentMI(ComponentDecl decl, String JavaDoc pkg_name)
300   {
301     ClassObject clazz = null;
302     ConstructorObject ct = null;
303     
304     // retrieve the name and create the class obj
305

306     String JavaDoc class_name = new String JavaDoc(decl.getName() + "_componentMI");
307     clazz = new ClassObjectImpl(class_name);
308     
309     // filling the class representation
310

311     clazz.addImport("org.objectweb.openccm.Containers.MetaInformation.*");
312     
313     clazz.addComment("This class is the component type MetaInformation");
314     clazz.addComment("for the given component. It gathers static properties");
315     clazz.addComment("of CCM concepts managed by the related component");
316     clazz.setModifier(ModifierKindImpl.mk_public); // class is public
317
clazz.setPackage(pkg_name); // set the package name
318
clazz.addInheritedObject("org.objectweb.openccm.Containers.MetaInformation.ComponentTypeImpl");
319     
320     // add the specific code for managed CCM concept for
321
// a given component, from its IDL3 declaration
322

323     ct = new ConstructorObjectImpl();
324     ct.getImpl().setMacro("COMPONENT_MI_CONSTRUCTOR"); // set velocity macro
325
AttributeObject param = new AttributeObjectImpl();
326     param.setName("home_type");
327     param.setType("HomeTypeImpl");
328     ct.addParameter(param);
329     
330     // for static component value
331

332     ct.getImpl().addContextValue("name", decl.getName());
333     ct.getImpl().addContextValue("rep_id", decl.getId());
334     ct.getImpl().addContextValue("type_instance_factory_entrypt", "???");
335     
336     // about inherited interface
337
// concerns facet, receptacles event source and sink
338

339     ComponentDecl _tmp_comp = decl.getBaseComponent();
340     
341     ///////////////////////////
342
// for supported interfaces
343
///////////////////////////
344

345     // get supported interface(s) list
346
InterfaceDecl[] _intf_decl = decl.getSupportedInterfaceList().getInterfaces();
347     // put a macro adding this interface to the suported interfaces
348
if(_intf_decl.length != 0)
349     {
350       ct.getImpl().addContextValue("int_list", _intf_decl);
351     }
352     // check for imported interfaces
353
for (int i = 0; i < _intf_decl.length; i++)
354     {
355       // declaration from another scope ?
356
if(!(_intf_decl[i].getAbsoluteName().startsWith(_scope_name)))
357       {
358         // generate the associated file
359
Declaration _imported = _ast.lookup(_intf_decl[i].getAbsoluteName());
360         mapInterfaceMI((InterfaceDecl) _imported, pkg_name);
361       }
362     }
363     
364     ///////////////////////////
365
// for managed attribute(s)
366
///////////////////////////
367

368     Declaration[] _att_decl = decl.getContents(false,DeclarationKind.dk_attribute);
369     
370     if(_att_decl.length != 0)
371     {
372       ct.getImpl().addContextValue("att_list", _att_decl);
373     }
374     
375     /////////////////////
376
// facet port type MI
377
/////////////////////
378

379     // get primary eclaration
380

381     Declaration[] _facet_decl = decl.getContents(false, DeclarationKind.dk_provides);
382     
383     if (_facet_decl.length != 0)
384     {
385       ct.getImpl().addContextValue("facet_list", _facet_decl);
386     }
387     
388     // check for imported interface
389

390     ProvidesDecl _prov_decl = null;
391     Declaration[] _tmp;
392    
393     for (int i = 0; i < _facet_decl.length; i++)
394     {
395       // check the interface declaration
396
_prov_decl = (ProvidesDecl) _facet_decl[i];
397       // take the first dependency
398
_tmp = _prov_decl.getDependencies();
399       // search in the ast (normally should be in !!!!!
400
Declaration itf_decl = _ast.lookup(_tmp[0].getAbsoluteName());
401       // check if it is an import
402
if(!(itf_decl.getAbsoluteName().startsWith(_scope_name)) && (itf_decl != null))
403       {
404         mapInterfaceMI((InterfaceDecl) itf_decl, pkg_name);
405       }
406     }
407     
408     //////////////////////////
409
// receptacle port type MI
410
//////////////////////////
411

412     // get the primary declaration
413

414     Declaration[] _receptacle_decl = decl.getContents(false,DeclarationKind.dk_uses);
415   
416     if(_receptacle_decl.length != 0)
417     {
418       ct.getImpl().addContextValue("receptacle_list", _receptacle_decl);
419     }
420     
421     // check for imported interface
422

423     UsesDecl _uses_decl = null;
424     
425     for (int i = 0; i < _receptacle_decl.length; i++)
426     {
427       // check the interface declaration
428
_uses_decl = (UsesDecl) _receptacle_decl[i];
429       // take the first dependency
430
_tmp = _uses_decl.getDependencies();
431       // search in the ast (normally should be in !!!!!
432
Declaration itf_decl = _ast.lookup(_tmp[0].getAbsoluteName());
433       // check if it is an import
434
if (!(itf_decl.getAbsoluteName().startsWith(_scope_name))
435         && (itf_decl != null))
436       {
437         mapInterfaceMI((InterfaceDecl) itf_decl, pkg_name);
438       }
439     }
440     
441     // consumer port type MI
442

443     Declaration[] _consumer_decl = decl.getContents(false,DeclarationKind.dk_consumes);
444    
445     if(_consumer_decl.length != 0)
446     {
447       ct.getImpl().addContextValue("consumer_list", _consumer_decl);
448     }
449
450     // emitter port type MI
451

452     Declaration[] _emitter_decl = decl.getContents(false, DeclarationKind.dk_emits);
453    
454     if(_emitter_decl.length != 0)
455     {
456       ct.getImpl().addContextValue("emitter_list", _emitter_decl);
457     }
458     
459     // publisher port type MI
460

461     Declaration[] _publisher_decl = decl.getContents(false, DeclarationKind.dk_publishes);
462    
463     if(_publisher_decl.length != 0)
464     {
465       ct.getImpl().addContextValue("publisher_list", _publisher_decl);
466     }
467     
468     // constructor is created and ready to by generated
469

470     clazz.addConstructor(ct);
471     _repository.addObject(clazz);
472     
473     
474     // a component also has an equivalent interface !
475
// As a component can offer a receptacle that reference an
476
// other component, home or interface.
477
// the creation of the home equivalent interface is delegated
478
// to an other class.
479

480     // naming the class
481

482     String JavaDoc comp_equv_itf_mi_class_name = decl.getName() + "_interfaceMI"; // name of the class
483
clazz = new ClassObjectImpl(comp_equv_itf_mi_class_name); // create its representation
484

485     // filling the class representation
486

487     // some comments on generated class
488
clazz.addComment("This class represents the component equivalent interface type MetaInformation ");
489     clazz.addComment("for the given " + decl.getName());
490     // class is public
491
clazz.setModifier(ModifierKindImpl.mk_public);
492     // set the package name for home MI class
493
clazz.setPackage(pkg_name);
494     // this class inherits from the home type MI implementation
495
clazz.addInheritedObject("org.objectweb.openccm.Containers.MetaInformation.InterfaceTypeImpl");
496
497     clazz.addImport("org.objectweb.openccm.Containers.MetaInformation.*");
498     // add specific home declaration elements
499
// in the constructor
500

501     ct = new ConstructorObjectImpl();
502     ct.getImpl().setMacro("COMP_EQUIV_ITF"); // set velocity macro the given component equivalent interface
503
AttributeObject param1 = new AttributeObjectImpl();
504     param1.setName("type_ref");
505     param1.setType("TypeImpl");
506     ct.addParameter(param1);
507     AttributeObject param2 = new AttributeObjectImpl();
508     param2.setName("type_ki");
509     param2.setType("type_kind");
510     ct.addParameter(param2);
511   
512     // Fill the specific Component equivalent interface Type MI
513

514     ct.getImpl().addContextValue("name", "???"); // TODO : find the good name
515
ct.getImpl().addContextValue("rep_id", "???"); // TODO : find the good repository ID
516
ct.getImpl().addContextValue("type_instance_factory_entrypt",
517                                     "???"); // TODO : find the good entrypt
518

519     // add the specific operations to the interface
520

521     Declaration[] _equiv_itf_decl = CCMObj_scope.getContents(false, DeclarationKind.dk_operation);
522         
523     ct.getImpl().addContextValue("opt_list",_equiv_itf_decl);
524       
525     clazz.addConstructor(ct);
526
527     // the class is finished and ready to be generated
528

529     _repository.addObject(clazz);
530     
531   };
532   
533   /**
534    * mapInterfaceMI
535    *
536    * generates the appropriate type MI class for a given
537    * interface declaration. Map the componnet type MI
538    * in a class
539    *
540    * @param _decl - the interface declaration
541    * @param pkg_name - the interface package name
542    *
543    */

544   
545   private void mapInterfaceMI(InterfaceDecl decl, String JavaDoc pkg_name)
546   {
547     ClassObject clazz = null;
548     ConstructorObject ct = null;
549     
550     // retrieve the name and create the class obj
551

552     String JavaDoc class_name = new String JavaDoc(decl.getName() + "_interfaceMI");
553     clazz = new ClassObjectImpl(class_name);
554
555     // filling the class representation
556
clazz.addImport("org.objectweb.openccm.Containers.MetaInformation.*");
557     clazz.addComment("This class is the interface type MetaInformation");
558     clazz.addComment("for the given interface. It gathers static properties");
559     clazz.addComment("of CCM concepts managed by the related interface");
560     clazz.setModifier(ModifierKindImpl.mk_public); // class is public
561
clazz.setPackage(pkg_name); // set the package name
562
clazz.addInheritedObject("org.objectweb.openccm.Containers.MetaInformation.InterfaceTypeImpl");
563
564     // add the specific code for managed CCM concept for
565
// a given component, from its IDL declaration
566

567     ct = new ConstructorObjectImpl();
568     ct.getImpl().setMacro("INTERFACE_MI_CONSTRUCTOR"); // set velocity macro
569
AttributeObject param1 = new AttributeObjectImpl();
570     param1.setName("type_ref");
571     param1.setType("TypeImpl");
572     ct.addParameter(param1);
573     AttributeObject param2 = new AttributeObjectImpl();
574     param2.setName("type_ki");
575     param2.setType("type_kind");
576     ct.addParameter(param2);
577     
578     // specific interface type MetaInformation
579

580     ct.getImpl().addContextValue("name", decl.getName());
581     ct.getImpl().addContextValue("rep_id",decl.getId());
582     ct.getImpl().addContextValue("type_instance_factory_entrypt",
583                                  "???");
584     // managed attributes
585

586     Declaration[] att_decl = decl.getContents(true, DeclarationKind.dk_attribute);
587     
588     if(att_decl.length != 0)
589     {
590       ct.getImpl().addContextValue("att_list", att_decl);
591     }
592     
593     // managed operation
594

595     Declaration[] opt_decl = decl.getContents(true, DeclarationKind.dk_operation);
596     
597     if(opt_decl.length != 0)
598     {
599       ct.getImpl().addContextValue("opt_list", opt_decl);
600     }
601     
602     clazz.addConstructor(ct);
603     _repository.addObject(clazz);
604   };
605   
606
607   // ==================================================================
608
//
609
// Public methods.
610
//
611
// ==================================================================
612

613   /**
614    * idl_to_java
615    *
616    * Generate java files for the MetaInformation ofType.
617    *
618    * @param base_dir - The base directory where files will be generated.
619    * @param scope - The scope to map.
620    *
621    */

622   
623   public void idl_to_java(String JavaDoc outputDir, Scope scope)
624   throws org.objectweb.openccm.generator.common.lib.GenerationException
625   {
626     // Local Variables
627
int i;
628     String JavaDoc package_name;
629     java.io.File JavaDoc out_dir = null;
630     _repository = new RepositoryImpl();
631  
632     Declaration[] declared_homes = null;
633     Declaration[] declared_components = null;
634     Declaration[] declared_interfaces = null;
635
636     // get a local reference to the scope
637
_scope_name = scope.getAbsoluteName();
638     
639     // Check and create the output directory
640
try{
641         out_dir = FileManager.mkdir(outputDir);
642         // TODO : OLD CODE out_dir = _translator.mkdir(outputDir);
643
}catch(GenerationException ex){
644         System.err.println(ex.getMessage());
645         return;
646     }
647     
648     // The directory is now created the treatment for
649
// declaration contained in the IDL can begin
650
// retrieve all pertinent declaration for type MI
651

652     declared_homes = scope.getContents(false, DeclarationKind.dk_home); // for homes
653
declared_components = scope.getContents(false, DeclarationKind.dk_component); // for components
654
declared_interfaces = scope.getContents(false, DeclarationKind.dk_interface);//getAllDeclarations(scope, DeclarationKind.dk_local_interface); // for interfaces
655

656     // The specific treatment could be performed on each
657
// declaration found in the IDL scope
658

659     // For each declared home, call the home MI mapping
660

661     for(i=0;i<declared_homes.length;i++)
662     {
663       // retrieve home declaration from the declaration array
664

665       HomeDecl _home_decl = (HomeDecl) declared_homes[i]; // retrieve the declaration
666
package_name = _translator.getPackage(_home_decl); // retrieve the package_name
667
mapHomeMI(_home_decl, package_name); // call the Home type MI mapping
668
}
669     
670     // for each declared component, call the component MI mapping
671

672     for(i=0;i<declared_components.length;i++)
673     {
674       ComponentDecl _comp_decl = (ComponentDecl) declared_components[i]; // retrieve the declaration
675
package_name = _translator.getPackage(_comp_decl); // retrieve the package_name
676
mapComponentMI(_comp_decl,package_name); // call the component MI mapping
677
}
678     
679     // for each declared interface, call the interface MI mapping
680

681     for(i=0;i<declared_interfaces.length;i++)
682     {
683       InterfaceDecl _intf_decls = (InterfaceDecl) declared_interfaces[i];
684       package_name = _translator.getPackage(_intf_decls);
685       mapInterfaceMI(_intf_decls, package_name);
686     }
687     
688     // mappings are done for type MI, the java files
689
// can be generated
690

691     org.objectweb.openccm.generator.java.core.lib.JavaGenerator java_gen = new JavaGenerator();
692     java.util.Vector JavaDoc v = new java.util.Vector JavaDoc();
693     v.add("org/objectweb/corba/generator/metainformation/metainformation.vm");
694     java_gen.initialize("metainformation", v);
695     java_gen.generate(_repository, outputDir);
696   }
697 }
698
Popular Tags