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.server;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URI;
26  import java.net.URL;
27  import java.util.Objects;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.apache.logging.log4j.core.LogEventListener;
32  import org.apache.logging.log4j.core.config.Configuration;
33  import org.apache.logging.log4j.core.config.ConfigurationSource;
34  import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
35  import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
36  import org.apache.logging.log4j.util.Strings;
37  
38  /**
39   * Abstract socket server for TCP and UDP implementations.
40   * 
41   * @param <T> The kind of input stream read
42   * 
43   * TODO Make a LifeCycle
44   */
45  public abstract class AbstractSocketServer<T extends InputStream> extends LogEventListener implements Runnable {
46  
47      /**
48       * Factory that creates a Configuration for the server.
49       */
50      protected static class ServerConfigurationFactory extends XmlConfigurationFactory {
51  
52          private final String path;
53  
54          public ServerConfigurationFactory(final String path) {
55              this.path = path;
56          }
57  
58          @Override
59          public Configuration getConfiguration(final String name, final URI configLocation) {
60              if (Strings.isNotEmpty(path)) {
61                  File file = null;
62                  ConfigurationSource source = null;
63                  try {
64                      file = new File(path);
65                      final FileInputStream is = new FileInputStream(file);
66                      source = new ConfigurationSource(is, file);
67                  } catch (final FileNotFoundException ex) {
68                      // Ignore this error
69                  }
70                  if (source == null) {
71                      try {
72                          final URL url = new URL(path);
73                          source = new ConfigurationSource(url.openStream(), url);
74                      } catch (final MalformedURLException mue) {
75                          // Ignore this error
76                      } catch (final IOException ioe) {
77                          // Ignore this error
78                      }
79                  }
80  
81                  try {
82                      if (source != null) {
83                          return new XmlConfiguration(source);
84                      }
85                  } catch (final Exception ex) {
86                      // Ignore this error.
87                  }
88                  System.err.println("Unable to process configuration at " + path + ", using default.");
89              }
90              return super.getConfiguration(name, configLocation);
91          }
92      }
93  
94      protected static final int MAX_PORT = 65534;
95  
96      private volatile boolean active = true;
97  
98      protected final LogEventBridge<T> logEventInput;
99  
100     protected final Logger logger;
101 
102     /**
103      * Creates a new socket server.
104      * 
105      * @param port listen to this port
106      * @param logEventInput Use this input to read log events.
107      */
108     public AbstractSocketServer(final int port, final LogEventBridge<T> logEventInput) {
109         this.logger = LogManager.getLogger(this.getClass().getName() + '.' + port);
110         this.logEventInput = Objects.requireNonNull(logEventInput, "LogEventInput");
111     }
112 
113     protected boolean isActive() {
114         return this.active;
115     }
116 
117     protected void setActive(final boolean isActive) {
118         this.active = isActive;
119     }
120 
121     /**
122      * Start this server in a new thread.
123      * 
124      * @return the new thread that running this server.
125      */
126     public Thread startNewThread() {
127         final Thread thread = new Thread(this);
128         thread.start();
129         return thread;
130     }
131 
132 }