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       * DatagramSocketManager's content format is specified by:<p/>
71       * Key: "protocol" Value: "udp"<p/>
72       * Key: "direction" Value: "out"
73       * @return Map of content format keys supporting DatagramSocketManager
74       */
75      @Override
76      public Map<String, String> getContentFormat()
77      {
78          final Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
79          result.put("protocol", "udp");
80          result.put("direction", "out");
81  
82          return result;
83      }
84  
85      /**
86       * Data for the factory.
87       */
88      private static class FactoryData {
89          private final String host;
90          private final int port;
91          private final Layout<? extends Serializable> layout;
92  
93          public FactoryData(final String host, final int port, final Layout<? extends Serializable> layout) {
94              this.host = host;
95              this.port = port;
96              this.layout = layout;
97          }
98      }
99  
100     /**
101      * Factory to create the DatagramSocketManager.
102      */
103     private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
104 
105         @Override
106         public DatagramSocketManager createManager(final String name, final FactoryData data) {
107             InetAddress inetAddress;
108             try {
109                 inetAddress = InetAddress.getByName(data.host);
110             } catch (final UnknownHostException ex) {
111                 LOGGER.error("Could not find address of " + data.host, ex);
112                 return null;
113             }
114             final OutputStream os = new DatagramOutputStream(data.host, data.port, data.layout.getHeader(),
115                     data.layout.getFooter());
116             return new DatagramSocketManager(name, os, inetAddress, data.host, data.port, data.layout);
117         }
118     }
119 }