001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Registry.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * RegistryDocument.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on July 20, 2005, 3:25 PM
037: */package com.sun.jbi.management.registry.xml;
038:
039: import com.sun.jbi.management.ComponentInfo;
040: import com.sun.jbi.management.ConfigurationCategory;
041:
042: import org.w3c.dom.Document;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.Node;
045: import org.w3c.dom.NodeList;
046: import org.w3c.dom.Text;
047:
048: import java.util.Map;
049: import java.util.HashMap;
050: import java.util.Properties;
051:
052: /**
053: * RegistryDocument encapsulates a read only registry DOM. This class has user friendly
054: * operations to get information from the registry w/o getting into the nitty-gritty
055: * details of parsing the registry XML doc. This class uses XPath expressions for faster
056: * processing.
057: *
058: * @author Sun Microsystems, Inc.
059: */
060: public class RegistryDocument {
061: /** The registry DOM */
062: private Document mRegistryDocument;
063:
064: /** Domain literal */
065: private static final String DOMAIN = "domain";
066:
067: public RegistryDocument(Document regDoc) {
068: mRegistryDocument = regDoc;
069: }
070:
071: /**
072: * Get the value of a specific configuration attribute for a target belonging to a
073: * particular category. If the attribute is overriden then the override is returned.
074: * If the attribute is missing or the registry configuration is empty a null value
075: * is returned.
076: *
077: * @param target - identification for the target
078: * @param category - configuration category
079: * @param name - attribute name
080: * @return the string value of the configuration attribute from the persisted
081: * registry or null if the attribute is missing or configuration is empty
082: */
083: public String getConfigurationAttribute(String target,
084: ConfigurationCategory category, String attrName) {
085: String value = null;
086: if (isAttributeOverriden(target, category, attrName)) {
087: value = getAttribute(target, category, attrName);
088: } else {
089: value = getAttribute(DOMAIN, category, attrName);
090: }
091: return value;
092: }
093:
094: /**
095: * Get a map of all the configuration attributes for a particular category. If an
096: * attribute is overriden then the specific value replaces the domain configuration
097: *
098: * @param target - identification for the target
099: * @param category - configuration category
100: * @return the configuration attributes from the persisted registry or an empty list
101: *
102: */
103: public Map<String, String> getConfigurationAttributes(
104: String target, ConfigurationCategory category) {
105: Map<String, String> targetSpecificProps = getProperties(target,
106: category);
107: return targetSpecificProps;
108: }
109:
110: /**
111: * Get the static component configuration attributes for a component.
112: */
113: public Properties getComponentConfigurationAttributes(
114: String target, boolean isServer, String componentName) {
115: Element targetElement = getTarget(target, isServer);
116: Element compRef = getComponentRef(targetElement, componentName);
117: return getComponentConfigurationProperties(compRef);
118: }
119:
120: /**
121: * Get the application variables set on a specific component.
122: *
123: * @param isServer - flag indicating whether the target is a server or cluster.
124: * @param target - target name
125: * @param componentName - identification for the component.
126: */
127: public ComponentInfo.Variable[] getComponentApplicationVariables(
128: String target, boolean isServer, String componentName) {
129: Element targetElement = getTarget(target, isServer);
130: Element compRef = getComponentRef(targetElement, componentName);
131: return getComponentApplicationVariables(compRef);
132: }
133:
134: /**
135: * Get all the named application configurations.
136: *
137: * @param isServer
138: * flag indicating whether the target is a server or cluster.
139: * @param target
140: * target name
141: * @param componentName
142: * identification for the component.
143: * @return a Map of configuration names and the named config properties.
144: */
145: public Map<String, Properties> getComponentApplicationConfiguration(
146: String target, boolean isServer, String componentName) {
147: Element targetElement = getTarget(target, isServer);
148: Element compRef = getComponentRef(targetElement, componentName);
149: return getComponentApplicationConfigurations(compRef);
150: }
151:
152: /*--------------------------------------------------------------------------------*\
153: * Private helpers *
154: \*--------------------------------------------------------------------------------*/
155:
156: /**
157: * @return true if an attribute is overridden
158: */
159:
160: private boolean isAttributeOverriden(String target,
161: ConfigurationCategory category, String attribName) {
162: NodeList list;
163: return (getConfig(target) != null);
164: }
165:
166: /**
167: * Get the value of a specific configuration attribute for a target belonging to a
168: * particular category.
169: *
170: * @param target - identification for the target
171: * @param category - configuration category
172: * @param name - attribute name
173: * @return the string value of the configuration attribute from the persisted
174: * registry or null if the attribute is missing or configuration is empty
175: */
176: public String getAttribute(String target,
177: ConfigurationCategory category, String attribName) {
178: String value = null;
179: Map<String, String> properties = getProperties(target, category);
180: if (properties.containsKey(attribName)) {
181: value = properties.get(attribName);
182: }
183: return value;
184: }
185:
186: /**
187: * Get the Property nodes for a given target and configuration catregory
188: *
189: * @param target - target name
190: * @param category - Configuration Category
191: */
192: private Map<String, String> getProperties(String target,
193: ConfigurationCategory category) {
194: NodeList nameList = null;
195: NodeList valueList = null;
196: Map properties = new Properties();
197:
198: Element targetConfig = getConfig(target);
199: if (targetConfig != null) {
200: // -- Get the category
201: NodeList configTypeList = targetConfig
202: .getElementsByTagName("config-type");
203: for (int i = 0; i < configTypeList.getLength(); i++) {
204: Element configTypeElem = (Element) configTypeList
205: .item(i);
206: if ((category.toString()).equals(configTypeElem
207: .getAttribute("category"))) {
208: properties = getProperties(configTypeElem);
209: }
210: }
211: }
212:
213: return properties;
214: }
215:
216: /**
217: *
218: * @return the "config" element for the target if found, null otherwise.
219: */
220: private Element getConfig(String target) {
221: Element targetConfig = null;
222:
223: if (mRegistryDocument != null) {
224: // -- Get the JBI element
225: Element jbi = mRegistryDocument.getDocumentElement();
226: Element configsElem = null;
227: if (jbi != null) {
228: NodeList configs = jbi.getElementsByTagName("configs");
229:
230: if (configs.getLength() >= 1) {
231: configsElem = (Element) configs.item(0);
232: }
233: }
234:
235: if (configsElem != null) {
236: // -- Get the config element for the target
237: NodeList configList = configsElem
238: .getElementsByTagName("config");
239: for (int i = 0; i < configList.getLength(); i++) {
240: Element configElem = (Element) configList.item(i);
241: if ((target + "-config").equals(configElem
242: .getAttribute("name"))) {
243: targetConfig = configElem;
244: }
245: }
246: }
247: }
248: return targetConfig;
249: }
250:
251: /**
252: *
253: * @return the component configuration properties for the component installed on the target
254: * if found, null otherwise.
255: */
256: private Properties getComponentConfigurationProperties(
257: Element compRef) {
258: Properties properties = new Properties();
259:
260: if (compRef != null) {
261: // -- Get the component config element for the component
262: NodeList componentConfigs = compRef
263: .getElementsByTagName("component-config");
264: Element componentCfgElem = null;
265: if (componentConfigs.getLength() >= 1) {
266: componentCfgElem = (Element) componentConfigs.item(0);
267: }
268:
269: if (componentCfgElem != null) {
270: // Get all the properties under this node
271: NodeList propertyList = componentCfgElem
272: .getElementsByTagName("property");
273: for (int j = 0; j < propertyList.getLength(); j++) {
274: Element propertyElem = (Element) propertyList
275: .item(j);
276:
277: // Get Name
278: Element nameElem = (Element) propertyElem
279: .getElementsByTagName("name").item(0);
280: String name = ((Text) nameElem.getFirstChild())
281: .getNodeValue();
282:
283: // Get value
284: Element valueElem = (Element) propertyElem
285: .getElementsByTagName("value").item(0);
286: String value = ((Text) valueElem.getFirstChild())
287: .getNodeValue();
288: properties.put(name, value);
289: }
290: }
291: }
292: return properties;
293: }
294:
295: /**
296: *
297: * @return the components application variables
298: * if found. An empty array is returned if there aren't any variables et on the
299: * component.
300: *
301: * @param compRef
302: */
303: ComponentInfo.Variable[] getComponentApplicationVariables(
304: Element compRef) {
305: ComponentInfo.Variable[] appVars = new ComponentInfo.Variable[] {};
306:
307: if (compRef != null) {
308: // -- Get the component config element for the component
309: NodeList componentConfigs = compRef
310: .getElementsByTagName("component-config");
311: Element componentCfgElem = null;
312: if (componentConfigs.getLength() >= 1) {
313: componentCfgElem = (Element) componentConfigs.item(0);
314: }
315:
316: if (componentCfgElem != null) {
317: // Get all the application variables
318: NodeList varList = componentCfgElem
319: .getElementsByTagName("application-variable");
320: appVars = new ComponentInfo.Variable[varList
321: .getLength()];
322:
323: for (int j = 0; j < varList.getLength(); j++) {
324: Element varElem = (Element) varList.item(j);
325:
326: // Get Name
327: Element nameElem = (Element) varElem
328: .getElementsByTagName("name").item(0);
329: String name = ((Text) nameElem.getFirstChild())
330: .getNodeValue();
331:
332: // Get value
333: Element valueElem = (Element) varElem
334: .getElementsByTagName("value").item(0);
335: String value = ((Text) valueElem.getFirstChild())
336: .getNodeValue();
337:
338: // Get type
339: Element typeElem = (Element) varElem
340: .getElementsByTagName("type").item(0);
341: String type = ((Text) typeElem.getFirstChild())
342: .getNodeValue();
343:
344: appVars[j] = new ComponentInfo.Variable(name,
345: value, type);
346: }
347: }
348: }
349: return appVars;
350: }
351:
352: /**
353: *
354: * @return the components application configurations
355: * if found. An empty Map is returned if there aren't any named configurations.
356: *
357: * @param compRef - conponent ref element
358: */
359: Map<String, Properties> getComponentApplicationConfigurations(
360: Element compRef) {
361: Map<String, Properties> configMap = new HashMap();
362:
363: if (compRef != null) {
364: // -- Get the component config element for the component
365: NodeList componentConfigs = compRef
366: .getElementsByTagName("component-config");
367: Element componentCfgElem = null;
368: if (componentConfigs.getLength() >= 1) {
369: componentCfgElem = (Element) componentConfigs.item(0);
370: }
371:
372: if (componentCfgElem != null) {
373: // Get all the application configurations
374: NodeList configList = componentCfgElem
375: .getElementsByTagName("application-configuration");
376:
377: for (int j = 0; j < configList.getLength(); j++) {
378: Element configElem = (Element) configList.item(j);
379:
380: String configName = configElem.getAttribute("name");
381:
382: Properties props = getProperties(configElem);
383:
384: configMap.put(configName, props);
385: }
386: }
387: }
388: return configMap;
389: }
390:
391: /**
392: *
393: * @return the "component-ref" element for the component installed on the target
394: * if found, null otherwise.
395: */
396: private Element getComponentRef(Element targetElement,
397: String compName) {
398: Element compRef = null;
399: if (targetElement != null) {
400: // -- Get the specific components reference
401: NodeList componentRefsList = targetElement
402: .getElementsByTagName("component-ref");
403: for (int i = 0; i < componentRefsList.getLength(); i++) {
404: Element componentElem = (Element) componentRefsList
405: .item(i);
406: if ((compName).equals(componentElem
407: .getAttribute("name-ref"))) {
408: compRef = componentElem;
409: }
410: }
411:
412: }
413: return compRef;
414: }
415:
416: /**
417: * @return the target reference element
418: */
419: public Element getTarget(String target, boolean isServer) {
420: Element targetRef = null;
421:
422: if (mRegistryDocument != null) {
423: // -- Get the JBI element
424: Element jbi = mRegistryDocument.getDocumentElement();
425: Element targetsElem = null;
426: if (jbi != null) {
427: NodeList targets = null;
428: if (isServer) {
429: targets = jbi.getElementsByTagName("servers");
430: } else {
431: targets = jbi.getElementsByTagName("clusters");
432: }
433:
434: if (targets.getLength() >= 1) {
435: targetsElem = (Element) targets.item(0);
436: }
437: }
438:
439: if (targetsElem != null) {
440: // -- Get the specific target reference
441: NodeList targetList = null;
442:
443: if (isServer) {
444: targetList = targetsElem
445: .getElementsByTagName("server");
446: } else {
447: targetList = targetsElem
448: .getElementsByTagName("cluster");
449: }
450:
451: for (int i = 0; i < targetList.getLength(); i++) {
452: Element targetElem = (Element) targetList.item(i);
453: if ((target).equals(targetElem
454: .getAttribute("name-ref"))) {
455: targetRef = targetElem;
456: break;
457: }
458: }
459: }
460: }
461: return targetRef;
462: }
463:
464: /**
465: * @return the properties from a single element
466: */
467: Properties getProperties(Element element) {
468: Properties properties = new Properties();
469: // Get all the properties under this node
470: NodeList propertyList = element
471: .getElementsByTagName("property");
472: for (int j = 0; j < propertyList.getLength(); j++) {
473: Element propertyElem = (Element) propertyList.item(j);
474:
475: // Get Name
476: Element nameElem = (Element) propertyElem
477: .getElementsByTagName("name").item(0);
478: String name = ((Text) nameElem.getFirstChild())
479: .getNodeValue();
480:
481: // Get value
482: Element valueElem = (Element) propertyElem
483: .getElementsByTagName("value").item(0);
484: String value = ((Text) valueElem.getFirstChild())
485: .getNodeValue();
486: properties.put(name, value);
487: }
488:
489: return properties;
490: }
491:
492: }
|