JAL-3071 IdPanel.ScrollThread for JalviewJS, Javadoc
[jalview.git] / srcjar2 / org / apache / log4j / net / SimpleSocketServer.java
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 package org.apache.log4j.net;
19
20 import java.net.ServerSocket;
21 import java.net.Socket;
22
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;
27
28
29 /**
30  *  A simple {@link SocketNode} based server.
31  *
32    <pre>
33    <b>Usage:</b> java org.apache.log4j.net.SimpleSocketServer port configFile
34
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.
38    </pre>
39   *
40   * @author  Ceki G&uuml;lc&uuml;
41   *
42   *  @since 0.8.4 
43   * */
44 public class SimpleSocketServer  {
45
46   static Logger cat = Logger.getLogger(SimpleSocketServer.class);
47
48   static int port;
49
50   public
51   static
52   void main(String argv[]) {
53     if(argv.length == 2) {
54       init(argv[0], argv[1]);
55     } else {
56       usage("Wrong number of arguments.");
57     }
58     
59     try {
60       cat.info("Listening on port " + port);
61       ServerSocket serverSocket = new ServerSocket(port);
62       while(true) {
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();
69       }
70     } catch(Exception e) {
71       e.printStackTrace();
72     }
73   }
74
75
76   static void  usage(String msg) {
77     System.err.println(msg);
78     System.err.println(
79       "Usage: java " +SimpleSocketServer.class.getName() + " port configFile");
80     System.exit(1);
81   }
82
83   static void init(String portStr, String configFile) {
84     try {
85       port = Integer.parseInt(portStr);
86     } catch(java.lang.NumberFormatException e) {
87       e.printStackTrace();
88       usage("Could not interpret port number ["+ portStr +"].");
89     }
90    
91     if(configFile.endsWith(".xml")) {
92       DOMConfigurator.configure(configFile);
93     } else {
94       PropertyConfigurator.configure(configFile);
95     }
96   }
97 }