KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > CIDLCOPIGenerator


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@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): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33 // import of OpenCCM modules
34
import org.objectweb.corba.ast.api.*;
35 import org.objectweb.corba.ast.lib.*;
36 import org.objectweb.corba.generator.java.lib.*;
37 import org.objectweb.corba.generator.java.ast.api.*;
38 import org.objectweb.corba.generator.java.ast.lib.*;
39 import org.objectweb.corba.generator.common.lib.GenerationException;
40
41 /**
42  ** <p>Generator class for client-side and server-side interceptors.</p>
43  **/

44 public class CIDLCOPIGenerator
45 extends org.objectweb.corba.generator.common.lib.GeneratorBase
46 {
47     // generator
48
private ExtendedJavaTranslator _translator;
49     private RepositoryImpl _repository;
50
51     // default constructor
52
public
53     CIDLCOPIGenerator(AST ast)
54     {
55         super(ast);
56
57         _translator = new ExtendedJavaTranslator();
58         _repository = null;
59     }
60
61     //
62
// internal operations
63
//
64

65     private ExtendedJavaTranslator
66     getTranslator()
67     {
68         return _translator;
69     }
70
71     private RepositoryImpl
72     getRepository()
73     {
74         return _repository;
75     }
76
77     // NOTE: not used currently, but will be in the future (maybe)
78
private void
79     generateForModule(ModuleDecl module)
80     {
81         Declaration[] decls = null;
82         // generate for all enclosed components
83
decls = module.getContents(false, DeclarationKind.dk_component);
84         for (int i=0;i<decls.length;i++) {
85             generateForComponent((ComponentDecl)decls[i]);
86         }
87
88         // generate for all enclosed homes
89
decls = module.getContents(false, DeclarationKind.dk_home);
90         for (int i=0;i<decls.length;i++) {
91             generateForHome((HomeDecl)decls[i]);
92         }
93
94         // recursively generate for all enclosed modules
95
decls = module.getContents(false, DeclarationKind.dk_module);
96         for (int i=0;i<decls.length;i++) {
97             generateForModule((ModuleDecl)decls[i]);
98         }
99     }
100
101     private void
102     generateForHome(HomeDecl home)
103     {
104         // generate skeleton interceptor for equiv itf
105
// generateSkI(home.getClientMapping(), home);
106

107         // interceptor factory
108
generateIFactory(home);
109     }
110
111     private void
112     generateForComponent(ComponentDecl comp)
113     {
114         // NOTE: skeleton interceptors are generated by segments, not by facet/sink
115
// As CIDL is currently not supported, we assume a monolithic component
116
// (i.e. all facets/sinks in the main segment)
117
Declaration[] facets = comp.getContents(false, DeclarationKind.dk_provides);
118         Declaration[] sinks = comp.getContents(false, DeclarationKind.dk_consumes);
119         Declaration[] mainseg = new Declaration[facets.length+sinks.length+1];
120
121         mainseg[0] = comp;
122         System.arraycopy(facets, 0, mainseg, 1, facets.length);
123         System.arraycopy(sinks, 0, mainseg, facets.length+1, sinks.length);
124         generateForSegment(org.omg.Components.COMPONENT_ID.value, comp, mainseg);
125
126         // receptacle
127
Declaration[] decls = null;
128         decls = comp.getContents(false, DeclarationKind.dk_uses);
129         for (int i=0;i<decls.length;i++) {
130             // generate stub interceptor for used itf
131
UsesDecl rec = (UsesDecl)decls[i];
132             InterfaceDecl itf = rec.getInterface();
133             // check if the used interface is a home interface
134
// in that case, use the equivalent interface
135
if (itf.getDeclKind()==DeclarationKind.dk_home) {
136                 HomeDecl home = (HomeDecl)itf;
137                 itf = home.getClientMapping();
138             }
139
140             generateStubI(itf, rec);
141         }
142
143         // emitter
144
decls = comp.getContents(false, DeclarationKind.dk_emits);
145         for (int i=0;i<decls.length;i++) {
146             // generate stub interceptor for consumer itf
147
EmitsDecl emitter = (EmitsDecl)decls[i];
148             generateStubI(emitter.getEvent().getClientMapping(), emitter);
149         }
150
151         // publisher
152
decls = comp.getContents(false, DeclarationKind.dk_publishes);
153         for (int i=0;i<decls.length;i++) {
154             // generate stub interceptor for consumer itf
155
PublishesDecl publisher = (PublishesDecl)decls[i];
156             generateStubI(publisher.getEvent().getClientMapping(), publisher);
157         }
158     }
159
160     private void
161     generateIFactory(HomeDecl home)
162     {
163         ComponentDecl comp = home.getManagedComponent();
164         String JavaDoc classname = home.getName()+"_IFact";
165         ClassObject ski = new ClassObjectImpl(classname);
166
167         // header
168
ski.setPackage(getTranslator().getPackage(comp));
169         ski.addImport("org.objectweb.corba.runtime.*");
170         ski.addImport("org.objectweb.ccm.runtime.*");
171
172         // class decl
173
// * public class $classname
174
// implements CallInterceptorFactory
175
ski.addComment(" ");
176         ski.addComment("Java interceptor factory for component "+comp.getName());
177         ski.addComment(" ");
178         ski.addComment("NOTE: generated class; DO NOT EDIT !");
179         ski.addComment(" ");
180         ski.setModifier(ModifierKindImpl.mk_public);
181         ski.addImplementedObject("CallInterceptorFactory");
182
183         // internal state
184
AttributeObject attr = null;
185         // * static private _class_name = "<class_name>";
186
attr = new AttributeObjectImpl();
187         attr.addComment("Class name of this class (used for debugging).");
188         attr.setStatic(true);
189         attr.setModifier(ModifierKindImpl.mk_private);
190         attr.setType("String");
191         attr.setName("_class_name");
192         attr.setInitialValue("\""+classname+"\"");
193         ski.addAttribute(attr);
194
195         // * private ORBService _orb_service;
196
attr = new AttributeObjectImpl();
197         attr.addComment(".");
198         attr.setModifier(ModifierKindImpl.mk_private);
199         attr.setType("ORBService");
200         attr.setName("_orb_service");
201         ski.addAttribute(attr);
202
203         // * private ServicesSet _services_set;
204
attr = new AttributeObjectImpl();
205         attr.addComment(".");
206         attr.setModifier(ModifierKindImpl.mk_private);
207         attr.setType("ServicesSet");
208         attr.setName("_services_set");
209         ski.addAttribute(attr);
210
211         // * private String _uuid;
212
attr = new AttributeObjectImpl();
213         attr.addComment(".");
214         attr.setModifier(ModifierKindImpl.mk_private);
215         attr.setType("String");
216         attr.setName("_uuid");
217         ski.addAttribute(attr);
218
219         // default constructor
220
// * protected $classname()
221
ConstructorObject constructor = new ConstructorObjectImpl();
222         constructor.getImpl().setMacro("IFACT_CONSTRUCTOR_BODY");
223         ski.addConstructor(constructor);
224
225         // entrypoint
226
// * static public CallInterceptorFactory create_factory()
227
MethodObject operation = new MethodObjectImpl();
228         operation.addComment(".");
229         operation.setStatic(true);
230         operation.setModifier(ModifierKindImpl.mk_public);
231         operation.setReturnType("CallInterceptorFactory");
232         operation.setName("create_factory");
233         operation.getImpl().setMacro("IFACT_CREATEFACTORY_BODY");
234         operation.getImpl().addContextValue("classname", classname);
235         ski.addMethod(operation);
236
237         // public operation
238
// * final public void setContextInfo(ORBService orbs, ServicesSet sset, String uuid)
239
operation = new MethodObjectImpl();
240         operation.addComment(".");
241         operation.setFinal(true);
242         operation.setModifier(ModifierKindImpl.mk_public);
243         operation.setReturnType("void");
244         operation.setName("setContextInfo");
245         ParameterObject parameter = new ParameterObjectImpl();
246         parameter.setName("orbs");
247         parameter.setType("ORBService");
248         operation.addParameter(parameter);
249         parameter = new ParameterObjectImpl();
250         parameter.setName("sset");
251         parameter.setType("ServicesSet");
252         operation.addParameter(parameter);
253         parameter = new ParameterObjectImpl();
254         parameter.setName("uuid");
255         parameter.setType("String");
256         operation.addParameter(parameter);
257         operation.getImpl().setMacro("IFACT_SETCONTEXTINFO_BODY");
258         ski.addMethod(operation);
259
260         // * public org.omg.PortableServer.Servant
261
// createSkeletonInterceptor(short sid, org.omg.CORBA.Object executor)
262
operation = new MethodObjectImpl();
263         operation.addComment(".");
264         operation.setFinal(true);
265         operation.setModifier(ModifierKindImpl.mk_public);
266         operation.setReturnType("org.omg.PortableServer.Servant");
267         operation.setName("createSkeletonInterceptor");
268         parameter = new ParameterObjectImpl();
269         parameter.setName("sid");
270         parameter.setType("short");
271         operation.addParameter(parameter);
272         parameter = new ParameterObjectImpl();
273         parameter.setName("executor");
274         parameter.setType("org.omg.CORBA.Object");
275         operation.addParameter(parameter);
276         operation.getImpl().setMacro("IFACT_CREATESKINTERCEPTOR_BODY");
277         // NOTE: only manages monolithic at present (always return main seg interceptor)
278
String JavaDoc mainseg_classname = comp.getName()+"_MainSegSkI";
279         operation.getImpl().addContextValue("mainseg_classname", mainseg_classname);
280         ski.addMethod(operation);
281
282         // * public org.omg.CORBA.Object
283
// createStubInterceptor(String name, org.omg.CORBA.Object delegate)
284
operation = new MethodObjectImpl();
285         operation.addComment(".");
286         operation.setFinal(true);
287         operation.setModifier(ModifierKindImpl.mk_public);
288         operation.setReturnType("org.omg.CORBA.Object");
289         operation.setName("createStubInterceptor");
290         parameter = new ParameterObjectImpl();
291         parameter.setName("name");
292         parameter.setType("String");
293         operation.addParameter(parameter);
294         parameter = new ParameterObjectImpl();
295         parameter.setName("delegate");
296         parameter.setType("org.omg.CORBA.Object");
297         operation.addParameter(parameter);
298         operation.getImpl().setMacro("IFACT_CREATESTUBINTERCEPTOR_BODY");
299         Declaration[] receptacles = comp.getContents(false, DeclarationKind.dk_uses);
300         Declaration[] emitters = comp.getContents(false, DeclarationKind.dk_emits);
301         Declaration[] publishers = comp.getContents(false, DeclarationKind.dk_publishes);
302         Boolean JavaDoc has_receptacles = new Boolean JavaDoc((receptacles.length!=0));
303         Boolean JavaDoc has_emitters = new Boolean JavaDoc((emitters.length!=0));
304         Boolean JavaDoc has_publishers = new Boolean JavaDoc((publishers.length!=0));
305         operation.getImpl().addContextValue("receptacles", receptacles);
306         operation.getImpl().addContextValue("emitters", emitters);
307         operation.getImpl().addContextValue("publishers", publishers);
308         operation.getImpl().addContextValue("has_receptacles", has_receptacles);
309         operation.getImpl().addContextValue("has_emitters", has_emitters);
310         operation.getImpl().addContextValue("has_publishers", has_publishers);
311         ski.addMethod(operation);
312
313         // * public org.omg.CORBA.Object
314
// createUntypedStubInterceptor(String name, org.omg.CORBA.Object delegate)
315
operation = new MethodObjectImpl();
316         operation.addComment(".");
317         operation.setFinal(true);
318         operation.setModifier(ModifierKindImpl.mk_public);
319         operation.setReturnType("org.omg.CORBA.Object");
320         operation.setName("createUntypedStubInterceptor");
321         parameter = new ParameterObjectImpl();
322         parameter.setName("name");
323         parameter.setType("String");
324         operation.addParameter(parameter);
325         parameter = new ParameterObjectImpl();
326         parameter.setName("delegate");
327         parameter.setType("org.omg.CORBA.Object");
328         operation.addParameter(parameter);
329         operation.getImpl().setMacro("IFACT_CREATEUNTYPEDSTUBINTERCEPTOR_BODY");
330         operation.getImpl().addContextValue("emitters", emitters);
331         operation.getImpl().addContextValue("publishers", publishers);
332         operation.getImpl().addContextValue("has_emitters", has_emitters);
333         operation.getImpl().addContextValue("has_publishers", has_publishers);
334         ski.addMethod(operation);
335
336         // add to repository
337
getRepository().addObject(ski);
338     }
339
340     private void
341     generateForSegment(short segid, ComponentDecl comp, Declaration[] idl3decls)
342     {
343         String JavaDoc classname = "";
344         String JavaDoc ssegid = java.lang.Short.toString(segid);
345         if (segid==org.omg.Components.COMPONENT_ID.value) {
346             // main segment
347
classname = comp.getName()+"_MainSegSkI";
348         } else {
349             // other segment
350
classname = comp.getName()+"_Seg"+ssegid+"SkI";
351         }
352
353         ClassObject ski = new ClassObjectImpl(classname);
354
355         // header
356
ski.setPackage(getTranslator().getPackage(comp));
357         ski.addImport("org.objectweb.corba.runtime.*");
358         ski.addImport("org.objectweb.ccm.runtime.*");
359
360         // class decl
361
// * public class $classname
362
// extends $comp.getName()_MainSegPOA
363
// or
364
// * public class $classname
365
// extends $comp.getName()_Seg${segid}POA
366
ski.addComment(" ");
367         if (segid==org.omg.Components.COMPONENT_ID.value) {
368             ski.addComment("Java server-side interceptor for main segment of component "+comp.getName());
369         } else {
370             ski.addComment("Java server-side interceptor for segment "+ssegid+" of component "+comp.getName());
371         }
372         ski.addComment(" ");
373         ski.addComment("NOTE: generated class; DO NOT EDIT !");
374         ski.addComment(" ");
375         ski.setModifier(ModifierKindImpl.mk_public);
376         if (segid==org.omg.Components.COMPONENT_ID.value) {
377             ski.addInheritedObject(comp.getName()+"_MainSegPOA");
378         } else {
379             ski.addInheritedObject(comp.getName()+"_Seg"+ssegid+"POA");
380         }
381
382         // internal state
383
AttributeObject attr = null;
384         // * static private _class_name = "<class_name>";
385
attr = new AttributeObjectImpl();
386         attr.addComment("Class name of this class (used for debugging).");
387         attr.setStatic(true);
388         attr.setModifier(ModifierKindImpl.mk_private);
389         attr.setType("String");
390         attr.setName("_class_name");
391         attr.setInitialValue("\""+classname+"\"");
392         ski.addAttribute(attr);
393
394         // * private ORBService _orb_service;
395
attr = new AttributeObjectImpl();
396         attr.addComment(".");
397         attr.setModifier(ModifierKindImpl.mk_private);
398         attr.setType("ORBService");
399         attr.setName("_orb_service");
400         ski.addAttribute(attr);
401
402         // * private ServicesSet _services_set;
403
attr = new AttributeObjectImpl();
404         attr.addComment(".");
405         attr.setModifier(ModifierKindImpl.mk_private);
406         attr.setType("ServicesSet");
407         attr.setName("_services_set");
408         ski.addAttribute(attr);
409
410         // * private String _uuid;
411
attr = new AttributeObjectImpl();
412         attr.addComment(".");
413         attr.setModifier(ModifierKindImpl.mk_private);
414         attr.setType("String");
415         attr.setName("_uuid");
416         ski.addAttribute(attr);
417
418         // * private ServerCallContextImpl _call_ctx;
419
attr = new AttributeObjectImpl();
420         attr.addComment(".");
421         attr.setModifier(ModifierKindImpl.mk_private);
422         attr.setType("ServerCallContextImpl");
423         attr.setName("_call_ctx");
424         ski.addAttribute(attr);
425
426         // * private java.util.Hashtable _informations;
427
attr = new AttributeObjectImpl();
428         attr.addComment(".");
429         attr.setModifier(ModifierKindImpl.mk_private);
430         attr.setType("java.util.Hashtable");
431         attr.setName("_informations");
432         ski.addAttribute(attr);
433
434         // * private $comp.getName()_MainSegOperations _delegate;
435
// or
436
// * private $comp.getName()_Seg${segid}Operations _delegate;
437
attr = new AttributeObjectImpl();
438         attr.addComment("Typed delegate.");
439         attr.setModifier(ModifierKindImpl.mk_private);
440         String JavaDoc delegate_type = null;
441         if (segid==org.omg.Components.COMPONENT_ID.value) {
442             delegate_type = comp.getName()+"_MainSegOperations";
443         } else {
444             delegate_type = comp.getName()+"_Seg"+ssegid+"Operations";
445         }
446         attr.setType(delegate_type);
447         attr.setName("_delegate");
448         ski.addAttribute(attr);
449
450         // default constructor
451
// * protected $classname(ORBService orbs, ServicesSet sset, String uuid,
452
// org.omg.CORBA.Object executor)
453
ConstructorObject constructor = new ConstructorObjectImpl();
454         // params
455
ParameterObject parameter = new ParameterObjectImpl();
456         parameter.setName("orbs");
457         parameter.setType("ORBService");
458         constructor.addParameter(parameter);
459         parameter = new ParameterObjectImpl();
460         parameter.setName("sset");
461         parameter.setType("ServicesSet");
462         constructor.addParameter(parameter);
463         parameter = new ParameterObjectImpl();
464         parameter.setName("uuid");
465         parameter.setType("String");
466         constructor.addParameter(parameter);
467         parameter = new ParameterObjectImpl();
468         parameter.setName("executor");
469         parameter.setType("org.omg.CORBA.Object");
470         constructor.addParameter(parameter);
471         // initialize all fields
472
constructor.getImpl().addContextValue("delegate_type", delegate_type);
473         constructor.getImpl().setMacro("SKI_CONSTRUCTOR_BODY");
474         ski.addConstructor(constructor);
475
476         // internal operation
477
// * final protected CallInformation _getCallInformation(String sname)
478
MethodObject operation = new MethodObjectImpl();
479         operation.addComment(".");
480         operation.setModifier(ModifierKindImpl.mk_private);
481         operation.setReturnType("CallInformation");
482         operation.setName("_getCallInformation");
483         parameter = new ParameterObjectImpl();
484         parameter.setName("name");
485         parameter.setType("String");
486         operation.addParameter(parameter);
487         operation.getImpl().setMacro("SKI_GETCALLINFORMATION_BODY");
488         ski.addMethod(operation);
489
490         // * private short _preinvoke(org.coach.ECA.ServerCallController[] ctrls)
491
operation = new MethodObjectImpl();
492         operation.addComment(".");
493         operation.setModifier(ModifierKindImpl.mk_private);
494         operation.setReturnType("short");
495         operation.setName("_preinvoke");
496         parameter = new ParameterObjectImpl();
497         parameter.setName("ctrls");
498         parameter.setType("org.coach.ECA.ServerCallController[]");
499         operation.addParameter(parameter);
500         operation.getImpl().setMacro("SKI_PREINVOKE_BODY");
501         ski.addMethod(operation);
502
503         // * private short _postinvoke(short fstate)
504
operation = new MethodObjectImpl();
505         operation.addComment(".");
506         operation.setModifier(ModifierKindImpl.mk_private);
507         operation.setReturnType("short");
508         operation.setName("_postinvoke");
509         parameter = new ParameterObjectImpl();
510         parameter.setName("fstate");
511         parameter.setType("short");
512         operation.addParameter(parameter);
513         operation.getImpl().setMacro("SKI_POSTINVOKE_BODY");
514         ski.addMethod(operation);
515
516         // generation for all declaration of the segment
517
for (int i=0;i<idl3decls.length;i++) {
518             generateSkIOps(ski, idl3decls[i]);
519         }
520
521         // add to repository
522
getRepository().addObject(ski);
523     }
524
525     private void
526     generateSkIOps(ClassObject ski, Declaration idl3decl)
527     {
528         InterfaceDecl itf = null;
529         String JavaDoc sname = "::"+idl3decl.getName();
530
531         if (idl3decl.getDeclKind()==DeclarationKind.dk_component) {
532             ComponentDecl comp = (ComponentDecl)idl3decl;
533             itf = comp.getClientMapping();
534         } else if (idl3decl.getDeclKind()==DeclarationKind.dk_provides) {
535             ProvidesDecl facet = (ProvidesDecl)idl3decl;
536             itf = facet.getInterface();
537
538             // its a facet, include component name
539
sname = "::"+idl3decl.getParent().getName()+sname;
540         } else if (idl3decl.getDeclKind()==DeclarationKind.dk_consumes) {
541             ConsumesDecl sink = (ConsumesDecl)idl3decl;
542             itf = sink.getEvent().getClientMapping();
543
544             // its a sink, include component name
545
sname = "::"+idl3decl.getParent().getName()+sname;
546         }
547
548         // wrapped business operations
549
Declaration[] decls = itf.getContents(false, DeclarationKind.dk_operation);
550         OperationDecl op = null;
551         for (int i=0;i<decls.length;i++) {
552             op = (OperationDecl)decls[i];
553
554             String JavaDoc ciname = null;
555             if (idl3decl.getDeclKind()==DeclarationKind.dk_consumes) {
556                 // NOTE: do not include op name in case of an operation coming
557
// from an event port
558
ciname = sname;
559             } else {
560                 ciname = sname+"::"+op.getName();
561             }
562
563             addWrappedOperation(ski, op, op.getType(), ciname,
564                                 op.getParameterList().getParameters(),
565                                 op.getExceptionList().getExceptions(),
566                                 "SKI_WRAPPED_OPERATION");
567         }
568
569         // wrapped attributes
570
decls = itf.getContents(false, DeclarationKind.dk_attribute);
571         AttributeDecl attrdecl = null;
572         for (int i=0;i<decls.length;i++) {
573             attrdecl = (AttributeDecl)decls[i];
574
575             // check readonly
576
if (attrdecl.isReadonly()) {
577                 // getter (no parameter)
578
addWrappedOperation(ski, attrdecl, attrdecl.getType(), sname+"::_get_"+attrdecl.getName(),
579                                     new Parameter[0], attrdecl.getExceptions(),
580                                     "SKI_WRAPPED_OPERATION");
581             }
582             else {
583                 // getter (no parameter)
584
addWrappedOperation(ski, attrdecl, attrdecl.getType(), sname+"::_get_"+attrdecl.getName(),
585                                     new Parameter[0], attrdecl.getGetExceptions(),
586                                     "SKI_WRAPPED_OPERATION");
587
588                 // setter (parameter is attribute type)
589
// use attribute name as parameter name
590
Parameter[] params = new Parameter[] {
591                     new ParameterImpl(attrdecl.getName(), attrdecl.getType(), org.omg.CORBA.ParameterMode.PARAM_IN)
592                 };
593
594                 addWrappedOperation(ski, attrdecl, getAST().getVoidType(), sname+"::_set_"+attrdecl.getName(),
595                                     params, attrdecl.getSetExceptions(),
596                                     "SKI_WRAPPED_OPERATION");
597
598             }
599         }
600     }
601
602     private void
603     generateStubI(InterfaceDecl itf, Declaration idl3decl)
604     {
605         String JavaDoc classname = "";
606
607         // check declaration kind to build the class name
608
if (idl3decl.getDeclKind()==DeclarationKind.dk_uses) {
609             classname = idl3decl.getParent().getName()+"_"+idl3decl.getName()+"StubI";
610         }
611         if (idl3decl.getDeclKind()==DeclarationKind.dk_emits) {
612             classname = idl3decl.getParent().getName()+"_"+idl3decl.getName()+"StubI";
613         }
614         if (idl3decl.getDeclKind()==DeclarationKind.dk_publishes) {
615             classname = idl3decl.getParent().getName()+"_"+idl3decl.getName()+"StubI";
616         }
617
618         ClassObject ski = new ClassObjectImpl(classname);
619
620         // header
621
ski.setPackage(getTranslator().getPackage(itf));
622         ski.addImport("org.objectweb.corba.runtime.*");
623         ski.addImport("org.objectweb.ccm.runtime.*");
624
625         // class decl
626
// * public class $classname
627
// extends _$itf.getName()Stub
628
ski.addComment(" ");
629         ski.addComment("Java client-side interceptor for interface "+itf.getName());
630         ski.addComment(" ");
631         ski.addComment("NOTE: generated class; DO NOT EDIT !");
632         ski.addComment(" ");
633         ski.setModifier(ModifierKindImpl.mk_public);
634         ski.addInheritedObject("_"+itf.getName()+"Stub");
635
636         // internal state
637
AttributeObject attr = null;
638         // * static private _class_name = "<class_name>";
639
attr = new AttributeObjectImpl();
640         attr.addComment("Class name of this class (used for debugging).");
641         attr.setStatic(true);
642         attr.setModifier(ModifierKindImpl.mk_private);
643         attr.setType("String");
644         attr.setName("_class_name");
645         attr.setInitialValue("\""+classname+"\"");
646         ski.addAttribute(attr);
647
648         // * private ORBService _orb_service;
649
attr = new AttributeObjectImpl();
650         attr.addComment(".");
651         attr.setModifier(ModifierKindImpl.mk_private);
652         attr.setType("ORBService");
653         attr.setName("_orb_service");
654         ski.addAttribute(attr);
655
656         // * private ServicesSet _services_set;
657
attr = new AttributeObjectImpl();
658         attr.addComment(".");
659         attr.setModifier(ModifierKindImpl.mk_private);
660         attr.setType("ServicesSet");
661         attr.setName("_services_set");
662         ski.addAttribute(attr);
663
664         // * private String _uuid;
665
attr = new AttributeObjectImpl();
666         attr.addComment(".");
667         attr.setModifier(ModifierKindImpl.mk_private);
668         attr.setType("String");
669         attr.setName("_uuid");
670         ski.addAttribute(attr);
671
672         // * private ClientCallContextImpl _call_ctx;
673
attr = new AttributeObjectImpl();
674         attr.addComment(".");
675         attr.setModifier(ModifierKindImpl.mk_private);
676         attr.setType("ClientCallContextImpl");
677         attr.setName("_call_ctx");
678         ski.addAttribute(attr);
679
680         // * private java.util.Hashtable _informations;
681
attr = new AttributeObjectImpl();
682         attr.addComment(".");
683         attr.setModifier(ModifierKindImpl.mk_private);
684         attr.setType("java.util.Hashtable");
685         attr.setName("_informations");
686         ski.addAttribute(attr);
687
688         // * private org.omg.Components.EventConsumerBase _consbase;
689
attr = new AttributeObjectImpl();
690         attr.addComment(".");
691         attr.setModifier(ModifierKindImpl.mk_private);
692         attr.setType("org.omg.Components.EventConsumerBase");
693         attr.setName("_consbase");
694         ski.addAttribute(attr);
695
696         // * private $itf.getName() _delegate;
697
attr = new AttributeObjectImpl();
698         attr.addComment("Typed delegate.");
699         attr.setModifier(ModifierKindImpl.mk_private);
700         attr.setType(getTranslator().getAbsoluteName(itf));
701         attr.setName("_delegate");
702         ski.addAttribute(attr);
703
704         // default constructor
705
// * protected $classname(ORBService orbs, ServicesSet sset, String uuid, org.omg.CORBA.Object del)
706
ConstructorObject constructor = new ConstructorObjectImpl();
707         // params
708
ParameterObject parameter = new ParameterObjectImpl();
709         parameter.setName("orbs");
710         parameter.setType("ORBService");
711         constructor.addParameter(parameter);
712         parameter = new ParameterObjectImpl();
713         parameter.setName("sset");
714         parameter.setType("ServicesSet");
715         constructor.addParameter(parameter);
716         parameter = new ParameterObjectImpl();
717         parameter.setName("uuid");
718         parameter.setType("String");
719         constructor.addParameter(parameter);
720         parameter = new ParameterObjectImpl();
721         parameter.setName("del");
722         parameter.setType("org.omg.CORBA.Object");
723         constructor.addParameter(parameter);
724         // initialize all fields
725
constructor.getImpl().setMacro("STUBI_CONSTRUCTOR_BODY");
726         constructor.getImpl().addContextValue("interface", itf);
727         ski.addConstructor(constructor);
728
729         // internal operation
730
// * final protected CallInformation _getCallInformation(String sname)
731
MethodObject operation = new MethodObjectImpl();
732         operation.addComment(".");
733         operation.setModifier(ModifierKindImpl.mk_private);
734         operation.setReturnType("CallInformation");
735         operation.setName("_getCallInformation");
736         parameter = new ParameterObjectImpl();
737         parameter.setName("name");
738         parameter.setType("String");
739         operation.addParameter(parameter);
740         operation.getImpl().setMacro("STUBI_GETCALLINFORMATION_BODY");
741         ski.addMethod(operation);
742
743         // * private short _preinvoke(org.coach.ECA.ClientCallController[] ctrls)
744
operation = new MethodObjectImpl();
745         operation.addComment(".");
746         operation.setModifier(ModifierKindImpl.mk_private);
747         operation.setReturnType("short");
748         operation.setName("_preinvoke");
749         parameter = new ParameterObjectImpl();
750         parameter.setName("ctrls");
751         parameter.setType("org.coach.ECA.ClientCallController[]");
752         operation.addParameter(parameter);
753         operation.getImpl().setMacro("STUBI_PREINVOKE_BODY");
754         ski.addMethod(operation);
755
756         // * private short _postinvoke(short fstate)
757
operation = new MethodObjectImpl();
758         operation.addComment(".");
759         operation.setModifier(ModifierKindImpl.mk_private);
760         operation.setReturnType("short");
761         operation.setName("_postinvoke");
762         parameter = new ParameterObjectImpl();
763         parameter.setName("fstate");
764         parameter.setType("short");
765         operation.addParameter(parameter);
766         operation.getImpl().setMacro("STUBI_POSTINVOKE_BODY");
767         ski.addMethod(operation);
768
769         // build scoped name
770
// NOTE: idl3decl may be a receptacle / emitter / publisher
771
String JavaDoc sname = "::"+idl3decl.getName();
772         if (idl3decl.getDeclKind()==DeclarationKind.dk_uses) {
773             // its a receptacle, include component name
774
sname = "::"+idl3decl.getParent().getName()+sname;
775         }
776         if (idl3decl.getDeclKind()==DeclarationKind.dk_emits) {
777             // its an emitter, include component name
778
sname = "::"+idl3decl.getParent().getName()+sname;
779         }
780         if (idl3decl.getDeclKind()==DeclarationKind.dk_publishes) {
781             // its a pubisher, include component name
782
sname = "::"+idl3decl.getParent().getName()+sname;
783         }
784
785         // wrapped business operations
786
Declaration[] decls = itf.getContents(false, DeclarationKind.dk_operation);
787         OperationDecl op = null;
788         for (int i=0;i<decls.length;i++) {
789             op = (OperationDecl)decls[i];
790
791
792             addWrappedOperation(ski, op, op.getType(), sname+"::"+op.getName(),
793                                 op.getParameterList().getParameters(),
794                                 op.getExceptionList().getExceptions(),
795                                 "STUBI_WRAPPED_OPERATION");
796         }
797
798         // wrapped attributes
799
decls = itf.getContents(false, DeclarationKind.dk_attribute);
800         AttributeDecl attrdecl = null;
801         for (int i=0;i<decls.length;i++) {
802             attrdecl = (AttributeDecl)decls[i];
803
804             // check readonly
805
if (attrdecl.isReadonly()) {
806                 // getter (no parameter)
807
addWrappedOperation(ski, attrdecl, attrdecl.getType(), sname+"::_get_"+attrdecl.getName(),
808                                     new Parameter[0], attrdecl.getExceptions(),
809                                     "STUBI_WRAPPED_OPERATION");
810             }
811             else {
812                 // getter (no parameter)
813
addWrappedOperation(ski, attrdecl, attrdecl.getType(), sname+"::_get_"+attrdecl.getName(),
814                                     new Parameter[0], attrdecl.getGetExceptions(),
815                                     "STUBI_WRAPPED_OPERATION");
816
817                 // setter (parameter is attribute type)
818
// use attribute name as parameter name
819
Parameter[] params = new Parameter[] {
820                     new ParameterImpl(attrdecl.getName(), attrdecl.getType(), org.omg.CORBA.ParameterMode.PARAM_IN)
821                 };
822
823                 addWrappedOperation(ski, attrdecl, getAST().getVoidType(), sname+"::_set_"+attrdecl.getName(),
824                                     params, attrdecl.getSetExceptions(),
825                                     "STUBI_WRAPPED_OPERATION");
826
827             }
828         }
829
830         // add to repository
831
getRepository().addObject(ski);
832     }
833
834     private void
835     addWrappedOperation(ClassObject ski, Declaration decl, TypeRef rtype, String JavaDoc ciname,
836                         Parameter[] params, ExceptionDecl[] exceptions,
837                         String JavaDoc macroname)
838     {
839         addWrappedOperation(ski, decl, rtype, ciname, params, exceptions, macroname, new String JavaDoc[0], new Object JavaDoc[0]);
840     }
841
842     private void
843     addWrappedOperation(ClassObject ski, Declaration decl, TypeRef rtype, String JavaDoc ciname,
844                         Parameter[] params, ExceptionDecl[] exceptions,
845                         String JavaDoc macroname, String JavaDoc ctxkey, Object JavaDoc ctxval)
846     {
847         String JavaDoc[] ctxkeys = { ctxkey };
848         Object JavaDoc[] ctxvals = { ctxval };
849
850         addWrappedOperation(ski, decl, rtype, ciname, params, exceptions, macroname, ctxkeys, ctxvals);
851     }
852
853     private void
854     addWrappedOperation(ClassObject ski, Declaration decl, TypeRef rtype, String JavaDoc ciname,
855                         Parameter[] params, ExceptionDecl[] exceptions,
856                         String JavaDoc macroname, String JavaDoc[] ctxkeys, Object JavaDoc[] ctxvals)
857     {
858         MethodObject operation = new MethodObjectImpl();
859         // return type (java mapping)
860
operation.setReturnType(getTranslator().toJava(rtype));
861         
862         // name
863
operation.setName(decl.getName());
864         
865         // parameters (java mapping)
866
ParameterObject parameter = null;
867         for (int j=0;j<params.length;j++) {
868             parameter = new ParameterObjectImpl();
869             parameter.setName(params[j].getName());
870             parameter.setType(getTranslator().toJava(params[j].getType(), params[j].getMode()));
871             operation.addParameter(parameter);
872         }
873         // build parameters list
874
String JavaDoc param_names = "";
875         // check if params set is not empty
876
if (params.length!=0) {
877             for (int j=0;j<params.length-1;j++) {
878                 param_names = param_names + params[j].getName() + ", ";
879             }
880             
881             param_names = param_names + params[params.length-1].getName();
882         }
883         operation.getImpl().addContextValue("parameter_list", param_names);
884         
885         // exceptions
886
if (exceptions.length!=0) {
887             operation.getImpl().addContextValue("has_exceptions", new Boolean JavaDoc(true));
888             String JavaDoc[] exc_absnames = new String JavaDoc[exceptions.length];
889             for (int j=0;j<exceptions.length;j++) {
890                 exc_absnames[j] = getTranslator().getAbsoluteName(exceptions[j]);
891                     operation.addException(exc_absnames[j]);
892             }
893             operation.getImpl().addContextValue("exception_absnames", exc_absnames);
894         }
895         else {
896                 operation.getImpl().addContextValue("has_exceptions", new Boolean JavaDoc(false));
897         }
898         
899         // body
900
// NOTE: only the body's macro differs from a client-side to a server-side wrapped operation
901
operation.getImpl().setMacro(macroname);
902         operation.getImpl().addContextValue("declaration", decl);
903         operation.getImpl().addContextValue("callinfo_name", ciname);
904         operation.getImpl().addContextValue("return_type", getTranslator().toJava(rtype));
905         
906         // add external context values
907
for (int i=0;i<ctxkeys.length;i++) {
908             operation.getImpl().addContextValue(ctxkeys[i], ctxvals[i]);
909         }
910
911         if (rtype.getTypeKind()!=TypeKind.tk_void) {
912             operation.getImpl().addContextValue("has_return_type", new Boolean JavaDoc(true));
913             // any extractor and insertor
914
String JavaDoc return_type_extractor = getTranslator().toJavaAnyExtractor(rtype);
915             String JavaDoc return_type_insertor = getTranslator().toJavaAnyInsertor(rtype);
916             // return null value
917
String JavaDoc return_null_value = getTranslator().toJavaNullValue(rtype);
918             if (rtype.isDeclaration()) {
919                 return_type_extractor = return_type_extractor+"(_call_ctx.result())";
920                 return_type_insertor = return_type_insertor+"(anyres, res)";
921             }
922             else {
923                 return_type_extractor = "_call_ctx.result()."+return_type_extractor+"()";
924                 return_type_insertor = "anyres."+return_type_insertor+"(res)";
925             }
926             operation.getImpl().addContextValue("return_type_extractor", return_type_extractor);
927             operation.getImpl().addContextValue("return_type_insertor", return_type_insertor);
928             operation.getImpl().addContextValue("return_null_value", return_null_value);
929         }
930         else {
931             operation.getImpl().addContextValue("has_return_type", new Boolean JavaDoc(false));
932         }
933         
934         
935         // add operation to class
936
ski.addMethod(operation);
937     }
938
939     //
940
// public operations
941
//
942

943     final public void
944     generate(String JavaDoc basedir, Scope scope)
945     throws GenerationException
946     {
947         _repository = new RepositoryImpl();
948
949         // obtain objects for generation
950
// check scope, should be:
951
// - a component
952
// - a module
953
if (scope.getDeclKind()==DeclarationKind.dk_component) {
954             generateForComponent((ComponentDecl)scope);
955         }
956         else if (scope.getDeclKind()==DeclarationKind.dk_module) {
957             generateForModule((ModuleDecl)scope);
958         }
959         else {
960             System.err.println("object is not a component: aborting");
961             return ;
962         }
963
964         // NOTE: issue: the template file "ski.vm" is not located in the OpenCCM template dir,
965
// but in the directory pointed by the "ecm.templates" property
966
// It could be usefull to be able to specify externally the template dirs
967
// in the initialization of the Java generator (along with the template files
968
// themselves)
969
// Currently, we are forced to copy the template file in the OpenCCM dir ...
970

971         String JavaDoc tpath = System.getProperty("ecm.templates");
972         java.io.File JavaDoc tdir = new java.io.File JavaDoc(tpath);
973
974         // Generate Java Files
975
JavaGenerator javagen = new JavaGenerator();
976         java.util.Vector JavaDoc v = new java.util.Vector JavaDoc();
977         v.add("ski.vm");
978         v.add("stubi.vm");
979         v.add("ifact.vm");
980         javagen.initialize("Container Interceptors", v);
981         javagen.generate(getRepository(), basedir);
982     }
983
984 }
985
986
987
Popular Tags