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