001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.framework.spec;
020:
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import org.dom4j.Document;
026: import org.dom4j.Element;
027: import org.mactor.framework.ConfigException;
028: import org.mactor.framework.MactorException;
029: import org.mactor.framework.ParseUtil;
030:
031: public class GlobalConfig {
032: private Map<String, String> values = new HashMap<String, String>();
033: private Map<String, Group> groups = new HashMap<String, Group>();
034: private int maxNumberOfTestThreads;
035: private MessageBrokersConfig brokersConfig;
036: private Document doc;
037:
038: public String asXML() {
039: return XmlUtil.getPrettyXML(doc.getRootElement());
040: }
041:
042: public String getValue(String valueName) {
043: return values.get(valueName);
044: }
045:
046: public Group getGroup(String groupName) {
047: return groups.get(groupName);
048: }
049:
050: public Group getRequieredGroup(String groupName)
051: throws ConfigException {
052: Group value = groups.get(groupName);
053: if (value == null)
054: throw new ConfigException(
055: "The requiered global config group named '"
056: + groupName
057: + "' was not found in the global config");
058: return value;
059: }
060:
061: public Map<String, String> getValues() {
062: return Collections.unmodifiableMap(values);
063: }
064:
065: public Map<String, Group> getGroups() {
066: return Collections.unmodifiableMap(groups);
067: }
068:
069: public static GlobalConfig readFromFile(String path)
070: throws MactorException {
071: return loadConfig(XmlUtil.readFromFile(path).getRootElement());
072: }
073:
074: private static GlobalConfig loadConfig(Element element)
075: throws MactorException {
076: if (element == null)
077: throw new MactorException("Invalid GlobalConfig: null");
078: if (!"global_config".equals(element.getName()))
079: throw new MactorException(
080: "Invalid GlobalConfig. Root node: '"
081: + element.getName()
082: + "'. Expected: 'global_config'");
083: GlobalConfig gc = new GlobalConfig();
084: gc.doc = element.getDocument();
085: gc.maxNumberOfTestThreads = ParseUtil.tryParseIntVal(element
086: .attributeValue("max_test_threads"));
087: if (gc.maxNumberOfTestThreads == 0)
088: gc.maxNumberOfTestThreads = 10;
089: gc.brokersConfig = MessageBrokersConfig.readFromFile(element
090: .attributeValue("message_brokers_config"));
091: Iterator it = element.elementIterator("value");
092: while (it.hasNext()) {
093: Element e = (Element) it.next();
094: gc.values.put(e.attributeValue("name"), e.getTextTrim());
095: }
096: it = element.elementIterator("group");
097: while (it.hasNext()) {
098: Group g = Group.loadGroup((Element) it.next());
099: gc.groups.put(g.name, g);
100: }
101: return gc;
102: }
103:
104: public static class Group {
105: private Map<String, String> values = new HashMap<String, String>();
106:
107: public String getValue(String valueName) {
108: return values.get(valueName);
109: }
110:
111: public String getRequieredValue(String valueName)
112: throws MactorException {
113: String value = getValue(valueName);
114: if (value == null)
115: throw new MactorException(
116: "The global config group named '"
117: + name
118: + "' does not specify the requiered value '"
119: + valueName + "'");
120: return value;
121: }
122:
123: public Map<String, String> getValues() {
124: return Collections.unmodifiableMap(values);
125: }
126:
127: private String name;
128:
129: static Group loadGroup(Element element) {
130: Group g = new Group();
131: g.name = element.attributeValue("name");
132: Iterator it = element.elementIterator("value");
133: while (it.hasNext()) {
134: Element e = (Element) it.next();
135: g.values.put(e.attributeValue("name"), e.getTextTrim());
136: }
137: return g;
138: }
139:
140: public String getName() {
141: return name;
142: }
143: }
144:
145: public int getMaxNumberOfTestThreads() {
146: return maxNumberOfTestThreads;
147: }
148:
149: public MessageBrokersConfig getBrokersConfig() {
150: return brokersConfig;
151: }
152: }
|