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