1 2 package SOFA.SOFAnode.Run.Deployment; 3 4 import javax.xml.parsers.DocumentBuilder ; 5 import javax.xml.parsers.DocumentBuilderFactory ; 6 import javax.xml.parsers.ParserConfigurationException ; 7 import javax.xml.transform.Transformer ; 8 import javax.xml.transform.TransformerConfigurationException ; 9 import javax.xml.transform.TransformerException ; 10 import javax.xml.transform.TransformerFactory ; 11 import javax.xml.transform.dom.DOMSource ; 12 import javax.xml.transform.stream.StreamResult ; 13 14 import org.w3c.dom.Document ; 15 import org.w3c.dom.Element ; 16 import org.w3c.dom.Node ; 17 import org.w3c.dom.NodeList ; 18 import org.xml.sax.SAXException ; 19 20 import SOFA.Util.Wildcards; 21 22 26 public class DeploymentDescriptorImpl implements DeploymentDescriptor { 27 28 29 protected Element element; 30 31 32 protected String releaseVersion; 33 34 35 protected String instanceId; 36 37 40 public DeploymentDescriptorImpl(Element el) { 41 element = el; 42 initialize(); 43 } 44 45 46 public DeploymentDescriptorImpl() { 47 } 48 49 52 public DeploymentDescriptorImpl(String fname) throws java.io.IOException , ParserConfigurationException , SAXException { 53 java.io.File file = new java.io.File (fname); 54 if (!file.exists()) { 55 throw new java.io.IOException ("File \""+fname+"\" doesn't exist."); 56 } 57 58 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 59 DocumentBuilder builder = factory.newDocumentBuilder(); 60 Document document = builder.parse(file); 61 element = document.getDocumentElement(); 62 element.normalize(); 63 } 64 65 68 public DeploymentDescriptorImpl(java.io.InputStream is) throws java.io.IOException , ParserConfigurationException , SAXException { 69 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 70 DocumentBuilder builder = factory.newDocumentBuilder(); 71 Document document = builder.parse(is); 72 element = document.getDocumentElement(); 73 element.normalize(); 74 } 75 76 77 private void initialize() { 78 NodeList nl = element.getChildNodes(); 79 for (int i=0; i<nl.getLength(); i++) { 80 Node 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 getReleaseVersion() { 93 return releaseVersion; 94 } 95 96 public String getItem(String name) { 97 NodeList nl = element.getChildNodes(); 98 for (int i=0; i<nl.getLength(); i++) { 99 Node n = nl.item(i); 100 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) { 101 Node fn = n.getFirstChild(); 102 if (fn != null) 103 return fn.getNodeValue(); 104 else 105 return ""; 106 } 107 } 108 return ""; 109 } 110 111 public String getInstanceName() { 112 return element.getAttribute("name"); 113 } 114 public void setInstanceName(String name) { 115 element.setAttribute("name", name); 116 } 117 118 public void setInstanceId(String id) { 119 instanceId = id; 120 } 121 122 public String getInstanceId() { return instanceId;} 123 124 public String getImplementationClass(String type) { 125 NodeList nl = element.getChildNodes(); 126 for (int i=0; i<nl.getLength(); i++) { 127 Node n = nl.item(i); 128 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("implementation")==0) && (((Element )n).getAttribute("type").compareTo(type)==0)) { 129 Node 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 getProperty(String name) { 140 NodeList nl = element.getChildNodes(); 141 for (int i=0; i<nl.getLength(); i++) { 142 Node n = nl.item(i); 143 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("property")==0) && (((Element )n).getAttribute("name").compareTo(name)==0)) { 144 return ((Element )n).getAttribute("value"); 145 } 146 } 147 return null; 148 } 149 150 public SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor getSubComponentDeploymentDescriptor(String name) { 151 NodeList nl = element.getChildNodes(); 153 for (int i=0; i<nl.getLength(); i++) { 154 Node n = nl.item(i); 155 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_component")==0) && ( Wildcards.match(name, ((Element )n).getAttribute("name"))) ) { 156 return new DeploymentDescriptorImpl((Element ) n); 157 } else { 158 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("unit")==0) ) { 159 NodeList nl1 = n.getChildNodes(); 160 for (int j = 0; j < nl1.getLength(); j++) { 161 Node n1 = nl1.item(j); 162 if (n1.getNodeType() == Node.ELEMENT_NODE) 163 if ((n1.getNodeName().compareTo("sofa_component")==0) && (Wildcards.match(name, ((Element )n1).getAttribute("name")))) 164 return new DeploymentDescriptorImpl(((Element )n1)); 165 } 166 } 167 } 168 } 169 return null; 170 } 171 172 public String getSubComponentLocation(String name) { 173 NodeList nl = element.getChildNodes(); 174 for (int i=0; i<nl.getLength(); i++) { 175 Node n = nl.item(i); 176 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_component")==0) && ( Wildcards.match(name, ((Element )n).getAttribute("name"))) ) { 177 return "sofa://local/local"; 178 } else { 179 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("unit")==0) ) { 180 String location = null; 181 NodeList nl1 = n.getChildNodes(); 182 for (int j = 0; j < nl1.getLength(); j++) { 183 Node 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 )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 name) { 201 NodeList nl = element.getChildNodes(); 202 for (int i=0; i<nl.getLength(); i++) { 203 Node n = nl.item(i); 204 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_connector")==0) && ( Wildcards.match(name, ((Element )n).getAttribute("name"))) ) { 205 if (((Element )n).hasAttribute("impl")) { 206 try { 207 return new SOFA.Connector.ECG.DeploymentDescriptor((Element ) 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 getCDLEntities() { 220 NodeList nl = element.getChildNodes(); 221 for (int i=0; i<nl.getLength(); i++) { 222 Node n = nl.item(i); 223 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("cdl_entities")==0)) { 224 NodeList nl1 = n.getChildNodes(); 225 java.util.Hashtable ret = new java.util.Hashtable (); 226 for (int j=0; j<nl1.getLength(); j++) { 227 Node n1 = nl1.item(j); 228 if ((n1.getNodeType() == Node.ELEMENT_NODE) && (n1.getNodeName().compareTo("entity")==0)) { 229 ret.put(((Element )n1).getAttribute("javaname"), ((Element )n1).getAttribute("cdlname")); 230 } 231 } 232 return ret; 233 } 234 } 235 return null; 236 } 237 238 public String getArchitectureAbsName() { 239 String arch = null; 240 NodeList nl = element.getChildNodes(); 241 for (int i=0; i<nl.getLength(); i++) { 242 Node 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 getArchitectureAbsNameWithVersion() { 253 String arch = null; 254 NodeList nl = element.getChildNodes(); 255 for (int i=0; i<nl.getLength(); i++) { 256 Node 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 { 266 try { 267 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 268 DocumentBuilder builder = factory.newDocumentBuilder(); 269 Document document = builder.parse(is); 270 element = document.getDocumentElement(); 271 initialize(); 272 } catch (SAXException e) { 273 throw new java.io.IOException ("SAXException: "+e.getMessage()); 274 } catch (ParserConfigurationException e) { 275 throw new java.io.IOException ("ParserConfigurationException: "+e.getMessage()); 276 } 277 } 278 279 public void _write(cz.cuni.sofa.lib.OutputStream os) throws java.io.IOException { 280 try { 281 TransformerFactory tFactory = TransformerFactory.newInstance(); 282 Transformer transformer = tFactory.newTransformer(); 283 DOMSource source = new DOMSource (element); 284 StreamResult result = new StreamResult (os); 285 transformer.transform(source, result); 286 } catch (TransformerConfigurationException e) { 287 throw new java.io.IOException ("TransformerConfigurationException: "+e.getMessage()); 288 } catch (TransformerException e) { 289 throw new java.io.IOException ("TransformerException: "+e.getMessage()); 290 } 291 } 292 } 293 | Popular Tags |