import org.apache.log4j.*;
import java.util.Properties;
import java.util.Enumeration;
import java.io.PrintStream;
import java.net.URL;

public class propertyConfigurator_t {
    private static final boolean debug = true;

    public propertyConfigurator_t() {
        // load the properties file
        ClassLoader cl = propertyConfigurator_t.class.getClassLoader();
        Properties props = new Properties();
        URL resourceURL = null;
        try {
            resourceURL = cl.getResource("application.properties");
            if (resourceURL == null)
                System.out.println("file application.properties not found");
            else
                props.load(resourceURL.openStream());
        }
        catch (Exception ex) {System.out.println(ex.toString());}

        String patternStr = ": %m";
        // initialize the log4j logging to the Console
        ConsoleAppender consAppend = 
            new ConsoleAppender(new PatternLayout(": %m%n"));
        consAppend.setName("consoleAppend");
        consAppend.setThreshold(Priority.DEBUG);
        Category.getRoot().addAppender(consAppend);
	if (debug)
          listRootAppenders();

        // add the log4j instructions contained in the properties file 
        // loaded from the jar file
	if (debug)
	  props.list(new PrintStream(System.out));
        PropertyConfigurator.configure(props);
	if (debug)
          listRootAppenders();

        Category.getRoot().info("Connected");
    }

    public void listRootAppenders() {
      int cnt = 0;
      Enumeration enum = Category.getRoot().getAllAppenders();
      for (; enum.hasMoreElements();)
      {
        System.out.println("  Name of appender is " 
               + ((AppenderSkeleton)(enum.nextElement())).getName());
        cnt++;
      }
      System.out.println("Root category number of appenders is " + cnt); 
    }

    public static void main(String args[])
    {
      new propertyConfigurator_t();
    }

} // propertyConfigurator_t
