/org.xerela.net/src/org/xerela/net/ping/icmp/PingConfigLoader.java

http://xerela.googlecode.com/ · Java · 159 lines · 101 code · 13 blank · 45 comment · 9 complexity · df8d83928b6a80dbc030ae952506d9cc MD5 · raw file

  1. /*
  2. * The contents of this file are subject to the Mozilla Public License
  3. * Version 1.1 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * The Original Code is Ziptie Client Framework.
  13. *
  14. * The Initial Developer of the Original Code is AlterPoint.
  15. * Portions created by AlterPoint are Copyright (C) 2007,
  16. * AlterPoint, Inc. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. */
  20. package org.xerela.net.ping.icmp;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.net.URL;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Element;
  27. import org.w3c.dom.NodeList;
  28. import org.xml.sax.SAXException;
  29. import org.xerela.common.DomReaderElf;
  30. import org.xerela.common.OsTypes;
  31. /**
  32. * Loads up the <code>PingConfig</code> from an XML file on the system.
  33. *
  34. * @author rkruse
  35. */
  36. @SuppressWarnings("nls")
  37. public class PingConfigLoader
  38. {
  39. private static final String MAC = "Mac";
  40. private static final String LINUX = "Linux";
  41. private static final String WINDOWS = "Windows";
  42. private static final String PING_CONFIG_FILE = "pingConfig.xml";
  43. private PingConfig pingConfig;
  44. /**
  45. * Loads up a PingConfig from the default XML file
  46. *
  47. * @throws PingException if the XML is malformed
  48. */
  49. public PingConfigLoader() throws PingException
  50. {
  51. pingConfig = new PingConfig();
  52. loadPingXmlConfig(PING_CONFIG_FILE);
  53. }
  54. /**
  55. * Initializes the pingConfig
  56. *
  57. * @param configFilename
  58. * @throws PingException
  59. */
  60. private void loadPingXmlConfig(String configFilename) throws PingException
  61. {
  62. InputStream input = null;
  63. try
  64. {
  65. URL url = PingConfigLoader.class.getResource("/nil/" + configFilename);
  66. if (url == null)
  67. {
  68. throw new RuntimeException("Could not find file in classpath. Filename: " + configFilename);
  69. }
  70. NodeList nodeList;
  71. input = url.openStream();
  72. Document doc = DomReaderElf.xmlReaderToDocument(new InputStreamReader(input));
  73. Element docElement = doc.getDocumentElement();
  74. String os = System.getProperty("os.name");
  75. if (os.startsWith(WINDOWS))
  76. {
  77. os = WINDOWS;
  78. pingConfig.setOs(OsTypes.Windows);
  79. }
  80. else if (os.startsWith(LINUX))
  81. {
  82. os = LINUX;
  83. pingConfig.setOs(OsTypes.Linux);
  84. }
  85. else if (os.startsWith(MAC))
  86. {
  87. os = MAC;
  88. pingConfig.setOs(OsTypes.Mac);
  89. }
  90. else
  91. {
  92. os = "Solaris";
  93. pingConfig.setOs(OsTypes.Solaris);
  94. }
  95. nodeList = docElement.getElementsByTagName(os);
  96. getPingAttributes((Element) nodeList.item(0));
  97. }
  98. catch (IOException i)
  99. {
  100. throw new PingException(i);
  101. }
  102. catch (SAXException s)
  103. {
  104. throw new PingException(s);
  105. }
  106. finally
  107. {
  108. if (input != null)
  109. {
  110. try
  111. {
  112. input.close();
  113. }
  114. catch (IOException e)
  115. {
  116. // eat
  117. return;
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * Given one of the OS specific elements, parse out the specifics and put them in the <code>PingConfig</code>
  124. *
  125. * @param element
  126. */
  127. private void getPingAttributes(Element element)
  128. {
  129. String pingCommand = element.getAttribute("command");
  130. String count = element.getAttribute("countFlag");
  131. String timeout = element.getAttribute("timeoutFlag");
  132. String size = element.getAttribute("sizeFlag");
  133. pingConfig.setCommand(pingCommand);
  134. pingConfig.setCountFlag(count);
  135. pingConfig.setTimeoutFlag(timeout);
  136. pingConfig.setSizeFlag(size);
  137. }
  138. /**
  139. *
  140. * @return the {@link PingConfig}
  141. */
  142. public PingConfig getPingConfig()
  143. {
  144. return pingConfig;
  145. }
  146. }