1 22 package org.jboss.logging; 23 24 import java.net.Socket ; 25 import java.net.ServerSocket ; 26 import java.net.InetAddress ; 27 28 import org.apache.log4j.LogManager; 29 import org.apache.log4j.MDC; 30 import org.apache.log4j.spi.LoggerRepository; 31 import org.apache.log4j.net.SocketNode; 32 33 import org.jboss.system.ServiceMBeanSupport; 34 import org.jboss.system.MissingAttributeException; 35 36 import org.jboss.logging.Logger; 37 38 53 public class Log4jSocketServer 54 extends ServiceMBeanSupport 55 implements Log4jSocketServerMBean 56 { 57 58 protected int port = -1; 59 60 61 protected int backlog = 50; 62 63 64 protected InetAddress bindAddress; 65 66 67 protected boolean listenerEnabled = true; 68 69 70 protected SocketListenerThread listenerThread; 71 72 73 protected ServerSocket serverSocket; 74 75 76 protected LoggerRepositoryFactory loggerRepositoryFactory; 77 78 81 public Log4jSocketServer() 82 { 83 super(); 84 } 85 86 89 public void setPort(final int port) 90 { 91 this.port = port; 92 } 93 94 97 public int getPort() 98 { 99 return port; 100 } 101 102 105 public void setBacklog(final int backlog) 106 { 107 this.backlog = backlog; 108 } 109 110 113 public int getBacklog() 114 { 115 return backlog; 116 } 117 118 121 public void setBindAddress(final InetAddress addr) 122 { 123 this.bindAddress = addr; 124 } 125 126 129 public InetAddress getBindAddress() 130 { 131 return bindAddress; 132 } 133 134 137 public void setListenerEnabled(final boolean enabled) 138 { 139 listenerEnabled = enabled; 140 } 141 142 145 public boolean setListenerEnabled() 146 { 147 return listenerEnabled; 148 } 149 150 153 public void setLoggerRepositoryFactoryType(final Class type) 154 throws InstantiationException , IllegalAccessException , ClassCastException 155 { 156 this.loggerRepositoryFactory = (LoggerRepositoryFactory)type.newInstance(); 157 } 158 159 162 public Class getLoggerRepositoryFactoryType() 163 { 164 if (loggerRepositoryFactory == null) 165 return null; 166 return loggerRepositoryFactory.getClass(); 167 } 168 169 172 public LoggerRepository getLoggerRepository(final InetAddress addr) 173 { 174 return loggerRepositoryFactory.create(addr); 175 } 176 177 178 182 protected class SocketListenerThread 183 extends Thread 184 { 185 protected Logger log = Logger.getLogger(SocketListenerThread.class); 186 protected boolean enabled; 187 protected boolean shuttingDown; 188 protected Object lock = new Object (); 189 190 public SocketListenerThread(final boolean enabled) 191 { 192 super("SocketListenerThread"); 193 194 this.enabled = enabled; 195 } 196 197 public void setEnabled(boolean enabled) 198 { 199 this.enabled = enabled; 200 201 synchronized (lock) 202 { 203 lock.notifyAll(); 204 } 205 log.debug("Notified that enabled: " + enabled); 206 } 207 208 public void shutdown() 209 { 210 enabled = false; 211 shuttingDown = true; 212 213 synchronized (lock) 214 { 215 lock.notifyAll(); 216 } 217 log.debug("Notified to shutdown"); 218 } 219 220 public void run() 221 { 222 while (!shuttingDown) 223 { 224 225 if (!enabled) 226 { 227 try 228 { 229 log.debug("Disabled, waiting for notification"); 230 synchronized (lock) 231 { 232 lock.wait(); 233 } 234 } 235 catch (InterruptedException ignore) 236 { 237 } 238 } 239 240 try 241 { 242 doRun(); 243 } 244 catch (Throwable e) 245 { 246 log.error("Exception caught from main loop; ignoring", e); 247 } 248 } 249 } 250 251 protected void doRun() throws Exception 252 { 253 while (enabled) 254 { 255 Socket socket = serverSocket.accept(); 256 InetAddress addr = socket.getInetAddress(); 257 log.debug("Connected to client at " + addr); 258 259 LoggerRepository repo = getLoggerRepository(addr); 260 log.debug("Using repository: " + repo); 261 262 266 log.debug("Starting new socket node"); 267 SocketNode node = new SocketNode(socket, repo); 268 271 String clientHost = addr.getHostName(); 272 SocketThread thread = new SocketThread(node, clientHost); 273 thread.start(); 274 log.debug("Socket node started"); 275 } 276 } 277 } 278 279 static class SocketThread 280 extends Thread 281 { 282 String host; 283 284 SocketThread(Runnable target, String host) 285 { 286 super(target, host+" LoggingEvent Thread"); 287 this.host = host; 288 } 289 public void run() 290 { 291 MDC.put("host", host); 292 super.run(); 293 } 294 } 295 296 300 public static interface LoggerRepositoryFactory 301 { 302 public LoggerRepository create(InetAddress addr); 303 } 304 305 309 public static class DefaultLoggerRepositoryFactory 310 implements LoggerRepositoryFactory 311 { 312 private LoggerRepository repo; 313 314 public LoggerRepository create(final InetAddress addr) 315 { 316 if (repo == null) 317 repo = LogManager.getLoggerRepository(); 318 return repo; 319 } 320 } 321 322 326 protected void createService() throws Exception 327 { 328 listenerThread = new SocketListenerThread(false); 329 listenerThread.setDaemon(true); 330 listenerThread.start(); 331 log.debug("Socket listener thread started"); 332 333 if (loggerRepositoryFactory == null) 334 { 335 log.debug("Using default logger repository factory"); 336 loggerRepositoryFactory = new DefaultLoggerRepositoryFactory(); 337 } 338 } 339 340 protected void startService() throws Exception 341 { 342 if (port == -1) 343 throw new MissingAttributeException("Port"); 344 345 if (bindAddress == null) 347 { 348 serverSocket = new ServerSocket (port, backlog); 349 } 350 else 351 { 352 serverSocket = new ServerSocket (port, backlog, bindAddress); 353 } 354 355 log.info("Listening on " + serverSocket); 356 357 listenerThread.setEnabled(listenerEnabled); 358 } 359 360 protected void stopService() throws Exception 361 { 362 listenerThread.setEnabled(false); 363 } 364 365 protected void destroyService() throws Exception 366 { 367 listenerThread.shutdown(); 368 listenerThread = null; 369 serverSocket = null; 370 } 371 } 372 | Popular Tags |