KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > DomainXMLHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.collect;
24
25 import com.sun.enterprise.diagnostics.util.DiagnosticServiceHelper;
26 import com.sun.enterprise.diagnostics.util.XmlUtils;
27 import org.w3c.dom.*;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.io.IOException JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import com.sun.logging.LogDomains;
36 import com.sun.enterprise.diagnostics.*;
37 /**
38  * A helper which reads/loads domain.xml and provides information such as
39  * list of confidential attributes.
40  * @author Manisha Umbarje
41  */

42 public class DomainXMLHelper {
43    
44     private Document configDoc;
45     private static final String JavaDoc PASSWORD = "password";
46     private static final String JavaDoc NAME = "name";
47     private static Logger JavaDoc logger =
48     LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
49      
50     private String JavaDoc repositoryDir;
51     /** Creates a new instance of DomainXMLHelper */
52     public DomainXMLHelper(String JavaDoc repositoryDir) {
53         this.repositoryDir = repositoryDir;
54     }
55     
56     public List JavaDoc<String JavaDoc> getAttrs() throws DiagnosticException {
57         if(configDoc == null)
58             loadXML();
59         ArrayList JavaDoc list = new ArrayList JavaDoc(5);
60         XmlUtils.getAttributes(configDoc.getDocumentElement(),
61             PASSWORD, list);
62         return list;
63     }
64     
65     /**
66      * Loads XML - domain.xml in local mode
67      */

68     private void loadXML() throws DiagnosticException {
69     try {
70         String JavaDoc configFile = repositoryDir +
71                     Constants.DOMAIN_XML;
72             String JavaDoc configDtdFile = DiagnosticServiceHelper.getInstallationRoot()+
73                     Constants.DOMAIN_XML_DTD;
74         logger.log(Level.FINE,
75                     "diagnostic-service.loadxml_configfile", configFile);
76         configDoc = XmlUtils.loadXML(configFile , configDtdFile);
77             
78     } catch (SAXException JavaDoc ex) {
79         logger.log(Level.SEVERE,
80                       "diagnostic-service.error_loading_xml",ex.getMessage());
81         throw new DiagnosticException (ex.getMessage());
82     } catch (IOException JavaDoc ioe) {
83         logger.log(Level.SEVERE,
84               "diagnostic-service.error_loading_xml",ioe.getMessage());
85         throw new DiagnosticException (ioe.getMessage());
86     } catch (ParserConfigurationException JavaDoc pce) {
87         logger.log(Level.SEVERE,
88               "diagnostic-service.error_loading_xml",pce.getMessage());
89         throw new DiagnosticException (pce.getMessage());
90     }
91         
92     }
93     /**
94      * Returns specified xml node
95      * @param tagName name of tag
96      * @param elementName value of tag
97      * @return element corresponding to combination of tagName and elementName
98      */

99     public Element getElement(String JavaDoc tagName, String JavaDoc elementName)
100     throws DiagnosticException {
101         if( tagName == null && elementName == null)
102             return null;
103         
104     if (configDoc == null)
105         loadXML();
106
107         NodeList list = configDoc.getDocumentElement().getElementsByTagName(tagName);
108         if (list != null) {
109             int length = list.getLength();
110             Element element = null;
111             for (int i = 0; i < length ; i++) {
112                 element = (Element) list.item(i);
113                 if(element.getAttribute(NAME).equals(elementName))
114                     return element;
115             }
116         }// if (list != null)
117

118     return null;
119     }//getElement
120

121     /**
122      * Assumes that there is only one child with given tag name
123      * @param element element
124      * @param tagName name of child element
125      * @return first child matching the tagName
126      */

127     public Element getElement(Element element, String JavaDoc tagName) {
128     NodeList list = element.getElementsByTagName(tagName);
129
130     if (list != null) {
131         return (Element)list.item(0);
132     }//if
133
return null;
134     }//getChildByTagName
135

136     /**
137      * Gets attribute value
138      * @param element element
139      * @param attrName name of attribute
140      * @return value of attribute
141      */

142     public String JavaDoc getAttribute(Element element , String JavaDoc attrName) {
143         if(element != null)
144             return element.getAttribute(attrName); //old value was config-ref
145
return null;
146     }
147     
148 }
149
Popular Tags