ShortSocketServer xref

    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
    18
    19  package org.apache.log4j.net;
    20
    21  import java.net.Socket;
    22  import java.net.ServerSocket;
    23
    24  import org.apache.log4j.Logger;
    25  import org.apache.log4j.LogManager;
    26  import org.apache.log4j.PropertyConfigurator;
    27  import org.apache.log4j.MDC;
    28  import org.apache.log4j.helpers.LogLog;
    29
    30  /**
    31   * This SocketServer exits after certain number of connections from a
    32   * client. This number is determined the totalsTest parameter, that is
    33   * the first argument on the commmand line. The second argument,
    34   * prefix, determines the prefix of the configuration file to
    35   * use. Each run of the server will use a different properties
    36   * file. For the i-th run, the path to the file is
    37   * (prefix+i+".properties").
    38   *
    39   * @author Ceki Gulcu */
    40
    41  public class ShortSocketServer  {
    42
    43    static Logger cat = Logger.getLogger(ShortSocketServer.class);
    44
    45    public
    46    static
    47    void main(String args[]) throws Exception {
    48      int totalTests = 0;
    49      String prefix = null;
    50
    51      if(args.length == 2) {
    52        totalTests = Integer.parseInt(args[0]);
    53        prefix = args[1];
    54      } else {
    55        usage("Wrong number of arguments.");
    56      }
    57
    58
    59        LogLog.debug("Listening on port " + SocketServerTestCase.PORT);
    60        ServerSocket serverSocket = new ServerSocket(SocketServerTestCase.PORT);
    61
    62        MDC.put("hostID", "shortSocketServer");
    63
    64        for(int i = 1; i <= totalTests; i++) {
    65  	PropertyConfigurator.configure(prefix+i+".properties");
    66  	LogLog.debug("Waiting to accept a new client.");
    67  	Socket socket = serverSocket.accept();
    68  	LogLog.debug("Connected to client at " + socket.getInetAddress());
    69  	LogLog.debug("Starting new socket node.");
    70  	SocketNode sn = new SocketNode(socket, LogManager.getLoggerRepository());
    71  	Thread t = new Thread(sn);
    72  	t.start();
    73  	t.join();
    74        }
    75    }
    76
    77
    78    static
    79    void usage(String msg) {
    80      System.err.println(msg);
    81      System.err.println(
    82        "Usage: java " +ShortSocketServer.class.getName() + " totalTests configFilePrefix");
    83      System.exit(1);
    84    }
    85  }