1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.net;
19
20 import java.io.File;
21 import java.net.InetAddress;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24 import java.util.Hashtable;
25
26 import org.apache.log4j.Hierarchy;
27 import org.apache.log4j.Level;
28 import org.apache.log4j.LogManager;
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.PropertyConfigurator;
31 import org.apache.log4j.spi.LoggerRepository;
32 import org.apache.log4j.spi.RootLogger;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public class SocketServer {
89
90 static String GENERIC = "generic";
91 static String CONFIG_FILE_EXT = ".lcf";
92
93 static Logger cat = Logger.getLogger(SocketServer.class);
94 static SocketServer server;
95 static int port;
96
97
98 Hashtable hierarchyMap;
99 LoggerRepository genericHierarchy;
100 File dir;
101
102 public
103 static
104 void main(String argv[]) {
105 if(argv.length == 3)
106 init(argv[0], argv[1], argv[2]);
107 else
108 usage("Wrong number of arguments.");
109
110 try {
111 cat.info("Listening on port " + port);
112 ServerSocket serverSocket = new ServerSocket(port);
113 while(true) {
114 cat.info("Waiting to accept a new client.");
115 Socket socket = serverSocket.accept();
116 InetAddress inetAddress = socket.getInetAddress();
117 cat.info("Connected to client at " + inetAddress);
118
119 LoggerRepository h = (LoggerRepository) server.hierarchyMap.get(inetAddress);
120 if(h == null) {
121 h = server.configureHierarchy(inetAddress);
122 }
123
124 cat.info("Starting new socket node.");
125 new Thread(new SocketNode(socket, h)).start();
126 }
127 }
128 catch(Exception e) {
129 e.printStackTrace();
130 }
131 }
132
133
134 static
135 void usage(String msg) {
136 System.err.println(msg);
137 System.err.println(
138 "Usage: java " +SocketServer.class.getName() + " port configFile directory");
139 System.exit(1);
140 }
141
142 static
143 void init(String portStr, String configFile, String dirStr) {
144 try {
145 port = Integer.parseInt(portStr);
146 }
147 catch(java.lang.NumberFormatException e) {
148 e.printStackTrace();
149 usage("Could not interpret port number ["+ portStr +"].");
150 }
151
152 PropertyConfigurator.configure(configFile);
153
154 File dir = new File(dirStr);
155 if(!dir.isDirectory()) {
156 usage("["+dirStr+"] is not a directory.");
157 }
158 server = new SocketServer(dir);
159 }
160
161
162 public
163 SocketServer(File directory) {
164 this.dir = directory;
165 hierarchyMap = new Hashtable(11);
166 }
167
168
169
170 LoggerRepository configureHierarchy(InetAddress inetAddress) {
171 cat.info("Locating configuration file for "+inetAddress);
172
173
174 String s = inetAddress.toString();
175 int i = s.indexOf("/");
176 if(i == -1) {
177 cat.warn("Could not parse the inetAddress ["+inetAddress+
178 "]. Using default hierarchy.");
179 return genericHierarchy();
180 } else {
181 String key = s.substring(0, i);
182
183 File configFile = new File(dir, key+CONFIG_FILE_EXT);
184 if(configFile.exists()) {
185 Hierarchy h = new Hierarchy(new RootLogger(Level.DEBUG));
186 hierarchyMap.put(inetAddress, h);
187
188 new PropertyConfigurator().doConfigure(configFile.getAbsolutePath(), h);
189
190 return h;
191 } else {
192 cat.warn("Could not find config file ["+configFile+"].");
193 return genericHierarchy();
194 }
195 }
196 }
197
198 LoggerRepository genericHierarchy() {
199 if(genericHierarchy == null) {
200 File f = new File(dir, GENERIC+CONFIG_FILE_EXT);
201 if(f.exists()) {
202 genericHierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
203 new PropertyConfigurator().doConfigure(f.getAbsolutePath(), genericHierarchy);
204 } else {
205 cat.warn("Could not find config file ["+f+
206 "]. Will use the default hierarchy.");
207 genericHierarchy = LogManager.getLoggerRepository();
208 }
209 }
210 return genericHierarchy;
211 }
212 }