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