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.flume.appender;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.config.plugins.Plugin;
021    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
022    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
023    import org.apache.logging.log4j.core.util.Integers;
024    import org.apache.logging.log4j.status.StatusLogger;
025    
026    /**
027     * Agent Specification for FlumeAvroAppender.
028     */
029    @Plugin(name = "Agent", category = "Core", printObject = true)
030    public final class Agent {
031    
032        private static final String DEFAULT_HOST = "localhost";
033    
034        private static final int DEFAULT_PORT = 35853;
035    
036        private static final Logger LOGGER = StatusLogger.getLogger();
037    
038        private final String host;
039    
040        private final int port;
041    
042        private Agent(final String host, final int port) {
043            this.host = host;
044            this.port = port;
045        }
046    
047        /**
048         * Retrieve the host name.
049         * @return The name of the host.
050         */
051        public String getHost() {
052            return host;
053        }
054    
055        /**
056         * Retrieve the port number.
057         * @return The port number.
058         */
059        public int getPort() {
060            return port;
061        }
062    
063        @Override
064        public String toString() {
065            return "host=" + host + " port=" + port;
066        }
067    
068        /**
069         * Create an Agent.
070         * @param host The host name.
071         * @param port The port number.
072         * @return The Agent.
073         */
074        @PluginFactory
075        public static Agent createAgent(@PluginAttribute("host") String host,
076                @PluginAttribute("port") final String port) {
077            if (host == null) {
078                host = DEFAULT_HOST;
079            }
080    
081            int portNum;
082            try {
083                portNum = Integers.parseInt(port, DEFAULT_PORT);
084            } catch (final Exception ex) {
085                LOGGER.error("Error parsing port number " + port, ex);
086                return null;
087            }
088            return new Agent(host, portNum);
089        }
090    }