1 16 17 package org.apache.log4j.net; 18 19 import java.net.Socket ; 20 import java.net.ServerSocket ; 21 import java.net.InetAddress ; 22 import java.io.File ; 23 import java.util.Hashtable ; 24 25 import org.apache.log4j.*; 26 import org.apache.log4j.spi.*; 27 28 29 81 82 public class SocketServer { 83 84 static String GENERIC = "generic"; 85 static String CONFIG_FILE_EXT = ".lcf"; 86 87 static Logger cat = Logger.getLogger(SocketServer.class); 88 static SocketServer server; 89 static int port; 90 91 Hashtable hierarchyMap; 93 LoggerRepository genericHierarchy; 94 File dir; 95 96 public 97 static 98 void main(String argv[]) { 99 if(argv.length == 3) 100 init(argv[0], argv[1], argv[2]); 101 else 102 usage("Wrong number of arguments."); 103 104 try { 105 cat.info("Listening on port " + port); 106 ServerSocket serverSocket = new ServerSocket (port); 107 while(true) { 108 cat.info("Waiting to accept a new client."); 109 Socket socket = serverSocket.accept(); 110 InetAddress inetAddress = socket.getInetAddress(); 111 cat.info("Connected to client at " + inetAddress); 112 113 LoggerRepository h = (LoggerRepository) server.hierarchyMap.get(inetAddress); 114 if(h == null) { 115 h = server.configureHierarchy(inetAddress); 116 } 117 118 cat.info("Starting new socket node."); 119 new Thread (new SocketNode(socket, h)).start(); 120 } 121 } 122 catch(Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 128 static 129 void usage(String msg) { 130 System.err.println(msg); 131 System.err.println( 132 "Usage: java " +SocketServer.class.getName() + " port configFile directory"); 133 System.exit(1); 134 } 135 136 static 137 void init(String portStr, String configFile, String dirStr) { 138 try { 139 port = Integer.parseInt(portStr); 140 } 141 catch(java.lang.NumberFormatException e) { 142 e.printStackTrace(); 143 usage("Could not interpret port number ["+ portStr +"]."); 144 } 145 146 PropertyConfigurator.configure(configFile); 147 148 File dir = new File (dirStr); 149 if(!dir.isDirectory()) { 150 usage("["+dirStr+"] is not a directory."); 151 } 152 server = new SocketServer(dir); 153 } 154 155 156 public 157 SocketServer(File directory) { 158 this.dir = directory; 159 hierarchyMap = new Hashtable (11); 160 } 161 162 LoggerRepository configureHierarchy(InetAddress inetAddress) { 165 cat.info("Locating configuration file for "+inetAddress); 166 String s = inetAddress.toString(); 169 int i = s.indexOf("/"); 170 if(i == -1) { 171 cat.warn("Could not parse the inetAddress ["+inetAddress+ 172 "]. Using default hierarchy."); 173 return genericHierarchy(); 174 } else { 175 String key = s.substring(0, i); 176 177 File configFile = new File (dir, key+CONFIG_FILE_EXT); 178 if(configFile.exists()) { 179 Hierarchy h = new Hierarchy(new RootLogger((Level) Priority.DEBUG)); 180 hierarchyMap.put(inetAddress, h); 181 182 new PropertyConfigurator().doConfigure(configFile.getAbsolutePath(), h); 183 184 return h; 185 } else { 186 cat.warn("Could not find config file ["+configFile+"]."); 187 return genericHierarchy(); 188 } 189 } 190 } 191 192 LoggerRepository genericHierarchy() { 193 if(genericHierarchy == null) { 194 File f = new File (dir, GENERIC+CONFIG_FILE_EXT); 195 if(f.exists()) { 196 genericHierarchy = new Hierarchy(new RootLogger((Level) Priority.DEBUG)); 197 new PropertyConfigurator().doConfigure(f.getAbsolutePath(), genericHierarchy); 198 } else { 199 cat.warn("Could not find config file ["+f+ 200 "]. Will use the default hierarchy."); 201 genericHierarchy = LogManager.getLoggerRepository(); 202 } 203 } 204 return genericHierarchy; 205 } 206 } 207 | Popular Tags |