1 2 package SOFA.SOFAnode.Util.ConnectorProtocolGen; 3 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 7 import org.w3c.dom.Document ; 8 import org.w3c.dom.Element ; 9 import org.w3c.dom.Node ; 10 11 import SOFA.SOFAnode.Made.CDL.BadBindException; 12 import SOFA.SOFAnode.Made.CDL.CompBind; 13 import SOFA.SOFAnode.Made.CDL.DetectedConnectors; 14 15 18 public final class GenFactory { 19 20 protected static java.util.HashMap generators=new java.util.HashMap (); 21 22 25 public static void init() throws CannotInitException { 26 27 generators.put("CSProcCall","SOFA.SOFAnode.Util.ConnectorProtocolGen.CSProcCall"); 28 29 String confFile=System.getProperty("SOFA.SOFAnode.Util.ConnectorProtocolGen.GeneratorsConfig"); 30 31 if (confFile!=null) { 32 try { 33 javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory; 34 javax.xml.parsers.DocumentBuilder documentBuilder; 35 36 documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance(); 37 documentBuilder=documentBuilderFactory.newDocumentBuilder(); 38 39 Document configDoc=documentBuilder.parse(new java.io.File (confFile)); 40 41 Element gens=configDoc.getDocumentElement(); 42 43 System.out.println("Loading connector protocol generators specification:"); 44 45 Node gen=gens.getFirstChild(); 46 while (gen!=null) { 47 if (gen.getNodeType()==Node.ELEMENT_NODE && gen.getNodeName().equals("generator")) { 48 Element elg=(Element )gen; 49 50 if (!(elg.hasAttribute("type") && elg.hasAttribute("class"))) { 51 throw new CannotInitException("Missing attributes in connector declaration."); 52 } 53 generators.put(elg.getAttribute("type"),elg.getAttribute("class")); 54 } 55 gen=gen.getNextSibling(); 56 } 57 } catch (Exception e) { 58 throw new CannotInitException("Can't parse configuration.",e); 59 } 60 } 61 } 62 63 68 public static void addDetectedConnector(String [] connectorOperations, DetectedConnectors detectedConnectors, CompBind componentBinding) throws GeneratorNotFoundException, BadBindException { 69 String connectorType=componentBinding.using; 70 String className=(String )generators.get(connectorType); 71 72 if (className==null) { 73 throw new GeneratorNotFoundException("Connector type '"+connectorType+"' in not registered in GenFactory."); 74 } 75 className+="Binding"; 76 77 try { 78 Class genClass=Class.forName(className); 79 Method mt=genClass.getMethod("addDetectedConnector",new Class [] {String [].class, DetectedConnectors.class, CompBind.class}); 80 mt.invoke(null,new Object [] {connectorOperations,detectedConnectors,componentBinding}); 81 } catch (InvocationTargetException e) { 82 throw (BadBindException)e.getCause(); 83 } catch (Exception e) { 84 throw new GeneratorNotFoundException("Cannot call static method 'addDetectedConnector' in class '"+className+"' for connector type '"+connectorType+"'.",e); 85 } 86 } 87 88 93 public static ConnectorProtocolGen getConnectorProtocolGen(String connectorType) throws GeneratorNotFoundException { 94 String className=(String )generators.get(connectorType); 95 if (className==null) { 96 throw new GeneratorNotFoundException("Connector type '"+connectorType+"' in not registered in GenFactory."); 97 } 98 99 try { 100 Class genClass=Class.forName(className); 101 return (ConnectorProtocolGen)genClass.newInstance(); 102 } catch (Exception e) { 103 throw new GeneratorNotFoundException("Cannot intantiate class '"+className+"' for connector type '"+connectorType+"'.",e); 104 } 105 } 106 } 107 108 | Popular Tags |