KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEM > Elements > DistributorImplGen


1 /*
2  * DistributorImplGen.java
3  *
4  * Created on 13. duben 2002, 21:08
5  */

6
7 package SOFA.Connector.EEG.EEM.Elements;
8
9 import SOFA.Connector.EEG.CodeWriter.JWriterDirectorException;
10 import SOFA.Connector.EEG.EEM.Actions.JIfaceCN;
11 import SOFA.Connector.EEG.EEM.Actions.JImplWriterDirectorBase;
12 import SOFA.SOFAnode.Made.TIR.CDLType;
13 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
14 import SOFA.SOFAnode.Made.TIR.OperationDef;
15 import SOFA.SOFAnode.Made.TIR.ParamDescr;
16 import SOFA.SOFAnode.Made.TIR.ParamMode;
17 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
18 import SOFA.SOFAnode.Made.TIR.PrimitiveKind;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
20
21 /**
22  *
23  * @author ghort
24  * @version
25  */

26 public class DistributorImplGen extends JImplWriterDirectorBase {
27
28     /** Creates new DistributorImplGen */
29     public DistributorImplGen(String JavaDoc provIfaceCDL, SOFA.Connector.Property[] params) {
30         super(provIfaceCDL,params);
31     }
32
33     public String JavaDoc[] getImplements() {
34         return new String JavaDoc[] {"SOFA.Connector.EEG.Types.Distributor"};
35     }
36     
37     public String JavaDoc getClassBody() {
38         return managerTieImpl() + "\n" +
39             "\tprotected java.util.HashSet listeners = new java.util.HashSet();\n\n"+
40             "\tpublic synchronized void link(Object listener) throws SOFA.Connector.LinkException {\n"+
41             "\t\tif (!listeners.add(listener)) {\n"+
42             "\t\t\tthrow new SOFA.Connector.LinkException(\"Listener already present.\");\n"+
43             "\t\t}\n"+
44             "\t}\n\n"+
45             "\tpublic synchronized void unlink(Object listener) throws SOFA.Connector.LinkException {\n"+
46             "\t\tif (!listeners.remove(listener)) {\n"+
47             "\t\t\tthrow new SOFA.Connector.LinkException(\"Listener not found.\");\n"+
48             "\t\t}\n"+
49             "\t}\n\n"+
50                         "\tpublic org.objectweb.fractal.api.Type getFcItfType() { return null; }\n"+
51                         "\tpublic boolean isFcInternalItf() { return false; }\n"+
52                         "\tpublic String getFcItfName() { throw new SOFA.Connector.ConnectorTransportException(\"Interconection via EventPassing connector - getFcItfName is not supported.\"); }\n"+
53                         "\tpublic org.objectweb.fractal.api.Component getFcItfOwner() { throw new SOFA.Connector.ConnectorTransportException(\"Interconection via EventPassing connector - getFcItfOwner is not supported.\"); }\n\n";
54     }
55
56     public String JavaDoc getMethodDef(OperationDef mt) throws JWriterDirectorException {
57         return JIfaceCN.getMethodDef(mt);
58     }
59     
60     public String JavaDoc getMethodBody(OperationDef method) throws JWriterDirectorException {
61         try {
62             String JavaDoc reqIfaceMapped=SOFA.Connector.Property.findFirst(params,"reqIfaceMapped");
63             
64             CDLType retType=method.result();
65             if (!(retType.get_def_kind().value()==DefinitionKind.dk_Primitive &&
66                 ((PrimitiveDef)retType).kind().value()==PrimitiveKind.pk_void)) {
67                 throw new JWriterDirectorException("Can't distribute calls which return value. (method: '"+method.get_identification().name()+"')");
68             }
69             int i;
70             ParamDescr[] params=method.params();
71
72             for (i=0;i<params.length;i++) {
73                 int mode=params[i].mode().value();
74                 if (mode==ParamMode.PARAM_INOUT || mode==ParamMode.PARAM_OUT) {
75                     throw new JWriterDirectorException("Can't distribute calls which which have in/inout parameters. (method: '"+method.get_identification().name()+"')");
76                 }
77             }
78
79             return
80                 "\t\tsynchronized (this) {\n"+
81                 "\t\t\tjava.util.Iterator iter=listeners.iterator();\n"+
82                 "\t\t\twhile (iter.hasNext()) {\n"+
83                 "\t\t\t\t"+reqIfaceMapped+" tgt=("+reqIfaceMapped+")iter.next();\n"+
84                 "\t\t\t\ttgt."+method.get_identification().name()+"("+JIfaceCN.targetCallParams(method)+");\n"+
85                 "\t\t\t}\n"+
86                 "\t\t}\n";
87         } catch (TIRExceptLock e) {
88             throw new JWriterDirectorException("Cannot access TIR.",e);
89         } catch (java.rmi.RemoteException JavaDoc e) {
90             throw new JWriterDirectorException("Cannot access TIR.",e);
91         }
92     }
93 }
94
Popular Tags