1 16 17 package org.apache.log4j.varia; 18 19 import java.io.*; 20 import java.net.Socket ; 21 import java.net.ServerSocket ; 22 import org.apache.log4j.helpers.LogLog; 23 import org.apache.log4j.RollingFileAppender; 24 import org.apache.log4j.helpers.LogLog; 25 26 46 public class ExternallyRolledFileAppender extends RollingFileAppender { 47 48 52 static final public String ROLL_OVER = "RollOver"; 53 54 58 static final public String OK = "OK"; 59 60 int port = 0; 61 HUP hup; 62 63 66 public 67 ExternallyRolledFileAppender() { 68 } 69 70 74 public 75 void setPort(int port) { 76 this.port = port; 77 } 78 79 82 public 83 int getPort() { 84 return port; 85 } 86 87 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 { 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 serverSocket = new ServerSocket (port); 120 while(true) { 121 Socket socket = serverSocket.accept(); 122 LogLog.debug("Connected to client at " + socket.getInetAddress()); 123 new Thread (new HUPNode(socket, er)).start(); 124 } 125 } 126 catch(Exception e) { 127 e.printStackTrace(); 128 } 129 } 130 } 131 } 132 133 class HUPNode implements Runnable { 134 135 Socket socket; 136 DataInputStream dis; 137 DataOutputStream dos; 138 ExternallyRolledFileAppender er; 139 140 public 141 HUPNode(Socket 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 e) { 149 e.printStackTrace(); 150 } 151 } 152 153 public void run() { 154 try { 155 String 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 e) { 169 LogLog.error("Unexpected exception. Exiting HUPNode.", e); 170 } 171 } 172 } 173 174 | Popular Tags |