Author: violetagg
Date: Sat Jan  2 20:36:49 2016
New Revision: 1722663

URL: http://svn.apache.org/viewvc?rev=1722663&view=rev
Log:
Fix a FindBugs issue (incorrect synchronization)

Modified:
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/modeler/ManagedBean.java

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/modeler/ManagedBean.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/modeler/ManagedBean.java?rev=1722663&r1=1722662&r2=1722663&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/modeler/ManagedBean.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/modeler/ManagedBean.java 
Sat Jan  2 20:36:49 2016
@@ -22,6 +22,9 @@ package org.apache.tomcat.util.modeler;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.management.AttributeNotFoundException;
 import javax.management.DynamicMBean;
@@ -53,6 +56,8 @@ public class ManagedBean implements java
     static final Class<?>[] NO_ARGS_PARAM_SIG = new Class[0];
 
 
+    private ReadWriteLock mBeanInfoLock = new ReentrantReadWriteLock();
+
     /**
      * The <code>ModelMBeanInfo</code> object that corresponds
      * to this <code>ManagedBean</code> instance.
@@ -112,8 +117,14 @@ public class ManagedBean implements java
     }
 
     public void setClassName(String className) {
-        this.className = className;
-        this.info = null;
+        Lock l = mBeanInfoLock.writeLock();
+        l.lock();
+        try {
+            this.className = className;
+            this.info = null;
+        } finally {
+            l.unlock();
+        }
     }
 
 
@@ -133,8 +144,14 @@ public class ManagedBean implements java
     }
 
     public void setDescription(String description) {
-        this.description = description;
-        this.info = null;
+        Lock l = mBeanInfoLock.writeLock();
+        l.lock();
+        try {
+            this.description = description;
+            this.info = null;
+        } finally {
+            l.unlock();
+        }
     }
 
 
@@ -182,8 +199,14 @@ public class ManagedBean implements java
     }
 
     public void setName(String name) {
-        this.name = name;
-        this.info = null;
+        Lock l = mBeanInfoLock.writeLock();
+        l.lock();
+        try {
+            this.name = name;
+            this.info = null;
+        } finally {
+            l.unlock();
+        }
     }
 
 
@@ -215,8 +238,14 @@ public class ManagedBean implements java
     }
 
     public void setType(String type) {
-        this.type = type;
-        this.info = null;
+        Lock l = mBeanInfoLock.writeLock();
+        l.lock();
+        try {
+            this.type = type;
+            this.info = null;
+        } finally {
+            l.unlock();
+        }
     }
 
 
@@ -270,7 +299,10 @@ public class ManagedBean implements java
      */
     public void addNotification(NotificationInfo notification) {
 
-        synchronized (notifications) {
+        Lock l = mBeanInfoLock.writeLock();
+
+        l.lock();
+        try {
             NotificationInfo results[] =
                 new NotificationInfo[notifications.length + 1];
             System.arraycopy(notifications, 0, results, 0,
@@ -278,8 +310,9 @@ public class ManagedBean implements java
             results[notifications.length] = notification;
             notifications = results;
             this.info = null;
+        } finally {
+            l.unlock();
         }
-
     }
 
 
@@ -398,21 +431,30 @@ public class ManagedBean implements java
     MBeanInfo getMBeanInfo() {
 
         // Return our cached information (if any)
-        if (info != null)
-            return (info);
+        Lock l = mBeanInfoLock.readLock();
+        l.lock();
+        try {
+            if (info != null)
+                return info;
+        } finally {
+            l.unlock();
+        }
 
-        // Create subordinate information descriptors as required
-        AttributeInfo attrs[] = getAttributes();
-        MBeanAttributeInfo attributes[] =
-            new MBeanAttributeInfo[attrs.length];
-        for (int i = 0; i < attrs.length; i++)
-            attributes[i] = attrs[i].createAttributeInfo();
-
-        OperationInfo opers[] = getOperations();
-        MBeanOperationInfo operations[] =
-            new MBeanOperationInfo[opers.length];
-        for (int i = 0; i < opers.length; i++)
-            operations[i] = opers[i].createOperationInfo();
+        l = mBeanInfoLock.writeLock();
+        l.lock();
+        try {
+            // Create subordinate information descriptors as required
+            AttributeInfo attrs[] = getAttributes();
+            MBeanAttributeInfo attributes[] =
+                new MBeanAttributeInfo[attrs.length];
+            for (int i = 0; i < attrs.length; i++)
+                attributes[i] = attrs[i].createAttributeInfo();
+
+            OperationInfo opers[] = getOperations();
+            MBeanOperationInfo operations[] =
+                new MBeanOperationInfo[opers.length];
+            for (int i = 0; i < opers.length; i++)
+                operations[i] = opers[i].createOperationInfo();
 
 
 //        ConstructorInfo consts[] = getConstructors();
@@ -420,21 +462,21 @@ public class ManagedBean implements java
 //            new ModelMBeanConstructorInfo[consts.length];
 //        for (int i = 0; i < consts.length; i++)
 //            constructors[i] = consts[i].createConstructorInfo();
-        
-        NotificationInfo notifs[] = getNotifications();
-        MBeanNotificationInfo notifications[] =
-            new MBeanNotificationInfo[notifs.length];
-        for (int i = 0; i < notifs.length; i++)
-            notifications[i] = notifs[i].createNotificationInfo();
 
-        
-        // Construct and return a new ModelMBeanInfo object
-        info = new MBeanInfo(getClassName(), 
-                             getDescription(),
-                             attributes, 
-                             new MBeanConstructorInfo[] {}, 
-                             operations, 
-                             notifications);
+            NotificationInfo notifs[] = getNotifications();
+            MBeanNotificationInfo notifications[] =
+                new MBeanNotificationInfo[notifs.length];
+            for (int i = 0; i < notifs.length; i++)
+                notifications[i] = notifs[i].createNotificationInfo();
+
+
+            // Construct and return a new ModelMBeanInfo object
+            info = new MBeanInfo(getClassName(), 
+                                 getDescription(),
+                                 attributes, 
+                                 new MBeanConstructorInfo[] {}, 
+                                 operations, 
+                                 notifications);
 //        try {
 //            Descriptor descriptor = info.getMBeanDescriptor();
 //            Iterator fields = getFields().iterator();
@@ -447,8 +489,10 @@ public class ManagedBean implements java
 //            ;
 //        }
 
-        return (info);
-
+            return info;
+        } finally {
+            l.unlock();
+        }
     }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to