1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19 package com.systemsunion.LoggingServer;
20
21 import java.net.Socket;
22 import java.net.ServerSocket;
23 import java.io.IOException;
24
25 import org.apache.log4j.Category;
26 import org.apache.log4j.PropertyConfigurator;
27 import org.apache.log4j.NDC;
28
29 /**
30 A simple {@link SocketNode} based server.
31
32 <pre>
33 <b>Usage:</b> java org.apache.log4j.net.SocketServer port configFile
34
35 where <em>port</em> is a part number where the server listens and
36 <em>configFile</em> is a configuration file fed to the {@link
37 PropertyConfigurator}.
38 </pre>
39
40
41
42
43
44 @author Ceki Gülcü
45
46 @since 0.8.4 */
47
48 public class SocketServer2 {
49
50 static Category cat = Category.getInstance(SocketServer2.class.getName());
51
52 static int port;
53
54 public
55 static
56 void main(String argv[]) {
57 if(argv.length == 2)
58 init(argv[0], argv[1]);
59 else
60 usage("Wrong number of arguments.");
61
62 try {
63 cat.info("Listening on port " + port);
64 ServerSocket serverSocket = new ServerSocket(port);
65 while(true) {
66 cat.info("Waiting to accept a new client.");
67 Socket socket = serverSocket.accept();
68 cat.info("Connected to client at " + socket.getInetAddress());
69 cat.info("Starting new socket node.");
70 new Thread(new SocketNode2(socket)).start();
71 }
72 }
73 catch(Exception e) {
74 e.printStackTrace();
75 }
76 }
77
78
79 static
80 void usage(String msg) {
81 System.err.println(msg);
82 System.err.println(
83 "Usage: java " + SocketServer2.class.getName() + " port configFile");
84 System.exit(1);
85 }
86
87 static
88 void init(String portStr, String configFile) {
89 try {
90 port = Integer.parseInt(portStr);
91 }
92 catch(java.lang.NumberFormatException e) {
93 e.printStackTrace();
94 usage("Could not interpret port number ["+ portStr +"].");
95 }
96 PropertyConfigurator.configure(configFile);
97 NDC.push("Server");
98 }
99 }