KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > Deployment > DeploymentDescriptorImpl


1 /* $Id: DeploymentDescriptorImpl.java,v 1.3 2004/06/11 09:33:59 bures Exp $ */
2 package SOFA.SOFAnode.Run.Deployment;
3
4 import javax.xml.parsers.DocumentBuilder JavaDoc;
5 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
6 import javax.xml.parsers.ParserConfigurationException JavaDoc;
7 import javax.xml.transform.Transformer JavaDoc;
8 import javax.xml.transform.TransformerConfigurationException JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.TransformerFactory JavaDoc;
11 import javax.xml.transform.dom.DOMSource JavaDoc;
12 import javax.xml.transform.stream.StreamResult JavaDoc;
13
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16 import org.w3c.dom.Node JavaDoc;
17 import org.w3c.dom.NodeList JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 import SOFA.Util.Wildcards;
21
22 /** Implementation of deployment descriptor.
23   *
24   * @author Petr Hnetynka
25   */

26 public class DeploymentDescriptorImpl implements DeploymentDescriptor {
27   
28   /** 'Root' of the DD */
29   protected Element JavaDoc element;
30   
31   /** Implemenatation version */
32   protected String JavaDoc releaseVersion;
33   
34   /** Instance ID. It is set by DeplDock in the <tt>instatiate</tt> method. */
35   protected String JavaDoc instanceId;
36   
37   /** Constructor.
38     * @param el root element of the dd
39     */

40   public DeploymentDescriptorImpl(Element JavaDoc el) {
41     element = el;
42     initialize();
43   }
44
45   /** Constructor */
46   public DeploymentDescriptorImpl() {
47   }
48   
49   /** Constructor.
50     * @param fname name of the file with depl. descr.
51     */

52   public DeploymentDescriptorImpl(String JavaDoc fname) throws java.io.IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
53     java.io.File JavaDoc file = new java.io.File JavaDoc(fname);
54     if (!file.exists()) {
55       throw new java.io.IOException JavaDoc("File \""+fname+"\" doesn't exist.");
56     }
57
58     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
59     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
60     Document JavaDoc document = builder.parse(file);
61     element = document.getDocumentElement();
62     element.normalize();
63   }
64
65   /** Constructor.
66     * @param is file stream with depl. descr.
67     */

68   public DeploymentDescriptorImpl(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
69     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
70     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
71     Document JavaDoc document = builder.parse(is);
72     element = document.getDocumentElement();
73     element.normalize();
74   }
75   
76   /** Initializing */
77   private void initialize() {
78     NodeList JavaDoc nl = element.getChildNodes();
79     for (int i=0; i<nl.getLength(); i++) {
80       Node JavaDoc n = nl.item(i);
81       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version")==0)) {
82         releaseVersion = n.getFirstChild().getNodeValue();
83         break;
84       }
85     }
86   }
87
88   public boolean isSystem() {
89     return ( element.getTagName().compareTo("sofa_system")==0);
90   }
91   
92   public String JavaDoc getReleaseVersion() {
93     return releaseVersion;
94   }
95
96   public String JavaDoc getItem(String JavaDoc name) {
97     NodeList JavaDoc nl = element.getChildNodes();
98     for (int i=0; i<nl.getLength(); i++) {
99       Node JavaDoc n = nl.item(i);
100       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) {
101         Node JavaDoc fn = n.getFirstChild();
102         if (fn != null)
103           return fn.getNodeValue();
104         else
105           return "";
106       }
107     }
108     return "";
109   }
110   
111   public String JavaDoc getInstanceName() {
112     return element.getAttribute("name");
113   }
114   public void setInstanceName(String JavaDoc name) {
115     element.setAttribute("name", name);
116   }
117   
118   public void setInstanceId(String JavaDoc id) {
119     instanceId = id;
120   }
121   
122   public String JavaDoc getInstanceId() { return instanceId;}
123   
124   public String JavaDoc getImplementationClass(String JavaDoc type) {
125     NodeList JavaDoc nl = element.getChildNodes();
126     for (int i=0; i<nl.getLength(); i++) {
127       Node JavaDoc n = nl.item(i);
128       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("implementation")==0) && (((Element JavaDoc)n).getAttribute("type").compareTo(type)==0)) {
129         Node JavaDoc fn = n.getFirstChild();
130         if (fn != null)
131           return fn.getNodeValue();
132         else
133           return null;
134       }
135     }
136     return null;
137   }
138   
139   public String JavaDoc getProperty(String JavaDoc name) {
140     NodeList JavaDoc nl = element.getChildNodes();
141     for (int i=0; i<nl.getLength(); i++) {
142       Node JavaDoc n = nl.item(i);
143       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("property")==0) && (((Element JavaDoc)n).getAttribute("name").compareTo(name)==0)) {
144         return ((Element JavaDoc)n).getAttribute("value");
145       }
146     }
147     return null;
148   }
149   
150   public SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor getSubComponentDeploymentDescriptor(String JavaDoc name) {
151     // ??? clone returned nodes ???
152
NodeList JavaDoc nl = element.getChildNodes();
153     for (int i=0; i<nl.getLength(); i++) {
154       Node JavaDoc n = nl.item(i);
155       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_component")==0) && ( Wildcards.match(name, ((Element JavaDoc)n).getAttribute("name"))) ) {
156         return new DeploymentDescriptorImpl((Element JavaDoc) n);
157       } else {
158         if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("unit")==0) ) {
159           NodeList JavaDoc nl1 = n.getChildNodes();
160           for (int j = 0; j < nl1.getLength(); j++) {
161             Node JavaDoc n1 = nl1.item(j);
162             if (n1.getNodeType() == Node.ELEMENT_NODE)
163               if ((n1.getNodeName().compareTo("sofa_component")==0) && (Wildcards.match(name, ((Element JavaDoc)n1).getAttribute("name"))))
164                 return new DeploymentDescriptorImpl(((Element JavaDoc)n1));
165           }
166         }
167       }
168     }
169     return null;
170   }
171   
172   public String JavaDoc getSubComponentLocation(String JavaDoc name) {
173     NodeList JavaDoc nl = element.getChildNodes();
174     for (int i=0; i<nl.getLength(); i++) {
175       Node JavaDoc n = nl.item(i);
176       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_component")==0) && ( Wildcards.match(name, ((Element JavaDoc)n).getAttribute("name"))) ) {
177         return "sofa://local/local";
178       } else {
179         if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("unit")==0) ) {
180           String JavaDoc location = null;
181           NodeList JavaDoc nl1 = n.getChildNodes();
182           for (int j = 0; j < nl1.getLength(); j++) {
183             Node JavaDoc n1 = nl1.item(j);
184             if (n1.getNodeType() == Node.ELEMENT_NODE) {
185               if ((n1.getNodeName().compareTo("location")==0)) {
186                 location = n1.getFirstChild().getNodeValue();
187               }
188               if ((n1.getNodeName().compareTo("sofa_component")==0) && (Wildcards.match(name, ((Element JavaDoc)n1).getAttribute("name")))) {
189                 return location;
190               }
191             }
192           }
193         }
194       }
195     }
196     return null;
197   }
198
199   
200   public SOFA.Connector.DeploymentDescriptor getConnectorDeploymentDescriptor(String JavaDoc name) {
201     NodeList JavaDoc nl = element.getChildNodes();
202     for (int i=0; i<nl.getLength(); i++) {
203       Node JavaDoc n = nl.item(i);
204       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_connector")==0) && ( Wildcards.match(name, ((Element JavaDoc)n).getAttribute("name"))) ) {
205         if (((Element JavaDoc)n).hasAttribute("impl")) {
206           try {
207             return new SOFA.Connector.ECG.DeploymentDescriptor((Element JavaDoc) n);
208           } catch (SOFA.Connector.DeploymentDescriptorException e) {
209             return null;
210           }
211         }
212       }
213     }
214     return null;
215   }
216
217
218   
219   public java.util.Hashtable JavaDoc getCDLEntities() {
220     NodeList JavaDoc nl = element.getChildNodes();
221     for (int i=0; i<nl.getLength(); i++) {
222       Node JavaDoc n = nl.item(i);
223       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("cdl_entities")==0)) {
224         NodeList JavaDoc nl1 = n.getChildNodes();
225         java.util.Hashtable JavaDoc ret = new java.util.Hashtable JavaDoc();
226         for (int j=0; j<nl1.getLength(); j++) {
227           Node JavaDoc n1 = nl1.item(j);
228           if ((n1.getNodeType() == Node.ELEMENT_NODE) && (n1.getNodeName().compareTo("entity")==0)) {
229             ret.put(((Element JavaDoc)n1).getAttribute("javaname"), ((Element JavaDoc)n1).getAttribute("cdlname"));
230           }
231         }
232         return ret;
233       }
234     }
235     return null;
236   }
237
238   public String JavaDoc getArchitectureAbsName() {
239     String JavaDoc arch = null;
240     NodeList JavaDoc nl = element.getChildNodes();
241     for (int i=0; i<nl.getLength(); i++) {
242       Node JavaDoc n = nl.item(i);
243       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
244         arch = n.getFirstChild().getNodeValue();
245         break;
246       }
247     }
248     int pos = arch.indexOf('?');
249     return arch.substring(0,pos);
250   }
251
252   public String JavaDoc getArchitectureAbsNameWithVersion() {
253     String JavaDoc arch = null;
254     NodeList JavaDoc nl = element.getChildNodes();
255     for (int i=0; i<nl.getLength(); i++) {
256       Node JavaDoc n = nl.item(i);
257       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
258         arch = n.getFirstChild().getNodeValue();
259         break;
260       }
261     }
262     return arch;
263   }
264
265   public void _read(cz.cuni.sofa.lib.InputStream is) throws java.io.IOException JavaDoc {
266     try {
267       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
268       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
269       Document JavaDoc document = builder.parse(is);
270       element = document.getDocumentElement();
271       initialize();
272     } catch (SAXException JavaDoc e) {
273       throw new java.io.IOException JavaDoc("SAXException: "+e.getMessage());
274     } catch (ParserConfigurationException JavaDoc e) {
275       throw new java.io.IOException JavaDoc("ParserConfigurationException: "+e.getMessage());
276     }
277   }
278   
279   public void _write(cz.cuni.sofa.lib.OutputStream os) throws java.io.IOException JavaDoc {
280     try {
281       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
282       Transformer JavaDoc transformer = tFactory.newTransformer();
283       DOMSource JavaDoc source = new DOMSource JavaDoc(element);
284       StreamResult JavaDoc result = new StreamResult JavaDoc(os);
285       transformer.transform(source, result);
286     } catch (TransformerConfigurationException JavaDoc e) {
287       throw new java.io.IOException JavaDoc("TransformerConfigurationException: "+e.getMessage());
288     } catch (TransformerException JavaDoc e) {
289       throw new java.io.IOException JavaDoc("TransformerException: "+e.getMessage());
290     }
291   }
292 }
293
Popular Tags