KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > ECG > ECGenerator


1 /*
2  * ECGenerator.java
3  *
4  * Created on 4. duben 2002, 11:22
5  */

6
7 package SOFA.Connector.ECG;
8
9 import java.io.File JavaDoc;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14
15 import SOFA.SOFAnode.Made.TIR.AttributeDef;
16 import SOFA.SOFAnode.Made.TIR.CDLType;
17 import SOFA.SOFAnode.Made.TIR.Contained;
18 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
19 import SOFA.SOFAnode.Made.TIR.ExceptionDef;
20 import SOFA.SOFAnode.Made.TIR.InterfaceDef;
21 import SOFA.SOFAnode.Made.TIR.OperationDef;
22 import SOFA.SOFAnode.Made.TIR.ParamDescr;
23 import SOFA.SOFAnode.Made.TIR.ParamMode;
24 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods;
25
26 /**
27  *
28  * @author ghort
29  * @version
30  */

31 public class ECGenerator {
32     
33     protected java.util.LinkedList JavaDoc plugin; /* of ECGPluginInterface */
34     
35     protected javax.xml.parsers.DocumentBuilderFactory JavaDoc documentBuilderFactory;
36     protected javax.xml.parsers.DocumentBuilder JavaDoc documentBuilder;
37     
38     protected Document JavaDoc configDoc;
39
40     protected SOFA.Connector.EEG.EEGenerator eeGenerator;
41
42     static public String JavaDoc getTypesPackage() {
43         return "SOFA.Connector.ECG.Types";
44     }
45         
46     /** Creates new EEGenerator */
47     public ECGenerator() throws ECGeneratorException {
48         try {
49             eeGenerator=new SOFA.Connector.EEG.EEGenerator();
50         } catch (SOFA.Connector.EEG.EEGeneratorException e) {
51             throw new ECGeneratorException("Can't initialize EEGenerator.",e);
52         }
53         
54         try {
55             documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
56             documentBuilder=documentBuilderFactory.newDocumentBuilder();
57             
58             configDoc=documentBuilder.parse(new java.io.File JavaDoc(getECGConfigRoot()+"/"+System.getProperty("SOFA.Connector.ECG.Config","conf.xml")));
59             
60             Element JavaDoc conParent=configDoc.getDocumentElement();
61             Node JavaDoc conPlugins, conPlugin, conProp;
62             
63             plugin=new java.util.LinkedList JavaDoc();
64             
65             System.out.println("Loading ECG plugins:");
66             conPlugins=conParent.getFirstChild();
67             while (conPlugins!=null) {
68                 if (conPlugins.getNodeType()==Node.ELEMENT_NODE && conPlugins.getNodeName().equals("ECGPlugins")) {
69                     conPlugin=conPlugins.getFirstChild();
70                     while (conPlugin!=null) {
71                         if (conPlugin.getNodeType()==Node.ELEMENT_NODE && conPlugin.getNodeName().equals("ECGPlugin")) {
72                             
73                             java.util.HashMap JavaDoc props=new java.util.HashMap JavaDoc();
74                             conProp=conPlugin.getFirstChild();
75                             while (conProp!=null) {
76                                 if (conProp.getNodeType()==Node.ELEMENT_NODE) {
77                                     Element JavaDoc el=(Element JavaDoc)conProp;
78                                     if (el.getTagName().equals("property") && el.hasAttribute("name") && el.hasAttribute("value")) {
79                                         props.put(el.getAttribute("name"),el.getAttribute("value"));
80                                     }
81                                 }
82                                 conProp=conProp.getNextSibling();
83                             }
84                             System.out.println(" Loading plugin ("+((Element JavaDoc)conPlugin).getAttribute("class")+"):");
85                             
86                             Class JavaDoc cls=Class.forName(((Element JavaDoc)conPlugin).getAttribute("class"));
87                             Class JavaDoc[] coPar=new Class JavaDoc[2];
88                             coPar[0]=Class.forName("SOFA.Connector.EEG.EEGenerator");
89                             coPar[1]=Class.forName("java.util.HashMap");
90                             java.lang.reflect.Constructor JavaDoc co=cls.getConstructor(coPar);
91                             
92                             Object JavaDoc[] coInst=new Object JavaDoc[2];
93                             coInst[0]=eeGenerator;
94                             coInst[1]=props;
95                             ECGPluginInterface plgObject=(ECGPluginInterface)co.newInstance(coInst);
96                             
97                             plugin.add(plgObject);
98                             System.out.println(" Plugin "+plgObject.getName()+" ("+((Element JavaDoc)conPlugin).getAttribute("class")+") loaded.");
99                         }
100                         conPlugin=conPlugin.getNextSibling();
101                     }
102                 }
103                 conPlugins=conPlugins.getNextSibling();
104             }
105             
106         } catch (Exception JavaDoc e) {
107             throw new ECGeneratorException("Can't parse configuration",e);
108         }
109     }
110     
111     public ConnectorsTechnologyDescriptor canGenerate(ConnectorQuery query) {
112         java.util.Iterator JavaDoc iter;
113         ECGPluginInterface plgObject;
114         ConnectorsTechnologyDescriptor ctDescr=new ConnectorsTechnologyDescriptor();
115         java.util.LinkedList JavaDoc tDescr=new java.util.LinkedList JavaDoc();
116         
117         ctDescr.type=query.type;
118         ctDescr.instanceName=query.instanceName;
119         iter=plugin.iterator();
120         try {
121             for (;;) {
122                 plgObject=(ECGPluginInterface)iter.next();
123                 tDescr.addAll(java.util.Arrays.asList(plgObject.canGenerate(query)));
124             }
125         } catch (java.util.NoSuchElementException JavaDoc e) {
126         }
127
128         int i;
129         Object JavaDoc arr[];
130         ctDescr.technologies=new ConnectorTechnologyDescriptor[(arr=tDescr.toArray()).length];
131         for (i=0;i<arr.length;i++) {
132             ctDescr.technologies[i]=(ConnectorTechnologyDescriptor)arr[i];
133         }
134
135         return ctDescr;
136     }
137     
138     public ConnectorOutputDescriptor generate(ConnectorInputDescriptor conDescr) throws ECGeneratorException {
139         java.util.Iterator JavaDoc iter;
140         ECGPluginInterface plgObject;
141         
142         int colonIndex=conDescr.implementation.indexOf(':');
143         String JavaDoc pluginName;
144         
145         if (colonIndex==-1) {
146             pluginName=conDescr.implementation;
147         } else {
148             pluginName=conDescr.implementation.substring(0,colonIndex);
149         }
150         
151                 SOFA.Connector.Property[] newProps = new SOFA.Connector.Property [conDescr.props.length + 1];
152                 for (int i=0; i<conDescr.props.length; i++) {
153                   newProps[i] = conDescr.props[i];
154                 }
155         String JavaDoc resolverFName = null;
156                 try {
157                   resolverFName = java.io.File.createTempFile("conGen", ".table").getAbsolutePath();
158                   newProps[conDescr.props.length] = new SOFA.Connector.Property("resolverFile", resolverFName);
159                 } catch (java.io.IOException JavaDoc e) {
160                   throw new ECGeneratorException ("Can't create temp file.", e);
161                 }
162                 conDescr.props = newProps;
163                 
164         System.out.println("Generating connector '"+conDescr.implementation+"':");
165
166         iter=plugin.iterator();
167         try {
168             for (;;) {
169                 plgObject=(ECGPluginInterface)iter.next();
170                 if (pluginName.equals(plgObject.getName())) {
171                     try {
172                         ConnectorOutputDescriptor ret;
173                         ret=plgObject.generate(conDescr);
174                         System.out.println("Connector generated.");
175                                                 System.out.print("Augmenting classes of this connector...");
176                         ResolverInfo info = prepareResolverInfo(resolverFName);
177                         callResolver(info);
178                                                 System.out.println("OK");
179                         return ret;
180                     } catch (Exception JavaDoc e) {
181                         throw new ECGeneratorException("Plugin failed",e);
182                     }
183                 }
184             }
185         } catch (java.util.NoSuchElementException JavaDoc e) {
186             throw new ECGeneratorException("Can't generate element.",e);
187         }
188     }
189
190     static public String JavaDoc getECGRoot() throws ECGeneratorException {
191         try {
192             return System.getProperty("SOFA.Connector.Root","SOFA/Connector")+"/ECG";
193         } catch (Exception JavaDoc e) {
194             throw new ECGeneratorException("Can't access property SOFA.Connector.Root",e);
195         }
196     }
197
198     static public String JavaDoc getECGConfigRoot() throws ECGeneratorException {
199         String JavaDoc configDir;
200         try {
201             configDir=System.getProperty("SOFA.Connector.Config");
202         } catch (Exception JavaDoc e) {
203             throw new ECGeneratorException("Can't access property SOFA.Connector.Config",e);
204         }
205         if (configDir==null) {
206             configDir=getECGRoot();
207         } else {
208             configDir+="/ECG";
209         }
210         return configDir;
211     }
212
213   private class ResolverInfo {
214     java.util.Hashtable JavaDoc table;
215     java.util.ArrayList JavaDoc classFiles;
216     java.util.ArrayList JavaDoc outClassFiles;
217     public ResolverInfo() {
218       table = new java.util.Hashtable JavaDoc ();
219       classFiles = new java.util.ArrayList JavaDoc ();
220       outClassFiles = new java.util.ArrayList JavaDoc ();
221     }
222   }
223
224   /** Prepares information for resolver.
225     *
226     * @param fname name with type and class file info
227     * @return resolver info
228     * @throws java.io.IOException io exception
229     */

230   private ResolverInfo prepareResolverInfo(String JavaDoc fname) throws java.io.IOException JavaDoc, java.rmi.RemoteException JavaDoc, SOFA.Connector.EEG.CodeWriter.CodeWriterException, SOFA.SOFAnode.Made.TIR.TIRExceptLock {
231     ResolverInfo info = new ResolverInfo();
232     java.io.BufferedReader JavaDoc fr = new java.io.BufferedReader JavaDoc(new java.io.FileReader JavaDoc(fname));
233     
234     String JavaDoc trDir=System.getProperty("sofa.tr.dir",null);
235     
236     String JavaDoc line = null;
237     while ((line = fr.readLine()) != null) {
238       java.util.ArrayList JavaDoc ifaces = new java.util.ArrayList JavaDoc ();
239       java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(line);
240       
241       String JavaDoc classFile=st.nextToken();
242       String JavaDoc outClassFile=trDir+File.separator+"conn"+File.separator+st.nextToken();
243       
244       while (st.hasMoreTokens()) {
245         ifaces.add(st.nextToken());
246       }
247       for (int i=0; i<ifaces.size(); i++) {
248         InterfaceDef rIface = (InterfaceDef) TIRAccessMethods.lookupCDLContained(SOFA.Connector.TIRAccess.TIRAccess.repository, (String JavaDoc) ifaces.get(i));
249         performCDLType(rIface, info.table);
250         Contained[] op = TIRAccessMethods.getOperAttrOfInterface(SOFA.Connector.TIRAccess.TIRAccess.repository, (String JavaDoc) ifaces.get(i));
251         for (int j=0; j<op.length; j++) {
252           if (op[j] instanceof AttributeDef) {
253             performCDLType(((AttributeDef) op[j]).type(), info.table);
254           } else { // it is OperationDef
255
performCDLType(((OperationDef) op[j]).result(), info.table);
256             ExceptionDef[] exc = ((OperationDef) op[j]).exceptions();
257             for (int k=0; k<exc.length; k++) {
258               performCDLType(exc[k], info.table);
259             }
260             ParamDescr[] par = ((OperationDef) op[j]).params();
261             for (int k=0; k<par.length; k++) {
262               performCDLType(par[k].type(), info.table);
263             }
264           }
265         }
266       }
267       info.classFiles.add(classFile);
268       info.outClassFiles.add(outClassFile);
269     }
270     fr.close();
271     return info;
272   }
273
274   private void performCDLType(CDLType type, java.util.Hashtable JavaDoc table) throws java.rmi.RemoteException JavaDoc, SOFA.Connector.EEG.CodeWriter.CodeWriterException {
275     if (type.get_def_kind().value() != DefinitionKind.dk_Primitive) {
276       String JavaDoc javaType = SOFA.Connector.EEG.CodeWriter.CDL2JavaMapping.getTypeName(type, ParamMode.PARAM_IN);
277       String JavaDoc javaTypeHolder = SOFA.Connector.EEG.CodeWriter.CDL2JavaMapping.getTypeName(type, ParamMode.PARAM_OUT);
278       String JavaDoc version = ((Contained) type).get_identification().version();
279       table.put(SOFA.Tools.Resolver.Resolver.classNameToVMClassName(javaType), SOFA.Tools.Resolver.Resolver.cdlClassNameToVMClassName(javaType, version));
280       table.put(SOFA.Tools.Resolver.Resolver.classNameToVMClassName(javaTypeHolder), SOFA.Tools.Resolver.Resolver.cdlClassNameToVMClassName(javaTypeHolder, version));
281     }
282     
283   }
284
285   private void callResolver(ResolverInfo info) throws java.io.IOException JavaDoc {
286     SOFA.Tools.Resolver.Resolver resolver = new SOFA.Tools.Resolver.Resolver (info.table);
287     for(int i=0; i<info.classFiles.size(); i++) {
288       java.io.FileInputStream JavaDoc is = new java.io.FileInputStream JavaDoc((String JavaDoc) info.classFiles.get(i));
289       byte[] b = resolver.process(is);
290       is.close();
291       
292       (new File JavaDoc((String JavaDoc)info.outClassFiles.get(i))).getParentFile().mkdirs();
293       java.io.FileOutputStream JavaDoc os = new java.io.FileOutputStream JavaDoc ((String JavaDoc) info.outClassFiles.get(i));
294       os.write(b);
295       os.close();
296     }
297   }
298 }
299
Popular Tags