You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/10/25 14:56:51 UTC

svn commit: r467628 - in /incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging: ErrorManager.java FileHandler.java Formatter.java Handler.java LogManager.java Logger.java SimpleFormatter.java XMLFormatter.java

Author: pyang
Date: Wed Oct 25 05:56:50 2006
New Revision: 467628

URL: http://svn.apache.org/viewvc?view=rev&rev=467628
Log:
Fix potential deadlocks, and other trivial compiler warnings

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/ErrorManager.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/FileHandler.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Formatter.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Handler.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/SimpleFormatter.java
    incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/ErrorManager.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/ErrorManager.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/ErrorManager.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/ErrorManager.java Wed Oct 25 05:56:50 2006
@@ -100,18 +100,17 @@
             if (called) {
                 return;
             }
-            synchronized (System.err) {
-                System.err.println(this.getClass().getName()+": "+FAILURES[errorCode]); //$NON-NLS-1$
-                if (message != null) {
-                    //logging.1E=Error message - {0}
-                    System.err.println(Messages.getString("logging.1E", message)); //$NON-NLS-1$
-                }
-                if (exception != null) {
-                    //logging.1F=Exception - {0}
-                    System.err.println(Messages.getString("logging.1F", exception)); //$NON-NLS-1$
-                }
-            }
             called = true;
+        }
+        System.err.println(this.getClass().getName()
+            + ": " + FAILURES[errorCode]); //$NON-NLS-1$
+        if (message != null) {
+            // logging.1E=Error message - {0}
+            System.err.println(Messages.getString("logging.1E", message)); //$NON-NLS-1$
+        }
+        if (exception != null) {
+            // logging.1F=Exception - {0}
+            System.err.println(Messages.getString("logging.1F", exception)); //$NON-NLS-1$
         }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/FileHandler.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/FileHandler.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/FileHandler.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/FileHandler.java Wed Oct 25 05:56:50 2006
@@ -201,7 +201,7 @@
             //FIXME: improve performance here
             for (int generation = 0; generation < count; generation++) {
                 //cache all file names for rotation use
-                files[generation] = new File(parseFileName(generation, uniqueID));
+                files[generation] = new File(parseFileName(generation));
             }
             fileName = files[0].getAbsolutePath();
             synchronized (allLocks) {
@@ -264,7 +264,7 @@
         files = new File[count];
     }
 
-    private void findNextGeneration() {
+    void findNextGeneration() {
         super.close();
         for (int i = count - 1; i > 0; i--) {
             if (files[i].exists()) {
@@ -289,10 +289,9 @@
      *  present
      *
      *  @param gen generation of this file
-     *  @param uniqueID distinguishing id of this file
      *  @return transformed filename ready for use
      */
-    private String parseFileName(int gen, int uniqueID) {
+    private String parseFileName(int gen) {
         int cur = 0;
         int next = 0;
         boolean hasUniqueID = false;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Formatter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Formatter.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Formatter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Formatter.java Wed Oct 25 05:56:50 2006
@@ -111,6 +111,7 @@
      *            the target handler
      * @return the head string used to wrap a set of log records
      */
+    @SuppressWarnings("unused")
     public String getHead(Handler h) {
         return ""; //$NON-NLS-1$
     }
@@ -123,6 +124,7 @@
      *            the target handler
      * @return the tail string used to wrap a set of log records
      */
+    @SuppressWarnings("unused")
     public String getTail(Handler h) {
         return ""; //$NON-NLS-1$
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Handler.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Handler.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Handler.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Handler.java Wed Oct 25 05:56:50 2006
@@ -133,6 +133,7 @@
      * init the common properties, including filter, level, formatter, and
      * encoding
      */
+    @SuppressWarnings("unused")
     void initProperties(String defaultLevel, String defaultFilter,
             String defaultFormatter, String defaultEncoding) {
         LogManager manager = LogManager.getLogManager();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/LogManager.java Wed Oct 25 05:56:50 2006
@@ -148,7 +148,7 @@
             "control", null); //$NON-NLS-1$
 
     // the singleton instance
-    private static LogManager manager;
+    static LogManager manager;
     
     /**
      * <p>The String value of the {@link LoggingMXBean}'s ObjectName.</p>
@@ -164,6 +164,7 @@
      * Instance variables
      * -------------------------------------------------------------------
      */
+    //FIXME: use weak reference to avoid heap memory leak    
     private Hashtable<String, Logger> loggers;
 
     // the configuration properties
@@ -405,9 +406,11 @@
                 input = new BufferedInputStream(new FileInputStream(configFile));
                 readConfigurationImpl(input);
             } finally {
-                try {
-                    input.close();
-                } catch (Exception e) {// ignore
+                if(input != null){
+                    try {
+                        input.close();
+                    } catch (Exception e) {// ignore
+                    }
                 }
             }
         }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/Logger.java Wed Oct 25 05:56:50 2006
@@ -346,18 +346,21 @@
     private static Logger getLoggerWithRes(String name,
             String resourceBundleName, boolean hasResourceName) {
         LogManager man = LogManager.getLogManager();
+        Logger l = null;
         synchronized (man) {
             // Try to find an existing logger with the specified name
-            Logger l = man.getLogger(name);
+            l = man.getLogger(name);
             // If no existing logger with the same name, create a new one
             if (null == l) {
                 l = new Logger(name, resourceBundleName);
                 man.addLogger(l);
-            } else if (hasResourceName) {
-                updateResourceBundle(l, resourceBundleName);
+                return l;
             }
-            return l;
         }
+        if (hasResourceName) {
+            updateResourceBundle(l, resourceBundleName);
+        }
+        return l;
     }
 
     /**
@@ -399,7 +402,7 @@
      *             If a security manager determines that the caller does not
      *             have the required permission.
      */
-    public synchronized void addHandler(Handler handler) {
+    public void addHandler(Handler handler) {
         if (null == handler) {
             // logging.A=The 'handler' parameter is null.
             throw new NullPointerException(Messages.getString("logging.A")); //$NON-NLS-1$
@@ -409,33 +412,49 @@
             LogManager.getLogManager().checkAccess();
         }
         initHandler();
-        this.handlers.add(handler);
+        synchronized(this){
+            this.handlers.add(handler);
+        }
     }
     
+    /*
+     * Be cautious to avoid deadlock when using this method, it gets lock on manager 
+     * at first, and then gets lock on this Logger, so any methods should not hold 
+     * lock on this Logger when invoking this method. 
+     */
     private void initHandler() {
-      if(!handlerInited){
-          handlerInited = true;
-          if(handlers == null){
-              handlers = new ArrayList<Handler>();
-          }
-          if(manager == null){
-              return;
-          }
-          String handlerStr = manager.getProperty("".equals(name)?"handlers":name+".handlers"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-          if (null == handlerStr) {
-              return;
-          }
-          StringTokenizer st = new StringTokenizer(handlerStr, " "); //$NON-NLS-1$
-          while (st.hasMoreTokens()) {
-              String handlerName = st.nextToken();
-              Handler handler = (Handler)LogManager.getInstanceByClass(handlerName);
-              handlers.add(handler);
-              String level = manager.getProperty(handlerName + ".level"); //$NON-NLS-1$
-              if (null != level) {
-                  handler.setLevel(Level.parse(level));
-              }
-          }
-      }
+        if(!handlerInited){
+            synchronized(LogManager.getLogManager()){
+                synchronized (this) {
+                    if (!handlerInited) {
+                        if (handlers == null) {
+                            handlers = new ArrayList<Handler>();
+                        }
+                        if (manager == null) {
+                            return;
+                        }
+                        
+                        String handlerStr = manager
+                                .getProperty("".equals(name) ? "handlers" : name + ".handlers"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+                        if (null == handlerStr) {
+                            return;
+                        }
+                        StringTokenizer st = new StringTokenizer(handlerStr, " "); //$NON-NLS-1$
+                        while (st.hasMoreTokens()) {
+                            String handlerName = st.nextToken();
+                            Handler handler = (Handler) LogManager
+                                    .getInstanceByClass(handlerName);
+                            handlers.add(handler);
+                            String level = manager.getProperty(handlerName + ".level"); //$NON-NLS-1$
+                            if (null != level) {
+                                handler.setLevel(Level.parse(level));
+                            }
+                        }
+                        handlerInited = true;
+                    }
+                }
+            }
+        }
     }
 
     /**
@@ -443,9 +462,11 @@
      * 
      * @return an array of all the handlers associated with this logger
      */
-    public synchronized Handler[] getHandlers() {
+    public Handler[] getHandlers() {
         initHandler();
-        return handlers.toArray(new Handler[handlers.size()]);
+        synchronized(this){
+            return handlers.toArray(new Handler[handlers.size()]);
+        }
     }
 
     /**
@@ -458,7 +479,7 @@
      *             If a security manager determines that the caller does not
      *             have the required permission.
      */
-    public synchronized void removeHandler(Handler handler) {
+    public void removeHandler(Handler handler) {
         // Anonymous loggers can always remove handlers
         if (this.isNamed) {
             LogManager.getLogManager().checkAccess();
@@ -467,7 +488,9 @@
             return;
         }
         initHandler();
-        this.handlers.remove(handler);
+        synchronized(this){
+            this.handlers.remove(handler);
+        }
     }
 
     /**
@@ -488,12 +511,14 @@
      *             If a security manager determines that the caller does not
      *             have the required permission.
      */
-    public synchronized void setFilter(Filter newFilter) {
+    public void setFilter(Filter newFilter) {
         // Anonymous loggers can always set the filter
         if (this.isNamed) {
             LogManager.getLogManager().checkAccess();
         }
-        this.filter = newFilter;
+        synchronized(this){
+            this.filter = newFilter;
+        }
     }
 
     /**
@@ -1051,7 +1076,7 @@
      * @param record
      *            the log record to be logged
      */
-    public synchronized void log(LogRecord record) {
+    public void log(LogRecord record) {
         /*
          * This method is synchronized so that all other log methods are
          * synchronized because they all call log(LogRecord) to actually perform
@@ -1069,29 +1094,33 @@
          * approaches are ugly but might be more efficient.
          */
         if (internalIsLoggable(record.getLevel())) {
-            // apply the filter if any
-            if (null != this.filter) {
-                if (!this.filter.isLoggable(record)) {
-                    return;
+            synchronized(this){
+                // apply the filter if any
+                if (null != this.filter) {
+                    if (!this.filter.isLoggable(record)) {
+                        return;
+                    }
                 }
             }
+            initHandler();
             /*
              * call the handlers of this logger, throw any exception that
              * occurs
              */
-            initHandler();
-            for (Handler element : handlers) {
-                element.publish(record);
-            }
-            // call the parent's handlers if set useParentHandlers
-            Logger temp = this;
-            while (temp.parent != null && temp.getUseParentHandlers()) {
-                Logger anyParent = temp.parent;
-                Handler[] ha = anyParent.getHandlers();
-                for (Handler element : ha) {
+            synchronized(this){
+                for (Handler element : handlers) {
                     element.publish(record);
                 }
-                temp = anyParent;   
+                // call the parent's handlers if set useParentHandlers
+                Logger temp = this;
+                while (temp.parent != null && temp.getUseParentHandlers()) {
+                    Logger anyParent = temp.parent;
+                    Handler[] ha = anyParent.getHandlers();
+                    for (Handler element : ha) {
+                        element.publish(record);
+                    }
+                    temp = anyParent;   
+                }
             }
         }
     }
@@ -1359,7 +1388,7 @@
     /*
      * This security manager is used to access the class context.
      */
-    private static class PrivateSecurityManager extends SecurityManager {
+    static class PrivateSecurityManager extends SecurityManager {
         public Class[] privateGetClassContext() {
             return super.getClassContext();
         }
@@ -1381,7 +1410,7 @@
         }        
     }
 
-    void reset() {
+    synchronized void reset() {
         levelObjVal = null;
         levelIntVal = Level.INFO.intValue();
         if(handlers != null){

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/SimpleFormatter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/SimpleFormatter.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/SimpleFormatter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/SimpleFormatter.java Wed Oct 25 05:56:50 2006
@@ -56,10 +56,12 @@
                 t.printStackTrace(pw);
                 sb.append(sw.toString());
             } finally {
-                try {
-                    pw.close();
-                } catch (Exception e) {
-                    // ignore
+                if(pw != null){
+                    try {
+                        pw.close();
+                    } catch (Exception e) {
+                        // ignore
+                    }
                 }
             }
         }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java?view=diff&rev=467628&r1=467627&r2=467628
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java Wed Oct 25 05:56:50 2006
@@ -190,6 +190,7 @@
      * @return the tail string for XML
      */
     @Override
+    @SuppressWarnings("unused")
     public String getTail(Handler h) {
         return "</log>"; //$NON-NLS-1$
     }