1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net;
18
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.apache.logging.log4j.core.appender.ManagerFactory;
24
25 import java.io.OutputStream;
26
27
28
29
30 public class DatagramSocketManager extends AbstractSocketManager {
31
32 private static final DatagramSocketManagerFactory FACTORY = new DatagramSocketManagerFactory();
33
34
35
36
37
38
39
40
41
42 protected DatagramSocketManager(final String name, final OutputStream os, InetAddress address, final String host, final int port) {
43 super(name, os, address, host, port);
44 }
45
46
47
48
49
50
51
52 public static DatagramSocketManager getSocketManager(final String host, final int port) {
53 if (host == null || host.length() == 0) {
54 throw new IllegalArgumentException("A host name is required");
55 }
56 if (port <= 0) {
57 throw new IllegalArgumentException("A port value is required");
58 }
59 return (DatagramSocketManager) getManager("UDP:" + host + ":" + port, new FactoryData(host, port), FACTORY
60 );
61 }
62
63
64
65
66
67
68
69 public Map<String, String> getContentFormat()
70 {
71 Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
72 result.put("protocol", "udp");
73 result.put("direction", "out");
74
75 return result;
76 }
77
78
79
80
81 private static class FactoryData {
82 private final String host;
83 private final int port;
84
85 public FactoryData(final String host, final int port) {
86 this.host = host;
87 this.port = port;
88 }
89 }
90
91
92
93
94 private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
95
96 public DatagramSocketManager createManager(final String name, final FactoryData data) {
97 InetAddress address;
98 final OutputStream os = new DatagramOutputStream(data.host, data.port);
99 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 }