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