1 2 package SOFA.Connector.Boot; 3 4 import java.lang.reflect.Method ; 5 6 import javax.xml.parsers.DocumentBuilderFactory ; 7 8 import org.w3c.dom.Document ; 9 import org.w3c.dom.Element ; 10 11 import SOFA.Connector.ConnectorTransportException; 12 13 17 public class Helper { 18 19 private static final String GENERCONNPACKAGE = "SOFA.GeneratedConnectors"; 20 21 23 public static SOFA.SOFAnode.Run.CCNS.ConnectorCacheNS ccns; 24 25 static { 26 try { 27 ccns=SOFA.Connector.CCNSAccess.CCNSAccess.openCCNS(); 28 } catch (SOFA.Connector.CCNSAccess.CCNSAccessException e) { 29 throw new RuntimeException ("Cannot access CCNS.", e); 30 } 31 } 32 33 39 public static SOFA.Connector.RoleBase createCltConn(String cdlName, SOFA.Connector.Reference ref) { 40 try { 41 SOFA.Connector.ECG.DeploymentDescriptor connDD = createCltConnDeplDesc(cdlName, ref); 42 String type = connDD.getConnectorBuilder(); 43 Class cl = Class.forName(type, true, SOFA.SOFAnode.Run.SOFACompClassLoader.getCompClassLoader()); 44 Class [] par = new Class [2]; 45 par[0] = Class.forName("SOFA.Connector.ECG.DeploymentDescriptor"); 46 par[1] = Class.forName("java.lang.ClassLoader"); 47 Method m = cl.getMethod("createConnector", par); 48 Object [] arg = new Object [2]; 49 arg[0] = connDD; 50 arg[1] = SOFA.SOFAnode.Run.SOFACompClassLoader.getCompClassLoader(); 51 SOFA.Connector.RoleBase conn = (SOFA.Connector.RoleBase) m.invoke(null, arg); 52 conn.link(ref); 55 return conn; 56 } catch (ClassNotFoundException e) { 57 throw new ConnectorTransportException("ClassNotFoundException: "+e.getMessage(), e); 58 } catch (NoSuchMethodException e) { 59 throw new ConnectorTransportException("NoSuchMethodException: "+e.getMessage(), e); 60 } catch (IllegalAccessException e) { 61 throw new ConnectorTransportException("IllegalAccessException: "+e.getMessage(), e); 62 } catch (java.lang.reflect.InvocationTargetException e) { 63 throw new ConnectorTransportException("InvocationTargetException: "+e.getMessage(), e); 64 } catch (SOFA.Connector.LinkException e) { 65 throw new ConnectorTransportException("SOFA.Connector.LinkException: "+e.getMessage(), e); 66 } 67 } 68 69 74 public static String getCDLNameOfObject(Object obj) { 75 try { 76 return (String ) (obj.getClass().getField("_CDLID").get(obj)); 77 } catch (Exception e) { 78 return null; 79 } 80 } 81 82 89 public static SOFA.Connector.ECG.DeploymentDescriptor createCltConnDeplDesc(String cdlName, SOFA.Connector.Reference ref) { 90 try { 91 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 92 Element root = doc.createElement("sofa_connector"); 93 doc.appendChild(root); 94 Element el; 95 String hash; 96 if (ref.conn_type.equals("SOFA.Connector.ECG.SGenerator.Connectors.CSProcCallImpl")) { 97 root.setAttribute("name", "NAME"); 98 root.setAttribute("unit", "Client"); 99 root.setAttribute("impl", "SOFA.Connector.ECG.SGenerator.Connectors.CSProcCallImpl"); 100 el = doc.createElement("connector_element"); 101 el.setAttribute("name", "cRole"); 102 if ((hash = translateMangled("CRoleImpl?"+cdlName))==null) 103 throw new ConnectorTransportException("No cRole for reference "+ref); 104 el.setAttribute("impl", GENERCONNPACKAGE + "."+hash+".CRoleImpl"); 105 root.appendChild(el); 106 for (int i=0; i<ref.profiles.length; i++) { 107 if (ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.LocalProfile) { 108 el = doc.createElement("connector_element"); 109 el.setAttribute("name", "stub"); 110 if ((hash = translateMangled("LocalStub?"+cdlName))==null) 111 throw new ConnectorTransportException("No localStub for reference "+ref); 112 el.setAttribute("impl", GENERCONNPACKAGE + "."+hash+".LocalStub"); 113 root.appendChild(el); 114 } else if (ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.RMIProfile) { 115 el = doc.createElement("connector_element"); 116 el.setAttribute("name", "stub"); 117 if ((hash = translateMangled("RMIStub?"+cdlName))==null) 118 throw new ConnectorTransportException("No RMIStub for reference "+ref); 119 el.setAttribute("impl", GENERCONNPACKAGE + "."+hash+".RMIStub"); 120 root.appendChild(el); 121 } else { el = doc.createElement("connector_element"); 123 el.setAttribute("name", "stub"); 124 if ((hash = translateMangled("CORBAStub?"+cdlName))==null) 125 throw new ConnectorTransportException("No CORBAStub for reference "+ref); 126 el.setAttribute("impl", GENERCONNPACKAGE + "."+hash+".CORBAStub"); 127 root.appendChild(el); 128 } 129 } 130 } else if (ref.conn_type.equals("SOFA.Connector.ECG.SGenerator.Connectors.EventPassingImpl")) { 131 throw new ConnectorTransportException("No suport for dynamic addition to EventPassing connector"); 132 } else if (ref.conn_type.equals("SOFA.Connector.ECG.SGenerator.Connectors.DataStreamImpl")) { 133 throw new ConnectorTransportException("No suport for dynamic addition to DataStream connector"); 134 } else { 135 throw new ConnectorTransportException("Unknown connector: "+ref.conn_type); 136 } 137 return new SOFA.Connector.ECG.DeploymentDescriptor(root); 138 } catch (javax.xml.parsers.ParserConfigurationException e) { 139 throw new ConnectorTransportException("Cannot create connector descriptor", e); 140 } catch (java.rmi.RemoteException e) { 141 throw new ConnectorTransportException("RemoteException occured", e); 142 } catch (SOFA.Connector.DeploymentDescriptorException e) { 143 throw new ConnectorTransportException("Cannot create connector descriptor", e); 144 } 145 } 146 147 153 public static String translateMangled(String mangled) throws java.rmi.RemoteException { 154 StringBuffer buf=new StringBuffer (); 155 int i, len=mangled.length(); 156 char a; 157 158 buf.append("EEM__"); 159 160 for (i=0;i<len;i++) { 161 a=mangled.charAt(i); 162 if (a=='_') { 163 buf.append("__"); 164 } else if (a==':') { 165 if (i+1<len && mangled.charAt(i+1)==':') { 166 i++; 167 buf.append("_CC_"); 168 } else { 169 buf.append("_C_"); 170 } 171 } else if (a=='/') { 172 buf.append("_S_"); 173 } else if (a=='.') { 174 buf.append("_D_"); 175 } else if (a=='?') { 176 buf.append("_Q_"); 177 } else if (a=='!') { 178 buf.append("_E_"); 179 } else if (Character.isJavaIdentifierPart(a)) { 180 buf.append(a); 181 } else { 182 buf.append("_"+Integer.toString(a,16)+"_"); 183 } 184 } 185 return ccns.getHashByName2(buf.toString()); 186 } 187 } 188 | Popular Tags |