KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
20 import java.net.Socket JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import org.apache.log4j.helpers.LogLog;
23 import org.apache.log4j.RollingFileAppender;
24 import org.apache.log4j.helpers.LogLog;
25
26 /**
27    This appender listens on a socket on the port specified by the
28    <b>Port</b> property for a "RollOver" message. When such a message
29    is received, the underlying log file is rolled over and an
30    acknowledgment message is sent back to the process initiating the
31    roll over.
32
33    <p>This method of triggering roll over has the advantage of being
34    operating system independent, fast and reliable.
35
36    <p>A simple application {@link Roller} is provided to initiate the
37    roll over.
38
39    <p>Note that the initiator is not authenticated. Anyone can trigger
40    a rollover. In production environments, it is recommended that you
41    add some form of protection to prevent undesired rollovers.
42
43
44    @author Ceki G&uuml;lc&uuml;
45    @since version 0.9.0 */

46 public class ExternallyRolledFileAppender extends RollingFileAppender {
47
48   /**
49      The string constant sent to initiate a roll over. Current value of
50      this string constant is <b>RollOver</b>.
51   */

52   static final public String JavaDoc ROLL_OVER = "RollOver";
53
54   /**
55      The string constant sent to acknowledge a roll over. Current value of
56       this string constant is <b>OK</b>.
57   */

58   static final public String JavaDoc OK = "OK";
59
60   int port = 0;
61   HUP hup;
62
63   /**
64      The default constructor does nothing but calls its super-class
65      constructor. */

66   public
67   ExternallyRolledFileAppender() {
68   }
69
70   /**
71      The <b>Port</b> [roperty is used for setting the port for
72      listening to external roll over messages.
73   */

74   public
75   void setPort(int port) {
76     this.port = port;
77   }
78
79   /**
80      Returns value of the <b>Port</b> option.
81    */

82   public
83   int getPort() {
84     return port;
85   }
86
87   /**
88      Start listening on the port specified by a preceding call to
89      {@link #setPort}. */

90   public
91   void activateOptions() {
92     super.activateOptions();
93     if(port != 0) {
94       if(hup != null) {
95     hup.interrupt();
96       }
97       hup = new HUP(this, port);
98       hup.setDaemon(true);
99       hup.start();
100     }
101   }
102 }
103
104
105 class HUP extends Thread JavaDoc {
106
107   int port;
108   ExternallyRolledFileAppender er;
109
110   HUP(ExternallyRolledFileAppender er, int port) {
111     this.er = er;
112     this.port = port;
113   }
114
115   public
116   void run() {
117     while(!isInterrupted()) {
118       try {
119     ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(port);
120     while(true) {
121       Socket JavaDoc socket = serverSocket.accept();
122       LogLog.debug("Connected to client at " + socket.getInetAddress());
123       new Thread JavaDoc(new HUPNode(socket, er)).start();
124     }
125       }
126       catch(Exception JavaDoc e) {
127     e.printStackTrace();
128       }
129     }
130   }
131 }
132
133 class HUPNode implements Runnable JavaDoc {
134
135   Socket JavaDoc socket;
136   DataInputStream dis;
137   DataOutputStream dos;
138   ExternallyRolledFileAppender er;
139
140   public
141   HUPNode(Socket JavaDoc socket, ExternallyRolledFileAppender er) {
142     this.socket = socket;
143     this.er = er;
144     try {
145       dis = new DataInputStream(socket.getInputStream());
146       dos = new DataOutputStream(socket.getOutputStream());
147     }
148     catch(Exception JavaDoc e) {
149       e.printStackTrace();
150     }
151   }
152
153   public void run() {
154     try {
155       String JavaDoc line = dis.readUTF();
156       LogLog.debug("Got external roll over signal.");
157       if(ExternallyRolledFileAppender.ROLL_OVER.equals(line)) {
158     synchronized(er) {
159       er.rollOver();
160     }
161     dos.writeUTF(ExternallyRolledFileAppender.OK);
162       }
163       else {
164     dos.writeUTF("Expecting [RollOver] string.");
165       }
166       dos.close();
167     }
168     catch(Exception JavaDoc e) {
169       LogLog.error("Unexpected exception. Exiting HUPNode.", e);
170     }
171   }
172 }
173
174
Popular Tags