scripts/mir-setup/README: update with link to new doc on wiki
[mir.git] / source / mir / log / log4j / LoggerImpl.java
index d821f5c..6f3b5ad 100755 (executable)
+/*
+ * Copyright (C) 2005 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License.
+ * You must obey the GNU General Public License in all respects for all of the code used
+ * other than the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
 package mir.log.log4j;
 
-import org.apache.log4j.PropertyConfigurator;
-import org.apache.log4j.Logger;
-
-import java.util.Map;
 import java.util.HashMap;
+import java.util.Map;
 
-import mir.misc.MirConfig;
-
+import mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerExc;
+import mir.log.LoggerFailure;
 
-public class LoggerImpl implements mir.log.Logger {
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
 
-    private static Map loggers = new HashMap();
 
-    public LoggerImpl() {
-      System.setProperty("log.home", MirConfig.getPropWithHome("Log.Home"));
-        PropertyConfigurator.configure(MirConfig.getPropWithHome("Log.log4j.ConfigurationFile").trim());
+public class LoggerImpl implements mir.log.Logger {
+  private static final Map loggers = new HashMap();
+
+  public LoggerImpl() throws LoggerExc {
+    reload();
+  }
+
+  /** {@inheritDoc} */
+  public void debug(Object o, String s) {
+    this.getLogger(o).debug(s);
+  }
+
+  /** {@inheritDoc} */
+  public void info(Object o, String s) {
+    this.getLogger(o).info(s);
+  }
+
+  /** {@inheritDoc} */
+  public void warn(Object o, String s) {
+    this.getLogger(o).warn(s);
+  }
+
+  /** {@inheritDoc} */
+  public void warn(Object o, String s, Throwable anException) {
+    this.getLogger(o).warn(s, anException);
+  }
+
+  /** {@inheritDoc} */
+  public void error(Object o, String s) {
+    this.getLogger(o).error(s);
+  }
+
+  /** {@inheritDoc} */
+  public void error(Object o, String s, Throwable anException) {
+    this.getLogger(o).error(s, anException);
+  }
+
+  /** {@inheritDoc} */
+  public void fatal(Object o, String s) {
+    this.getLogger(o).fatal(s);
+  }
+
+  /** {@inheritDoc} */
+  public void reload() throws LoggerExc, LoggerFailure {
+    try {
+      synchronized (loggers) {
+        System.setProperty("log.home",
+            MirPropertiesConfiguration.instance().getFile("Log.Home").getAbsolutePath());
+
+        PropertyConfigurator.configure(
+            MirPropertiesConfiguration.instance().getFile("Log.log4j.ConfigurationFile").getAbsolutePath());
+
+        loggers.clear();
+      }
     }
-
-
-    public void debug( Object o, String s ) {
-        this.getLogger(o).debug(s);
+    catch (Throwable t) {
+      throw new LoggerFailure(t);
     }
+  }
 
-    public void info( Object o, String s ) {
-        this.getLogger(o).info(s);
-    }
+  private Logger getLogger(Object o) {
+    String name;
+    Logger l;
 
-    public void warn( Object o, String s ) {
-        this.getLogger(o).warn(s);
+    if (o instanceof String) {
+      name = (String) o;
     }
-
-    public void error( Object o, String s ) {
-        this.getLogger(o).error(s);
+    else if (o instanceof Class) {
+      name = ( (Class) o).getName();
     }
-
-    public void fatal( Object o, String s ) {
-        this.getLogger(o).fatal(s);
+    else if (o != null) {
+      name = o.getClass().getName();
+    }
+    else {
+      name = "generic";
     }
 
-
-    private Logger getLogger( Object o ) {
-        String name;
-
-        if (o instanceof String) {
-            name = (String) o;
-        } else if (o instanceof Class) {
-            name = ((Class)o).getName();
-        } else if (o!=null) {
-            name = o.getClass().getName();
-        } else {
-            name = "generic";
-        }
-
-        Logger l = (Logger)loggers.get(name);
-        if (l==null) {
-            l = Logger.getLogger(name);
-            loggers.put(name, l);
+    synchronized (loggers) {
+      l = (Logger) loggers.get(name);
+      if (l == null) {
+        if (!loggers.containsKey(name)) {
+          l = Logger.getLogger(name);
+          loggers.put(name, l);
         }
-        return l;
+        l = (Logger) loggers.get(name);
+      }
     }
+
+    return l;
+  }
 }
\ No newline at end of file