001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.log4j.varia; 019 020import org.apache.log4j.Logger; 021import org.apache.log4j.BasicConfigurator; 022 023import java.io.IOException; 024import java.io.DataInputStream; 025import java.io.DataOutputStream; 026import java.net.Socket; 027 028/** 029 A simple application to send roll over messages to a potentially 030 remote {@link ExternallyRolledFileAppender}. 031 032 <p>It takes two arguments, the <code>host_name</code> and 033 <code>port_number</code> where the 034 <code>ExternallyRolledFileAppender</code> is listening. 035 036 037 @author Ceki Gülcü 038 @since version 0.9.0 */ 039public class Roller { 040 041 static Logger cat = Logger.getLogger(Roller.class); 042 043 044 static String host; 045 static int port; 046 047 // Static class. 048 Roller() { 049 } 050 051 /** 052 Send a "RollOver" message to 053 <code>ExternallyRolledFileAppender</code> on <code>host</code> 054 and <code>port</code>. 055 056 */ 057 public 058 static 059 void main(String argv[]) { 060 061 BasicConfigurator.configure(); 062 063 if(argv.length == 2) 064 init(argv[0], argv[1]); 065 else 066 usage("Wrong number of arguments."); 067 068 roll(); 069 } 070 071 static 072 void usage(String msg) { 073 System.err.println(msg); 074 System.err.println( "Usage: java " + Roller.class.getName() + 075 "host_name port_number"); 076 System.exit(1); 077 } 078 079 static 080 void init(String hostArg, String portArg) { 081 host = hostArg; 082 try { 083 port = Integer.parseInt(portArg); 084 } 085 catch(java.lang.NumberFormatException e) { 086 usage("Second argument "+portArg+" is not a valid integer."); 087 } 088 } 089 090 static 091 void roll() { 092 try { 093 Socket socket = new Socket(host, port); 094 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 095 DataInputStream dis = new DataInputStream(socket.getInputStream()); 096 dos.writeUTF(ExternallyRolledFileAppender.ROLL_OVER); 097 String rc = dis.readUTF(); 098 if(ExternallyRolledFileAppender.OK.equals(rc)) { 099 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}