View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * Socket Manager for UDP connections.
29   */
30  public class DatagramSocketManager extends AbstractSocketManager {
31  
32      private static final DatagramSocketManagerFactory FACTORY = new DatagramSocketManagerFactory();
33  
34      /**
35       * The Constructor.
36       * @param name The unique name of the connection.
37       * @param os The OutputStream.
38       * @param address
39       * @param host The host to connect to.
40       * @param port The port on the host.
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       * Obtain a SocketManager.
48       * @param host The host to connect to.
49       * @param port The port on the host.
50       * @return A DatagramSocketManager.
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       * DatagramSocketManager's content format is specified by:<p/>
65       * Key: "protocol" Value: "udp"<p/>
66       * Key: "direction" Value: "out"
67       * @return Map of content format keys supporting DatagramSocketManager
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       * Data for the factory.
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       * Factory to create the DatagramSocketManager.
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 }