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.io.OutputStream;
20  import java.io.Serializable;
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.logging.log4j.core.Layout;
27  import org.apache.logging.log4j.core.appender.ManagerFactory;
28  import org.apache.logging.log4j.util.Strings;
29  
30  /**
31   * Socket Manager for UDP connections.
32   */
33  public class DatagramSocketManager extends AbstractSocketManager {
34  
35      private static final DatagramSocketManagerFactory FACTORY = new DatagramSocketManagerFactory();
36  
37      /**
38       * The Constructor.
39       * @param name The unique name of the connection.
40       * @param os The OutputStream.
41       * @param inetAddress
42       * @param host The host to connect to.
43       * @param port The port on the host.
44       * @param layout The layout
45       * @param bufferSize The buffer size
46       */
47      protected DatagramSocketManager(final String name, final OutputStream os, final InetAddress inetAddress, final String host,
48                  final int port, final Layout<? extends Serializable> layout, final int bufferSize) {
49          super(name, os, inetAddress, host, port, layout, true, bufferSize);
50      }
51  
52      /**
53       * Obtain a SocketManager.
54       * @param host The host to connect to.
55       * @param port The port on the host.
56       * @param layout The layout.
57       * @param bufferSize The buffer size.
58       * @return A DatagramSocketManager.
59       */
60      public static DatagramSocketManager getSocketManager(final String host, final int port,
61              final Layout<? extends Serializable> layout, final int bufferSize) {
62          if (Strings.isEmpty(host)) {
63              throw new IllegalArgumentException("A host name is required");
64          }
65          if (port <= 0) {
66              throw new IllegalArgumentException("A port value is required");
67          }
68          return (DatagramSocketManager) getManager("UDP:" + host + ':' + port,
69                  new FactoryData(host, port, layout, bufferSize), FACTORY);
70      }
71  
72      /**
73       * Gets this DatagramSocketManager's content format. Specified by:
74       * <ul>
75       * <li>Key: "protocol" Value: "udp"</li>
76       * <li>Key: "direction" Value: "out"</li>
77       * </ul>
78       *
79       * @return Map of content format keys supporting DatagramSocketManager
80       */
81      @Override
82      public Map<String, String> getContentFormat() {
83          final Map<String, String> result = new HashMap<>(super.getContentFormat());
84          result.put("protocol", "udp");
85          result.put("direction", "out");
86          return result;
87      }
88  
89      /**
90       * Data for the factory.
91       */
92      private static class FactoryData {
93          private final String host;
94          private final int port;
95          private final Layout<? extends Serializable> layout;
96          private final int bufferSize;
97  
98          public FactoryData(final String host, final int port, final Layout<? extends Serializable> layout, final int bufferSize) {
99              this.host = host;
100             this.port = port;
101             this.layout = layout;
102             this.bufferSize = bufferSize;
103         }
104     }
105 
106     /**
107      * Factory to create the DatagramSocketManager.
108      */
109     private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
110 
111         @Override
112         public DatagramSocketManager createManager(final String name, final FactoryData data) {
113             InetAddress inetAddress;
114             try {
115                 inetAddress = InetAddress.getByName(data.host);
116             } catch (final UnknownHostException ex) {
117                 LOGGER.error("Could not find address of " + data.host, ex);
118                 return null;
119             }
120             final OutputStream os = new DatagramOutputStream(data.host, data.port, data.layout.getHeader(),
121                     data.layout.getFooter());
122             return new DatagramSocketManager(name, os, inetAddress, data.host, data.port, data.layout, data.bufferSize);
123         }
124     }
125 }