Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
A SocketNode based server that uses a different hierarchy for each client.
Usage: java port configFile configDir

where port is a part number where the server listens,
configFile is a configuration file fed to the org.apache.log4j.PropertyConfigurator and
configDir is a path to a directory containing configuration files, possibly one for each client host.

The configFile is used to configure the log4j default hierarchy that the SocketServer will use to report on its actions.

When a new connection is opened from a previously unknown host, say, then the SocketServer will search for a configuration file called under the directory configDir that was passed as the third argument. If the file can be found, then a new hierarchy is instantiated and configured using the configuration file If and when the host opens another connection to the server, then the previously configured hierarchy is used.

In case there is no file called under the directory configDir, then the generic hierarchy is used. The generic hierarchy is configured using a configuration file called generic.lcf under the configDir directory. If no such file exists, then the generic hierarchy will be identical to the log4j default hierarchy.

Having different client hosts log using different hierarchies ensures the total independence of the clients with respect to their logging settings.

Currently, the hierarchy that will be used for a given request depends on the IP address of the client host. For example, two separate applicatons running on the same host and logging to the same server will share the same hierarchy. This is perfectly safe except that it might not provide the right amount of independence between applications. The SocketServer is intended as an example to be enhanced in order to implement more elaborate policies.

Ceki Gülcü
 public class SocketServer  {
   static String GENERIC = "generic";
   static String CONFIG_FILE_EXT = ".lcf";
   static Logger cat = Logger.getLogger(SocketServer.class);
   static SocketServer server;
   static int port;
   // key=inetAddress, value=hierarchy
  File dir;
  void main(String argv[]) {
    if(argv.length == 3)
      init(argv[0], argv[1], argv[2]);
      usage("Wrong number of arguments.");
    try {
      .info("Listening on port " + );
      ServerSocket serverSocket = new ServerSocket();
      while(true) {
	.info("Waiting to accept a new client.");
	Socket socket = serverSocket.accept();
	InetAddress inetAddress =  socket.getInetAddress();
	.info("Connected to client at " + inetAddress);
	if(h == null) {
	  h = .configureHierarchy(inetAddress);
	.info("Starting new socket node.");
	new Thread(new SocketNode(socketh)).start();
    catch(Exception e) {
  void  usage(String msg) {
      "Usage: java " +SocketServer.class.getName() + " port configFile directory");
  void init(String portStrString configFileString dirStr) {
    try {
       = Integer.parseInt(portStr);
    catch(java.lang.NumberFormatException e) {
      usage("Could not interpret port number ["portStr +"].");
    File dir = new File(dirStr);
    if(!dir.isDirectory()) {
      usage("["+dirStr+"] is not a directory.");
     = new SocketServer(dir);
  SocketServer(File directory) {
    this. = directory;
     = new Hashtable(11);
  // This method assumes that there is no hiearchy for inetAddress
  // yet. It will configure one and return it.
    .info("Locating configuration file for "+inetAddress);
    // We assume that the toSting method of InetAddress returns is in
    // the format hostname/d1.d2.d3.d4 e.g. torino/
    String s = inetAddress.toString();
    int i = s.indexOf("/");
    if(i == -1) {
      .warn("Could not parse the inetAddress ["+inetAddress+
	       "]. Using default hierarchy.");
      return genericHierarchy();
    } else {
      String key = s.substring(0, i);
      File configFile = new File(key+);
      if(configFile.exists()) {
	return h;
      } else {
	.warn("Could not find config file ["+configFile+"].");
    if( == null) {
      File f = new File(+);
      if(f.exists()) {
      } else {
	.warn("Could not find config file ["+f+
		 "]. Will use the default hierarchy.");
    return ;
New to GrepCode? Check out our FAQ X