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 org.apache.logging.log4j.core.Layout;
020import org.apache.logging.log4j.core.appender.OutputStreamManager;
021
022import java.io.OutputStream;
023import java.io.Serializable;
024import java.net.InetAddress;
025import java.util.HashMap;
026import java.util.Map;
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 address;
037    /**
038     * The name of the host.
039     */
040    protected final String host;
041    /**
042     * The port on the host.
043     */
044    protected final int port;
045
046    /**
047     * The Constructor.
048     * @param name The unique name of this connection.
049     * @param os The OutputStream to manage.
050     * @param addr The internet address.
051     * @param host The target host name.
052     * @param port The target port number.
053     */
054    public AbstractSocketManager(final String name, final OutputStream os, final InetAddress addr, final String host,
055                                 final int port, final Layout<? extends Serializable> layout) {
056        super(os, name, layout);
057        this.address = addr;
058        this.host = host;
059        this.port = port;
060    }
061
062    /**
063     * AbstractSocketManager's content format is specified by:<p/>
064     * Key: "port" Value: provided "port" param<p/>
065     * Key: "address" Value: provided "address" param
066     * @return Map of content format keys supporting AbstractSocketManager
067     */
068    @Override
069    public Map<String, String> getContentFormat()
070    {
071        final Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
072        result.put("port", Integer.toString(port));
073        result.put("address", address.getHostAddress());
074
075        return result;
076    }
077}