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       */
46      protected DatagramSocketManager(final String name, final OutputStream os, final InetAddress inetAddress, final String host,
47                  final int port, final Layout<? extends Serializable> layout) {
48          super(name, os, inetAddress, host, port, layout);
49      }
50  
51      /**
52       * Obtain a SocketManager.
53       * @param host The host to connect to.
54       * @param port The port on the host.
55       * @param layout The layout.
56       * @return A DatagramSocketManager.
57       */
58      public static DatagramSocketManager getSocketManager(final String host, final int port, final Layout<? extends Serializable> layout) {
59          if (Strings.isEmpty(host)) {
60              throw new IllegalArgumentException("A host name is required");
61          }
62          if (port <= 0) {
63              throw new IllegalArgumentException("A port value is required");
64          }
65          return (DatagramSocketManager) getManager("UDP:" + host + ':' + port, new FactoryData(host, port, layout),
66              FACTORY);
67      }
68  
69      /**
70       * Gets this DatagramSocketManager's content format. Specified by:
71       * <ul>
72       * <li>Key: "protocol" Value: "udp"</li>
73       * <li>Key: "direction" Value: "out"</li>
74       * </ul>
75       * 
76       * @return Map of content format keys supporting DatagramSocketManager
77       */
78      @Override
79      public Map<String, String> getContentFormat() {
80          final Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
81          result.put("protocol", "udp");
82          result.put("direction", "out");
83          return result;
84      }
85  
86      /**
87       * Data for the factory.
88       */
89      private static class FactoryData {
90          private final String host;
91          private final int port;
92          private final Layout<? extends Serializable> layout;
93  
94          public FactoryData(final String host, final int port, final Layout<? extends Serializable> layout) {
95              this.host = host;
96              this.port = port;
97              this.layout = layout;
98          }
99      }
100 
101     /**
102      * Factory to create the DatagramSocketManager.
103      */
104     private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
105 
106         @Override
107         public DatagramSocketManager createManager(final String name, final FactoryData data) {
108             InetAddress inetAddress;
109             try {
110                 inetAddress = InetAddress.getByName(data.host);
111             } catch (final UnknownHostException ex) {
112                 LOGGER.error("Could not find address of " + data.host, ex);
113                 return null;
114             }
115             final OutputStream os = new DatagramOutputStream(data.host, data.port, data.layout.getHeader(),
116                     data.layout.getFooter());
117             return new DatagramSocketManager(name, os, inetAddress, data.host, data.port, data.layout);
118         }
119     }
120 }