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 */ 017package org.apache.logging.log4j.core.net; 018 019import java.io.OutputStream; 020import java.io.Serializable; 021import java.net.InetAddress; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.logging.log4j.core.Layout; 026import org.apache.logging.log4j.core.appender.OutputStreamManager; 027 028/** 029 * Abstract base class for managing sockets. 030 */ 031public abstract class AbstractSocketManager extends OutputStreamManager { 032 033 /** 034 * The Internet address of the host. 035 */ 036 protected final InetAddress inetAddress; 037 038 /** 039 * The name of the host. 040 */ 041 protected final String host; 042 043 /** 044 * The port on the host. 045 */ 046 protected final int port; 047 048 /** 049 * The Constructor. 050 * @param name The unique name of this connection. 051 * @param os The OutputStream to manage. 052 * @param inetAddress The Internet address. 053 * @param host The target host name. 054 * @param port The target port number. 055 * @param bufferSize The buffer size. 056 */ 057 public AbstractSocketManager(final String name, final OutputStream os, final InetAddress inetAddress, 058 final String host, final int port, final Layout<? extends Serializable> layout, final boolean writeHeader, 059 final int bufferSize) { 060 super(os, name, layout, writeHeader, bufferSize); 061 this.inetAddress = inetAddress; 062 this.host = host; 063 this.port = port; 064 } 065 066 /** 067 * Gets this AbstractSocketManager's content format. Specified by: 068 * <ul> 069 * <li>Key: "port" Value: provided "port" param</li> 070 * <li>Key: "address" Value: provided "address" param</li> 071 * </ul> 072 * 073 * @return Map of content format keys supporting AbstractSocketManager 074 */ 075 @Override 076 public Map<String, String> getContentFormat() { 077 final Map<String, String> result = new HashMap<>(super.getContentFormat()); 078 result.put("port", Integer.toString(port)); 079 result.put("address", inetAddress.getHostAddress()); 080 return result; 081 } 082}