001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.net;
056:
057: import java.io.IOException;
058: import java.io.ObjectOutputStream;
059: import java.net.InetAddress;
060: import java.net.Socket;
061: import org.apache.log.LogEvent;
062: import org.apache.log.output.AbstractOutputTarget;
063:
064: /**
065: * SocketOutputTarget
066: *
067: * Useful for writing the output to a TCP/IP client socket.
068: *
069: * @author <a href="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
070: */
071: public class SocketOutputTarget extends AbstractOutputTarget {
072:
073: /** Socket to communicate with the server */
074: private Socket m_socket;
075:
076: /** Output strem to write the log */
077: private ObjectOutputStream m_outputStream;
078:
079: /**
080: * Creates output target with the end point specified by the address and port
081: *
082: * @param address end point address
083: * @param port the end point port
084: * @exception IOException if an I/O error ocurrs when creating socket
085: */
086: public SocketOutputTarget(final InetAddress address, final int port)
087: throws IOException {
088: m_socket = new Socket(address, port);
089: m_outputStream = new ObjectOutputStream(m_socket
090: .getOutputStream());
091: super .open();
092: }
093:
094: /**
095: * Creates the output target with the end point specified by host and port
096: *
097: * @param host end point host
098: * @param port the end point port
099: * @exception IOException if an I/O error ocurrs when creating socket
100: */
101: public SocketOutputTarget(final String host, final int port)
102: throws IOException {
103: m_socket = new Socket(host, port);
104: m_outputStream = new ObjectOutputStream(m_socket
105: .getOutputStream());
106: super .open();
107: }
108:
109: /**
110: * Writes the output as a LogEvent without formatting.
111: * Formatting ia applied on the server side where it is log.
112: *
113: * @param event the LogEvent
114: */
115: protected void write(LogEvent event) {
116: try {
117: m_outputStream.writeObject(event);
118: } catch (final IOException ioex) {
119: getErrorHandler().error("Error writting to socket", ioex,
120: null);
121: }
122: }
123:
124: /**
125: * To process the LogEvent
126: *
127: * @param event the LogEvent
128: */
129: protected void doProcessEvent(LogEvent event) {
130: write(event);
131: }
132:
133: /**
134: * Shutdown target.
135: * Attempting to write to target after close() will cause errors to be logged.
136: */
137: public synchronized void close() {
138: super.close();
139: m_socket = null;
140: }
141: }
|