Revision: 8471
Author: [email protected]
Date: Thu Aug  5 19:03:31 2010
Log: Split LogManager class into an set of Impl classes so that any LogManager calls
in code will compile out cleanly when logging is disabled

Review at http://gwt-code-reviews.appspot.com/638802

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8471

Added:
 /trunk/user/src/com/google/gwt/logging/impl/LogManagerImpl.java
 /trunk/user/src/com/google/gwt/logging/impl/LogManagerImplNull.java
 /trunk/user/src/com/google/gwt/logging/impl/LogManagerImplRegular.java
Modified:
 /trunk/user/src/com/google/gwt/logging/LogImpl.gwt.xml
 /trunk/user/super/com/google/gwt/emul/java/util/logging/LogManager.java

=======================================
--- /dev/null
+++ /trunk/user/src/com/google/gwt/logging/impl/LogManagerImpl.java Thu Aug 5 19:03:31 2010
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package com.google.gwt.logging.impl;
+
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+/**
+ * Interface for the implementation of LogManager. We use a LogManagerImplNull
+ * to ensure that logging code compiles out when logging is disabled, and a
+ * LogManagerImplRegular to provide normal functionality when logging is enabled
+ */
+public interface LogManagerImpl {
+  boolean addLogger(Logger logger);
+  Logger getLogger(String name);
+  LogManager getLogManager();
+}
=======================================
--- /dev/null
+++ /trunk/user/src/com/google/gwt/logging/impl/LogManagerImplNull.java Thu Aug 5 19:03:31 2010
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package com.google.gwt.logging.impl;
+
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+/**
+ * Implementation for the LogManager class when logging is disabled.
+ */
+public class LogManagerImplNull {
+
+  boolean addLogger(Logger logger) {
+    return false;
+  }
+
+  Logger getLogger(String name) {
+    return null;
+  }
+
+  LogManager getLogManager() {
+    return null;
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/src/com/google/gwt/logging/impl/LogManagerImplRegular.java Thu Aug 5 19:03:31 2010
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package com.google.gwt.logging.impl;
+
+import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+/**
+ * Implementation for the LogManager class when logging is enabled.
+ */
+public class LogManagerImplRegular {
+
+  /**
+ * Since the Impl class is in a different package than the LogManager class,
+   * we need to work around the fact that the Impl class cannot access the
+   * protected constructor.
+   */
+ private static class LogManagerWithExposedConstructor extends LogManager {
+    public LogManagerWithExposedConstructor() {
+      super();
+    }
+  }
+
+  /**
+ * Since the Logger constructor is protected, the LogManager cannot create + * one directly, so we create a RootLogger which has an exposed constructor.
+   */
+  private class RootLogger extends Logger {
+    public RootLogger() {
+      super("", null);
+      setLevel(Level.ALL);
+    }
+  }
+
+  private static LogManager singleton;
+
+  public static LogManager getLogManager() {
+    if (singleton == null) {
+      singleton = new LogManagerWithExposedConstructor();
+    }
+    return singleton;
+  }
+
+  private HashMap<String, Logger> loggerList;
+  private Logger rootLogger;
+
+  public LogManagerImplRegular() {
+    loggerList = new HashMap<String, Logger>();
+    rootLogger = new RootLogger();
+    loggerList.put("", rootLogger);
+  }
+
+  public boolean addLogger(Logger logger) {
+    if (getLogger(logger.getName()) != null) {
+      return false;
+    }
+    addLoggerWithoutDuplicationChecking(logger);
+    return true;
+  }
+
+  public Logger getLogger(String name) {
+    return loggerList.get(name);
+  }
+
+  /**
+ * Helper function to add a logger when we have already determined that it
+   *  does not exist.  When we add a logger, we recursively add all of it's
+ * ancestors. Since loggers do not get removed, logger creation is cheap,
+   *  and there are not usually too many loggers in an ancestry chain,
+ * this is a simple way to ensure that the parent/child relationships are
+   *  always correctly set up.
+   */
+  private void addLoggerWithoutDuplicationChecking(Logger logger) {
+    String name = logger.getName();
+ String parentName = name.substring(0, Math.max(0, name.lastIndexOf('.')));
+    Logger parent = getOrAddLogger(parentName);
+    loggerList.put(logger.getName(), logger);
+    logger.setParent(parent);
+  }
+
+  /**
+ * Helper function to create a logger if it does not exist since the public + * APIs for getLogger and addLogger make it difficult to use those functions
+   *  for this.
+   */
+  private Logger getOrAddLogger(String name) {
+    Logger logger = getLogger(name);
+    if (logger == null) {
+      Logger newLogger = new LoggerWithExposedConstructor(name);
+      addLoggerWithoutDuplicationChecking(newLogger);
+      return newLogger;
+    }
+    return logger;
+  }
+}
=======================================
--- /trunk/user/src/com/google/gwt/logging/LogImpl.gwt.xml Tue Jun 15 15:09:25 2010 +++ /trunk/user/src/com/google/gwt/logging/LogImpl.gwt.xml Thu Aug 5 19:03:31 2010
@@ -3,10 +3,15 @@

   <!-- Set up and handle the gwt.logging property -->
   <define-property name="gwt.logging.enabled" values="TRUE, FALSE" />
+
   <replace-with class="com.google.gwt.logging.impl.LoggerImplRegular">
     <when-type-is class="com.google.gwt.logging.impl.LoggerImplNull"/>
     <when-property-is name="gwt.logging.enabled" value="TRUE" />
   </replace-with>
+  <replace-with class="com.google.gwt.logging.impl.LogManagerImplRegular">
+    <when-type-is class="com.google.gwt.logging.impl.LogManagerImplNull"/>
+    <when-property-is name="gwt.logging.enabled" value="TRUE" />
+  </replace-with>
   <replace-with class="com.google.gwt.logging.impl.LevelImplRegular">
     <when-type-is class="com.google.gwt.logging.impl.LevelImplNull"/>
     <when-property-is name="gwt.logging.enabled" value="TRUE" />
=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/logging/LogManager.java Tue Jun 15 15:09:25 2010 +++ /trunk/user/super/com/google/gwt/emul/java/util/logging/LogManager.java Thu Aug 5 19:03:31 2010
@@ -16,7 +16,9 @@

 package java.util.logging;

-import java.util.HashMap;
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.logging.impl.LogManagerImpl;
+import com.google.gwt.logging.impl.LogManagerImplNull;

 /**
  *  An emulation of the java.util.logging.LogManager class. See
@@ -24,76 +26,25 @@
  *  The Java API doc for details</a>
  */
 public class LogManager {
-  /**
- * Since the Logger constructor is protected, the LogManager cannot create - * one directly, so we create a RootLogger which has an exposed constructor.
-   */
-  private class RootLogger extends Logger {
-    public RootLogger() {
-      super("", null);
-      setLevel(Level.ALL);
-    }
-  }
-
-  private static LogManager singleton;
-
+  private static LogManagerImpl staticImpl =
+    GWT.create(LogManagerImplNull.class);
+
   public static LogManager getLogManager() {
-    if (singleton == null) {
-      singleton = new LogManager();
-    }
-    return singleton;
+    return staticImpl.getLogManager();
   }

-  private HashMap<String, Logger> loggerList;
-  private Logger rootLogger;
+  private LogManagerImpl impl;

   protected LogManager() {
-    loggerList = new HashMap<String, Logger>();
-    rootLogger = new RootLogger();
-    loggerList.put("", rootLogger);
+    impl = GWT.create(LogManagerImplNull.class);
   }

   public boolean addLogger(Logger logger) {
-    if (getLogger(logger.getName()) != null) {
-      return false;
-    }
-    addLoggerWithoutDuplicationChecking(logger);
-    return true;
+    return impl.addLogger(logger);
   }

   public Logger getLogger(String name) {
-    return loggerList.get(name);
-  }
-
-  /**
- * Helper function to add a logger when we have already determined that it
-   *  does not exist.  When we add a logger, we recursively add all of it's
- * ancestors. Since loggers do not get removed, logger creation is cheap,
-   *  and there are not usually too many loggers in an ancestry chain,
- * this is a simple way to ensure that the parent/child relationships are
-   *  always correctly set up.
-   */
-  private void addLoggerWithoutDuplicationChecking(Logger logger) {
-    String name = logger.getName();
- String parentName = name.substring(0, Math.max(0, name.lastIndexOf('.')));
-    Logger parent = getOrAddLogger(parentName);
-    loggerList.put(logger.getName(), logger);
-    logger.setParent(parent);
-  }
-
-  /**
- * Helper function to create a logger if it does not exist since the public - * APIs for getLogger and addLogger make it difficult to use those functions
-   *  for this.
-   */
-  private Logger getOrAddLogger(String name) {
-    Logger logger = getLogger(name);
-    if (logger == null) {
-      Logger newLogger = new Logger(name, null);
-      addLoggerWithoutDuplicationChecking(newLogger);
-      return newLogger;
-    }
-    return logger;
+    return impl.getLogger(name);
   }

   /* Not Implemented */

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to