KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > DomainEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * DomainEditor.java
21  *
22  * Created on April 14, 2006, 10:33 AM
23  *
24  */

25
26 package org.netbeans.modules.j2ee.sun.ide.j2ee;
27
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
38 import javax.xml.parsers.DocumentBuilder JavaDoc;
39 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
40 import javax.xml.transform.OutputKeys JavaDoc;
41 import javax.xml.transform.Transformer JavaDoc;
42 import javax.xml.transform.TransformerFactory JavaDoc;
43 import javax.xml.transform.dom.DOMSource JavaDoc;
44 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.NamedNodeMap JavaDoc;
51 import org.w3c.dom.Node JavaDoc;
52 import org.w3c.dom.NodeList JavaDoc;
53 import org.w3c.dom.Text JavaDoc;
54 import org.xml.sax.EntityResolver JavaDoc;
55 import org.xml.sax.InputSource JavaDoc;
56 import org.xml.sax.SAXException JavaDoc;
57
58 /**
59  * Parses and edits domain.xml
60  * Used for Profiler, HTTP Proxy, DataSources
61  * @author Nitya Doraisamy
62  */

63 public class DomainEditor {
64     
65     private static DeploymentManager JavaDoc dm;
66     
67     private static String JavaDoc SAMPLE_DATASOURCE = "jdbc/sample"; //NOI18N
68
private static String JavaDoc SAMPLE_CONNPOOL = "SamplePool"; //NOI18N
69

70     private static String JavaDoc NBPROFILERNAME = "NetBeansProfiler"; //NOI18N
71

72     private static String JavaDoc CONST_USER = "User"; // NOI18N
73
private static String JavaDoc CONST_PASSWORD = "Password"; // NOI18N
74
private static String JavaDoc CONST_URL = "URL"; // NOI18N
75
private static String JavaDoc CONST_LOWER_DATABASE_NAME = "databaseName"; // NOI18N
76
private static String JavaDoc CONST_LOWER_PORT_NUMBER = "portNumber"; // NOI18N
77
private static String JavaDoc CONST_DATABASE_NAME = "DatabaseName"; // NOI18N
78
private static String JavaDoc CONST_PORT_NUMBER = "PortNumber"; // NOI18N
79
private static String JavaDoc CONST_SID = "SID"; // NOI18N
80
private static String JavaDoc CONST_SERVER_NAME = "serverName"; // NOI18N
81
static private String JavaDoc CONST_NAME = "name"; // NOI18N
82
static private String JavaDoc CONST_JVM_OPTIONS = "jvm-options"; // NOI18N
83

84     /**
85      * Creates a new instance of DomainEditor
86      * @param dm Deployment Manager of Target Server
87      */

88     public DomainEditor(DeploymentManager JavaDoc dm) {
89         this.dm = dm;
90     }
91     
92     /**
93      * Get the location of the server's domain.xml
94      * @return String representing path to domain.xml
95      */

96     public String JavaDoc getDomainLocation(){
97         DeploymentManagerProperties dmProps = new DeploymentManagerProperties(this.dm);
98         String JavaDoc domainScriptFilePath = dmProps.getLocation()+"/" + dmProps.getDomainName() + //NOI18N
99
"/config/domain.xml"; //NOI18N
100
return domainScriptFilePath;
101     }
102     
103     /**
104      * Get Document Object representing the domain.xml
105      * @return Document object representing given domain.xml
106      */

107     public Document JavaDoc getDomainDocument(){
108         String JavaDoc domainLoc = getDomainLocation();
109         
110         // Load domain.xml
111
Document JavaDoc domainScriptDocument = getDomainDocument(domainLoc);
112         return domainScriptDocument;
113     }
114     
115     /**
116      * Get Document Object representing the domain.xml
117      * @param domainLoc Location of domain.xml
118      * @return Document object representing given domain.xml
119      */

120     public Document JavaDoc getDomainDocument(String JavaDoc domainLoc){
121         // Load domain.xml
122
Document JavaDoc domainScriptDocument = loadDomainScriptFile(domainLoc);
123         return domainScriptDocument;
124     }
125     
126     /**
127      * Perform server instrumentation for profiling
128      * @param domainDoc Document object representing domain.xml
129      * @param nativeLibraryPath Native Library Path
130      * @param jvmOptions Values for jvm-options to enable profiling
131      * @return returns true if server is ready for profiling
132      */

133     public boolean addProfilerElements(Document JavaDoc domainDoc, String JavaDoc nativeLibraryPath, String JavaDoc[] jvmOptions){
134         String JavaDoc domainPath = getDomainLocation();
135         
136         // Remove any previously defined 'profiler' element(s)
137
removeProfiler(domainDoc);
138         
139         // If no 'profiler' element needs to be defined, the existing one is simply removed (by the code above)
140
// (This won't happen for NetBeans Profiler, but is a valid scenario)
141
// Otherwise new 'profiler' element is inserted according to provided parameters
142
if (nativeLibraryPath != null || jvmOptions != null) {
143             
144             // Create "profiler" element
145
Element JavaDoc profilerElement = domainDoc.createElement("profiler");//NOI18N
146
profilerElement.setAttribute("enabled", "true");//NOI18N
147
profilerElement.setAttribute(CONST_NAME, NBPROFILERNAME);//NOI18N
148
if (nativeLibraryPath != null) {
149                 profilerElement.setAttribute("native-library-path", nativeLibraryPath);//NOI18N
150
}
151             
152             File JavaDoc appServerLocation = ((SunDeploymentManagerInterface)getDeploymentManager()).getPlatformRoot();
153             // Create "jvm-options" element
154
if (jvmOptions != null) {
155                 for (int i = 0; i < jvmOptions.length; i++) {
156                     Element JavaDoc jvmOptionsElement = domainDoc.createElement(CONST_JVM_OPTIONS);
157                     Text JavaDoc tt = domainDoc.createTextNode(formatJvmOption(jvmOptions[i] , appServerLocation));
158                     jvmOptionsElement.appendChild(tt);
159                     profilerElement.appendChild(jvmOptionsElement);
160                 }
161             }
162             
163             // Find the "java-config" element
164
NodeList JavaDoc 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             // Insert the "profiler" element as a first child of "java-config" element
171
Node JavaDoc javaConfigNode = javaConfigNodeList.item(0);
172             if (javaConfigNode.getFirstChild() != null)
173                 javaConfigNode.insertBefore(profilerElement, javaConfigNode.getFirstChild());
174             else
175                 javaConfigNode.appendChild(profilerElement);
176             
177         }
178         // Save domain.xml
179
return saveDomainScriptFile(domainDoc, domainPath);
180     }
181     
182     /**
183      * Remove server instrumentation to disable profiling
184      * @param domainDoc Document object representing domain.xml
185      * @return true if profiling support has been removed
186      */

187     public boolean removeProfilerElements(Document JavaDoc domainDoc){
188         boolean eleRemoved = removeProfiler(domainDoc);
189         if(eleRemoved){
190             // Save domain.xml
191
return saveDomainScriptFile(domainDoc, getDomainLocation());
192         }else{
193             //no need to save.
194
return true;
195         }
196     }
197     
198     private boolean removeProfiler(Document JavaDoc domainDoc){
199         // Remove any previously defined 'profiler' element(s)
200
NodeList JavaDoc profilerElementNodeList = domainDoc.getElementsByTagName("profiler");//NOI18N
201
if (profilerElementNodeList != null && profilerElementNodeList.getLength() > 0){
202             Vector JavaDoc nodes = new Vector JavaDoc(); //temp storage for the nodes to delete
203
//we only want to delete the NBPROFILERNAME nodes.
204
// otherwise, see bug # 77026
205
for (int i = 0; i < profilerElementNodeList.getLength(); i++) {
206                 Node JavaDoc n= profilerElementNodeList.item(i);
207                 Node JavaDoc a= n.getAttributes().getNamedItem(CONST_NAME);//NOI18N
208
if ((a!=null)&&(a.getNodeValue().equals(NBPROFILERNAME))){//NOI18N
209
nodes.add(n);
210                 }
211             }
212             for(int i=0; i<nodes.size(); i++){
213                 Node JavaDoc nd = (Node JavaDoc)nodes.get(i);
214                 nd.getParentNode().removeChild(nd);
215             }
216             return true;
217         }
218             
219         return false;
220     }
221        
222     public String JavaDoc[] getHttpProxyOptions(){
223         ArrayList JavaDoc httpProxyOptions = new ArrayList JavaDoc();
224         Document JavaDoc domainDoc = getDomainDocument();
225         NodeList JavaDoc javaConfigNodeList = domainDoc.getElementsByTagName("java-config");
226         if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) {
227             return (String JavaDoc[])httpProxyOptions.toArray(new String JavaDoc[httpProxyOptions.size()]);
228         }
229         
230         NodeList JavaDoc jvmOptionNodeList = domainDoc.getElementsByTagName(CONST_JVM_OPTIONS);
231         for(int i=0; i<jvmOptionNodeList.getLength(); i++){
232             Node JavaDoc nd = jvmOptionNodeList.item(i);
233             if(nd.hasChildNodes()) {
234                 Node JavaDoc childNode = nd.getFirstChild();
235                 String JavaDoc 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 JavaDoc[] opts = new String JavaDoc[httpProxyOptions.size()];
247         return (String JavaDoc[])httpProxyOptions.toArray(opts);
248         
249     }
250     
251     public boolean setHttpProxyOptions(String JavaDoc[] httpProxyOptions){
252         Document JavaDoc domainDoc = getDomainDocument();
253         NodeList JavaDoc javaConfigNodeList = domainDoc.getElementsByTagName("java-config");
254         if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) {
255             return false;
256         }
257         
258         //Iterates through the existing proxy attributes and deletes them
259
removeProxyOptions(domainDoc, javaConfigNodeList.item(0));
260                 
261         //Add new set of proxy options
262
for(int j=0; j<httpProxyOptions.length; j++){
263             String JavaDoc option = httpProxyOptions[j];
264             Element JavaDoc jvmOptionsElement = domainDoc.createElement(CONST_JVM_OPTIONS);
265             Text JavaDoc 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 JavaDoc domainDoc, Node JavaDoc javaConfigNode){
274         NodeList JavaDoc jvmOptionNodeList = domainDoc.getElementsByTagName(CONST_JVM_OPTIONS);
275         
276         Vector JavaDoc nodes = new Vector JavaDoc();
277         for(int i=0; i<jvmOptionNodeList.getLength(); i++){
278             Node JavaDoc nd = jvmOptionNodeList.item(i);
279             if(nd.hasChildNodes()) {
280                 Node JavaDoc childNode = nd.getFirstChild();
281                 String JavaDoc 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 JavaDoc)nodes.get(i));
293         }
294         
295         return saveDomainScriptFile(domainDoc, getDomainLocation(), false);
296     }
297     
298     /*
299      * Creates Document instance from domain.xml
300      * @param domainScriptFilePath Path to domain.xml
301      */

302     private Document JavaDoc loadDomainScriptFile(String JavaDoc domainScriptFilePath) {
303         try {
304             DocumentBuilderFactory JavaDoc dbFactory = DocumentBuilderFactory.newInstance();
305             dbFactory.setValidating(false);
306             DocumentBuilder JavaDoc dBuilder = dbFactory.newDocumentBuilder();
307             
308             dBuilder.setEntityResolver(new EntityResolver JavaDoc() {
309                 public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
310                     StringReader JavaDoc reader = new StringReader JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
311
InputSource JavaDoc source = new InputSource JavaDoc(reader);
312                     source.setPublicId(publicId);
313                     source.setSystemId(systemId);
314                     return source;
315                 }
316             });
317             
318             return dBuilder.parse(new File JavaDoc(domainScriptFilePath));
319         } catch (Exception JavaDoc e) {
320             System.err.println("ConfigFilesUtils: unable to parse domain config file " + domainScriptFilePath);
321             return null;
322         }
323     }
324     
325     private boolean saveDomainScriptFile(Document JavaDoc domainScriptDocument, String JavaDoc domainScriptFilePath) {
326         return saveDomainScriptFile(domainScriptDocument, domainScriptFilePath, true);
327     }
328     /*
329      * Saves Document instance to domain.xml
330      * @param domainScriptDocument Document representing the xml
331      * @param domainScriptFilePath Path to domain.xml
332      */

333     private boolean saveDomainScriptFile(Document JavaDoc domainScriptDocument, String JavaDoc domainScriptFilePath, boolean indent) {
334         boolean result = false;
335         FileWriter JavaDoc domainScriptFileWriter = null;
336         try {
337             domainScriptFileWriter = new FileWriter JavaDoc(domainScriptFilePath);
338             try {
339                 TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
340                 Transformer JavaDoc 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 JavaDoc domSource = new DOMSource JavaDoc(domainScriptDocument);
350                 StreamResult JavaDoc streamResult = new StreamResult JavaDoc(domainScriptFileWriter);
351                 
352                 transformer.transform(domSource, streamResult);
353                 result = true;
354             } catch (Exception JavaDoc e) {
355                 System.err.println("ConfigFilesUtils: Unable to save domain config file " + domainScriptFilePath);
356                 result = false;
357             }
358         } catch (IOException JavaDoc 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 JavaDoc ioex2) {
367                 System.err.println("SunAS8IntegrationProvider: cannot close output stream for " + domainScriptFilePath);
368             };
369         }
370         
371         return result;
372     }
373     
374     // Converts -agentpath:"C:\Program Files\lib\profileragent.dll=\"C:\Program Files\lib\"",5140
375
// to -agentpath:C:\Program Files\lib\profileragent.dll="C:\Program Files\lib",5140 (AS 8.1 and AS 8.2)
376
// or to "-agentpath:C:\Program Files\lib\profileragent.dll=\"C:\Program Files\lib\",5140" (GlassFish or AS 9.0)
377
private String JavaDoc formatJvmOption(String JavaDoc jvmOption, File JavaDoc appServerLocation) {
378         // only jvmOption containing \" needs to be formatted
379
if (jvmOption.indexOf("\\\"") != -1) {
380
381             // Modification for AS 8.1, 8.2, initial modification for AS 9.0, GlassFish
382
// Converts -agentpath:"C:\Program Files\lib\profileragent.dll=\"C:\Program Files\lib\"",5140
383
// to -agentpath:C:\Program Files\lib\profileragent.dll="C:\Program Files\lib",5140
384
String JavaDoc modifiedOption = jvmOption.replaceAll("\\\\\"", "#"); // replace every \" by #
385
modifiedOption = modifiedOption.replaceAll("\\\"", ""); // delete all "
386
modifiedOption = modifiedOption.replaceAll("#", "\""); // replace every # by "
387

388             // Modification for AS 9.0, GlassFish should be done only if native launcher isn't used,
389
// otherwise will cause server startup failure. It seems that currently native launcher is used
390
// for starting the servers from the IDE.
391
// boolean usingNativeLauncher = false;
392
String JavaDoc osType=System.getProperty("os.name");//NOI18N
393
if ((osType.startsWith("Mac OS"))||(ServerLocationManager.isGlassFish(appServerLocation))){//no native for mac of glassfish
394
// if (!usingNativeLauncher) {
395

396                 // Modification for AS 9.0, GlassFish
397
// Converts -agentpath:C:\Program Files\lib\profileragent.dll="C:\Program Files\lib",5140
398
// "-agentpath:C:\Program Files\lib\profileragent.dll=\"C:\Program Files\lib\",5140"
399

400                     modifiedOption = "\"" + modifiedOption.replaceAll("\\\"", "\\\\\"") + "\"";
401
402             }
403
404             // return correctly formatted jvmOption
405
return modifiedOption;
406         }
407         // return original jvmOption
408
return jvmOption;
409      }
410     
411     static final String JavaDoc[] sysDatasources = {"jdbc/__TimerPool", "jdbc/__CallFlowPool", "jdbc/__default"}; //NOI18N
412

413     
414             
415     public HashMap JavaDoc getSunDatasourcesFromXml(){
416         HashMap JavaDoc dSources = new HashMap JavaDoc();
417         Document JavaDoc domainDoc = getDomainDocument();
418         HashMap JavaDoc dsMap = getDataSourcesAttrMap(domainDoc);
419         HashMap JavaDoc cpMap = getConnPoolsNodeMap(domainDoc);
420         dsMap.keySet().removeAll(Arrays.asList(sysDatasources));
421         String JavaDoc[] ds = (String JavaDoc[])dsMap.keySet().toArray(new String JavaDoc[dsMap.size()]);
422         
423         for(int i=0; i<ds.length; i++){
424             String JavaDoc jndiName = ds[i];
425             HashMap JavaDoc pValues = new HashMap JavaDoc();
426             NamedNodeMap JavaDoc dsAttrMap = (NamedNodeMap JavaDoc)dsMap.get(jndiName);
427             String JavaDoc poolName = dsAttrMap.getNamedItem("pool-name").getNodeValue();
428             
429             //Get the Connection Pool used by this jdbc-resource
430
Node JavaDoc cpNode = (Node JavaDoc)cpMap.get(poolName);
431             NamedNodeMap JavaDoc cpAttrMap = cpNode.getAttributes();
432             String JavaDoc dsClassName = cpAttrMap.getNamedItem("datasource-classname").getNodeValue();
433             
434             //Get property values
435
Element JavaDoc cpElement = (Element JavaDoc) cpNode;
436             NodeList JavaDoc propsNodeList = cpElement.getElementsByTagName("property");
437                         
438             //Cycle through each property element
439
HashMap JavaDoc map = new HashMap JavaDoc();
440             for(int j=0; j<propsNodeList.getLength(); j++){
441                 Node JavaDoc propNode = propsNodeList.item(j);
442                 NamedNodeMap JavaDoc propsMap = propNode.getAttributes();
443                 
444                 for(int m=0; m<propsMap.getLength(); m++){
445                     String JavaDoc mkey = propsMap.getNamedItem(CONST_NAME).getNodeValue();
446                     String JavaDoc mkeyValue = propsMap.getNamedItem("value").getNodeValue();
447                     map.put(mkey, mkeyValue);
448                 }
449             } // connection-pool properties
450

451             pValues.put(CONST_USER, (String JavaDoc)map.get(CONST_USER));
452             pValues.put(CONST_PASSWORD, (String JavaDoc)map.get(CONST_PASSWORD));
453             pValues.put(CONST_URL, (String JavaDoc)map.get(CONST_URL));
454             pValues.put(CONST_LOWER_DATABASE_NAME, (String JavaDoc)map.get(CONST_LOWER_DATABASE_NAME));
455             pValues.put(CONST_SERVER_NAME, (String JavaDoc)map.get(CONST_SERVER_NAME));
456             pValues.put(CONST_PORT_NUMBER, (String JavaDoc)map.get(CONST_PORT_NUMBER));
457             pValues.put(CONST_LOWER_PORT_NUMBER, (String JavaDoc)map.get(CONST_LOWER_PORT_NUMBER));
458             pValues.put(CONST_DATABASE_NAME, (String JavaDoc)map.get(CONST_DATABASE_NAME));
459             pValues.put(CONST_SID, (String JavaDoc)map.get(CONST_SID));
460             pValues.put("dsClassName", dsClassName);
461             
462             dSources.put(jndiName, pValues);
463         } // for each jdbc-resource
464
return dSources;
465     }
466     
467     public HashMap JavaDoc getConnPoolsFromXml(){
468         HashMap JavaDoc pools = new HashMap JavaDoc();
469         Document JavaDoc domainDoc = getDomainDocument();
470         HashMap JavaDoc cpMap = getConnPoolsNodeMap(domainDoc);
471         
472         String JavaDoc[] cp = (String JavaDoc[])cpMap.keySet().toArray(new String JavaDoc[cpMap.size()]);
473         for(int i=0; i<cp.length; i++){
474             String JavaDoc name = cp[i];
475             HashMap JavaDoc pValues = new HashMap JavaDoc();
476             Node JavaDoc cpNode = (Node JavaDoc)cpMap.get(name);
477             NamedNodeMap JavaDoc cpAttrMap = cpNode.getAttributes();
478             String JavaDoc dsClassName = cpAttrMap.getNamedItem("datasource-classname").getNodeValue();
479             
480             //Get property values
481
Element JavaDoc cpElement = (Element JavaDoc) cpNode;
482             NodeList JavaDoc propsNodeList = cpElement.getElementsByTagName("property");
483                         
484             //Cycle through each property element
485
HashMap JavaDoc map = new HashMap JavaDoc();
486             for(int j=0; j<propsNodeList.getLength(); j++){
487                 Node JavaDoc propNode = propsNodeList.item(j);
488                 NamedNodeMap JavaDoc propsMap = propNode.getAttributes();
489                 
490                 for(int m=0; m<propsMap.getLength(); m++){
491                     String JavaDoc mkey = propsMap.getNamedItem(CONST_NAME).getNodeValue();
492                     String JavaDoc mkeyValue = propsMap.getNamedItem("value").getNodeValue();
493                     map.put(mkey, mkeyValue);
494                 }
495             } // connection-pool properties
496

497             pValues.put(CONST_USER, (String JavaDoc)map.get(CONST_USER));
498             pValues.put(CONST_PASSWORD, (String JavaDoc)map.get(CONST_PASSWORD));
499             pValues.put(CONST_URL, (String JavaDoc)map.get(CONST_URL));
500             pValues.put(CONST_LOWER_DATABASE_NAME, (String JavaDoc)map.get(CONST_LOWER_DATABASE_NAME));
501             pValues.put(CONST_SERVER_NAME, (String JavaDoc)map.get(CONST_SERVER_NAME));
502             pValues.put(CONST_PORT_NUMBER, (String JavaDoc)map.get(CONST_PORT_NUMBER));
503             pValues.put(CONST_LOWER_PORT_NUMBER, (String JavaDoc)map.get(CONST_LOWER_PORT_NUMBER));
504             pValues.put(CONST_DATABASE_NAME, (String JavaDoc)map.get(CONST_DATABASE_NAME));
505             pValues.put(CONST_SID, (String JavaDoc)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 JavaDoc getDataSourcesAttrMap(Document JavaDoc domainDoc){
515         HashMap JavaDoc dataSourceMap = new HashMap JavaDoc();
516         updateWithSampleDataSource(domainDoc);
517         NodeList JavaDoc dataSourceNodeList = domainDoc.getElementsByTagName("jdbc-resource");
518         for(int i=0; i<dataSourceNodeList.getLength(); i++){
519             Node JavaDoc dsNode = dataSourceNodeList.item(i);
520             NamedNodeMap JavaDoc dsAttrMap = dsNode.getAttributes();
521             String JavaDoc jndiName = dsAttrMap.getNamedItem("jndi-name").getNodeValue();
522             dataSourceMap.put(jndiName, dsAttrMap);
523         }
524         return dataSourceMap;
525     }
526     
527     private boolean updateWithSampleDataSource(Document JavaDoc domainDoc){
528         boolean sampleExists = false;
529         NodeList JavaDoc dataSourceNodeList = domainDoc.getElementsByTagName("jdbc-resource");
530         for(int i=0; i<dataSourceNodeList.getLength(); i++){
531             Node JavaDoc dsNode = dataSourceNodeList.item(i);
532             NamedNodeMap JavaDoc dsAttrMap = dsNode.getAttributes();
533             String JavaDoc 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 JavaDoc getConnPoolsNodeMap(Document JavaDoc domainDoc){
545         HashMap JavaDoc connPoolMap = new HashMap JavaDoc();
546         NodeList JavaDoc connPoolNodeList = domainDoc.getElementsByTagName("jdbc-connection-pool");
547         for(int i=0; i<connPoolNodeList.getLength(); i++){
548             Node JavaDoc cpNode = connPoolNodeList.item(i);
549             NamedNodeMap JavaDoc cpAttrMap = cpNode.getAttributes();
550             String JavaDoc cpName = cpAttrMap.getNamedItem(CONST_NAME).getNodeValue();
551             connPoolMap.put(cpName, cpNode);
552         }
553         return connPoolMap;
554     }
555         
556     public boolean createSampleDatasource(Document JavaDoc domainDoc){
557         NodeList JavaDoc resourcesNodeList = domainDoc.getElementsByTagName("resources");
558         NodeList JavaDoc serverNodeList = domainDoc.getElementsByTagName("server");
559         if (resourcesNodeList == null || resourcesNodeList.getLength() == 0 ||
560                 serverNodeList == null || serverNodeList.getLength() == 0) {
561             return true;
562         }
563         Node JavaDoc resourcesNode = resourcesNodeList.item(0);
564         
565         HashMap JavaDoc cpMap = getConnPoolsNodeMap(domainDoc);
566         if(! cpMap.containsKey(SAMPLE_CONNPOOL)){
567             Node JavaDoc oldNode = (Node JavaDoc)cpMap.get("DerbyPool");
568             Node JavaDoc cpNode = oldNode.cloneNode(false);
569             NamedNodeMap JavaDoc cpAttrMap = cpNode.getAttributes();
570             cpAttrMap.getNamedItem(CONST_NAME).setNodeValue(SAMPLE_CONNPOOL);
571             HashMap JavaDoc poolProps = new HashMap JavaDoc();
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 JavaDoc[] propNames = poolProps.keySet().toArray();
579             for(int i=0; i<propNames.length; i++){
580                 String JavaDoc keyName = (String JavaDoc)propNames[i];
581                 Element JavaDoc propElement = domainDoc.createElement("property");
582                 propElement.setAttribute(CONST_NAME, keyName);
583                 propElement.setAttribute("value", (String JavaDoc)poolProps.get(keyName));
584                 cpNode.appendChild(propElement);
585             }
586             resourcesNode.appendChild(cpNode);
587         }
588                 
589         Element JavaDoc 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         // Insert the ds __Sample as a first child of "resources" element
596
if (resourcesNode.getFirstChild() != null)
597             resourcesNode.insertBefore(dsElement, resourcesNode.getFirstChild());
598         else
599             resourcesNode.appendChild(dsElement);
600         
601         //<resource-ref enabled="true" ref="jdbc/__default"/>
602
Element JavaDoc dsResRefElement = domainDoc.createElement("resource-ref");
603         dsResRefElement.setAttribute("ref", SAMPLE_DATASOURCE);
604         dsResRefElement.setAttribute("enabled", "true");
605         // Insert the ds reference __Sample as last child of "server" element
606
Node JavaDoc 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     /**
616      *
617      */

618     public DeploymentManager JavaDoc getDeploymentManager() {
619         return this.dm;
620     }
621     
622 }
623
Popular Tags