KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > Tools > MakeAppl


1 /* $Id: MakeAppl.java,v 1.4 2004/06/03 14:52:49 bures Exp $ */
2 package SOFA.SOFAnode.Made.Tools;
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5
6 import javax.xml.transform.Transformer JavaDoc;
7 import javax.xml.transform.TransformerConfigurationException JavaDoc;
8 import javax.xml.transform.TransformerException JavaDoc;
9 import javax.xml.transform.TransformerFactory JavaDoc;
10 import javax.xml.transform.dom.DOMSource JavaDoc;
11 import javax.xml.transform.stream.StreamResult JavaDoc;
12
13 import org.w3c.dom.Document JavaDoc;
14 import org.w3c.dom.Element JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 import SOFA.SOFAnode.Made.TIR.Repository;
19 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods;
20 import SOFA.SOFAnode.Run.SpecFile;
21
22 /** Make application from separated depldescriptors in TR.
23   * 1st argument is full application name
24   *
25   * @author Petr Hnetynka
26   */

27 public class MakeAppl {
28   private static String JavaDoc trImplRoot;
29   
30   public static void main(String JavaDoc[] argv) {
31     if (argv.length < 1) {
32       System.out.println("Too few arguments.");
33       System.out.println(" 1st argument - name of the specfile");
34       System.exit(1);
35     }
36     
37     SpecFile specFile = null;
38     try {
39       specFile = SpecFile.getSpecFileFromFile(argv[0]);
40     } catch (java.io.IOException JavaDoc e) {
41       System.out.println("IOException: "+e.getMessage());
42       System.exit(1);
43     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
44       System.out.println("ParserConfigurationException: "+e.getMessage() );
45       System.exit(1);
46     } catch (org.xml.sax.SAXException JavaDoc e) {
47       System.out.println("SAXException: "+e.getMessage() );
48       System.exit(1);
49     }
50
51     Element JavaDoc specFileElement = specFile.getRootElement();
52     
53     String JavaDoc trDir = System.getProperty("sofa.tr.dir", null);
54     if (trDir == null) {
55       System.out.println("Specify TR directrory (java property \"sofa.tr.dir\")");
56       System.exit(1);
57     }
58
59     trImplRoot = trDir + File.separator + "impl" + File.separator;
60     
61
62     Repository tir = null;
63     try {
64       tir = TIRAccessMethods.getRepository();
65     } catch (java.net.MalformedURLException JavaDoc e) {
66       System.out.println("MalformedURLException: "+e.getMessage() );
67       System.exit(1);
68     } catch (java.rmi.NotBoundException JavaDoc e) {
69       System.out.println("NotBoundException: "+e.getMessage() );
70       System.exit(1);
71     } catch (java.rmi.RemoteException JavaDoc e) {
72       System.out.println("RemoteException: "+e.getMessage() );
73       System.exit(1);
74     }
75     
76     processSpecFileElement(tir, null, specFileElement);
77
78     try {
79       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
80       Transformer JavaDoc transformer = tFactory.newTransformer();
81       transformer.setOutputProperty("indent", "yes");
82       transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
83       DOMSource JavaDoc source = new DOMSource JavaDoc(specFileElement);
84       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc( trImplRoot + fullNameToFileName(SpecFile.getFullApplName(specFileElement), "run.dc"));
85       StreamResult JavaDoc result = new StreamResult JavaDoc(out);
86       transformer.transform(source, result);
87       out.close();
88     } catch (TransformerConfigurationException JavaDoc e) {
89       System.out.println("TransformerConfigurationException: "+e.getMessage() );
90       System.exit(1);
91     } catch (TransformerException JavaDoc e) {
92       System.out.println("TransformerException: "+e.getMessage() );
93       System.exit(1);
94     } catch (java.io.IOException JavaDoc e) {
95       System.out.println("IOException: "+e.getMessage());
96       System.exit(1);
97     }
98     
99   }
100
101   private static void processSpecFileElement(Repository tir, SOFA.SOFAnode.Run.GenSpecFile.InfoArray riarray, Element JavaDoc specFileElem) {
102     Document JavaDoc specDoc = specFileElem.getOwnerDocument();
103     String JavaDoc fullName = SpecFile.getFullApplName(specFileElem);
104 // System.out.println(fullName);
105
String JavaDoc archName = SpecFile.getFullArchName(specFileElem);
106     String JavaDoc inst = specFileElem.getAttribute("name");
107     SOFA.SOFAnode.Run.GenSpecFile.InfoArray iarray = SOFA.SOFAnode.Run.GenSpecFile.getBindConnInfo(tir, archName);
108     Element JavaDoc archRootElement = null;
109     try {
110       archRootElement = InstallToTR.getDDElem(trImplRoot + fullNameToFileName(fullName, "index.dc") );
111     } catch (java.io.IOException JavaDoc e) {
112       System.out.println("IOException: "+e.getMessage());
113       System.exit(1);
114     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
115       System.out.println("ParserConfigurationException: "+e.getMessage() );
116       System.exit(1);
117     } catch (org.xml.sax.SAXException JavaDoc e) {
118       System.out.println("SAXException: "+e.getMessage() );
119       System.exit(1);
120     }
121     java.util.ArrayList JavaDoc cons = getAllElements(specFileElem, "connector");
122     for (int i=0; i<cons.size(); i++) {
123       processCon(riarray, inst, (Element JavaDoc) cons.get(i));
124       removeTextNodes((Element JavaDoc) cons.get(i));
125     }
126     String JavaDoc version = getElement(specFileElem, "version").getFirstChild().getNodeValue();
127     Node JavaDoc specFileNextVerNode = getElement(specFileElem, "version").getNextSibling();
128     Element JavaDoc cdlEnts = getElement(archRootElement, "cdl_entities");
129     removeTextNodes(cdlEnts);
130     java.util.ArrayList JavaDoc impls = getAllElements(archRootElement, "implementation");
131     specFileElem.insertBefore(specDoc.importNode(cdlEnts, true), specFileNextVerNode);
132     for (int i=0; i<impls.size(); i++) {
133       Element JavaDoc implEl = (Element JavaDoc) specDoc.importNode((Element JavaDoc) impls.get(i), true);
134      /* if (implEl.getAttribute("type").compareTo("builder")==0) {
135         Node fn = implEl.getFirstChild();
136         if (fn != null) {
137           String b = fn.getNodeValue();
138           b += InstallToTR.implVersionToFileName(version);
139           fn.setNodeValue(b);
140         }
141       } */

142       specFileElem.insertBefore(implEl, specFileNextVerNode);
143     }
144     java.util.ArrayList JavaDoc comps = getAllElements(specFileElem, "sofa_component");
145     for (int i=0; i<comps.size(); i++) {
146       processSpecFileElement(tir, iarray, (Element JavaDoc) comps.get(i));
147     }
148     java.util.ArrayList JavaDoc units = getAllElements(specFileElem, "unit");
149     for (int i=0; i<units.size(); i++) {
150       comps = getAllElements((Element JavaDoc) units.get(i) , "sofa_component");
151       for (int j=0; j<comps.size(); j++) {
152         processSpecFileElement(tir, iarray, (Element JavaDoc) comps.get(j));
153       }
154     }
155   }
156
157   private static Element JavaDoc addApplName(Element JavaDoc element) {
158     NodeList JavaDoc nl = element.getChildNodes();
159     for (int i=0; i<nl.getLength(); i++) {
160       Node JavaDoc n = nl.item(i);
161       if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
162         String JavaDoc ar = n.getFirstChild().getNodeValue();
163         ar = ar.substring(ar.lastIndexOf(':')+1, ar.indexOf('?'));
164         element.setAttribute("name", ar);
165       }
166     }
167     return element;
168   }
169
170   private static String JavaDoc fullNameToFileName(String JavaDoc clname, String JavaDoc fname) {
171 // clname = clname.replaceAll("::", File.separator);
172
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
173     boolean s = true;
174     for (int i=0; i<clname.length(); i++) {
175       if (clname.charAt(i)==':') {
176         if (s) {
177           sb.append(":");
178           s = false;
179         }
180       } else {
181         s = true;
182         sb.append(clname.charAt(i));
183       }
184     }
185     clname = sb.toString().replace(':', File.separatorChar);
186     clname = clname.replace('[', File.separatorChar);
187     clname = clname.replace(']', File.separatorChar);
188     clname = clname + fname;
189     return clname;
190   }
191   
192   private static Element JavaDoc getElement(Element JavaDoc element, String JavaDoc name) {
193     NodeList JavaDoc nl = element.getChildNodes();
194     for (int i=0; i<nl.getLength(); i++) {
195       Node JavaDoc n = nl.item(i);
196       if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) {
197         return (Element JavaDoc) n;
198       }
199     }
200     return null;
201   }
202   
203   private static java.util.ArrayList JavaDoc getAllElements(Element JavaDoc element, String JavaDoc name) {
204     java.util.ArrayList JavaDoc ret = new java.util.ArrayList JavaDoc();
205     NodeList JavaDoc nl = element.getChildNodes();
206     for (int i=0; i<nl.getLength(); i++) {
207       Node JavaDoc n = nl.item(i);
208       if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) {
209         ret.add(n);
210       }
211     }
212     return ret;
213   }
214
215   private static final int CSR = 0;
216   private static final int CSP = 1;
217   private static final int EPR = 2;
218   private static final int EPP = 3;
219   private static final int DSR = 4;
220   private static final int DSP = 5;
221
222   private static void processCon(SOFA.SOFAnode.Run.GenSpecFile.InfoArray riarray, String JavaDoc inst, Element JavaDoc conEl) {
223     Document JavaDoc doc = conEl.getOwnerDocument();
224     String JavaDoc connName = conEl.getAttribute("name");
225     String JavaDoc connFrame = conEl.getAttribute("frame");
226 // System.out.println(connFrame);
227

228     int ckind = 0;
229     if (connName.startsWith("_delegate_")) {
230       conEl.removeAttribute("frame");
231       conEl.setAttribute("impl", "SGenerator:CSProcCallImpl");
232       conEl.setAttribute("unit", "Client");
233       ckind = CSR;
234     } else if (connName.startsWith("_subsume_")) {
235       conEl.removeAttribute("frame");
236       conEl.setAttribute("impl", "SGenerator:CSProcCallImpl");
237       conEl.setAttribute("unit", "Server");
238       ckind = CSP;
239     } else if (connFrame.compareTo("SGenerator:CSProcCallImpl")==0) {
240       conEl.removeAttribute("frame");
241       conEl.setAttribute("impl", "SGenerator:CSProcCallImpl");
242       SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName);
243       if (bi == null) {
244         System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+".");
245         System.out.println(" You will have to correct the resulting run.dc file by yourself.");
246 // System.exit(1);
247
conEl.setAttribute("unit", "!!! set unit here !!!");
248       } else {
249         if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) {
250           conEl.setAttribute("unit", "Server");
251           ckind = CSP;
252         } else {
253           conEl.setAttribute("unit", "Client");
254           ckind = CSR;
255         }
256       }
257     } else if (connFrame.compareTo("SGenerator:EventPassingImpl")==0) {
258       conEl.removeAttribute("frame");
259       conEl.setAttribute("impl", "SGenerator:EventPassingImpl");
260       SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName);
261       if (bi == null) {
262         System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+".");
263         System.out.println(" You will have to correct the resulting run.dc file by yourself.");
264 // System.exit(1);
265
conEl.setAttribute("unit", "!!! set unit here !!!");
266       } else {
267         if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) {
268           conEl.setAttribute("unit", "Consumer");
269           ckind = EPP;
270         } else {
271           conEl.setAttribute("unit", "Supplier");
272           ckind = EPR;
273         }
274       }
275     } else if (connFrame.compareTo("SGenerator:DataStreamImpl")==0) {
276       conEl.removeAttribute("frame");
277       conEl.setAttribute("impl", "SGenerator:DataStreamImpl");
278       SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName);
279       if (bi == null) {
280         System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+".");
281         System.out.println(" You will have to correct the resulting run.dc file by yourself.");
282 // System.exit(1);
283
conEl.setAttribute("unit", "!!! set unit here !!!");
284       } else {
285         if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) {
286           conEl.setAttribute("unit", "Receiver");
287           ckind = DSP;
288           conEl.setAttribute("requires","::SOFA::Connector::EEG::Types::DataStream::OutputStream?nenya.ms.mff.cuni.cz!0");
289           conEl.setAttribute("provides","::SOFA::Connector::EEG::Types::DataStream::InputStream?nenya.ms.mff.cuni.cz!0");
290         } else {
291           conEl.setAttribute("unit", "Sender");
292           ckind = DSR;
293           conEl.setAttribute("provides","::SOFA::Connector::EEG::Types::DataStream::OutputStream?nenya.ms.mff.cuni.cz!0");
294           conEl.setAttribute("requires","::SOFA::Connector::EEG::Types::DataStream::InputStream?nenya.ms.mff.cuni.cz!0");
295         }
296       }
297
298     } else {
299       System.out.println("Unknown connector frame: "+connFrame);
300       System.exit(1);
301     }
302     // now process transport types
303
Element JavaDoc newEl = null;
304     NodeList JavaDoc nl = conEl.getChildNodes();
305     for (int i=0; i<nl.getLength(); i++) {
306       Node JavaDoc n = nl.item(i);
307       if (n.getNodeType()==Node.ELEMENT_NODE) {
308         if (n.getNodeName().compareTo("transport")==0) {
309           String JavaDoc type = ((Element JavaDoc)n).getAttribute("type");
310           switch (ckind) {
311           case CSP:
312             conEl.removeChild(n);
313             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sRole", "impl", "EEM:SRoleImpl"));
314             if (type.compareTo("LOCAL")==0) {
315               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:LocalSkeleton"));
316             } else if (type.compareTo("RMI")==0) {
317               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:RMISkeleton"));
318             } else if (type.compareTo("CORBA")==0) {
319               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:CORBASkeleton"));
320             } else {
321               System.out.println("Unknown transport type" + type);
322               System.exit(1);
323             }
324             break;
325           case CSR:
326             conEl.removeChild(n);
327             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "cRole", "impl", "EEM:CRoleImpl"));
328             if (type.compareTo("LOCAL")==0) {
329               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:LocalStub"));
330             } else if (type.compareTo("RMI")==0) {
331               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:RMIStub"));
332             } else if (type.compareTo("CORBA")==0) {
333               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:CORBAStub"));
334             } else {
335               System.out.println("Unknown transport type" + type);
336               System.exit(1);
337             }
338             break;
339           case EPR:
340             conEl.removeChild(n);
341             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sRole", "impl", "EEM:CRoleImpl"));
342             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "distributor", "impl", "EEM:DistributorImpl"));
343             if (type.compareTo("LOCAL")==0) {
344               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:LocalStub"));
345             } else if (type.compareTo("RMI")==0) {
346               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:RMIStub"));
347             } else if (type.compareTo("CORBA")==0) {
348               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:CORBAStub"));
349             } else {
350               System.out.println("Unknown transport type" + type);
351               System.exit(1);
352             }
353             break;
354           case EPP:
355             conEl.removeChild(n);
356             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "cRole", "impl", "EEM:SRoleImpl"));
357             if (type.compareTo("LOCAL")==0) {
358               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:LocalSkeleton"));
359             } else if (type.compareTo("RMI")==0) {
360               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:RMISkeleton"));
361             } else if (type.compareTo("CORBA")==0) {
362               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:CORBASkeleton"));
363             } else {
364               System.out.println("Unknown transport type" + type);
365               System.exit(1);
366             }
367             break;
368           case DSP:
369             conEl.removeChild(n);
370             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "pushRRole", "impl", "EEM:SRoleImpl"));
371             if (type.compareTo("LOCAL")==0) {
372               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "receiver", "impl", "EEM:DataStreamLocalReceiver"));
373               conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "senderMode", "value", "push"));
374               conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "receiverMode", "value", "push"));
375             } else {
376               System.out.println("DataStream can be only LOCAL.");
377               System.exit(1);
378             }
379             break;
380           case DSR:
381             conEl.removeChild(n);
382             conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "pushSRole", "impl", "EEM:CRoleImpl"));
383             if (type.compareTo("LOCAL")==0) {
384               conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sender", "impl", "EEM:DataStreamLocalSender"));
385               conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "senderMode", "value", "push"));
386               conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "receiverMode", "value", "push"));
387             } else {
388               System.out.println("DataStream can be only LOCAL.");
389               System.exit(1);
390             }
391             break;
392           }
393         }
394       }
395     }
396     
397   }
398
399   private static Element JavaDoc newElemWithTwoAttribs(Document JavaDoc doc, String JavaDoc elName, String JavaDoc name1, String JavaDoc value1, String JavaDoc name2, String JavaDoc value2) {
400     Element JavaDoc newEl = doc.createElement(elName);
401     newEl.setAttribute(name1, value1);
402     newEl.setAttribute(name2, value2);
403     return newEl;
404   }
405
406   private static void removeTextNodes(Element JavaDoc el) {
407     NodeList JavaDoc nl = el.getChildNodes();
408     for (int i=0; i<nl.getLength(); i++) {
409       Node JavaDoc n = nl.item(i);
410       if (n.getNodeType() == Node.TEXT_NODE) {
411         el.removeChild(n);
412       }
413     }
414   }
415 }
416
Popular Tags