001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020
021import java.io.Writer;
022import java.net.DatagramSocket;
023import java.net.InetAddress;
024import java.net.DatagramPacket;
025import java.net.UnknownHostException;
026import java.net.SocketException;
027import java.io.IOException;
028import java.net.URL;
029import java.net.MalformedURLException;
030
031/**
032   SyslogWriter is a wrapper around the java.net.DatagramSocket class
033   so that it behaves like a java.io.Writer.
034
035   @since 0.7.3
036*/
037public class SyslogWriter extends Writer {
038
039  final int SYSLOG_PORT = 514;
040  /**
041   *  Host string from last constructed SyslogWriter.
042   *  @deprecated
043   */
044  static String syslogHost;
045  
046  private InetAddress address;
047  private final int port;
048  private DatagramSocket ds;
049
050  /**
051   *  Constructs a new instance of SyslogWriter.
052   *  @param syslogHost host name, may not be null.  A port
053   *  may be specified by following the name or IPv4 literal address with
054   *  a colon and a decimal port number.  To specify a port with an IPv6
055   *  address, enclose the IPv6 address in square brackets before appending
056   *  the colon and decimal port number.
057   */
058  public
059  SyslogWriter(final String syslogHost) {
060    SyslogWriter.syslogHost = syslogHost;
061    if (syslogHost == null) {
062        throw new NullPointerException("syslogHost");
063    }
064    
065    String host = syslogHost;
066    int urlPort = -1;
067    
068    //
069    //  If not an unbracketed IPv6 address then
070    //      parse as a URL
071    //
072    if (host.indexOf("[") != -1 || host.indexOf(':') == host.lastIndexOf(':')) {
073        try {
074            URL url = new URL("http://" + host);
075            if (url.getHost() != null) {
076                host = url.getHost();
077                //   if host is a IPv6 literal, strip off the brackets
078                if(host.startsWith("[") && host.charAt(host.length() - 1) == ']') {
079                    host = host.substring(1, host.length() - 1);
080                }
081                urlPort = url.getPort();
082            }
083        } catch(MalformedURLException e) {
084                LogLog.error("Malformed URL: will attempt to interpret as InetAddress.", e);
085        }
086    }
087    
088    if (urlPort == -1) {
089        urlPort = SYSLOG_PORT;
090    }
091    port = urlPort;
092
093    try {      
094      this.address = InetAddress.getByName(host);
095    }
096    catch (UnknownHostException e) {
097      LogLog.error("Could not find " + host +
098                         ". All logging will FAIL.", e);
099    }
100
101    try {
102      this.ds = new DatagramSocket();
103    }
104    catch (SocketException e) {
105      e.printStackTrace();
106      LogLog.error("Could not instantiate DatagramSocket to " + host +
107                         ". All logging will FAIL.", e);
108    }
109    
110  }
111
112
113  public
114  void write(char[] buf, int off, int len) throws IOException {
115    this.write(new String(buf, off, len));
116  }
117  
118  public
119  void write(final String string) throws IOException {
120
121    if(this.ds != null && this.address != null) {
122        byte[] bytes = string.getBytes();
123        //
124        //  syslog packets must be less than 1024 bytes
125        //
126        int bytesLength = bytes.length;
127        if (bytesLength >= 1024) {
128            bytesLength = 1024;
129        }
130        DatagramPacket packet = new DatagramPacket(bytes, bytesLength,
131                               address, port);
132        ds.send(packet);
133    }
134    
135  }
136
137  public
138  void flush() {}
139
140  public void close() {
141      if (ds != null) {
142          ds.close();
143      }
144  }
145}