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.Collections;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.LinkedList;
026: import java.util.List;
027: import java.util.Map;
028: import org.dom4j.Document;
029: import org.dom4j.Element;
030: import org.mactor.framework.MactorException;
031: import org.mactor.framework.ParseUtil;
032:
033: public class MessageBrokersConfig {
034: private List<MessageBrokerConfig> brokers = new LinkedList<MessageBrokerConfig>();
035: private Document doc;
036:
037: public String asXML() {
038: return XmlUtil.getPrettyXML(doc.getRootElement());
039: }
040:
041: public List<MessageBrokerConfig> getMessageBrokers() {
042: return Collections.unmodifiableList(brokers);
043: }
044:
045: public static MessageBrokersConfig readFromFile(String path)
046: throws MactorException {
047: return loadConfig(XmlUtil.readFromFile(new File(path))
048: .getRootElement());
049: }
050:
051: private static MessageBrokersConfig loadConfig(Element element)
052: throws MactorException {
053: if (element == null)
054: throw new MactorException(
055: "Invalid MessageBrokerConfig: null");
056: if (!"message_broker_config".equals(element.getName()))
057: throw new MactorException(
058: "Invalid MessageBrokerConfig. Root node: '"
059: + element.getName()
060: + "'. Expected: 'message_broker_config'");
061: MessageBrokersConfig mbc = new MessageBrokersConfig();
062: mbc.doc = element.getDocument();
063: Iterator it = element.elementIterator("message_broker");
064: while (it.hasNext()) {
065: mbc.brokers.add(MessageBrokerConfig.loadConfig((Element) it
066: .next()));
067: }
068: return mbc;
069: }
070:
071: public static class MessageBrokerConfig {
072: private Map<String, ChannelConfig> channels = new HashMap<String, ChannelConfig>();
073: private Map<String, String> values = new HashMap<String, String>();
074: private String brokerClass;
075: private String name;
076: private String archivePath;
077: private boolean arhiveDeadLetterMessage;
078: private boolean archiveConsumedMessages;
079: private int messageReadLimit;
080: private int messageReadIntervalSeconds;
081:
082: public boolean isArchiveConsumedMessages() {
083: return archiveConsumedMessages;
084: }
085:
086: public String getArchivePath() {
087: return archivePath;
088: }
089:
090: public boolean isArhiveDeadLetterMessage() {
091: return arhiveDeadLetterMessage;
092: }
093:
094: public String getBrokerClass() {
095: return brokerClass;
096: }
097:
098: public Map<String, ChannelConfig> getChannels() {
099: return channels;
100: }
101:
102: public String getName() {
103: return name;
104: }
105:
106: public Map<String, String> getValues() {
107: return values;
108: }
109:
110: public String getValue(String name) {
111: return values.get(name);
112: }
113:
114: public ChannelConfig getChannelConfig(String channelName) {
115: return channels.get(channelName);
116: }
117:
118: public ChannelConfig getRequieredChannelConfig(
119: String channelName) throws MactorException {
120: ChannelConfig cf = channels.get(channelName);
121: if (cf == null)
122: throw new MactorException(
123: "There is no channel config for channel '"
124: + channelName + "'");
125: return cf;
126: }
127:
128: private static MessageBrokerConfig loadConfig(Element element)
129: throws MactorException {
130: MessageBrokerConfig mbc = new MessageBrokerConfig();
131: mbc.name = element.attributeValue("name");
132: mbc.archivePath = element.attributeValue("archive_path");
133: mbc.brokerClass = element.attributeValue("broker_class");
134: mbc.messageReadIntervalSeconds = ParseUtil
135: .tryParseIntVal(element
136: .attributeValue("message_read_interval_seconds"));
137: if (mbc.messageReadIntervalSeconds == 0)
138: mbc.messageReadIntervalSeconds = 10;
139: mbc.messageReadLimit = ParseUtil.tryParseIntVal(element
140: .attributeValue("message_read_limit"));
141: if (mbc.messageReadLimit == 0)
142: mbc.messageReadLimit = 20;
143: mbc.archiveConsumedMessages = Boolean.parseBoolean(element
144: .attributeValue("archive_consumed_messages"));
145: mbc.arhiveDeadLetterMessage = Boolean.parseBoolean(element
146: .attributeValue("archive_dead_letter_messages"));
147: Iterator it = element.elementIterator("channel");
148: while (it.hasNext()) {
149: ChannelConfig c = ChannelConfig.loadConfig((Element) it
150: .next());
151: mbc.channels.put(c.getName(), c);
152: }
153: it = element.elementIterator("value");
154: while (it.hasNext()) {
155: Element e = (Element) it.next();
156: mbc.values.put(e.attributeValue("name"), e
157: .getTextTrim());
158: }
159: return mbc;
160: }
161:
162: public static class ChannelConfig {
163: private Map<String, String> values = new HashMap<String, String>();
164: private String name;
165:
166: public String getValue(String valueName) {
167: return values.get(valueName);
168: }
169:
170: public String getRequieredValue(String valueName)
171: throws MactorException {
172: String value = getValue(valueName);
173: if (value == null)
174: throw new MactorException(
175: "The channel config named '"
176: + name
177: + "' does not specify the requiered value '"
178: + valueName + "'");
179: return value;
180: }
181:
182: public String getValue(String valueName, String defValue) {
183: String v = values.get(valueName);
184: return v == null ? defValue : v;
185: }
186:
187: public static ChannelConfig loadConfig(Element element)
188: throws MactorException {
189: ChannelConfig c = new ChannelConfig();
190: c.name = element.attributeValue("name");
191: Iterator it = element.elementIterator("value");
192: while (it.hasNext()) {
193: Element e = (Element) it.next();
194: c.values.put(e.attributeValue("name"), e
195: .getTextTrim());
196: }
197: return c;
198: }
199:
200: public String getName() {
201: return name;
202: }
203:
204: public Map<String, String> getValues() {
205: return values;
206: }
207: }
208:
209: public int getMessageReadIntervalSeconds() {
210: return messageReadIntervalSeconds;
211: }
212:
213: public int getMessageReadLimit() {
214: return messageReadLimit;
215: }
216: }
217: }
|