KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > ConnectorProtocolGen > GenFactory


1 /* $Id: GenFactory.java,v 1.2 2004/05/20 14:23:53 bures Exp $ */
2 package SOFA.SOFAnode.Util.ConnectorProtocolGen;
3
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 import org.w3c.dom.Document JavaDoc;
8 import org.w3c.dom.Element JavaDoc;
9 import org.w3c.dom.Node JavaDoc;
10
11 import SOFA.SOFAnode.Made.CDL.BadBindException;
12 import SOFA.SOFAnode.Made.CDL.CompBind;
13 import SOFA.SOFAnode.Made.CDL.DetectedConnectors;
14
15 /** Class providing public methods for working with connector protocol generators.
16   * @author Tomas Bures
17   */

18 public final class GenFactory {
19   
20     protected static java.util.HashMap JavaDoc generators=new java.util.HashMap JavaDoc();
21
22   /** Initializes the GenFactory.
23     * @throws CannotInitException thrown when it is not possible to initialize the factory. (e.g. bad configuration file)
24     */

25   public static void init() throws CannotInitException {
26     
27     generators.put("CSProcCall","SOFA.SOFAnode.Util.ConnectorProtocolGen.CSProcCall");
28
29     String JavaDoc confFile=System.getProperty("SOFA.SOFAnode.Util.ConnectorProtocolGen.GeneratorsConfig");
30
31     if (confFile!=null) {
32       try {
33         javax.xml.parsers.DocumentBuilderFactory JavaDoc documentBuilderFactory;
34         javax.xml.parsers.DocumentBuilder JavaDoc documentBuilder;
35
36         documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
37         documentBuilder=documentBuilderFactory.newDocumentBuilder();
38         
39         Document JavaDoc configDoc=documentBuilder.parse(new java.io.File JavaDoc(confFile));
40
41         Element JavaDoc gens=configDoc.getDocumentElement();
42
43         System.out.println("Loading connector protocol generators specification:");
44
45         Node JavaDoc gen=gens.getFirstChild();
46         while (gen!=null) {
47           if (gen.getNodeType()==Node.ELEMENT_NODE && gen.getNodeName().equals("generator")) {
48             Element JavaDoc elg=(Element JavaDoc)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 JavaDoc e) {
58         throw new CannotInitException("Can't parse configuration.",e);
59       }
60     }
61   }
62
63   /** Combines bindings which belong to one connector.
64     * @param detectedConnectors list of so far identified connectors
65     * @param componentBinding binding of two component interfaces (via keyword "using")
66     * @throws BadBindException thrown if the binding is invalid (e.g. interface is connected using different connector types)
67     */

68   public static void addDetectedConnector(String JavaDoc[] connectorOperations, DetectedConnectors detectedConnectors, CompBind componentBinding) throws GeneratorNotFoundException, BadBindException {
69     String JavaDoc connectorType=componentBinding.using;
70     String JavaDoc className=(String JavaDoc)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 JavaDoc genClass=Class.forName(className);
79       Method JavaDoc mt=genClass.getMethod("addDetectedConnector",new Class JavaDoc[] {String JavaDoc[].class, DetectedConnectors.class, CompBind.class});
80       mt.invoke(null,new Object JavaDoc[] {connectorOperations,detectedConnectors,componentBinding});
81     } catch (InvocationTargetException JavaDoc e) {
82       throw (BadBindException)e.getCause();
83     } catch (Exception JavaDoc e) {
84       throw new GeneratorNotFoundException("Cannot call static method 'addDetectedConnector' in class '"+className+"' for connector type '"+connectorType+"'.",e);
85     }
86   }
87
88   /** Creates a new connector protocol generator according to connector type
89     * @param connectorType type of connector (e.g. "CSProcCall")
90     * @return created connector protocol generator
91     * @throws GeneratorNotFoundException thrown if the specified connector type is not registered
92     */

93   public static ConnectorProtocolGen getConnectorProtocolGen(String JavaDoc connectorType) throws GeneratorNotFoundException {
94     String JavaDoc className=(String JavaDoc)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 JavaDoc genClass=Class.forName(className);
101       return (ConnectorProtocolGen)genClass.newInstance();
102     } catch (Exception JavaDoc e) {
103       throw new GeneratorNotFoundException("Cannot intantiate class '"+className+"' for connector type '"+connectorType+"'.",e);
104     }
105   }
106 }
107
108
Popular Tags