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 org.apache.logging.log4j.core.appender.ManagerFactory;
020    
021    import java.io.OutputStream;
022    
023    /**
024     * Socket Manager for UDP connections.
025     */
026    public class DatagramSocketManager extends AbstractSocketManager {
027    
028        private static final DatagramSocketManagerFactory factory = new DatagramSocketManagerFactory();
029    
030        /**
031         * The Constructor.
032         * @param os The OutputStream.
033         * @param name The unique name of the connection.
034         * @param host The host to connect to.
035         * @param port The port on the host.
036         */
037        protected DatagramSocketManager(OutputStream os, String name, String host, int port) {
038            super(name, os, null, host, port);
039        }
040    
041        /**
042         * Obtain a SocketManager.
043         * @param host The host to connect to.
044         * @param port The port on the host.
045         * @return A DatagramSocketManager.
046         */
047        public static DatagramSocketManager getSocketManager(String host, int port) {
048            if (host == null || host.length() == 0) {
049                throw new IllegalArgumentException("A host name is required");
050            }
051            if (port <= 0) {
052                throw new IllegalArgumentException("A port value is required");
053            }
054            return (DatagramSocketManager) getManager("UDP:" + host + ":" + port, new FactoryData(host, port), factory
055            );
056        }
057    
058        /**
059         * Data for the factory.
060         */
061        private static class FactoryData {
062            private String host;
063            private int port;
064    
065            public FactoryData(String host, int port) {
066                this.host = host;
067                this.port = port;
068            }
069        }
070    
071        /**
072         * Factory to create the DatagramSocketManager.
073         */
074        private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
075    
076            public DatagramSocketManager createManager(String name, FactoryData data) {
077                OutputStream os = new DatagramOutputStream(data.host, data.port);
078                return new DatagramSocketManager(os, name, data.host, data.port);
079            }
080        }
081    }