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.net.InetAddress;
020    import java.net.UnknownHostException;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.logging.log4j.core.Layout;
025    import org.apache.logging.log4j.core.appender.ManagerFactory;
026    
027    import java.io.OutputStream;
028    
029    /**
030     * Socket Manager for UDP connections.
031     */
032    public class DatagramSocketManager extends AbstractSocketManager {
033    
034        private static final DatagramSocketManagerFactory FACTORY = new DatagramSocketManagerFactory();
035    
036        /**
037         * The Constructor.
038         * @param name The unique name of the connection.
039         * @param os The OutputStream.
040         * @param address
041         * @param host The host to connect to.
042         * @param port The port on the host.
043         * @param layout The layout
044         */
045        protected DatagramSocketManager(final String name, final OutputStream os, InetAddress address, final String host,
046                                        final int port, final Layout layout) {
047            super(name, os, address, host, port, layout);
048        }
049    
050        /**
051         * Obtain a SocketManager.
052         * @param host The host to connect to.
053         * @param port The port on the host.
054         * @param layout The layout.
055         * @return A DatagramSocketManager.
056         */
057        public static DatagramSocketManager getSocketManager(final String host, final int port, final Layout layout) {
058            if (host == null || host.length() == 0) {
059                throw new IllegalArgumentException("A host name is required");
060            }
061            if (port <= 0) {
062                throw new IllegalArgumentException("A port value is required");
063            }
064            return (DatagramSocketManager) getManager("UDP:" + host + ":" + port, new FactoryData(host, port, layout),
065                FACTORY);
066        }
067    
068        /**
069         * DatagramSocketManager's content format is specified by:<p/>
070         * Key: "protocol" Value: "udp"<p/>
071         * Key: "direction" Value: "out"
072         * @return Map of content format keys supporting DatagramSocketManager
073         */
074        @Override
075        public Map<String, String> getContentFormat()
076        {
077            Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
078            result.put("protocol", "udp");
079            result.put("direction", "out");
080    
081            return result;
082        }
083    
084        /**
085         * Data for the factory.
086         */
087        private static class FactoryData {
088            private final String host;
089            private final int port;
090            private final Layout layout;
091    
092            public FactoryData(final String host, final int port, final Layout layout) {
093                this.host = host;
094                this.port = port;
095                this.layout = layout;
096            }
097        }
098    
099        /**
100         * Factory to create the DatagramSocketManager.
101         */
102        private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
103    
104            @Override
105            public DatagramSocketManager createManager(final String name, final FactoryData data) {
106                InetAddress address;
107                final OutputStream os = new DatagramOutputStream(data.host, data.port, data.layout.getHeader(),
108                    data.layout.getFooter());
109                try {
110                    address = InetAddress.getByName(data.host);
111                } catch (final UnknownHostException ex) {
112                    LOGGER.error("Could not find address of " + data.host, ex);
113                    return null;
114                }
115                return new DatagramSocketManager(name, os, address, data.host, data.port, data.layout);
116            }
117        }
118    }