KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > net > DatagramStringWriter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.net;
18
19 import java.io.Writer JavaDoc;
20 import java.net.DatagramSocket JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.DatagramPacket JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.net.SocketException JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.log4j.helpers.LogLog;
28
29 /**
30  * DatagramStringWriter is a wrapper around the java.net.DatagramSocket class
31  * so that it behaves like a java.io.Writer.
32  */

33 public class DatagramStringWriter extends Writer JavaDoc {
34
35   static final int SYSLOG_PORT = 514;
36
37   private int port;
38   private String JavaDoc host;
39   private String JavaDoc encoding;
40   private String JavaDoc prefix;
41
42   private InetAddress JavaDoc address;
43   private DatagramSocket JavaDoc ds;
44
45   /**
46    * This constructor assumes that it is sending to a remote syslog daemon
47    * on the normal syslog port (514), and uses the default platform character
48    * encoding when converting the message string to a byte sequence.
49    */

50   public
51   DatagramStringWriter(String JavaDoc host) {
52     this(host, SYSLOG_PORT, null, null);
53   }
54
55   /**
56    * This constructor sends messages to the specified host and port, and
57    * uses the default platform character encoding when converting the message
58    * string to a byte sequence.
59    */

60   public
61   DatagramStringWriter(String JavaDoc host, int port) {
62     this(host, port, null, null);
63   }
64
65   /**
66    * This constructor sends messages to the specified host and port, and
67    * uses the specified character encoding when converting the message
68    * string to a byte sequence.
69    */

70   public
71   DatagramStringWriter(String JavaDoc host, int port, String JavaDoc encoding) {
72     this(host, port, null, null);
73   }
74   /**
75    * This constructor sends messages to the specified host and port, and
76    * uses the specified character encoding when converting the message
77    * string to a byte sequence; the specified prefix (which may be null)
78    * is prepended to each message.
79    */

80   public
81   DatagramStringWriter(String JavaDoc host, int port, String JavaDoc encoding, String JavaDoc prefix) {
82     this.host = host;
83     this.port = port;
84     this.encoding = encoding;
85     this.prefix = prefix;
86
87     try {
88       this.address = InetAddress.getByName(host);
89     }
90     catch (UnknownHostException JavaDoc e) {
91       LogLog.error("Could not find " + host +
92              ". All logging will FAIL.", e);
93     }
94
95     try {
96       this.ds = new DatagramSocket JavaDoc();
97     }
98     catch (SocketException JavaDoc e) {
99       e.printStackTrace();
100       LogLog.error("Could not instantiate DatagramSocket to " + host +
101              ". All logging will FAIL.", e);
102     }
103   }
104
105
106   public
107   void write(char[] buf, int off, int len) throws IOException JavaDoc {
108     this.write(new String JavaDoc(buf, off, len));
109   }
110
111   public
112   void write(String JavaDoc string) throws IOException JavaDoc {
113     if (prefix != null) {
114       string = prefix + string;
115     }
116     
117     byte[] rawData;
118     if (this.encoding == null)
119     {
120       // convert to byte sequence using platform's default encoding
121
rawData = string.getBytes();
122     }
123     else
124     {
125       // convert to specified encoding - which may be sequence of
126
// 8-bit chars, or multi-byte encodings like UTF-8 or UTF-16.
127
// The receiving end had better be expecting whatever encoding
128
// is used here on the sending end!
129
rawData = string.getBytes(encoding);
130     }
131
132     DatagramPacket JavaDoc packet =
133       new DatagramPacket JavaDoc(
134                  rawData,
135                            rawData.length,
136                            address,
137                  port);
138
139     if(this.ds != null)
140     {
141       ds.send(packet);
142     }
143     else
144     {
145       LogLog.error(
146         "write: failed to create DatagramPacket");
147     }
148   }
149
150   public
151   void flush() {}
152
153   public
154   void close() {}
155
156   /**
157    * Set a string to be prefixed to every message sent by this Writer.
158    * For example, this method could be used to prepend a syslog
159    * facility/priority code on the front of each message.
160    * <p>
161    * Note that this method is not synchronised, so should not be called in
162    * a situation where other threads may be logging messages at the same
163    * moment.
164    * <p>
165    * @param prefix may be a prefix string, or null which indicates no
166    * prefix should be added.
167    */

168   public
169   void setPrefix(String JavaDoc prefix){
170     this.prefix = prefix;
171   }
172 }
173
Popular Tags