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