PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/helpers/XMLConfiguration.java

http://mobicents.googlecode.com/
Java | 627 lines | 508 code | 60 blank | 59 comment | 158 complexity | e3e4ed6d2ded83d802dbc6991fa29d9b MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2006, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jdiameter.client.impl.helpers;
  23. import static org.jdiameter.client.impl.helpers.Parameters.AcctApplId;
  24. import static org.jdiameter.client.impl.helpers.Parameters.Agent;
  25. import static org.jdiameter.client.impl.helpers.Parameters.ApplicationId;
  26. import static org.jdiameter.client.impl.helpers.Parameters.AuthApplId;
  27. import static org.jdiameter.client.impl.helpers.Parameters.CeaTimeOut;
  28. import static org.jdiameter.client.impl.helpers.Parameters.CipherSuites;
  29. import static org.jdiameter.client.impl.helpers.Parameters.Concurrent;
  30. import static org.jdiameter.client.impl.helpers.Parameters.ConcurrentEntityDescription;
  31. import static org.jdiameter.client.impl.helpers.Parameters.ConcurrentEntityName;
  32. import static org.jdiameter.client.impl.helpers.Parameters.ConcurrentEntityPoolSize;
  33. import static org.jdiameter.client.impl.helpers.Parameters.Dictionary;
  34. import static org.jdiameter.client.impl.helpers.Parameters.DictionaryClass;
  35. import static org.jdiameter.client.impl.helpers.Parameters.DictionaryEnabled;
  36. import static org.jdiameter.client.impl.helpers.Parameters.DictionaryReceiveLevel;
  37. import static org.jdiameter.client.impl.helpers.Parameters.DictionarySendLevel;
  38. import static org.jdiameter.client.impl.helpers.Parameters.DpaTimeOut;
  39. import static org.jdiameter.client.impl.helpers.Parameters.DwaTimeOut;
  40. import static org.jdiameter.client.impl.helpers.Parameters.IacTimeOut;
  41. import static org.jdiameter.client.impl.helpers.Parameters.KDFile;
  42. import static org.jdiameter.client.impl.helpers.Parameters.KDManager;
  43. import static org.jdiameter.client.impl.helpers.Parameters.KDPwd;
  44. import static org.jdiameter.client.impl.helpers.Parameters.KDStore;
  45. import static org.jdiameter.client.impl.helpers.Parameters.KeyData;
  46. import static org.jdiameter.client.impl.helpers.Parameters.MessageTimeOut;
  47. import static org.jdiameter.client.impl.helpers.Parameters.OwnDiameterURI;
  48. import static org.jdiameter.client.impl.helpers.Parameters.OwnFirmwareRevision;
  49. import static org.jdiameter.client.impl.helpers.Parameters.OwnIPAddress;
  50. import static org.jdiameter.client.impl.helpers.Parameters.OwnProductName;
  51. import static org.jdiameter.client.impl.helpers.Parameters.OwnRealm;
  52. import static org.jdiameter.client.impl.helpers.Parameters.OwnVendorID;
  53. import static org.jdiameter.client.impl.helpers.Parameters.PeerIp;
  54. import static org.jdiameter.client.impl.helpers.Parameters.PeerLocalPortRange;
  55. import static org.jdiameter.client.impl.helpers.Parameters.PeerName;
  56. import static org.jdiameter.client.impl.helpers.Parameters.PeerRating;
  57. import static org.jdiameter.client.impl.helpers.Parameters.PeerTable;
  58. import static org.jdiameter.client.impl.helpers.Parameters.Properties;
  59. import static org.jdiameter.client.impl.helpers.Parameters.PropertyName;
  60. import static org.jdiameter.client.impl.helpers.Parameters.PropertyValue;
  61. import static org.jdiameter.client.impl.helpers.Parameters.QueueSize;
  62. import static org.jdiameter.client.impl.helpers.Parameters.RealmEntry;
  63. import static org.jdiameter.client.impl.helpers.Parameters.RealmTable;
  64. import static org.jdiameter.client.impl.helpers.Parameters.RecTimeOut;
  65. import static org.jdiameter.client.impl.helpers.Parameters.SDEnableSessionCreation;
  66. import static org.jdiameter.client.impl.helpers.Parameters.SDName;
  67. import static org.jdiameter.client.impl.helpers.Parameters.SDProtocol;
  68. import static org.jdiameter.client.impl.helpers.Parameters.SDUseClientMode;
  69. import static org.jdiameter.client.impl.helpers.Parameters.Security;
  70. import static org.jdiameter.client.impl.helpers.Parameters.SecurityRef;
  71. import static org.jdiameter.client.impl.helpers.Parameters.Statistics;
  72. import static org.jdiameter.client.impl.helpers.Parameters.StatisticsActiveList;
  73. import static org.jdiameter.client.impl.helpers.Parameters.StatisticsEnabled;
  74. import static org.jdiameter.client.impl.helpers.Parameters.StatisticsLoggerDelay;
  75. import static org.jdiameter.client.impl.helpers.Parameters.StatisticsLoggerPause;
  76. import static org.jdiameter.client.impl.helpers.Parameters.StopTimeOut;
  77. import static org.jdiameter.client.impl.helpers.Parameters.TDFile;
  78. import static org.jdiameter.client.impl.helpers.Parameters.TDManager;
  79. import static org.jdiameter.client.impl.helpers.Parameters.TDPwd;
  80. import static org.jdiameter.client.impl.helpers.Parameters.TDStore;
  81. import static org.jdiameter.client.impl.helpers.Parameters.ThreadPool;
  82. import static org.jdiameter.client.impl.helpers.Parameters.ThreadPoolPriority;
  83. import static org.jdiameter.client.impl.helpers.Parameters.ThreadPoolSize;
  84. import static org.jdiameter.client.impl.helpers.Parameters.TrustData;
  85. import static org.jdiameter.client.impl.helpers.Parameters.UseUriAsFqdn;
  86. import static org.jdiameter.client.impl.helpers.Parameters.VendorId;
  87. import static org.jdiameter.server.impl.helpers.Parameters.RealmEntryExpTime;
  88. import static org.jdiameter.server.impl.helpers.Parameters.RealmEntryIsDynamic;
  89. import static org.jdiameter.server.impl.helpers.Parameters.RealmHosts;
  90. import static org.jdiameter.server.impl.helpers.Parameters.RealmLocalAction;
  91. import static org.jdiameter.server.impl.helpers.Parameters.RealmName;
  92. import java.io.File;
  93. import java.io.InputStream;
  94. import java.util.ArrayList;
  95. import java.util.Hashtable;
  96. import java.util.List;
  97. import javax.xml.XMLConstants;
  98. import javax.xml.parsers.DocumentBuilder;
  99. import javax.xml.parsers.DocumentBuilderFactory;
  100. import javax.xml.transform.Source;
  101. import javax.xml.transform.dom.DOMSource;
  102. import javax.xml.transform.stream.StreamSource;
  103. import javax.xml.validation.Schema;
  104. import javax.xml.validation.SchemaFactory;
  105. import javax.xml.validation.Validator;
  106. import org.jdiameter.api.Configuration;
  107. import org.jdiameter.server.impl.helpers.ExtensionPoint;
  108. import org.w3c.dom.Document;
  109. import org.w3c.dom.Element;
  110. import org.w3c.dom.NamedNodeMap;
  111. import org.w3c.dom.Node;
  112. import org.w3c.dom.NodeList;
  113. /**
  114. * This class provide loading and verification configuration for client from XML file
  115. *
  116. * @author erick.svenson@yahoo.com
  117. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  118. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  119. */
  120. public class XMLConfiguration extends EmptyConfiguration {
  121. /**
  122. * Create instance of class and load file from defined input stream
  123. *
  124. * @param in input stream
  125. * @throws Exception
  126. */
  127. public XMLConfiguration(InputStream in) throws Exception {
  128. this(in, null, null, false);
  129. }
  130. /**
  131. * Create instance of class and load file from defined input stream
  132. *
  133. * @param in input stream
  134. * @param attributes attributes for DocumentBuilderFactory
  135. * @param features features for DocumentBuilderFactory
  136. * @throws Exception
  137. */
  138. public XMLConfiguration(InputStream in, Hashtable<String, Object> attributes, Hashtable<String, Boolean> features) throws Exception {
  139. this(in, attributes, features, false);
  140. }
  141. /**
  142. * Create instance of class and load file from defined file name
  143. *
  144. * @param filename configuration file name
  145. * @throws Exception
  146. */
  147. public XMLConfiguration(String filename) throws Exception {
  148. this(filename, null, null, false);
  149. }
  150. /**
  151. * Create instance of class and load file from defined input stream
  152. *
  153. * @param filename configuration file name
  154. * @param attributes attributes for DocumentBuilderFactory
  155. * @param features features for DocumentBuilderFactory
  156. * @throws Exception
  157. */
  158. public XMLConfiguration(String filename, Hashtable<String, Object> attributes, Hashtable<String, Boolean> features) throws Exception {
  159. this(filename, attributes, features, false);
  160. }
  161. protected XMLConfiguration(Object in, Hashtable<String, Object> attributes, Hashtable<String, Boolean> features, boolean nop) throws Exception {
  162. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  163. factory.setNamespaceAware(true);
  164. if (attributes != null)
  165. for (String key : attributes.keySet())
  166. factory.setAttribute(key, attributes.get(key));
  167. if (features != null)
  168. for (String key : features.keySet())
  169. factory.setFeature(key, features.get(key));
  170. DocumentBuilder builder = factory.newDocumentBuilder();
  171. Document document;
  172. if (in instanceof InputStream) {
  173. document = builder.parse((InputStream) in);
  174. }
  175. else if (in instanceof String) {
  176. document = builder.parse(new File((String) in));
  177. }
  178. else {
  179. throw new Exception("Unknown type of input data");
  180. }
  181. validate(document);
  182. processing(document);
  183. }
  184. protected void validate(Document document) throws Exception {
  185. SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  186. Source schemaFile = new StreamSource(getClass().getResourceAsStream("/META-INF/jdiameter-client.xsd"));
  187. Schema schema = factory.newSchema(schemaFile);
  188. Validator validator = schema.newValidator();
  189. validator.validate(new DOMSource(document));
  190. }
  191. protected void processing(Document document) {
  192. Element element = document.getDocumentElement();
  193. NodeList c = element.getChildNodes();
  194. for (int i = 0; i < c.getLength(); i++) {
  195. String nodeName = c.item(i).getNodeName();
  196. if (nodeName.equals("LocalPeer")) {
  197. addLocalPeer(c.item(i));
  198. }
  199. else if (nodeName.equals("Parameters")) {
  200. addParameters(c.item(i));
  201. }
  202. else if (nodeName.equals("Network")) {
  203. addNetwork(c.item(i));
  204. }
  205. else if (nodeName.equals("Security")) {
  206. addSecurity(c.item(i));
  207. }
  208. else if (nodeName.equals("Extensions")) {
  209. addExtensions(c.item(i));
  210. }
  211. }
  212. }
  213. protected void addLocalPeer(Node node) {
  214. NodeList c = node.getChildNodes();
  215. if (node.getAttributes().getNamedItem("security_ref") != null) {
  216. add(SecurityRef, node.getAttributes().getNamedItem("security_ref").getNodeValue());
  217. }
  218. for (int i = 0; i < c.getLength(); i++) {
  219. String nodeName = c.item(i).getNodeName();
  220. if (nodeName.equals("URI")) {
  221. add(OwnDiameterURI, getValue(c.item(i)));
  222. }
  223. addIPAddress(c.item(i));
  224. if (nodeName.equals("Realm")) {
  225. add(OwnRealm, getValue(c.item(i)));
  226. }
  227. if (nodeName.equals("VendorID")) {
  228. add(OwnVendorID, getLongValue(c.item(i)));
  229. }
  230. if (nodeName.equals("ProductName")) {
  231. add(OwnProductName, getValue(c.item(i)));
  232. }
  233. if (nodeName.equals("FirmwareRevision")) {
  234. add(OwnFirmwareRevision, getLongValue(c.item(i)));
  235. }
  236. if (nodeName.equals("Applications")) {
  237. addApplications(c.item(i));
  238. }
  239. }
  240. }
  241. protected void addIPAddress(Node node) {
  242. String nodeName = node.getNodeName();
  243. if (nodeName.equals("IPAddress"))
  244. add(OwnIPAddress, getValue(node));
  245. }
  246. protected void addApplications(Node node) {
  247. NodeList c = node.getChildNodes();
  248. ArrayList<Configuration> items = new ArrayList<Configuration>();
  249. for (int i = 0; i < c.getLength(); i++) {
  250. String nodeName = c.item(i).getNodeName();
  251. if (nodeName.equals("ApplicationID")) {
  252. items.add(addApplication(c.item(i)));
  253. }
  254. }
  255. add(ApplicationId, items.toArray(EMPTY_ARRAY));
  256. }
  257. protected Configuration addApplication(Node node) {
  258. NodeList c = node.getChildNodes();
  259. AppConfiguration e = getInstance();
  260. for (int i = 0; i < c.getLength(); i++) {
  261. String nodeName = c.item(i).getNodeName();
  262. if (nodeName.equals("VendorId")) {
  263. e.add(VendorId, getLongValue(c.item(i)));
  264. }
  265. else if (nodeName.equals("AuthApplId")) {
  266. e.add(AuthApplId, getLongValue(c.item(i)));
  267. }
  268. else if (nodeName.equals("AcctApplId")) {
  269. e.add(AcctApplId, getLongValue(c.item(i)));
  270. }
  271. }
  272. return e;
  273. }
  274. protected void addParameters(Node node) {
  275. NodeList c = node.getChildNodes();
  276. for (int i = 0; i < c.getLength(); i++) {
  277. String nodeName = c.item(i).getNodeName();
  278. if (nodeName.equals("UseUriAsFqdn")) { add(UseUriAsFqdn, Boolean.valueOf(getValue(c.item(i)))); }
  279. else if (nodeName.equals("QueueSize")) { add(QueueSize, getIntValue(c.item(i))); }
  280. else if (nodeName.equals("MessageTimeOut")) { add(MessageTimeOut, getLongValue(c.item(i))); }
  281. else if (nodeName.equals("StopTimeOut")) { add(StopTimeOut, getLongValue(c.item(i))); }
  282. else if (nodeName.equals("CeaTimeOut")) { add(CeaTimeOut, getLongValue(c.item(i))); }
  283. else if (nodeName.equals("IacTimeOut")) { add(IacTimeOut, getLongValue(c.item(i))); }
  284. else if (nodeName.equals("DwaTimeOut")) { add(DwaTimeOut, getLongValue(c.item(i))); }
  285. else if (nodeName.equals("DpaTimeOut")) { add(DpaTimeOut, getLongValue(c.item(i))); }
  286. else if (nodeName.equals("RecTimeOut")) { add(RecTimeOut, getLongValue(c.item(i))); }
  287. else if (nodeName.equals("Statistics")) { addStatisticLogger(Statistics, c.item(i)); }
  288. else if (nodeName.equals("Concurrent")) { addConcurrent(Concurrent, c.item(i)); }
  289. else if (nodeName.equals("Dictionary")) { addDictionary(Dictionary, c.item(i)); }
  290. else
  291. appendOtherParameter(c.item(i));
  292. }
  293. }
  294. protected void addConcurrent(org.jdiameter.client.impl.helpers.Parameters name, Node node) {
  295. NodeList c = node.getChildNodes();
  296. List<Configuration> items = new ArrayList<Configuration>();
  297. for (int i = 0; i < c.getLength(); i++) {
  298. String nodeName = c.item(i).getNodeName();
  299. if (nodeName.equals("Entity")) addConcurrentEntity(items, c.item(i));
  300. }
  301. add(name, items.toArray(new Configuration[items.size()]));
  302. }
  303. protected void addConcurrentEntity(List<Configuration> items, Node node) {
  304. AppConfiguration cfg = getInstance();
  305. String name = node.getAttributes().getNamedItem("name").getNodeValue();
  306. cfg.add(ConcurrentEntityName, name);
  307. if (node.getAttributes().getNamedItem("description") != null) {
  308. String descr = node.getAttributes().getNamedItem("description").getNodeValue();
  309. cfg.add(ConcurrentEntityDescription, descr);
  310. }
  311. if (node.getAttributes().getNamedItem("size") != null) {
  312. String size = node.getAttributes().getNamedItem("size").getNodeValue();
  313. cfg.add(ConcurrentEntityPoolSize, Integer.parseInt(size));
  314. }
  315. items.add(cfg);
  316. }
  317. protected void addStatisticLogger(org.jdiameter.client.impl.helpers.Parameters name, Node node) {
  318. String pause = node.getAttributes().getNamedItem("pause").getNodeValue();
  319. String delay = node.getAttributes().getNamedItem("delay").getNodeValue();
  320. String enabled = node.getAttributes().getNamedItem("enabled").getNodeValue();
  321. String active_records;
  322. if (node.getAttributes().getNamedItem("active_records") != null) {
  323. active_records = node.getAttributes().getNamedItem("active_records").getNodeValue();
  324. }
  325. else {
  326. active_records = (String) StatisticsActiveList.defValue();
  327. }
  328. add(name, getInstance().add(StatisticsLoggerPause, Long.parseLong(pause))
  329. .add(StatisticsLoggerDelay, Long.parseLong(delay))
  330. .add(StatisticsEnabled, Boolean.parseBoolean(enabled))
  331. .add(StatisticsActiveList, active_records));
  332. }
  333. protected void addDictionary(org.jdiameter.client.impl.helpers.Parameters name, Node node) {
  334. AppConfiguration dicConfiguration = getInstance();
  335. Node param = node.getAttributes().getNamedItem("class");
  336. if(param != null) {
  337. String clazz = param.getNodeValue();
  338. dicConfiguration.add(DictionaryClass, clazz);
  339. }
  340. param = node.getAttributes().getNamedItem("enabled");
  341. if(param != null) {
  342. String enabled = param.getNodeValue();
  343. dicConfiguration.add(DictionaryEnabled, Boolean.valueOf(enabled));
  344. }
  345. param = node.getAttributes().getNamedItem("sendLevel");
  346. if(param != null) {
  347. String sendLevel = param.getNodeValue();
  348. dicConfiguration.add(DictionarySendLevel, sendLevel);
  349. }
  350. param = node.getAttributes().getNamedItem("receiveLevel");
  351. if(param != null) {
  352. String receiveLevel = param.getNodeValue();
  353. dicConfiguration.add(DictionaryReceiveLevel, receiveLevel);
  354. }
  355. add(name, dicConfiguration);
  356. }
  357. protected void appendOtherParameter(Node node) {
  358. }
  359. protected void addThreadPool(Node item) {
  360. AppConfiguration threadPoolConfiguration = EmptyConfiguration.getInstance();
  361. NamedNodeMap attributes = item.getAttributes();
  362. for (int index = 0; index < attributes.getLength(); index++) {
  363. Node n = attributes.item(index);
  364. int v = Integer.parseInt(n.getNodeValue());
  365. if (n.getNodeName().equals("size")) {
  366. threadPoolConfiguration.add(ThreadPoolSize, v);
  367. }
  368. else if (n.getNodeName().equals("priority")) {
  369. threadPoolConfiguration.add(ThreadPoolPriority, v);
  370. }
  371. else {
  372. //log.error("Unkonwn attribute on " + item.getNodeName() + ", attribute name: " + n.getNodeName());
  373. }
  374. }
  375. if (!threadPoolConfiguration.isAttributeExist(ThreadPoolSize.ordinal())) {
  376. threadPoolConfiguration.add(ThreadPoolSize, ThreadPoolSize.defValue());
  377. }
  378. if (!threadPoolConfiguration.isAttributeExist(ThreadPoolPriority.ordinal())) {
  379. threadPoolConfiguration.add(ThreadPoolPriority, ThreadPoolPriority.defValue());
  380. }
  381. this.add(ThreadPool, threadPoolConfiguration);
  382. }
  383. protected void addSecurity(Node node) {
  384. NodeList c = node.getChildNodes();
  385. List<Configuration> items = new ArrayList<Configuration>();
  386. for (int i = 0; i < c.getLength(); i++) {
  387. String nodeName = c.item(i).getNodeName();
  388. if (nodeName.equals("SecurityData"))
  389. items.add(addSecurityData(c.item(i)));
  390. }
  391. add(Security, items.toArray(EMPTY_ARRAY));
  392. }
  393. protected Configuration addSecurityData(Node node) {
  394. AppConfiguration sd = getInstance().add(SDName, node.getAttributes().getNamedItem("name").getNodeValue())
  395. .add(SDProtocol, node.getAttributes().getNamedItem("protocol").getNodeValue())
  396. .add(SDEnableSessionCreation, Boolean.valueOf(node.getAttributes().getNamedItem("enable_session_creation").getNodeValue()))
  397. .add(SDUseClientMode, Boolean.valueOf(node.getAttributes().getNamedItem("use_client_mode").getNodeValue()));
  398. NodeList c = node.getChildNodes();
  399. for (int i = 0; i < c.getLength(); i++) {
  400. Node cnode = c.item(i);
  401. String nodeName = cnode.getNodeName();
  402. if (nodeName.equals("CipherSuites")) {
  403. sd.add(CipherSuites, cnode.getTextContent().trim());
  404. }
  405. if (nodeName.equals("KeyData")) {
  406. sd.add(KeyData, getInstance().add(KDManager, cnode.getAttributes().getNamedItem("manager").getNodeValue())
  407. .add(KDStore, cnode.getAttributes().getNamedItem("store").getNodeValue())
  408. .add(KDFile, cnode.getAttributes().getNamedItem("file").getNodeValue())
  409. .add(KDPwd, cnode.getAttributes().getNamedItem("pwd").getNodeValue()));
  410. }
  411. if (nodeName.equals("TrustData")) {
  412. sd.add(TrustData, getInstance().add(TDManager, cnode.getAttributes().getNamedItem("manager").getNodeValue())
  413. .add(TDStore, cnode.getAttributes().getNamedItem("store").getNodeValue())
  414. .add(TDFile, cnode.getAttributes().getNamedItem("file").getNodeValue())
  415. .add(TDPwd, cnode.getAttributes().getNamedItem("pwd").getNodeValue()));
  416. }
  417. }
  418. return sd;
  419. }
  420. protected void addNetwork(Node node) {
  421. NodeList c = node.getChildNodes();
  422. for (int i = 0; i < c.getLength(); i++) {
  423. String nodeName = c.item(i).getNodeName();
  424. if (nodeName.equals("Peers")) addPeers(c.item(i));
  425. else if (nodeName.equals("Realms")) addRealms(c.item(i));
  426. }
  427. }
  428. protected void addPeers(Node node) {
  429. NodeList c = node.getChildNodes();
  430. ArrayList<Configuration> items = new ArrayList<Configuration>();
  431. for (int i = 0; i < c.getLength(); i++) {
  432. String nodeName = c.item(i).getNodeName();
  433. if (nodeName.equals("Peer"))
  434. items.add(addPeer(c.item(i)));
  435. }
  436. add(PeerTable, items.toArray(EMPTY_ARRAY));
  437. }
  438. protected void addRealms(Node node) {
  439. NodeList c = node.getChildNodes();
  440. ArrayList<Configuration> items = new ArrayList<Configuration>();
  441. for (int i = 0; i < c.getLength(); i++) {
  442. String nodeName = c.item(i).getNodeName();
  443. if (nodeName.equals("Realm"))
  444. items.add(addRealm(c.item(i)));
  445. }
  446. add(RealmTable, items.toArray(EMPTY_ARRAY));
  447. }
  448. protected Configuration addPeer(Node node) {
  449. AppConfiguration peerConfig = getInstance()
  450. .add(PeerRating, new Integer(node.getAttributes().getNamedItem("rating").getNodeValue()))
  451. .add(PeerName, node.getAttributes().getNamedItem("name").getNodeValue());
  452. if (node.getAttributes().getNamedItem("ip") != null) {
  453. peerConfig.add(PeerIp, node.getAttributes().getNamedItem("ip").getNodeValue());
  454. }
  455. if (node.getAttributes().getNamedItem("portRange") != null) {
  456. peerConfig.add(PeerLocalPortRange, node.getAttributes().getNamedItem("portRange").getNodeValue());
  457. }
  458. if (node.getAttributes().getNamedItem("security_ref") != null) {
  459. peerConfig.add(SecurityRef, node.getAttributes().getNamedItem("security_ref").getNodeValue());
  460. }
  461. return peerConfig;
  462. }
  463. protected Configuration addRealm(Node node) {
  464. AppConfiguration realmEntry = getInstance().
  465. add(ApplicationId, new Configuration[] {addApplicationID(node.getChildNodes())}).
  466. add(RealmName, getAttrValue(node, "name")).
  467. add(RealmHosts, getAttrValue(node, "peers")).
  468. add(RealmLocalAction, getAttrValue(node, "local_action")).
  469. add(RealmEntryIsDynamic, Boolean.valueOf(getAttrValue(node, "dynamic"))).
  470. add(RealmEntryExpTime, Long.valueOf(getAttrValue(node, "exp_time")));
  471. NodeList childNodes = node.getChildNodes();
  472. for (int i = 0; i < childNodes.getLength(); i++) {
  473. String nodeName = childNodes.item(i).getNodeName();
  474. if (nodeName.equals("Agent")) {
  475. realmEntry.add(Agent, addAgent(childNodes.item(i)));
  476. }
  477. }
  478. return getInstance().add(RealmEntry, realmEntry);
  479. }
  480. protected Configuration addAgent(Node node) {
  481. AppConfiguration agentConf = getInstance();
  482. NodeList agentChildren = node.getChildNodes();
  483. for(int index = 0; index < agentChildren.getLength(); index++) {
  484. Node n = agentChildren.item(index);
  485. if(n.getNodeName().equals("Properties")) {
  486. agentConf.add(Properties, getProperties(n).toArray(EMPTY_ARRAY));
  487. }
  488. }
  489. return agentConf;
  490. }
  491. protected List<Configuration> getProperties(Node node) {
  492. List<Configuration> props = new ArrayList<Configuration>();
  493. NodeList propertiesChildren = node.getChildNodes();
  494. for(int index = 0; index < propertiesChildren.getLength(); index++) {
  495. Node n = propertiesChildren.item(index);
  496. if(n.getNodeName().equals("Property")) {
  497. AppConfiguration property = getInstance();
  498. property.add(PropertyName, n.getAttributes().getNamedItem(PropertyName.name()).getNodeValue());
  499. property.add(PropertyValue, n.getAttributes().getNamedItem(PropertyValue.name()).getNodeValue());
  500. props.add(property);
  501. }
  502. }
  503. return props;
  504. }
  505. protected Configuration addApplicationID(NodeList node) {
  506. for (int i = 0; i < node.getLength(); i++) {
  507. String nodeName = node.item(i).getNodeName();
  508. if (nodeName.equals("ApplicationID"))
  509. return addApplicationID(node.item(i));
  510. }
  511. return null;
  512. }
  513. protected Configuration addApplicationID(Node node) {
  514. NodeList c = node.getChildNodes();
  515. AppConfiguration e = getInstance();
  516. for (int i = 0; i < c.getLength(); i++) {
  517. String nodeName = c.item(i).getNodeName();
  518. if (nodeName.equals("VendorId")) e.add(VendorId, getLongValue(c.item(i)));
  519. else if (nodeName.equals("AuthApplId")) e.add(AuthApplId, getLongValue(c.item(i)));
  520. else if (nodeName.equals("AcctApplId")) e.add(AcctApplId, getLongValue(c.item(i)));
  521. }
  522. return e;
  523. }
  524. protected void addExtensions(Node node) {
  525. NodeList c = node.getChildNodes();
  526. for (int i = 0; i < c.getLength(); i++) {
  527. String nodeName = c.item(i).getNodeName();
  528. if (nodeName.equals("MetaData")) { add(ExtensionPoint.InternalMetaData,getValue(c.item(i))); }
  529. else if (nodeName.equals("MessageParser")) { add(ExtensionPoint.InternalMessageParser,getValue(c.item(i))); }
  530. else if (nodeName.equals("ElementParser")) { add(ExtensionPoint.InternalElementParser,getValue(c.item(i))); }
  531. else if (nodeName.equals("RouterEngine")) { add(ExtensionPoint.InternalRouterEngine,getValue(c.item(i))); }
  532. else if (nodeName.equals("PeerController")) { add(ExtensionPoint.InternalPeerController,getValue(c.item(i))); }
  533. else if (nodeName.equals("RealmController")) { add(ExtensionPoint.InternalRealmController,getValue(c.item(i))); }
  534. else if (nodeName.equals("SessionFactory")) { add(ExtensionPoint.InternalSessionFactory,getValue(c.item(i))); }
  535. else if (nodeName.equals("TransportFactory")) { add(ExtensionPoint.InternalTransportFactory, getValue(c.item(i))); }
  536. else if (nodeName.equals("Connection")) { add(ExtensionPoint.InternalConnectionClass, getValue(c.item(i))); }
  537. else if (nodeName.equals("PeerFsmFactory")) { add(ExtensionPoint.InternalPeerFsmFactory,getValue(c.item(i))); }
  538. else if (nodeName.equals("StatisticFactory")) { add(ExtensionPoint.InternalStatisticFactory,getValue(c.item(i))); }
  539. else if (nodeName.equals("ConcurrentFactory")) { add(ExtensionPoint.InternalConcurrentFactory,getValue(c.item(i))); }
  540. else if (nodeName.equals("ConcurrentEntityFactory")) { add(ExtensionPoint.InternalConcurrentEntityFactory,getValue(c.item(i))); }
  541. else if (nodeName.equals("SessionDatasource")) { add(ExtensionPoint.InternalSessionDatasource, getValue(c.item(i))); }
  542. else if (nodeName.equals("TimerFacility")) { add(ExtensionPoint.InternalTimerFacility, getValue(c.item(i))); }
  543. //FIXME: possibly should not be in client...
  544. else if (nodeName.equals("AgentRedirect")) { add(ExtensionPoint.InternalAgentRedirect, getValue(c.item(i))); }
  545. else if (nodeName.equals("AgentConfiguration")) { add(ExtensionPoint.InternalAgentConfiguration,getValue(c.item(i))) ; }
  546. else if (nodeName.equals("StatisticProcessor")) { add(ExtensionPoint.InternalStatisticProcessor,getValue(c.item(i))) ; }
  547. else {
  548. appendOtherExtension(c.item(i));
  549. }
  550. }
  551. }
  552. private void appendOtherExtension(Node item) {
  553. // Nothing to do here, so far...
  554. }
  555. protected Long getLongValue(Node node) {
  556. return new Long(getValue(node));
  557. }
  558. protected Integer getIntValue(Node node) {
  559. return new Integer(getValue(node));
  560. }
  561. protected String getValue(Node node) {
  562. return node.getAttributes().getNamedItem("value").getNodeValue();
  563. }
  564. protected String getAttrValue(Node node, String name) {
  565. return node.getAttributes().getNamedItem(name).getNodeValue();
  566. }
  567. }