001 /******************************************************************************
002 * Copyright (C) MActor Developers. 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 ******************************************************************************/
020 package org.mactor.framework.spec;
021
022 import java.util.Collections;
023 import java.util.HashMap;
024 import java.util.Iterator;
025 import java.util.Map;
026
027 import org.dom4j.Document;
028 import org.dom4j.Element;
029 import org.mactor.framework.ConfigException;
030 import org.mactor.framework.MactorException;
031 import org.mactor.framework.ParseUtil;
032
033 public class GlobalConfig {
034 private Map<String, String> values = new HashMap<String, String>();
035 private Map<String, Group> groups = new HashMap<String, Group>();
036 private int maxNumberOfTestThreads;
037 private Document doc;
038 public String asXML() {
039 return XmlUtil.getPrettyXML(doc.getRootElement());
040 }
041 public String getValue(String valueName) {
042 return values.get(valueName);
043 }
044 public Group getGroup(String groupName) {
045 return groups.get(groupName);
046 }
047 public Group getRequieredGroup(String groupName) throws ConfigException {
048 Group value = groups.get(groupName);
049 if (value == null)
050 throw new ConfigException("The requiered global config group named '" + groupName + "' was not found in the global config");
051 return value;
052 }
053 public Map<String, String> getValues() {
054 return Collections.unmodifiableMap(values);
055 }
056 public Map<String, Group> getGroups() {
057 return Collections.unmodifiableMap(groups);
058 }
059 public GlobalConfig(Document globalConfigDoc) throws MactorException {
060 if (globalConfigDoc == null || globalConfigDoc.getRootElement() == null)
061 throw new MactorException("Invalid GlobalConfig: null");
062 Element element = globalConfigDoc.getRootElement();
063 if (!"global_config".equals(element.getName()))
064 throw new MactorException("Invalid GlobalConfig. Root node: '" + element.getName() + "'. Expected: 'global_config'");
065 doc = element.getDocument();
066 maxNumberOfTestThreads = ParseUtil.tryParseIntVal(element.attributeValue("max_test_threads"));
067 if (maxNumberOfTestThreads == 0)
068 maxNumberOfTestThreads = 10;
069 Iterator it = element.elementIterator("value");
070 while (it.hasNext()) {
071 Element e = (Element) it.next();
072 values.put(e.attributeValue("name"), e.getTextTrim());
073 }
074 it = element.elementIterator("group");
075 while (it.hasNext()) {
076 Group g = Group.loadGroup((Element) it.next());
077 groups.put(g.name, g);
078 }
079 }
080 public static class Group {
081 private Map<String, String> values = new HashMap<String, String>();
082 public String getValue(String valueName) {
083 return values.get(valueName);
084 }
085 public String getRequieredValue(String valueName) throws MactorException {
086 String value = getValue(valueName);
087 if (value == null)
088 throw new MactorException("The global config group named '" + name + "' does not specify the requiered value '" + valueName + "'");
089 return value;
090 }
091 public Map<String, String> getValues() {
092 return Collections.unmodifiableMap(values);
093 }
094 private String name;
095 public static Group loadGroup(Element element) {
096 Group g = new Group();
097 g.name = element.attributeValue("name");
098 Iterator it = element.elementIterator("value");
099 while (it.hasNext()) {
100 Element e = (Element) it.next();
101 g.values.put(e.attributeValue("name"), e.getTextTrim());
102 }
103 return g;
104 }
105 public String getName() {
106 return name;
107 }
108 }
109 public int getMaxNumberOfTestThreads() {
110 return maxNumberOfTestThreads;
111 }
112 }