JAL-3253 srcjar_unused/ moved to unused/
[jalview.git] / unused / srcjar_unused / org / apache / log4j / varia / Roller.java
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     
69     roll();
70   }
71
72   static
73   void usage(String msg) {
74     System.err.println(msg);
75     System.err.println( "Usage: java " + Roller.class.getName() +
76                         "host_name port_number");
77     System.exit(1);
78   }
79
80   static 
81   void init(String hostArg, String portArg) {
82     host = hostArg;
83     try {
84       port =  Integer.parseInt(portArg);
85     }
86     catch(java.lang.NumberFormatException e) {
87       usage("Second argument "+portArg+" is not a valid integer.");
88     }
89   }
90
91   static
92   void roll() {
93     try {
94       Socket socket = new Socket(host, port);
95       DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
96       DataInputStream dis = new DataInputStream(socket.getInputStream());
97       dos.writeUTF(ExternallyRolledFileAppender.ROLL_OVER);
98       String rc = dis.readUTF();
99       if(ExternallyRolledFileAppender.OK.equals(rc)) {
100         cat.info("Roll over signal acknowledged by remote appender.");
101       } else {
102         cat.warn("Unexpected return code "+rc+" from remote entity.");
103         System.exit(2);
104       }
105     } catch(IOException e) {
106       cat.error("Could not send roll signal on host "+host+" port "+port+" .",
107                 e);
108       System.exit(2);
109     }
110     System.exit(0);
111   }
112 }