1 6 7 package SOFA.Connector.ECG; 8 9 import java.io.File ; 10 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Node ; 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 31 public class ECGenerator { 32 33 protected java.util.LinkedList plugin; 34 35 protected javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory; 36 protected javax.xml.parsers.DocumentBuilder documentBuilder; 37 38 protected Document configDoc; 39 40 protected SOFA.Connector.EEG.EEGenerator eeGenerator; 41 42 static public String getTypesPackage() { 43 return "SOFA.Connector.ECG.Types"; 44 } 45 46 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 (getECGConfigRoot()+"/"+System.getProperty("SOFA.Connector.ECG.Config","conf.xml"))); 59 60 Element conParent=configDoc.getDocumentElement(); 61 Node conPlugins, conPlugin, conProp; 62 63 plugin=new java.util.LinkedList (); 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 props=new java.util.HashMap (); 74 conProp=conPlugin.getFirstChild(); 75 while (conProp!=null) { 76 if (conProp.getNodeType()==Node.ELEMENT_NODE) { 77 Element el=(Element )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 )conPlugin).getAttribute("class")+"):"); 85 86 Class cls=Class.forName(((Element )conPlugin).getAttribute("class")); 87 Class [] coPar=new Class [2]; 88 coPar[0]=Class.forName("SOFA.Connector.EEG.EEGenerator"); 89 coPar[1]=Class.forName("java.util.HashMap"); 90 java.lang.reflect.Constructor co=cls.getConstructor(coPar); 91 92 Object [] coInst=new Object [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 )conPlugin).getAttribute("class")+") loaded."); 99 } 100 conPlugin=conPlugin.getNextSibling(); 101 } 102 } 103 conPlugins=conPlugins.getNextSibling(); 104 } 105 106 } catch (Exception e) { 107 throw new ECGeneratorException("Can't parse configuration",e); 108 } 109 } 110 111 public ConnectorsTechnologyDescriptor canGenerate(ConnectorQuery query) { 112 java.util.Iterator iter; 113 ECGPluginInterface plgObject; 114 ConnectorsTechnologyDescriptor ctDescr=new ConnectorsTechnologyDescriptor(); 115 java.util.LinkedList tDescr=new java.util.LinkedList (); 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 e) { 126 } 127 128 int i; 129 Object 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 iter; 140 ECGPluginInterface plgObject; 141 142 int colonIndex=conDescr.implementation.indexOf(':'); 143 String 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 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 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 e) { 181 throw new ECGeneratorException("Plugin failed",e); 182 } 183 } 184 } 185 } catch (java.util.NoSuchElementException e) { 186 throw new ECGeneratorException("Can't generate element.",e); 187 } 188 } 189 190 static public String getECGRoot() throws ECGeneratorException { 191 try { 192 return System.getProperty("SOFA.Connector.Root","SOFA/Connector")+"/ECG"; 193 } catch (Exception e) { 194 throw new ECGeneratorException("Can't access property SOFA.Connector.Root",e); 195 } 196 } 197 198 static public String getECGConfigRoot() throws ECGeneratorException { 199 String configDir; 200 try { 201 configDir=System.getProperty("SOFA.Connector.Config"); 202 } catch (Exception 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 table; 215 java.util.ArrayList classFiles; 216 java.util.ArrayList outClassFiles; 217 public ResolverInfo() { 218 table = new java.util.Hashtable (); 219 classFiles = new java.util.ArrayList (); 220 outClassFiles = new java.util.ArrayList (); 221 } 222 } 223 224 230 private ResolverInfo prepareResolverInfo(String fname) throws java.io.IOException , java.rmi.RemoteException , SOFA.Connector.EEG.CodeWriter.CodeWriterException, SOFA.SOFAnode.Made.TIR.TIRExceptLock { 231 ResolverInfo info = new ResolverInfo(); 232 java.io.BufferedReader fr = new java.io.BufferedReader (new java.io.FileReader (fname)); 233 234 String trDir=System.getProperty("sofa.tr.dir",null); 235 236 String line = null; 237 while ((line = fr.readLine()) != null) { 238 java.util.ArrayList ifaces = new java.util.ArrayList (); 239 java.util.StringTokenizer st = new java.util.StringTokenizer (line); 240 241 String classFile=st.nextToken(); 242 String 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 ) ifaces.get(i)); 249 performCDLType(rIface, info.table); 250 Contained[] op = TIRAccessMethods.getOperAttrOfInterface(SOFA.Connector.TIRAccess.TIRAccess.repository, (String ) 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 { 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 table) throws java.rmi.RemoteException , SOFA.Connector.EEG.CodeWriter.CodeWriterException { 275 if (type.get_def_kind().value() != DefinitionKind.dk_Primitive) { 276 String javaType = SOFA.Connector.EEG.CodeWriter.CDL2JavaMapping.getTypeName(type, ParamMode.PARAM_IN); 277 String javaTypeHolder = SOFA.Connector.EEG.CodeWriter.CDL2JavaMapping.getTypeName(type, ParamMode.PARAM_OUT); 278 String 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 { 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 is = new java.io.FileInputStream ((String ) info.classFiles.get(i)); 289 byte[] b = resolver.process(is); 290 is.close(); 291 292 (new File ((String )info.outClassFiles.get(i))).getParentFile().mkdirs(); 293 java.io.FileOutputStream os = new java.io.FileOutputStream ((String ) info.outClassFiles.get(i)); 294 os.write(b); 295 os.close(); 296 } 297 } 298 } 299 | Popular Tags |