1 16 17 package org.apache.log4j.net; 18 19 import java.net.Socket ; 20 import java.net.ServerSocket ; 21 22 import org.apache.log4j.Logger; 23 import org.apache.log4j.LogManager; 24 import org.apache.log4j.PropertyConfigurator; 25 import org.apache.log4j.xml.DOMConfigurator; 26 27 28 43 public class SimpleSocketServer { 44 45 static Logger cat = Logger.getLogger(SimpleSocketServer.class); 46 47 static int port; 48 49 public 50 static 51 void main(String argv[]) { 52 if(argv.length == 2) { 53 init(argv[0], argv[1]); 54 } else { 55 usage("Wrong number of arguments."); 56 } 57 58 try { 59 cat.info("Listening on port " + port); 60 ServerSocket serverSocket = new ServerSocket (port); 61 while(true) { 62 cat.info("Waiting to accept a new client."); 63 Socket socket = serverSocket.accept(); 64 cat.info("Connected to client at " + socket.getInetAddress()); 65 cat.info("Starting new socket node."); 66 new Thread (new SocketNode(socket, 67 LogManager.getLoggerRepository())).start(); 68 } 69 } catch(Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 74 75 static void usage(String msg) { 76 System.err.println(msg); 77 System.err.println( 78 "Usage: java " +SimpleSocketServer.class.getName() + " port configFile"); 79 System.exit(1); 80 } 81 82 static void init(String portStr, String configFile) { 83 try { 84 port = Integer.parseInt(portStr); 85 } catch(java.lang.NumberFormatException e) { 86 e.printStackTrace(); 87 usage("Could not interpret port number ["+ portStr +"]."); 88 } 89 90 if(configFile.endsWith(".xml")) { 91 new DOMConfigurator().configure(configFile); 92 } else { 93 new PropertyConfigurator().configure(configFile); 94 } 95 } 96 } 97 | Popular Tags |