Roller xref

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  package org.apache.log4j.varia;
    19
    20  import org.apache.log4j.Logger;
    21  import org.apache.log4j.BasicConfigurator;
    22
    23  import java.io.IOException;
    24  import java.io.DataInputStream;
    25  import java.io.DataOutputStream;
    26  import java.net.Socket;
    27
    28  /**
    29     A simple application to send roll over messages to a potentially
    30     remote {@link ExternallyRolledFileAppender}.
    31
    32     <p>It takes two arguments, the <code>host_name</code> and
    33     <code>port_number</code> where the
    34     <code>ExternallyRolledFileAppender</code> is listening.
    35
    36
    37     @author Ceki G&uuml;lc&uuml;
    38     @since version 0.9.0 */
    39  public class Roller {
    40
    41    static Logger cat = Logger.getLogger(Roller.class);
    42
    43
    44    static String host;
    45    static int port;
    46
    47    // Static class.
    48    Roller() {
    49    }
    50
    51    /**
    52       Send a "RollOver" message to
    53       <code>ExternallyRolledFileAppender</code> on <code>host</code>
    54       and <code>port</code>.
    55
    56     */
    57    public
    58    static
    59    void main(String argv[]) {
    60
    61      BasicConfigurator.configure();
    62
    63      if(argv.length == 2)
    64        init(argv[0], argv[1]);
    65      else
    66        usage("Wrong number of arguments.");
    67
    68      roll();
    69    }
    70
    71    static
    72    void usage(String msg) {
    73      System.err.println(msg);
    74      System.err.println( "Usage: java " + Roller.class.getName() +
    75  			"host_name port_number");
    76      System.exit(1);
    77    }
    78
    79    static
    80    void init(String hostArg, String portArg) {
    81      host = hostArg;
    82      try {
    83        port =  Integer.parseInt(portArg);
    84      }
    85      catch(java.lang.NumberFormatException e) {
    86        usage("Second argument "+portArg+" is not a valid integer.");
    87      }
    88    }
    89
    90    static
    91    void roll() {
    92      try {
    93        Socket socket = new Socket(host, port);
    94        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    95        DataInputStream dis = new DataInputStream(socket.getInputStream());
    96        dos.writeUTF(ExternallyRolledFileAppender.ROLL_OVER);
    97        String rc = dis.readUTF();
    98        if(ExternallyRolledFileAppender.OK.equals(rc)) {
    99  	cat.info("Roll over signal acknowledged by remote appender.");
    100       } else {
    101 	cat.warn("Unexpected return code "+rc+" from remote entity.");
    102 	System.exit(2);
    103       }
    104     } catch(IOException e) {
    105       cat.error("Could not send roll signal on host "+host+" port "+port+" .",
    106 		e);
    107       System.exit(2);
    108     }
    109     System.exit(0);
    110   }
    111 }