SocketHandler.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* SocketHandler.java -- a class for publishing log messages to network sockets
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.logging;
  32. /**
  33. * A <code>SocketHandler</code> publishes log records to
  34. * a TCP/IP socket.
  35. *
  36. * <p><strong>Configuration:</strong> Values of the subsequent
  37. * <code>LogManager</code> properties are taken into consideration
  38. * when a <code>SocketHandler</code> is initialized.
  39. * If a property is not defined, or if it has an invalid
  40. * value, a default is taken without an exception being thrown.
  41. *
  42. * <ul>
  43. *
  44. * <li><code>java.util.SocketHandler.level</code> - specifies
  45. * the initial severity level threshold. Default value:
  46. * <code>Level.ALL</code>.</li>
  47. *
  48. * <li><code>java.util.SocketHandler.filter</code> - specifies
  49. * the name of a Filter class. Default value: No Filter.</li>
  50. *
  51. * <li><code>java.util.SocketHandler.formatter</code> - specifies
  52. * the name of a Formatter class. Default value:
  53. * <code>java.util.logging.XMLFormatter</code>.</li>
  54. *
  55. * <li><code>java.util.SocketHandler.encoding</code> - specifies
  56. * the name of the character encoding. Default value:
  57. * the default platform encoding.</li>
  58. *
  59. * <li><code>java.util.SocketHandler.host</code> - specifies
  60. * the name of the host to which records are published.
  61. * There is no default value for this property; if it is
  62. * not set, the SocketHandler constructor will throw
  63. * an exception.</li>
  64. *
  65. * <li><code>java.util.SocketHandler.port</code> - specifies
  66. * the TCP/IP port to which records are published.
  67. * There is no default value for this property; if it is
  68. * not set, the SocketHandler constructor will throw
  69. * an exception.</li>
  70. *
  71. * </ul>
  72. *
  73. * @author Sascha Brawer (brawer@acm.org)
  74. */
  75. public class SocketHandler
  76. extends StreamHandler
  77. {
  78. /**
  79. * Constructs a <code>SocketHandler</code> that publishes log
  80. * records to a TCP/IP socket. Tthe initial configuration is
  81. * determined by the <code>LogManager</code> properties described
  82. * above.
  83. *
  84. * @throws java.io.IOException if the connection to the specified
  85. * network host and port cannot be established.
  86. *
  87. * @throws java.lang.IllegalArgumentException if either the
  88. * <code>java.util.logging.SocketHandler.host</code>
  89. * or <code>java.util.logging.SocketHandler.port</code>
  90. * LogManager properties is not defined, or specifies
  91. * an invalid value.
  92. */
  93. public SocketHandler()
  94. throws java.io.IOException
  95. {
  96. this(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.host"),
  97. getPortNumber());
  98. }
  99. /**
  100. * Constructs a <code>SocketHandler</code> that publishes log
  101. * records to a TCP/IP socket. With the exception of the internet
  102. * host and port, the initial configuration is determined by the
  103. * <code>LogManager</code> properties described above.
  104. *
  105. * @param host the Internet host to which log records will be
  106. * forwarded.
  107. *
  108. * @param port the port at the host which will accept a request
  109. * for a TCP/IP connection.
  110. *
  111. * @throws java.io.IOException if the connection to the specified
  112. * network host and port cannot be established.
  113. *
  114. * @throws java.lang.IllegalArgumentException if either
  115. * <code>host</code> or <code>port</code> specify
  116. * an invalid value.
  117. */
  118. public SocketHandler(String host, int port)
  119. throws java.io.IOException
  120. {
  121. super(createSocket(host, port),
  122. "java.util.logging.SocketHandler",
  123. /* default level */ Level.ALL,
  124. /* formatter */ null,
  125. /* default formatter */ XMLFormatter.class);
  126. }
  127. /**
  128. * Retrieves the port number from the java.util.logging.SocketHandler.port
  129. * LogManager property.
  130. *
  131. * @throws IllegalArgumentException if the property is not defined or
  132. * does not specify an integer value.
  133. */
  134. private static int getPortNumber()
  135. {
  136. try {
  137. return Integer.parseInt(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.port"));
  138. } catch (Exception ex) {
  139. throw new IllegalArgumentException();
  140. }
  141. }
  142. /**
  143. * Creates an OutputStream for publishing log records to an Internet
  144. * host and port. This private method is a helper for use by the
  145. * constructor of SocketHandler.
  146. *
  147. * @param host the Internet host to which log records will be
  148. * forwarded.
  149. *
  150. * @param port the port at the host which will accept a request
  151. * for a TCP/IP connection.
  152. *
  153. * @throws java.io.IOException if the connection to the specified
  154. * network host and port cannot be established.
  155. *
  156. * @throws java.lang.IllegalArgumentException if either
  157. * <code>host</code> or <code>port</code> specify
  158. * an invalid value.
  159. */
  160. private static java.io.OutputStream createSocket(String host, int port)
  161. throws java.io.IOException, java.lang.IllegalArgumentException
  162. {
  163. java.net.Socket socket;
  164. if ((host == null) || (port < 1))
  165. throw new IllegalArgumentException();
  166. socket = new java.net.Socket(host, port);
  167. socket.shutdownInput();
  168. /* The architecture of the logging framework provides replaceable
  169. * formatters. Because these formatters perform their task by
  170. * returning one single String for each LogRecord to be formatted,
  171. * there is no need to buffer.
  172. */
  173. socket.setTcpNoDelay(true);
  174. return socket.getOutputStream();
  175. }
  176. /**
  177. * Publishes a <code>LogRecord</code> to the network socket,
  178. * provided the record passes all tests for being loggable.
  179. * In addition, all data that may have been buffered will
  180. * be forced to the network stream.
  181. *
  182. * <p>Most applications do not need to call this method directly.
  183. * Instead, they will use a {@link Logger} instance, which will
  184. * create LogRecords and distribute them to registered handlers.
  185. *
  186. * <p>In case of an I/O failure, the <code>ErrorManager</code>
  187. * of this <code>SocketHandler</code> will be informed, but the caller
  188. * of this method will not receive an exception.
  189. *
  190. * @param record the log event to be published.
  191. */
  192. public void publish(LogRecord record)
  193. {
  194. super.publish(record);
  195. flush();
  196. }
  197. }