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.io.File;
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.MactorException;
028: import org.mactor.framework.ParseUtil;
029:
030: public class TestSpec extends ContainerSpec {
031: private String globalConfigPath;
032: private String messageBrokersConfigPath;
033: private int delayBeforeStartSeconds;
034: private Map<String, SpecNode> nodeMap = new HashMap<String, SpecNode>();
035: private Document doc;
036:
037: public String asXML() {
038: return XmlUtil.getPrettyXML(doc.getRootElement());
039: }
040:
041: private void buildIndex() throws MactorException {
042: addContainerSpecToMap(this );
043: }
044:
045: private void addContainerSpecToMap(ContainerSpec node)
046: throws MactorException {
047: addToNodeMap(node);
048: if (node instanceof MessageReceiveSpec
049: && !nodeMap.containsKey(((MessageReceiveSpec) node)
050: .getMessageSubscribeNodeName())) {
051: throw new MactorException(
052: "The MessageReceiveSpec node '"
053: + node.getName()
054: + "' refers to an unknown MessageSubscribeSpec node '"
055: + ((MessageReceiveSpec) node)
056: .getMessageSubscribeNodeName()
057: + "'");
058: }
059: Iterator it = node.getSpecNodes().iterator();
060: while (it.hasNext()) {
061: SpecNode n = (SpecNode) it.next();
062: if (n instanceof ContainerSpec) {
063: addContainerSpecToMap((ContainerSpec) n);
064: } else {
065: addToNodeMap(n);
066: }
067: }
068: }
069:
070: private void addToNodeMap(SpecNode node) throws MactorException {
071: if (nodeMap.containsKey(node.getName())) {
072: throw new MactorException(
073: "Nameable nodes in the test-spec must have unique names. The name '"
074: + node.getName()
075: + "' has been used on multiple nodes");
076: }
077: nodeMap.put(node.getName(), node);
078: }
079:
080: public int getDelayBeforeStartSeconds() {
081: return delayBeforeStartSeconds;
082: }
083:
084: public static TestSpec loadFromFile(File file)
085: throws MactorException {
086: return TestSpec.loadSpec(XmlUtil.readFromFile(file)
087: .getRootElement());
088: }
089:
090: public SpecNode findSpecNode(String nodeName) {
091: return nodeMap.get(nodeName);
092: }
093:
094: public static TestSpec loadSpec(Element element)
095: throws MactorException {
096: if (element == null)
097: throw new MactorException("Invalid Test: null");
098: if (!"test".equals(element.getName()))
099: throw new MactorException("Invalid Test spec. Root node: '"
100: + element.getName() + "'. Expected: 'test'");
101: TestSpec s = new TestSpec();
102: s.doc = element.getDocument();
103: s.name = element.attributeValue("name");
104: s.globalConfigPath = element.attributeValue("global_config");
105: s.messageBrokersConfigPath = element
106: .attributeValue("message_brokers_config");
107: s.delayBeforeStartSeconds = ParseUtil.tryParseIntVal(element
108: .attributeValue("delay_before_start_seconds"));
109: s.loadContainedNodes(element);
110: s.buildIndex();
111: return s;
112: }
113:
114: public String getGlobalConfigPath() {
115: return globalConfigPath;
116: }
117:
118: public String getMessageBrokersConfigPath() {
119: return messageBrokersConfigPath;
120: }
121:
122: public String getShortDescription() {
123: return "Test node - '" + name + "'";
124: }
125:
126: public String getDescription() {
127: return "Delay defore start (seconds):"
128: + delayBeforeStartSeconds;
129: }
130: }
|