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 org.apache.logging.log4j.core.appender.ManagerFactory;
20  
21  import java.io.OutputStream;
22  
23  /**
24   * Socket Manager for UDP connections.
25   */
26  public class DatagramSocketManager extends AbstractSocketManager {
27  
28      private static final DatagramSocketManagerFactory factory = new DatagramSocketManagerFactory();
29  
30      /**
31       * The Constructor.
32       * @param os The OutputStream.
33       * @param name The unique name of the connection.
34       * @param host The host to connect to.
35       * @param port The port on the host.
36       */
37      protected DatagramSocketManager(OutputStream os, String name, String host, int port) {
38          super(name, os, null, host, port);
39      }
40  
41      /**
42       * Obtain a SocketManager.
43       * @param host The host to connect to.
44       * @param port The port on the host.
45       * @return A DatagramSocketManager.
46       */
47      public static DatagramSocketManager getSocketManager(String host, int port) {
48          if (host == null || host.length() == 0) {
49              throw new IllegalArgumentException("A host name is required");
50          }
51          if (port <= 0) {
52              throw new IllegalArgumentException("A port value is required");
53          }
54          return (DatagramSocketManager) getManager("UDP:" + host + ":" + port, new FactoryData(host, port), factory
55          );
56      }
57  
58      /**
59       * Data for the factory.
60       */
61      private static class FactoryData {
62          private String host;
63          private int port;
64  
65          public FactoryData(String host, int port) {
66              this.host = host;
67              this.port = port;
68          }
69      }
70  
71      /**
72       * Factory to create the DatagramSocketManager.
73       */
74      private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
75  
76          public DatagramSocketManager createManager(String name, FactoryData data) {
77              OutputStream os = new DatagramOutputStream(data.host, data.port);
78              return new DatagramSocketManager(os, name, data.host, data.port);
79          }
80      }
81  }