KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ObjectOutputStream JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.Socket JavaDoc;
23 import org.apache.log.LogEvent;
24 import org.apache.log.output.AbstractOutputTarget;
25
26 /**
27  * SocketOutputTarget
28  *
29  * Useful for writing the output to a TCP/IP client socket.
30  *
31  * @author <a HREF="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
32  */

33 public class SocketOutputTarget extends AbstractOutputTarget
34 {
35
36     /** Socket to communicate with the server */
37     private Socket JavaDoc m_socket;
38
39     /** Output strem to write the log */
40     private ObjectOutputStream JavaDoc m_outputStream;
41
42     /**
43      * Creates output target with the end point specified by the address and port
44      *
45      * @param address end point address
46      * @param port the end point port
47      * @exception IOException if an I/O error ocurrs when creating socket
48      */

49     public SocketOutputTarget( final InetAddress JavaDoc address,
50                                final int port )
51         throws IOException JavaDoc
52     {
53         m_socket = new Socket JavaDoc( address, port );
54         m_outputStream = new ObjectOutputStream JavaDoc( m_socket.getOutputStream() );
55         super.open();
56     }
57
58     /**
59      * Creates the output target with the end point specified by host and port
60      *
61      * @param host end point host
62      * @param port the end point port
63      * @exception IOException if an I/O error ocurrs when creating socket
64      */

65     public SocketOutputTarget( final String JavaDoc host,
66                                final int port )
67         throws IOException JavaDoc
68     {
69         m_socket = new Socket JavaDoc( host, port );
70         m_outputStream = new ObjectOutputStream JavaDoc( m_socket.getOutputStream() );
71         super.open();
72     }
73
74     /**
75      * Writes the output as a LogEvent without formatting.
76      * Formatting ia applied on the server side where it is log.
77      *
78      * @param event the LogEvent
79      */

80     protected void write( LogEvent event )
81     {
82         try
83         {
84             m_outputStream.writeObject( event );
85         }
86         catch( final IOException JavaDoc ioex )
87         {
88             getErrorHandler().error( "Error writting to socket", ioex, null );
89         }
90     }
91
92     /**
93      * To process the LogEvent
94      *
95      * @param event the LogEvent
96      */

97     protected void doProcessEvent( LogEvent event )
98     {
99         write( event );
100     }
101
102     /**
103      * Shutdown target.
104      * Attempting to write to target after close() will cause errors to be logged.
105      */

106     public synchronized void close()
107     {
108         super.close();
109         m_socket = null;
110     }
111 }
112
Popular Tags