KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CDL > CompFrame


1 /* $Id: CompFrame.java,v 1.2 2004/05/20 14:23:51 bures Exp $ */
2 package SOFA.SOFAnode.Made.CDL;
3 import java.rmi.RemoteException JavaDoc;
4
5 import SOFA.SOFAnode.Made.TIR.CDLRepository;
6 import SOFA.SOFAnode.Made.TIR.CDLType;
7 import SOFA.SOFAnode.Made.TIR.Contained;
8 import SOFA.SOFAnode.Made.TIR.Container;
9 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
10 import SOFA.SOFAnode.Made.TIR.FrameDef;
11 import SOFA.SOFAnode.Made.TIR.PropertyDef;
12 import SOFA.SOFAnode.Made.TIR.ProtocolDef;
13 import SOFA.SOFAnode.Made.TIR.ProtocolOperDef;
14 import SOFA.SOFAnode.Made.TIR.ProvideDef;
15 import SOFA.SOFAnode.Made.TIR.RequireDef;
16 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
17 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
18
19 public class CompFrame extends CompContainedContainerIm implements CompType {
20   CompProtOper protocol;
21   EnumList propNames;
22   public boolean isSystem;
23   boolean consistChecked;
24
25   public CompFrame(ID id, CompContainer in, CompRepository inRep, boolean sys) {
26     super(ObjectsKind.o_Frame, id, in, inRep);
27     protocol = null;
28     propNames = new EnumList();
29     isSystem = sys;
30     consistChecked = false;
31 // Debug.message("Vytvarim frame: "+id);
32
}
33
34   public boolean addObject(CompContained o) {
35     o.getIdent().version = id.version;
36     if (o.objectKind() == ObjectsKind.o_Property) {
37       propNames.addName(o.getIdent().name);
38     }
39     return super.addObject(o);
40   }
41
42   public static EnumList getPropNames(CompReffer ref, CompRepository rep) throws CDLExceptRemote, CDLExceptLock {
43     int i,j;
44     if (ref.isNew) {
45       CompContainer con = rep;
46       ref.ref.sn.names.toFirst();
47       for(i=0;i<ref.ref.sn.names.size();i++) {
48         CompContained[] a = con.lookup((String JavaDoc) ref.ref.sn.names.aktual());
49         if (a==null || a.length==0)
50           return null;
51         if (a.length==1) {
52            if (a[0].isContainer()) con = (CompContainer) a[0];
53              else return null;
54         } else {
55           return null;
56         }
57         ref.ref.sn.names.toNext();
58       }
59       CompContained sec = con.lookup(ref.ref.name, ref.ref.version);
60       if (sec!=null && sec.objectKind()==ObjectsKind.o_Frame)
61         return ((CompFrame) sec).propNames;
62       else
63         return null;
64     } else {
65       try {
66         Container con = rep.cdlRepository;
67         ref.ref.sn.names.toFirst();
68         for(i=0;i<ref.ref.sn.names.size();i++) {
69           Contained[] a = con.lookup_name((String JavaDoc) ref.ref.sn.names.aktual());
70           if (a==null || a.length==0)
71             return null;
72           if (a.length==1) {
73             if (a[0] instanceof Container) con = (Container) a[0];
74             else return null;
75           } else {
76             boolean found = false;
77             for(j=0;j<a.length;j++) {
78               if (a[j].get_identification().version().compareTo(ref.ref.version)==0) {
79                 if ( !(a[j] instanceof Container)) return null;
80                 found = true;
81                 con = (Container) a[j];
82                 break;
83               }
84             }
85             if (!found) return null;
86           }
87           ref.ref.sn.names.toNext();
88         }
89         Contained sec = con.lookup(ref.ref.name, ref.ref.version);
90         if (sec!=null && sec.get_def_kind().value() == DefinitionKind.dk_Frame ) {
91           DefinitionKind dkprop = rep.workRepository.get_spec_def_kind(DefinitionKind.dk_Property);
92           Contained[] prop = ((FrameDef) sec).contents(dkprop);
93           EnumList ret = new EnumList();
94           for (i=0;i<prop.length;i++) {
95             ret.addName(prop[i].get_identification().name());
96           }
97           return ret;
98         } else {
99           return null;
100         }
101       } catch (TIRExceptLock e) {
102         throw new CDLExceptLock("Repository is locked.");
103       } catch (RemoteException JavaDoc e) {
104         throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
105       }
106       
107     }
108   }
109
110   public void checkConsist(EnumList props, CompRepository rep) throws CDLExceptCheck, CDLExceptLock, CDLExceptRemote {
111     int i;
112     CompContained[] cont = contents();
113     for (i=0;i<cont.length;i++) {
114       cont[i].checkConsist(propNames, rep);
115     }
116
117     if (rep.useProtocols) {
118       if (protocol == null && contents().length!=0)
119         throw new CDLExceptCheck("No protocol for frame "+fullName());
120     }
121     if (!consistChecked) {
122       if (rep.testProtocols) {
123         String JavaDoc res;
124         for (i=0;i<cont.length;i++) {
125           if (cont[i].objectKind() == ObjectsKind.o_Provides) {
126         CompProvide p = (CompProvide) cont[i];
127             Output.out.println("Checking protocol of provides "+p.getIdent().name+" of frame: "+getIdent().name);
128             String JavaDoc[] operations = p.operations.toArray();
129             prependStringArray("?"+p.getIdent().name+".", operations);
130             try {
131               String JavaDoc ifaceProt = p.ifaceProtocol();
132               if (ifaceProt.compareTo("")==0) {
133                 continue;
134               }
135               if (System.getProperty("cdl.debug","no").compareTo("yes")==0) {
136                 res = SOFA.SOFAnode.Util.ProtocolChecker.int2frame(ifaceProt, true, operations, protocol.toText());
137               }
138               long tmb = java.util.Calendar.getInstance().getTime().getTime();
139               res = repository().protocolChecker.int2frame(ifaceProt, true, operations, protocol.toText());
140               long tme = java.util.Calendar.getInstance().getTime().getTime();
141               Output.out.println("Time of protocol checking of provides: "+getIdent().name+"."+p.getIdent().name+": "+Long.toString(tme-tmb)+" ms");
142               if (res != null)
143                 Output.out.println("Warning: "+res+" ("+fullName()+")");
144             } catch (SOFA.SOFAnode.Util.SyntaxErrorException e) {
145               Output.out.println("Protocol syntax error\n"+e.getMessage());
146             }
147       }
148           if (cont[i].objectKind() == ObjectsKind.o_Requires) {
149         CompRequire r = (CompRequire) cont[i];
150             Output.out.println("Checking protocol of requires "+r.getIdent().name+" of frame: "+getIdent().name);
151             String JavaDoc[] operations = r.operations.toArray();
152             prependStringArray("!"+r.getIdent().name+".", operations);
153             try {
154               String JavaDoc ifaceProt = r.ifaceProtocol();
155               if (ifaceProt.compareTo("")==0) {
156                 continue;
157               }
158               if (System.getProperty("cdl.debug","no").compareTo("yes")==0) {
159                 res = SOFA.SOFAnode.Util.ProtocolChecker.int2frame(ifaceProt, false, operations, protocol.toText());
160               }
161               long tmb = java.util.Calendar.getInstance().getTime().getTime();
162               res = repository().protocolChecker.int2frame(ifaceProt, false, operations, protocol.toText());
163               long tme = java.util.Calendar.getInstance().getTime().getTime();
164               Output.out.println("Time of protocol checking of requires: "+getIdent().name+"."+r.getIdent().name+": "+Long.toString(tme-tmb)+" ms");
165               if (res != null)
166             Output.out.println("Warning: "+res+" ("+fullName()+")");
167             } catch (SOFA.SOFAnode.Util.SyntaxErrorException e) {
168               Output.out.println("Protocol syntax error\n"+e.getMessage());
169             }
170       }
171         }
172         System.gc();
173       }
174     }
175     consistChecked = true;
176   }
177
178   public void addToNormal(Container obj, CDLRepository normRep, java.util.ArrayList JavaDoc delayed) throws CDLExceptToNormal, CDLExceptRemote, CDLExceptLock {
179     if (id.isin == IDKind.versiontag) {
180       try {
181         ((Contained)obj).tag(id.tag);
182       } catch (RemoteException JavaDoc e) {
183         throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
184       } catch (TIRExceptLock e) {
185         throw new CDLExceptLock("Repository is locked.");
186       } catch (TIRExceptCreate ecr) {
187         throw new CDLExceptToNormal("Can't set tag for "+ fullName());
188       }
189     }
190     cont.toFirst();
191     try {
192       for (int i=0;i<cont.size(); i++) {
193         CompContained akt = (CompContained) cont.aktual();
194         switch (akt.objectKind()) {
195         // *****************
196
case ObjectsKind.o_Property:
197           try {
198             PropertyDef prp = ((FrameDef) obj).create_property(akt.getIdent().name,((CompProperty)akt).tp.toNormal(normRep) );
199             if (prp == null)
200               throw new CDLExceptToNormal("Can't create property "+((CompContained)akt).fullName());
201           } catch (TIRExceptCreate ecr) {
202             throw new CDLExceptToNormal("Can't create property "+((CompContained)akt).fullName()+"\n"+
203                                          ecr.getMessage() );
204           }
205           break;
206         // *****************
207
case ObjectsKind.o_Provides:
208           try {
209             ProvideDef prov = ((FrameDef) obj).create_provide(akt.getIdent().name,((CompProvide)akt).type.toNormal(normRep) );
210             if (prov == null)
211               throw new CDLExceptToNormal("Can't create provide "+((CompContained)akt).fullName());
212           } catch (TIRExceptCreate ecr) {
213             throw new CDLExceptToNormal("Can't create provide "+((CompContained)akt).fullName());
214           }
215           break;
216         // *****************
217
case ObjectsKind.o_Requires:
218           try {
219             RequireDef req = ((FrameDef) obj).create_require(akt.getIdent().name,((CompRequire)akt).type.toNormal(normRep) );
220             if (req == null)
221               throw new CDLExceptToNormal("Can't create require "+((CompContained)akt).fullName());
222           } catch (TIRExceptCreate ecr) {
223             throw new CDLExceptToNormal("Can't create require "+((CompContained)akt).fullName());
224           }
225           break;
226         default:
227           throw new CDLExceptToNormal("Unexcepted kind of object");
228         }
229         cont.toNext();
230       }
231       if (protocol != null) {
232         try {
233           ProtocolDef newProt = ((FrameDef) obj).create_protocol();
234           ProtocolOperDef p = CompProtOper.toNormalFrame(protocol, newProt);
235       newProt.protocol(p);
236     } catch (TIRExceptCreate ecr) {
237       throw new CDLExceptToNormal("Can't create protocol in "+ fullName()+"\n"+
238                                          ecr.getMessage());
239     }
240       }
241      
242     } catch (RemoteException JavaDoc e) {
243       throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
244     } catch (TIRExceptLock e) {
245       throw new CDLExceptLock("Repository is locked.");
246     }
247   }
248
249   /** from CompType */
250   public CDLType toNormal(CDLRepository newRep) throws CDLExceptToNormal {
251     // never call this
252
throw new CDLExceptToNormal("It should never throw");
253   }
254
255   public boolean isProvReqOperation(String JavaDoc req, String JavaDoc opername) {
256     cont.toFirst();
257     for (int i=0; i<cont.size(); i++) {
258       if (((CompContained) cont.aktual()).getIdent().isNameEqual(req)) {
259         if (((CompObject)cont.aktual()).objectKind() == ObjectsKind.o_Requires) {
260           return ((CompRequire) cont.aktual()).isOperation(opername);
261         } else {
262           if (((CompObject)cont.aktual()).objectKind() == ObjectsKind.o_Provides) {
263             return ((CompProvide) cont.aktual()).isOperation(opername);
264       }
265     }
266         break;
267       }
268       cont.toNext();
269     }
270     return false;
271   }
272
273   public String JavaDoc protocolToText() {
274     if (protocol != null) {
275       return protocol.toText();
276     }
277     return "-- no protocol --";
278   }
279   
280   /** prepend string "what" to all strings in "a" */
281   static void prependStringArray(String JavaDoc what, String JavaDoc[] a) {
282     for (int i=0;i<a.length;i++) {
283       a[i] = what + a[i];
284     }
285   }
286 }
287
Popular Tags