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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.log4j.net;
20 import java.net.ServerSocket;
21 import java.net.Socket;
23 import org.apache.log4j.LogManager;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.PropertyConfigurator;
26 import org.apache.log4j.xml.DOMConfigurator;
30 * A simple {@link SocketNode} based server.
33 <b>Usage:</b> java org.apache.log4j.net.SimpleSocketServer port configFile
35 where <em>port</em> is a port number where the server listens and
36 <em>configFile</em> is a configuration file fed to the {@link
37 PropertyConfigurator} or to {@link DOMConfigurator} if an XML file.
40 * @author Ceki Gülcü
44 public class SimpleSocketServer {
46 static Logger cat = Logger.getLogger(SimpleSocketServer.class);
52 void main(String argv[]) {
53 if(argv.length == 2) {
54 init(argv[0], argv[1]);
56 usage("Wrong number of arguments.");
60 cat.info("Listening on port " + port);
61 ServerSocket serverSocket = new ServerSocket(port);
63 cat.info("Waiting to accept a new client.");
64 Socket socket = serverSocket.accept();
65 cat.info("Connected to client at " + socket.getInetAddress());
66 cat.info("Starting new socket node.");
67 new Thread(new SocketNode(socket,
68 LogManager.getLoggerRepository()),"SimpleSocketServer-" + port).start();
70 } catch(Exception e) {
76 static void usage(String msg) {
77 System.err.println(msg);
79 "Usage: java " +SimpleSocketServer.class.getName() + " port configFile");
83 static void init(String portStr, String configFile) {
85 port = Integer.parseInt(portStr);
86 } catch(java.lang.NumberFormatException e) {
88 usage("Could not interpret port number ["+ portStr +"].");
91 if(configFile.endsWith(".xml")) {
92 DOMConfigurator.configure(configFile);
94 PropertyConfigurator.configure(configFile);