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    package org.apache.logging.log4j.core.net;
018    
019    import java.io.OutputStream;
020    import java.io.Serializable;
021    import java.net.InetAddress;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.apache.logging.log4j.core.Layout;
026    import org.apache.logging.log4j.core.appender.OutputStreamManager;
027    
028    /**
029     * Abstract base class for managing sockets.
030     */
031    public 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         */
056        public AbstractSocketManager(final String name, final OutputStream os, final InetAddress inetAddress, final String host,
057                                     final int port, final Layout<? extends Serializable> layout) {
058            super(os, name, layout);
059            this.inetAddress = inetAddress;
060            this.host = host;
061            this.port = port;
062        }
063    
064        /**
065         * Gets this AbstractSocketManager's content format. Specified by:
066         * <ul>
067         * <li>Key: "port" Value: provided "port" param</li>
068         * <li>Key: "address" Value: provided "address" param</li>
069         * </ul>
070         * 
071         * @return Map of content format keys supporting AbstractSocketManager
072         */
073        @Override
074        public Map<String, String> getContentFormat() {
075            final Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
076            result.put("port", Integer.toString(port));
077            result.put("address", inetAddress.getHostAddress());
078            return result;
079        }
080    }