KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > Boot > Helper


1 /* $Id: Helper.java,v 1.2 2004/05/20 14:23:50 bures Exp $ */
2 package SOFA.Connector.Boot;
3
4 import java.lang.reflect.Method JavaDoc;
5
6 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
7
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.Element JavaDoc;
10
11 import SOFA.Connector.ConnectorTransportException;
12
13 /** Class with helper static methods.
14   *
15   * @author Petr Hnetynka
16   */

17 public class Helper {
18
19   private static final String JavaDoc GENERCONNPACKAGE = "SOFA.GeneratedConnectors";
20
21   /** Refernce to CCNS
22     */

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 JavaDoc("Cannot access CCNS.", e);
30     }
31   }
32
33   /** Creates client side part of the connector for specified CDL type and the reference.
34     *
35     * @param cdlName cdl full name
36     * @param ref SOFA reference to the connector (server side)
37     * @return client side of the connector
38     */

39   public static SOFA.Connector.RoleBase createCltConn(String JavaDoc cdlName, SOFA.Connector.Reference ref) {
40     try {
41       SOFA.Connector.ECG.DeploymentDescriptor connDD = createCltConnDeplDesc(cdlName, ref);
42       String JavaDoc type = connDD.getConnectorBuilder();
43       Class JavaDoc cl = Class.forName(type, true, SOFA.SOFAnode.Run.SOFACompClassLoader.getCompClassLoader());
44       Class JavaDoc[] par = new Class JavaDoc[2];
45       par[0] = Class.forName("SOFA.Connector.ECG.DeploymentDescriptor");
46       par[1] = Class.forName("java.lang.ClassLoader");
47       Method JavaDoc m = cl.getMethod("createConnector", par);
48       Object JavaDoc[] arg = new Object JavaDoc[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.setThrIDRegistry(thrRegistry);
53
// conn.setRegister(this);
54
conn.link(ref);
55       return conn;
56     } catch (ClassNotFoundException JavaDoc e) {
57       throw new ConnectorTransportException("ClassNotFoundException: "+e.getMessage(), e);
58     } catch (NoSuchMethodException JavaDoc e) {
59       throw new ConnectorTransportException("NoSuchMethodException: "+e.getMessage(), e);
60     } catch (IllegalAccessException JavaDoc e) {
61       throw new ConnectorTransportException("IllegalAccessException: "+e.getMessage(), e);
62     } catch (java.lang.reflect.InvocationTargetException JavaDoc 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   /** Returns full name of the CDL object from which the object was generated.
70     *
71     * @param obj object
72     * @return CDL full name or null;
73     */

74   public static String JavaDoc getCDLNameOfObject(Object JavaDoc obj) {
75     try {
76       return (String JavaDoc) (obj.getClass().getField("_CDLID").get(obj));
77     } catch (Exception JavaDoc e) {
78       return null;
79     }
80   }
81
82   /** Creates connector deployment descriptor for client side of the connector
83     * to the specified cdl type.
84     *
85     * @param cdlName cdl full name
86     * @param ref SOFA reference to the connector (server side)
87     * @return connector depl. descriptor
88     */

89   public static SOFA.Connector.ECG.DeploymentDescriptor createCltConnDeplDesc(String JavaDoc cdlName, SOFA.Connector.Reference ref) {
90     try {
91       Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
92       Element JavaDoc root = doc.createElement("sofa_connector");
93       doc.appendChild(root);
94       Element JavaDoc el;
95       String JavaDoc 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 { // CORBA stub
122
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 JavaDoc e) {
139       throw new ConnectorTransportException("Cannot create connector descriptor", e);
140     } catch (java.rmi.RemoteException JavaDoc 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   /** Translated mangled version of the name.
148     *
149     * @param mangled name to be translated
150     * @return translated name
151     * @throws java.rmi.RemoteException remote exception
152     */

153   public static String JavaDoc translateMangled(String JavaDoc mangled) throws java.rmi.RemoteException JavaDoc {
154     StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
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