001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.log4j.net; 019 020import java.net.ServerSocket; 021import java.net.Socket; 022 023import org.apache.log4j.LogManager; 024import org.apache.log4j.Logger; 025import org.apache.log4j.PropertyConfigurator; 026import org.apache.log4j.xml.DOMConfigurator; 027 028 029/** 030 * A simple {@link SocketNode} based server. 031 * 032 <pre> 033 <b>Usage:</b> java org.apache.log4j.net.SimpleSocketServer port configFile 034 035 where <em>port</em> is a part number where the server listens and 036 <em>configFile</em> is a configuration file fed to the {@link 037 PropertyConfigurator} or to {@link DOMConfigurator} if an XML file. 038 </pre> 039 * 040 * @author Ceki Gülcü 041 * 042 * @since 0.8.4 043 * */ 044public class SimpleSocketServer { 045 046 static Logger cat = Logger.getLogger(SimpleSocketServer.class); 047 048 static int port; 049 050 public 051 static 052 void main(String argv[]) { 053 if(argv.length == 2) { 054 init(argv[0], argv[1]); 055 } else { 056 usage("Wrong number of arguments."); 057 } 058 059 try { 060 cat.info("Listening on port " + port); 061 ServerSocket serverSocket = new ServerSocket(port); 062 while(true) { 063 cat.info("Waiting to accept a new client."); 064 Socket socket = serverSocket.accept(); 065 cat.info("Connected to client at " + socket.getInetAddress()); 066 cat.info("Starting new socket node."); 067 new Thread(new SocketNode(socket, 068 LogManager.getLoggerRepository()),"SimpleSocketServer-" + port).start(); 069 } 070 } catch(Exception e) { 071 e.printStackTrace(); 072 } 073 } 074 075 076 static void usage(String msg) { 077 System.err.println(msg); 078 System.err.println( 079 "Usage: java " +SimpleSocketServer.class.getName() + " port configFile"); 080 System.exit(1); 081 } 082 083 static void init(String portStr, String configFile) { 084 try { 085 port = Integer.parseInt(portStr); 086 } catch(java.lang.NumberFormatException e) { 087 e.printStackTrace(); 088 usage("Could not interpret port number ["+ portStr +"]."); 089 } 090 091 if(configFile.endsWith(".xml")) { 092 DOMConfigurator.configure(configFile); 093 } else { 094 PropertyConfigurator.configure(configFile); 095 } 096 } 097}