1 19 25 26 package org.netbeans.modules.j2ee.sun.ide.j2ee; 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.io.StringReader ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.Vector ; 36 37 import javax.enterprise.deploy.spi.DeploymentManager ; 38 import javax.xml.parsers.DocumentBuilder ; 39 import javax.xml.parsers.DocumentBuilderFactory ; 40 import javax.xml.transform.OutputKeys ; 41 import javax.xml.transform.Transformer ; 42 import javax.xml.transform.TransformerFactory ; 43 import javax.xml.transform.dom.DOMSource ; 44 import javax.xml.transform.stream.StreamResult ; 45 46 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 47 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 48 import org.w3c.dom.Document ; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.NamedNodeMap ; 51 import org.w3c.dom.Node ; 52 import org.w3c.dom.NodeList ; 53 import org.w3c.dom.Text ; 54 import org.xml.sax.EntityResolver ; 55 import org.xml.sax.InputSource ; 56 import org.xml.sax.SAXException ; 57 58 63 public class DomainEditor { 64 65 private static DeploymentManager dm; 66 67 private static String SAMPLE_DATASOURCE = "jdbc/sample"; private static String SAMPLE_CONNPOOL = "SamplePool"; 70 private static String NBPROFILERNAME = "NetBeansProfiler"; 72 private static String CONST_USER = "User"; private static String CONST_PASSWORD = "Password"; private static String CONST_URL = "URL"; private static String CONST_LOWER_DATABASE_NAME = "databaseName"; private static String CONST_LOWER_PORT_NUMBER = "portNumber"; private static String CONST_DATABASE_NAME = "DatabaseName"; private static String CONST_PORT_NUMBER = "PortNumber"; private static String CONST_SID = "SID"; private static String CONST_SERVER_NAME = "serverName"; static private String CONST_NAME = "name"; static private String CONST_JVM_OPTIONS = "jvm-options"; 84 88 public DomainEditor(DeploymentManager dm) { 89 this.dm = dm; 90 } 91 92 96 public String getDomainLocation(){ 97 DeploymentManagerProperties dmProps = new DeploymentManagerProperties(this.dm); 98 String domainScriptFilePath = dmProps.getLocation()+"/" + dmProps.getDomainName() + "/config/domain.xml"; return domainScriptFilePath; 101 } 102 103 107 public Document getDomainDocument(){ 108 String domainLoc = getDomainLocation(); 109 110 Document domainScriptDocument = getDomainDocument(domainLoc); 112 return domainScriptDocument; 113 } 114 115 120 public Document getDomainDocument(String domainLoc){ 121 Document domainScriptDocument = loadDomainScriptFile(domainLoc); 123 return domainScriptDocument; 124 } 125 126 133 public boolean addProfilerElements(Document domainDoc, String nativeLibraryPath, String [] jvmOptions){ 134 String domainPath = getDomainLocation(); 135 136 removeProfiler(domainDoc); 138 139 if (nativeLibraryPath != null || jvmOptions != null) { 143 144 Element profilerElement = domainDoc.createElement("profiler"); profilerElement.setAttribute("enabled", "true"); profilerElement.setAttribute(CONST_NAME, NBPROFILERNAME); if (nativeLibraryPath != null) { 149 profilerElement.setAttribute("native-library-path", nativeLibraryPath); } 151 152 File appServerLocation = ((SunDeploymentManagerInterface)getDeploymentManager()).getPlatformRoot(); 153 if (jvmOptions != null) { 155 for (int i = 0; i < jvmOptions.length; i++) { 156 Element jvmOptionsElement = domainDoc.createElement(CONST_JVM_OPTIONS); 157 Text tt = domainDoc.createTextNode(formatJvmOption(jvmOptions[i] , appServerLocation)); 158 jvmOptionsElement.appendChild(tt); 159 profilerElement.appendChild(jvmOptionsElement); 160 } 161 } 162 163 NodeList javaConfigNodeList = domainDoc.getElementsByTagName("java-config"); 165 if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) { 166 System.err.println("ConfigFilesUtils: cannot find 'java-config' section in domain config file " + domainPath); 167 return false; 168 } 169 170 Node javaConfigNode = javaConfigNodeList.item(0); 172 if (javaConfigNode.getFirstChild() != null) 173 javaConfigNode.insertBefore(profilerElement, javaConfigNode.getFirstChild()); 174 else 175 javaConfigNode.appendChild(profilerElement); 176 177 } 178 return saveDomainScriptFile(domainDoc, domainPath); 180 } 181 182 187 public boolean removeProfilerElements(Document domainDoc){ 188 boolean eleRemoved = removeProfiler(domainDoc); 189 if(eleRemoved){ 190 return saveDomainScriptFile(domainDoc, getDomainLocation()); 192 }else{ 193 return true; 195 } 196 } 197 198 private boolean removeProfiler(Document domainDoc){ 199 NodeList profilerElementNodeList = domainDoc.getElementsByTagName("profiler"); if (profilerElementNodeList != null && profilerElementNodeList.getLength() > 0){ 202 Vector nodes = new Vector (); for (int i = 0; i < profilerElementNodeList.getLength(); i++) { 206 Node n= profilerElementNodeList.item(i); 207 Node a= n.getAttributes().getNamedItem(CONST_NAME); if ((a!=null)&&(a.getNodeValue().equals(NBPROFILERNAME))){ nodes.add(n); 210 } 211 } 212 for(int i=0; i<nodes.size(); i++){ 213 Node nd = (Node )nodes.get(i); 214 nd.getParentNode().removeChild(nd); 215 } 216 return true; 217 } 218 219 return false; 220 } 221 222 public String [] getHttpProxyOptions(){ 223 ArrayList httpProxyOptions = new ArrayList (); 224 Document domainDoc = getDomainDocument(); 225 NodeList javaConfigNodeList = domainDoc.getElementsByTagName("java-config"); 226 if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) { 227 return (String [])httpProxyOptions.toArray(new String [httpProxyOptions.size()]); 228 } 229 230 NodeList jvmOptionNodeList = domainDoc.getElementsByTagName(CONST_JVM_OPTIONS); 231 for(int i=0; i<jvmOptionNodeList.getLength(); i++){ 232 Node nd = jvmOptionNodeList.item(i); 233 if(nd.hasChildNodes()) { 234 Node childNode = nd.getFirstChild(); 235 String childValue = childNode.getNodeValue(); 236 if(childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_HOST) != -1 237 || childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_PORT) != -1 238 || childValue.indexOf(HttpProxyUpdater.HTTPS_PROXY_HOST) != -1 239 || childValue.indexOf(HttpProxyUpdater.HTTPS_PROXY_PORT) != -1 240 || childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_NO_HOST) != -1){ 241 httpProxyOptions.add(childValue); 242 } 243 } 244 } 245 246 String [] opts = new String [httpProxyOptions.size()]; 247 return (String [])httpProxyOptions.toArray(opts); 248 249 } 250 251 public boolean setHttpProxyOptions(String [] httpProxyOptions){ 252 Document domainDoc = getDomainDocument(); 253 NodeList javaConfigNodeList = domainDoc.getElementsByTagName("java-config"); 254 if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) { 255 return false; 256 } 257 258 removeProxyOptions(domainDoc, javaConfigNodeList.item(0)); 260 261 for(int j=0; j<httpProxyOptions.length; j++){ 263 String option = httpProxyOptions[j]; 264 Element jvmOptionsElement = domainDoc.createElement(CONST_JVM_OPTIONS); 265 Text proxyOption = domainDoc.createTextNode(option); 266 jvmOptionsElement.appendChild(proxyOption); 267 javaConfigNodeList.item(0).appendChild(jvmOptionsElement); 268 } 269 270 return saveDomainScriptFile(domainDoc, getDomainLocation(), false); 271 } 272 273 private boolean removeProxyOptions(Document domainDoc, Node javaConfigNode){ 274 NodeList jvmOptionNodeList = domainDoc.getElementsByTagName(CONST_JVM_OPTIONS); 275 276 Vector nodes = new Vector (); 277 for(int i=0; i<jvmOptionNodeList.getLength(); i++){ 278 Node nd = jvmOptionNodeList.item(i); 279 if(nd.hasChildNodes()) { 280 Node childNode = nd.getFirstChild(); 281 String childValue = childNode.getNodeValue(); 282 if(childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_HOST) != -1 283 || childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_PORT) != -1 284 || childValue.indexOf(HttpProxyUpdater.HTTPS_PROXY_HOST) != -1 285 || childValue.indexOf(HttpProxyUpdater.HTTPS_PROXY_PORT) != -1 286 || childValue.indexOf(HttpProxyUpdater.HTTP_PROXY_NO_HOST) != -1){ 287 nodes.add(nd); 288 } 289 } 290 } 291 for(int i=0; i<nodes.size(); i++){ 292 javaConfigNode.removeChild((Node )nodes.get(i)); 293 } 294 295 return saveDomainScriptFile(domainDoc, getDomainLocation(), false); 296 } 297 298 302 private Document loadDomainScriptFile(String domainScriptFilePath) { 303 try { 304 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 305 dbFactory.setValidating(false); 306 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 307 308 dBuilder.setEntityResolver(new EntityResolver () { 309 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 310 StringReader reader = new StringReader ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); InputSource source = new InputSource (reader); 312 source.setPublicId(publicId); 313 source.setSystemId(systemId); 314 return source; 315 } 316 }); 317 318 return dBuilder.parse(new File (domainScriptFilePath)); 319 } catch (Exception e) { 320 System.err.println("ConfigFilesUtils: unable to parse domain config file " + domainScriptFilePath); 321 return null; 322 } 323 } 324 325 private boolean saveDomainScriptFile(Document domainScriptDocument, String domainScriptFilePath) { 326 return saveDomainScriptFile(domainScriptDocument, domainScriptFilePath, true); 327 } 328 333 private boolean saveDomainScriptFile(Document domainScriptDocument, String domainScriptFilePath, boolean indent) { 334 boolean result = false; 335 FileWriter domainScriptFileWriter = null; 336 try { 337 domainScriptFileWriter = new FileWriter (domainScriptFilePath); 338 try { 339 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 340 Transformer transformer = transformerFactory.newTransformer(); 341 if(indent) { 342 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 343 } 344 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 345 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, domainScriptDocument.getDoctype().getPublicId()); 346 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, domainScriptDocument.getDoctype().getSystemId()); 347 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 348 349 DOMSource domSource = new DOMSource (domainScriptDocument); 350 StreamResult streamResult = new StreamResult (domainScriptFileWriter); 351 352 transformer.transform(domSource, streamResult); 353 result = true; 354 } catch (Exception e) { 355 System.err.println("ConfigFilesUtils: Unable to save domain config file " + domainScriptFilePath); 356 result = false; 357 } 358 } catch (IOException ioex) { 359 System.err.println("ConfigFilesUtils: cannot create output stream for domain config file " + domainScriptFilePath); 360 result = false; 361 } finally { 362 try { 363 if (domainScriptFileWriter != null) { 364 domainScriptFileWriter.close(); 365 } 366 } catch (IOException ioex2) { 367 System.err.println("SunAS8IntegrationProvider: cannot close output stream for " + domainScriptFilePath); 368 }; 369 } 370 371 return result; 372 } 373 374 private String formatJvmOption(String jvmOption, File appServerLocation) { 378 if (jvmOption.indexOf("\\\"") != -1) { 380 381 String modifiedOption = jvmOption.replaceAll("\\\\\"", "#"); modifiedOption = modifiedOption.replaceAll("\\\"", ""); modifiedOption = modifiedOption.replaceAll("#", "\""); 388 String osType=System.getProperty("os.name"); if ((osType.startsWith("Mac OS"))||(ServerLocationManager.isGlassFish(appServerLocation))){ 396 400 modifiedOption = "\"" + modifiedOption.replaceAll("\\\"", "\\\\\"") + "\""; 401 402 } 403 404 return modifiedOption; 406 } 407 return jvmOption; 409 } 410 411 static final String [] sysDatasources = {"jdbc/__TimerPool", "jdbc/__CallFlowPool", "jdbc/__default"}; 413 414 415 public HashMap getSunDatasourcesFromXml(){ 416 HashMap dSources = new HashMap (); 417 Document domainDoc = getDomainDocument(); 418 HashMap dsMap = getDataSourcesAttrMap(domainDoc); 419 HashMap cpMap = getConnPoolsNodeMap(domainDoc); 420 dsMap.keySet().removeAll(Arrays.asList(sysDatasources)); 421 String [] ds = (String [])dsMap.keySet().toArray(new String [dsMap.size()]); 422 423 for(int i=0; i<ds.length; i++){ 424 String jndiName = ds[i]; 425 HashMap pValues = new HashMap (); 426 NamedNodeMap dsAttrMap = (NamedNodeMap )dsMap.get(jndiName); 427 String poolName = dsAttrMap.getNamedItem("pool-name").getNodeValue(); 428 429 Node cpNode = (Node )cpMap.get(poolName); 431 NamedNodeMap cpAttrMap = cpNode.getAttributes(); 432 String dsClassName = cpAttrMap.getNamedItem("datasource-classname").getNodeValue(); 433 434 Element cpElement = (Element ) cpNode; 436 NodeList propsNodeList = cpElement.getElementsByTagName("property"); 437 438 HashMap map = new HashMap (); 440 for(int j=0; j<propsNodeList.getLength(); j++){ 441 Node propNode = propsNodeList.item(j); 442 NamedNodeMap propsMap = propNode.getAttributes(); 443 444 for(int m=0; m<propsMap.getLength(); m++){ 445 String mkey = propsMap.getNamedItem(CONST_NAME).getNodeValue(); 446 String mkeyValue = propsMap.getNamedItem("value").getNodeValue(); 447 map.put(mkey, mkeyValue); 448 } 449 } 451 pValues.put(CONST_USER, (String )map.get(CONST_USER)); 452 pValues.put(CONST_PASSWORD, (String )map.get(CONST_PASSWORD)); 453 pValues.put(CONST_URL, (String )map.get(CONST_URL)); 454 pValues.put(CONST_LOWER_DATABASE_NAME, (String )map.get(CONST_LOWER_DATABASE_NAME)); 455 pValues.put(CONST_SERVER_NAME, (String )map.get(CONST_SERVER_NAME)); 456 pValues.put(CONST_PORT_NUMBER, (String )map.get(CONST_PORT_NUMBER)); 457 pValues.put(CONST_LOWER_PORT_NUMBER, (String )map.get(CONST_LOWER_PORT_NUMBER)); 458 pValues.put(CONST_DATABASE_NAME, (String )map.get(CONST_DATABASE_NAME)); 459 pValues.put(CONST_SID, (String )map.get(CONST_SID)); 460 pValues.put("dsClassName", dsClassName); 461 462 dSources.put(jndiName, pValues); 463 } return dSources; 465 } 466 467 public HashMap getConnPoolsFromXml(){ 468 HashMap pools = new HashMap (); 469 Document domainDoc = getDomainDocument(); 470 HashMap cpMap = getConnPoolsNodeMap(domainDoc); 471 472 String [] cp = (String [])cpMap.keySet().toArray(new String [cpMap.size()]); 473 for(int i=0; i<cp.length; i++){ 474 String name = cp[i]; 475 HashMap pValues = new HashMap (); 476 Node cpNode = (Node )cpMap.get(name); 477 NamedNodeMap cpAttrMap = cpNode.getAttributes(); 478 String dsClassName = cpAttrMap.getNamedItem("datasource-classname").getNodeValue(); 479 480 Element cpElement = (Element ) cpNode; 482 NodeList propsNodeList = cpElement.getElementsByTagName("property"); 483 484 HashMap map = new HashMap (); 486 for(int j=0; j<propsNodeList.getLength(); j++){ 487 Node propNode = propsNodeList.item(j); 488 NamedNodeMap propsMap = propNode.getAttributes(); 489 490 for(int m=0; m<propsMap.getLength(); m++){ 491 String mkey = propsMap.getNamedItem(CONST_NAME).getNodeValue(); 492 String mkeyValue = propsMap.getNamedItem("value").getNodeValue(); 493 map.put(mkey, mkeyValue); 494 } 495 } 497 pValues.put(CONST_USER, (String )map.get(CONST_USER)); 498 pValues.put(CONST_PASSWORD, (String )map.get(CONST_PASSWORD)); 499 pValues.put(CONST_URL, (String )map.get(CONST_URL)); 500 pValues.put(CONST_LOWER_DATABASE_NAME, (String )map.get(CONST_LOWER_DATABASE_NAME)); 501 pValues.put(CONST_SERVER_NAME, (String )map.get(CONST_SERVER_NAME)); 502 pValues.put(CONST_PORT_NUMBER, (String )map.get(CONST_PORT_NUMBER)); 503 pValues.put(CONST_LOWER_PORT_NUMBER, (String )map.get(CONST_LOWER_PORT_NUMBER)); 504 pValues.put(CONST_DATABASE_NAME, (String )map.get(CONST_DATABASE_NAME)); 505 pValues.put(CONST_SID, (String )map.get(CONST_SID)); 506 pValues.put("dsClassName", dsClassName); 507 508 pools.put(name, pValues); 509 } 510 511 return pools; 512 } 513 514 private HashMap getDataSourcesAttrMap(Document domainDoc){ 515 HashMap dataSourceMap = new HashMap (); 516 updateWithSampleDataSource(domainDoc); 517 NodeList dataSourceNodeList = domainDoc.getElementsByTagName("jdbc-resource"); 518 for(int i=0; i<dataSourceNodeList.getLength(); i++){ 519 Node dsNode = dataSourceNodeList.item(i); 520 NamedNodeMap dsAttrMap = dsNode.getAttributes(); 521 String jndiName = dsAttrMap.getNamedItem("jndi-name").getNodeValue(); 522 dataSourceMap.put(jndiName, dsAttrMap); 523 } 524 return dataSourceMap; 525 } 526 527 private boolean updateWithSampleDataSource(Document domainDoc){ 528 boolean sampleExists = false; 529 NodeList dataSourceNodeList = domainDoc.getElementsByTagName("jdbc-resource"); 530 for(int i=0; i<dataSourceNodeList.getLength(); i++){ 531 Node dsNode = dataSourceNodeList.item(i); 532 NamedNodeMap dsAttrMap = dsNode.getAttributes(); 533 String jndiName = dsAttrMap.getNamedItem("jndi-name").getNodeValue(); 534 if(jndiName.equals(SAMPLE_DATASOURCE)) { 535 sampleExists = true; 536 } 537 } 538 if(!sampleExists) { 539 return createSampleDatasource(domainDoc); 540 } 541 return true; 542 } 543 544 private HashMap getConnPoolsNodeMap(Document domainDoc){ 545 HashMap connPoolMap = new HashMap (); 546 NodeList connPoolNodeList = domainDoc.getElementsByTagName("jdbc-connection-pool"); 547 for(int i=0; i<connPoolNodeList.getLength(); i++){ 548 Node cpNode = connPoolNodeList.item(i); 549 NamedNodeMap cpAttrMap = cpNode.getAttributes(); 550 String cpName = cpAttrMap.getNamedItem(CONST_NAME).getNodeValue(); 551 connPoolMap.put(cpName, cpNode); 552 } 553 return connPoolMap; 554 } 555 556 public boolean createSampleDatasource(Document domainDoc){ 557 NodeList resourcesNodeList = domainDoc.getElementsByTagName("resources"); 558 NodeList serverNodeList = domainDoc.getElementsByTagName("server"); 559 if (resourcesNodeList == null || resourcesNodeList.getLength() == 0 || 560 serverNodeList == null || serverNodeList.getLength() == 0) { 561 return true; 562 } 563 Node resourcesNode = resourcesNodeList.item(0); 564 565 HashMap cpMap = getConnPoolsNodeMap(domainDoc); 566 if(! cpMap.containsKey(SAMPLE_CONNPOOL)){ 567 Node oldNode = (Node )cpMap.get("DerbyPool"); 568 Node cpNode = oldNode.cloneNode(false); 569 NamedNodeMap cpAttrMap = cpNode.getAttributes(); 570 cpAttrMap.getNamedItem(CONST_NAME).setNodeValue(SAMPLE_CONNPOOL); 571 HashMap poolProps = new HashMap (); 572 poolProps.put(CONST_SERVER_NAME, "localhost"); 573 poolProps.put(CONST_PASSWORD, "app"); 574 poolProps.put(CONST_USER, "app"); 575 poolProps.put(CONST_DATABASE_NAME, "sample"); 576 poolProps.put(CONST_PORT_NUMBER, "1527"); 577 578 Object [] propNames = poolProps.keySet().toArray(); 579 for(int i=0; i<propNames.length; i++){ 580 String keyName = (String )propNames[i]; 581 Element propElement = domainDoc.createElement("property"); 582 propElement.setAttribute(CONST_NAME, keyName); 583 propElement.setAttribute("value", (String )poolProps.get(keyName)); 584 cpNode.appendChild(propElement); 585 } 586 resourcesNode.appendChild(cpNode); 587 } 588 589 Element dsElement = domainDoc.createElement("jdbc-resource"); 590 dsElement.setAttribute("jndi-name", SAMPLE_DATASOURCE); 591 dsElement.setAttribute("pool-name", SAMPLE_CONNPOOL); 592 dsElement.setAttribute("object-type", "user"); 593 dsElement.setAttribute("enabled", "true"); 594 595 if (resourcesNode.getFirstChild() != null) 597 resourcesNode.insertBefore(dsElement, resourcesNode.getFirstChild()); 598 else 599 resourcesNode.appendChild(dsElement); 600 601 Element dsResRefElement = domainDoc.createElement("resource-ref"); 603 dsResRefElement.setAttribute("ref", SAMPLE_DATASOURCE); 604 dsResRefElement.setAttribute("enabled", "true"); 605 Node serverNode = serverNodeList.item(0); 607 if (serverNode.getLastChild() != null) 608 serverNode.insertBefore(dsResRefElement, serverNode.getLastChild()); 609 else 610 serverNode.appendChild(dsResRefElement); 611 612 return saveDomainScriptFile(domainDoc, getDomainLocation()); 613 } 614 615 618 public DeploymentManager getDeploymentManager() { 619 return this.dm; 620 } 621 622 } 623 | Popular Tags |