KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > net > DatagramOutputTarget


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

17 package org.apache.log.output.net;
18
19 import java.io.IOException JavaDoc;
20 import java.net.DatagramPacket JavaDoc;
21 import java.net.DatagramSocket JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import org.apache.log.format.Formatter;
24 import org.apache.log.output.AbstractOutputTarget;
25
26 /**
27  * A datagram output target.
28  * Useful for writing using custom protocols or writing to syslog daemons.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @author Peter Donald
32  */

33 public class DatagramOutputTarget
34     extends AbstractOutputTarget
35 {
36     ///Default encoding of datagram
37
private static final String JavaDoc DEFAULT_ENCODING = "US-ASCII";
38
39     ///Socket on which to send datagrams
40
private DatagramSocket JavaDoc m_socket;
41
42     ///The encoding to use when creating byte array from string
43
private String JavaDoc m_encoding;
44
45     /**
46      * Create a output target with end point specified by address and port.
47      *
48      * @param address the address endpoint
49      * @param port the address port
50      * @param formatter the message formatter
51      * @param encoding the encoding to use when encoding string
52      * @exception IOException if an error occurs
53      */

54     public DatagramOutputTarget( final InetAddress JavaDoc address,
55                                  final int port,
56                                  final Formatter formatter,
57                                  final String JavaDoc encoding )
58         throws IOException JavaDoc
59     {
60         super( formatter );
61         m_socket = new DatagramSocket JavaDoc();
62         m_socket.connect( address, port );
63         m_encoding = encoding;
64         open();
65     }
66
67     /**
68      * Create a output target with end point specified by address and port.
69      *
70      * @param address the address endpoint
71      * @param port the address port
72      * @param formatter the message formatter
73      * @exception IOException if an error occurs
74      */

75     public DatagramOutputTarget( final InetAddress JavaDoc address,
76                                  final int port,
77                                  final Formatter formatter )
78         throws IOException JavaDoc
79     {
80         this( address, port, formatter, DEFAULT_ENCODING );
81     }
82
83     /**
84      * Create a output target with end point specified by address and port.
85      *
86      * @param address the address endpoint
87      * @param port the address port
88      * @exception IOException if an error occurs
89      */

90     public DatagramOutputTarget( final InetAddress JavaDoc address, final int port )
91         throws IOException JavaDoc
92     {
93         this( address, port, null );
94     }
95
96     /**
97      * Method to write output to datagram.
98      *
99      * @param stringData the data to be output
100      */

101     protected void write( final String JavaDoc stringData )
102     {
103
104         try
105         {
106             final byte[] data = stringData.getBytes( m_encoding );
107             final DatagramPacket JavaDoc packet = new DatagramPacket JavaDoc( data, data.length );
108             m_socket.send( packet );
109         }
110         catch( final IOException JavaDoc ioe )
111         {
112             getErrorHandler().error( "Error sending datagram.", ioe, null );
113         }
114     }
115
116     /**
117      * Shutdown target.
118      * Attempting to write to target after close() will cause errors to be logged.
119      */

120     public synchronized void close()
121     {
122         super.close();
123         m_socket = null;
124     }
125 }
126
Popular Tags