KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > varia > Roller


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.varia;
18
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.BasicConfigurator;
21
22 import java.io.IOException JavaDoc;
23 import java.io.DataInputStream JavaDoc;
24 import java.io.DataOutputStream JavaDoc;
25 import java.net.Socket JavaDoc;
26
27 /**
28    A simple application to send roll over messages to a potentially
29    remote {@link ExternallyRolledFileAppender}.
30
31    <p>It takes two arguments, the <code>host_name</code> and
32    <code>port_number</code> where the
33    <code>ExternallyRolledFileAppender</code> is listening.
34    
35
36    @author Ceki G&uuml;lc&uuml;
37    @since version 0.9.0 */

38 public class Roller {
39
40   static Logger cat = Logger.getLogger(Roller.class);
41   
42
43   static String JavaDoc host;
44   static int port;
45
46   // Static class.
47
Roller() {
48   }
49
50   /**
51      Send a "RollOver" message to
52      <code>ExternallyRolledFileAppender</code> on <code>host</code>
53      and <code>port</code>.
54
55    */

56   public
57   static
58   void main(String JavaDoc argv[]) {
59
60     BasicConfigurator.configure();
61
62     if(argv.length == 2)
63       init(argv[0], argv[1]);
64     else
65       usage("Wrong number of arguments.");
66     
67     roll();
68   }
69
70   static
71   void usage(String JavaDoc msg) {
72     System.err.println(msg);
73     System.err.println( "Usage: java " + Roller.class.getName() +
74             "host_name port_number");
75     System.exit(1);
76   }
77
78   static
79   void init(String JavaDoc hostArg, String JavaDoc portArg) {
80     host = hostArg;
81     try {
82       port = Integer.parseInt(portArg);
83     }
84     catch(java.lang.NumberFormatException JavaDoc e) {
85       usage("Second argument "+portArg+" is not a valid integer.");
86     }
87   }
88
89   static
90   void roll() {
91     try {
92       Socket JavaDoc socket = new Socket JavaDoc(host, port);
93       DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(socket.getOutputStream());
94       DataInputStream JavaDoc dis = new DataInputStream JavaDoc(socket.getInputStream());
95       dos.writeUTF(ExternallyRolledFileAppender.ROLL_OVER);
96       String JavaDoc rc = dis.readUTF();
97       if(ExternallyRolledFileAppender.OK.equals(rc)) {
98     cat.info("Roll over signal acknowledged by remote appender.");
99       } else {
100     cat.warn("Unexpected return code "+rc+" from remote entity.");
101     System.exit(2);
102       }
103     } catch(IOException JavaDoc e) {
104       cat.error("Could not send roll signal on host "+host+" port "+port+" .",
105         e);
106       System.exit(2);
107     }
108     System.exit(0);
109   }
110 }
111
Popular Tags