KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > GenSpecFile


1 /* $Id: GenSpecFile.java,v 1.3 2004/06/03 14:29:08 bures Exp $ */
2 package SOFA.SOFAnode.Run;
3 import java.util.Iterator JavaDoc;
4
5 import javax.xml.transform.TransformerConfigurationException JavaDoc;
6 import javax.xml.transform.TransformerException JavaDoc;
7
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.Element JavaDoc;
10 import org.w3c.dom.Node JavaDoc;
11 import org.w3c.dom.NodeList JavaDoc;
12
13 import SOFA.SOFAnode.Made.TIR.ArchitectureDef;
14 import SOFA.SOFAnode.Made.TIR.ArrayDef;
15 import SOFA.SOFAnode.Made.TIR.BindDef;
16 import SOFA.SOFAnode.Made.TIR.BindMode;
17 import SOFA.SOFAnode.Made.TIR.BindOperKind;
18 import SOFA.SOFAnode.Made.TIR.BindOperSub;
19 import SOFA.SOFAnode.Made.TIR.BindType;
20 import SOFA.SOFAnode.Made.TIR.BindTypeArray;
21 import SOFA.SOFAnode.Made.TIR.BindTypeKind;
22 import SOFA.SOFAnode.Made.TIR.BindTypeNormal;
23 import SOFA.SOFAnode.Made.TIR.Contained;
24 import SOFA.SOFAnode.Made.TIR.CDLType;
25 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
26 import SOFA.SOFAnode.Made.TIR.FrameDef;
27 import SOFA.SOFAnode.Made.TIR.InstDef;
28 import SOFA.SOFAnode.Made.TIR.ProvideDef;
29 import SOFA.SOFAnode.Made.TIR.Repository;
30 import SOFA.SOFAnode.Made.TIR.RequireDef;
31 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
32 import SOFA.SOFAnode.Made.TIR.TypedefDef;
33 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods;
34
35 /** Generates SOFA specfile for the choosen application.
36   *
37   * @author Petr Hnetynka
38   */

39 public class GenSpecFile {
40
41   /** Main method. Neccessary properies: sofa.rmihost, sofa.rmiport, sofa.tr.url.
42     *
43     * @param argv 1st argument - full application name, 2nd name of the generated specfile
44     *
45     */

46   public static void main(String JavaDoc[] argv) {
47     if (argv.length < 2) {
48       System.out.println("Too few arguments.");
49       System.out.println(" 1st argument - full application name");
50       System.out.println(" 2nd argument - name of the generated specfile");
51       System.exit(1);
52     }
53
54     if (!argv[0].matches("^.+\\[.+\\]$")) {
55       System.out.println("Bad format of application name.");
56       System.exit(1);
57     }
58
59     Document JavaDoc depldesc = null;
60     try {
61       depldesc = TRAccess.getDeplDescrXMLLocal(argv[0], "index.dc");
62     } catch (java.net.MalformedURLException JavaDoc e) {
63       System.out.println("MalformedURLException: "+e.getMessage());
64       e.printStackTrace();
65       System.exit(1);
66     } catch (java.io.IOException JavaDoc e) {
67       System.out.println("IOException: "+e.getMessage());
68       System.exit(1);
69     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
70       System.out.println("ParserConfigurationExceptioni: "+e.getMessage() );
71       System.exit(1);
72     } catch (org.xml.sax.SAXException JavaDoc e) {
73       System.out.println("SAXException: "+e.getMessage() );
74       System.exit(1);
75     }
76
77     Element JavaDoc applRootElement = depldesc.getDocumentElement();
78     applRootElement.normalize();
79
80     if (applRootElement.getTagName().compareTo("sofa_system") != 0) {
81       System.out.println(argv[0]+" is not system component.");
82       System.exit(1);
83     }
84
85     Repository tir = null;
86     try {
87       tir = TIRAccessMethods.getRepository();
88     } catch (java.net.MalformedURLException JavaDoc e) {
89       System.out.println("MalformedURLException: "+e.getMessage() );
90       System.exit(1);
91     } catch (java.rmi.NotBoundException JavaDoc e) {
92       System.out.println("NotBoundException: "+e.getMessage() );
93       System.exit(1);
94     } catch (java.rmi.RemoteException JavaDoc e) {
95       System.out.println("RemoteException: "+e.getMessage() );
96       System.exit(1);
97     }
98     
99     System.out.print("Generating specfile...");
100     
101     applRootElement = processElement(tir, null, null, applRootElement);
102     applRootElement = addApplName(applRootElement);
103
104     
105     SpecFile sf = SpecFile.getSpecFileFromDeplDescriptorXMLDoc(applRootElement);
106     try {
107       sf.toFile(argv[1]+".ssf");
108     } catch (TransformerConfigurationException JavaDoc e) {
109       System.out.println("TransformerConfigurationException: "+e.getMessage() );
110       System.exit(1);
111     } catch (TransformerException JavaDoc e) {
112       System.out.println("TransformerException: "+e.getMessage() );
113       System.exit(1);
114     } catch (java.io.IOException JavaDoc e) {
115       System.out.println("IOException: "+e.getMessage());
116       System.exit(1);
117     }
118
119     System.out.println("OK");
120   }
121
122   private static Element JavaDoc processElement(Repository tir, String JavaDoc inst, InfoArray riarray, Element JavaDoc element) {
123     String JavaDoc archName = null;
124     InfoArray iarray = null;
125     NodeList JavaDoc nl = element.getChildNodes();
126     for (int i=0; i<nl.getLength(); i++) {
127       Node JavaDoc n = nl.item(i);
128       if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("component_ref")==0)) {
129         String JavaDoc instName = ((Element JavaDoc)n).getAttribute("inst");
130         String JavaDoc name = ((Element JavaDoc)n).getAttribute("arch");
131         String JavaDoc version = ((Element JavaDoc)n).getAttribute("version");
132         Element JavaDoc archRootElement = null;
133         try {
134           Document JavaDoc archDoc = TRAccess.getDeplDescrXMLLocal(name+"["+version+"]", "index.dc");
135           archRootElement = archDoc.getDocumentElement();
136           archRootElement.normalize();
137         } catch (java.net.MalformedURLException JavaDoc e) {
138           System.out.println("MalformedURLException: "+e.getMessage());
139           e.printStackTrace();
140           System.exit(1);
141         } catch (java.io.IOException JavaDoc e) {
142           System.out.println("IOException: "+e.getMessage());
143           System.exit(1);
144         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
145           System.out.println("ParserConfigurationException: "+e.getMessage() );
146           System.exit(1);
147         } catch (org.xml.sax.SAXException JavaDoc e) {
148           System.out.println("SAXException: "+e.getMessage() );
149           System.exit(1);
150         }
151         archRootElement.setAttribute("name", instName);
152         archRootElement = processElement(tir, instName, iarray, archRootElement);
153         element.removeChild(n);
154         element.appendChild(element.getOwnerDocument().importNode(archRootElement, true));
155       } else if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
156         archName = n.getFirstChild().getNodeValue();
157         iarray = getBindConnInfo(tir, archName);
158       } else if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("connector")==0)) {
159         String JavaDoc connName = ((Element JavaDoc)n).getAttribute("name");
160         if (connName.startsWith("_delegate_") || connName.startsWith("_subsume_")) {
161           ((Element JavaDoc) n).setAttribute("frame", "SGenerator:CSProcCallImpl");
162         } else {
163           String JavaDoc usingConn = riarray.getConn(inst, connName);
164           if (usingConn == null) {
165             ((Element JavaDoc) n).setAttribute("frame", "SGenerator:!Add connector type here!Impl");
166           } else {
167             ((Element JavaDoc) n).setAttribute("frame", "SGenerator:"+usingConn+"Impl");
168           }
169         }
170         Element JavaDoc newEl = n.getOwnerDocument().createElement("transport");
171         newEl.setAttribute("type", "LOCAL");
172         n.appendChild(newEl);
173       }
174     }
175     
176     return element;
177   }
178
179   private static Element JavaDoc addApplName(Element JavaDoc element) {
180     NodeList JavaDoc nl = element.getChildNodes();
181     for (int i=0; i<nl.getLength(); i++) {
182       Node JavaDoc n = nl.item(i);
183       if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
184         String JavaDoc ar = n.getFirstChild().getNodeValue();
185         ar = ar.substring(ar.lastIndexOf(':')+1, ar.indexOf('?'));
186         element.setAttribute("name", ar);
187       }
188     }
189     return element;
190   }
191
192   public static class BindConnInfo {
193     public String JavaDoc inst;
194     public String JavaDoc prname;
195     public String JavaDoc con;
196     public int type;
197     public static final int PROV = 0;
198     public static final int REQ = 1;
199     public BindConnInfo(String JavaDoc i, String JavaDoc pr, String JavaDoc c, int t) {
200       inst = i;
201       prname = pr;
202       con = c;
203       type = t;
204     }
205   }
206
207   public static class InfoArray extends java.util.ArrayList JavaDoc {
208     public void addInfo(String JavaDoc inst, String JavaDoc pr, String JavaDoc using, int type) {
209       add(new BindConnInfo(inst, pr, using, type));
210     }
211   
212     public void replaceInfoUsingField(String JavaDoc inst, String JavaDoc pr, String JavaDoc using, int type) {
213       Iterator JavaDoc iter=iterator();
214       
215       while (iter.hasNext()) {
216         BindConnInfo iterElement;
217         iterElement = (BindConnInfo)iter.next();
218         
219         if (iterElement.inst.equals(inst) && iterElement.prname.equals(pr) && iterElement.type==type) {
220             iterElement.con=using;
221             break;
222         }
223       }
224     }
225
226     public BindConnInfo getInfo(String JavaDoc inst, String JavaDoc pr) {
227       BindConnInfo bi = null;
228       for (int i=0; i<size(); i++) {
229         bi = (BindConnInfo) get(i);
230         if ((bi.inst.compareTo(inst) == 0) && (bi.prname.compareTo(pr) == 0)) {
231           return bi;
232         }
233       }
234       return null;
235     }
236
237     public String JavaDoc getConn(String JavaDoc inst, String JavaDoc pr) {
238       BindConnInfo i = getInfo(inst, pr);
239       if (i != null)
240         return i.con;
241       else
242         return null;
243     }
244   }
245
246   public static InfoArray getBindConnInfo(Repository rep, String JavaDoc archFullName) {
247     try {
248     ArchitectureDef arch = (ArchitectureDef) TIRAccessMethods.lookupCDLContained(rep, archFullName);
249       if (arch != null) {
250         InfoArray iarray = new InfoArray();
251         
252         Contained[] componentInstances=arch.contents(rep.get_spec_def_kind(DefinitionKind.dk_Inst));
253         for (int componentInstanceIdx=0; componentInstanceIdx<componentInstances.length; componentInstanceIdx++) {
254             InstDef componentInstance=(InstDef) componentInstances[componentInstanceIdx];
255
256           // A componentInstance.type does not have to be just frame. It can be a typedef, array, array of typedef, etc. This code goes
257
// through the hierarchy and tries to find a frame type.
258
CDLType instanceType=componentInstance.type();
259           int instanceTypeDefKind;
260           FrameDef instanceFrame=null;
261           String JavaDoc instanceTypeDefKindChain=null;
262
263           while (true) {
264             instanceTypeDefKind=instanceType.get_def_kind().value();
265             instanceTypeDefKindChain=(instanceTypeDefKindChain==null ? "" : instanceTypeDefKindChain + "-") + instanceTypeDefKind;
266
267             if (instanceTypeDefKind==DefinitionKind.dk_Frame) { // frame
268
instanceFrame=(FrameDef)instanceType;
269               break;
270               
271             } else if (instanceTypeDefKind==DefinitionKind.dk_Array) { // array
272
instanceType=((ArrayDef)instanceType).element_type();
273               
274             } else if (instanceTypeDefKind==DefinitionKind.dk_Typedef) { // typedef
275
instanceType=((TypedefDef)instanceType).original_type();
276
277             } else {
278               System.out.println("Unsupported instance type: "+instanceTypeDefKindChain);
279               System.exit(1);
280             }
281           }
282
283             Contained[] frameProvides=instanceFrame.contents(rep.get_spec_def_kind(DefinitionKind.dk_Provides));
284             for (int frameProvideIdx=0; frameProvideIdx<frameProvides.length; frameProvideIdx++) {
285                 ProvideDef frameProvide=(ProvideDef) frameProvides[frameProvideIdx];
286                 iarray.addInfo(componentInstance.get_identification().name(), frameProvide.get_identification().name(), null, BindConnInfo.PROV);
287             }
288
289             Contained[] frameRequires=instanceFrame.contents(rep.get_spec_def_kind(DefinitionKind.dk_Requires));
290             for (int frameRequireIdx=0; frameRequireIdx<frameRequires.length; frameRequireIdx++) {
291                 RequireDef frameRequire=(RequireDef) frameRequires[frameRequireIdx];
292                 iarray.addInfo(componentInstance.get_identification().name(), frameRequire.get_identification().name(), null, BindConnInfo.REQ);
293             }
294         }
295         
296         Contained[] binds = arch.contents(rep.get_spec_def_kind(DefinitionKind.dk_Bind));
297         for (int i=0; i<binds.length; i++) {
298           BindDef bind = (BindDef) binds[i];
299           String JavaDoc using = bind.using();
300           switch (bind.mode().value()) {
301             case BindMode.DELEGATE:
302               {
303                 BindOperSub rhs = (BindOperSub) bind.rhsop();
304                 BindType[] relems = rhs.elements();
305                 BindType[] rsubs = rhs.subs();
306                 String JavaDoc rcompname;
307                 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
308                   rcompname = ((BindTypeNormal) rsubs[0]).name();
309                 } else {
310                   rcompname = ((BindTypeArray) rsubs[0]).name();
311                 }
312                 String JavaDoc relemname;
313                 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
314                   relemname = ((BindTypeNormal) relems[0]).name();
315                 } else {
316                   relemname = ((BindTypeArray) relems[0]).name();
317                 }
318                 iarray.replaceInfoUsingField(rcompname, relemname, using, BindConnInfo.PROV);
319               }
320               break;
321             case BindMode.SUBSUME:
322               {
323                 BindOperSub lhs = (BindOperSub) bind.lhsop();
324                 BindType[] lelems = lhs.elements();
325                 BindType[] lsubs = lhs.subs();
326                 String JavaDoc lcompname;
327                 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
328                   lcompname = ((BindTypeNormal) lsubs[0]).name();
329                 } else {
330                   lcompname = ((BindTypeArray) lsubs[0]).name();
331                 }
332                 String JavaDoc lelemname;
333                 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
334                   lelemname = ((BindTypeNormal) lelems[0]).name();
335                 } else {
336                   lelemname = ((BindTypeArray) lelems[0]).name();
337                 }
338                 iarray.replaceInfoUsingField(lcompname, lelemname, using, BindConnInfo.REQ);
339               }
340               break;
341             case BindMode.BIND:
342               if (bind.lhsop().get_bo_kind().value() == BindOperKind.bok_provreq) {
343                 ;
344               } else {
345                 BindOperSub lhs = (BindOperSub) bind.lhsop();
346                 BindOperSub rhs = (BindOperSub) bind.rhsop();
347                 BindType[] lelems = lhs.elements();
348                 BindType[] lsubs = lhs.subs();
349                 BindType[] relems = rhs.elements();
350                 BindType[] rsubs = rhs.subs();
351                 String JavaDoc lcompname;
352                 String JavaDoc rcompname;
353                 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
354                   lcompname = ((BindTypeNormal) lsubs[0]).name();
355                 } else {
356                   lcompname = ((BindTypeArray) lsubs[0]).name();
357                 }
358                 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
359                   rcompname = ((BindTypeNormal) rsubs[0]).name();
360                 } else {
361                   rcompname = ((BindTypeArray) rsubs[0]).name();
362                 }
363                 String JavaDoc lelemname;
364                 String JavaDoc relemname;
365                 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
366                   lelemname = ((BindTypeNormal) lelems[0]).name();
367                 } else {
368                   lelemname = ((BindTypeArray) lelems[0]).name();
369                 }
370                 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) {
371                   relemname = ((BindTypeNormal) relems[0]).name();
372                 } else {
373                   relemname = ((BindTypeArray) relems[0]).name();
374                 }
375                 iarray.replaceInfoUsingField(lcompname, lelemname, using, BindConnInfo.REQ);
376                 iarray.replaceInfoUsingField(rcompname, relemname, using, BindConnInfo.PROV);
377               }
378               break;
379           }
380         }
381         return iarray;
382       } else {
383         return null;
384       }
385     } catch (java.rmi.RemoteException JavaDoc e) {
386       System.out.println("RemoteException: "+e.getMessage());
387       e.printStackTrace();
388       System.exit(1);
389     } catch (TIRExceptLock e) {
390       System.out.println("TIRExceptLock: "+e.getMessage());
391       e.printStackTrace();
392       System.exit(1);
393     }
394     return null;
395   }
396
397 }
398
Popular Tags