KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > net > SimpleSocketServer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.net;
18
19 import java.net.Socket JavaDoc;
20 import java.net.ServerSocket JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.LogManager;
24 import org.apache.log4j.PropertyConfigurator;
25 import org.apache.log4j.xml.DOMConfigurator;
26
27
28 /**
29  * A simple {@link SocketNode} based server.
30  *
31    <pre>
32    <b>Usage:</b> java org.apache.log4j.net.SimpleSocketServer port configFile
33
34    where <em>port</em> is a part number where the server listens and
35    <em>configFile</em> is a configuration file fed to the {@link
36    PropertyConfigurator} or to {@link DOMConfigurator} if an XML file.
37    </pre>
38   *
39   * @author Ceki G&uuml;lc&uuml;
40   *
41   * @since 0.8.4
42   * */

43 public class SimpleSocketServer {
44
45   static Logger cat = Logger.getLogger(SimpleSocketServer.class);
46
47   static int port;
48
49   public
50   static
51   void main(String JavaDoc argv[]) {
52     if(argv.length == 2) {
53       init(argv[0], argv[1]);
54     } else {
55       usage("Wrong number of arguments.");
56     }
57     
58     try {
59       cat.info("Listening on port " + port);
60       ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(port);
61       while(true) {
62     cat.info("Waiting to accept a new client.");
63     Socket JavaDoc socket = serverSocket.accept();
64     cat.info("Connected to client at " + socket.getInetAddress());
65     cat.info("Starting new socket node.");
66     new Thread JavaDoc(new SocketNode(socket,
67                   LogManager.getLoggerRepository())).start();
68       }
69     } catch(Exception JavaDoc e) {
70       e.printStackTrace();
71     }
72   }
73
74
75   static void usage(String JavaDoc msg) {
76     System.err.println(msg);
77     System.err.println(
78       "Usage: java " +SimpleSocketServer.class.getName() + " port configFile");
79     System.exit(1);
80   }
81
82   static void init(String JavaDoc portStr, String JavaDoc configFile) {
83     try {
84       port = Integer.parseInt(portStr);
85     } catch(java.lang.NumberFormatException JavaDoc e) {
86       e.printStackTrace();
87       usage("Could not interpret port number ["+ portStr +"].");
88     }
89    
90     if(configFile.endsWith(".xml")) {
91       new DOMConfigurator().configure(configFile);
92     } else {
93       new PropertyConfigurator().configure(configFile);
94     }
95   }
96 }
97
Popular Tags