You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2016/12/05 15:20:31 UTC

svn commit: r1772687 - /tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java

Author: markt
Date: Mon Dec  5 15:20:30 2016
New Revision: 1772687

URL: http://svn.apache.org/viewvc?rev=1772687&view=rev
Log:
Refactor. Reduce duplication identified by Simian.
No functional change.

Modified:
    tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java

Modified: tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java?rev=1772687&r1=1772686&r2=1772687&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java (original)
+++ tomcat/trunk/java/org/apache/catalina/mbeans/MBeanFactory.java Mon Dec  5 15:20:30 2016
@@ -27,6 +27,7 @@ import org.apache.catalina.Context;
 import org.apache.catalina.Engine;
 import org.apache.catalina.Host;
 import org.apache.catalina.JmxEnabled;
+import org.apache.catalina.Realm;
 import org.apache.catalina.Server;
 import org.apache.catalina.Service;
 import org.apache.catalina.Valve;
@@ -203,6 +204,7 @@ public class MBeanFactory {
         return createConnector(parent, address, port, true, false);
     }
 
+
     /**
      * Create a new DataSource Realm.
      *
@@ -230,20 +232,28 @@ public class MBeanFactory {
         realm.setUserTable(userTable);
 
         // Add the new instance to its parent component
+        return addRealmToParent(parent, realm);
+    }
+
+
+    private String addRealmToParent(String parent, Realm realm) throws Exception {
         ObjectName pname = new ObjectName(parent);
         Container container = getParentContainerFromParent(pname);
         // Add the new instance to its parent component
         container.setRealm(realm);
         // Return the corresponding MBean name
-        ObjectName oname = realm.getObjectName();
+        ObjectName oname = null;
+        if (realm instanceof JmxEnabled) {
+            oname = ((JmxEnabled) realm).getObjectName();
+        }
         if (oname != null) {
             return (oname.toString());
         } else {
             return null;
         }
-
     }
 
+
     /**
      * Create a new HttpConnector
      *
@@ -259,6 +269,7 @@ public class MBeanFactory {
         return createConnector(parent, address, port, false, false);
     }
 
+
     /**
      * Create a new Connector
      *
@@ -311,6 +322,7 @@ public class MBeanFactory {
         return createConnector(parent, address, port, false, true);
     }
 
+
     /**
      * Create a new JDBC Realm.
      *
@@ -335,19 +347,7 @@ public class MBeanFactory {
         realm.setConnectionURL(connectionURL);
 
         // Add the new instance to its parent component
-        ObjectName pname = new ObjectName(parent);
-        Container container = getParentContainerFromParent(pname);
-        // Add the new instance to its parent component
-        container.setRealm(realm);
-        // Return the corresponding MBean name
-        ObjectName oname = realm.getObjectName();
-
-        if (oname != null) {
-            return (oname.toString());
-        } else {
-            return null;
-        }
-
+        return addRealmToParent(parent, realm);
     }
 
 
@@ -359,27 +359,13 @@ public class MBeanFactory {
      *
      * @exception Exception if an MBean cannot be created or registered
      */
-    public String createJNDIRealm(String parent)
-        throws Exception {
+    public String createJNDIRealm(String parent) throws Exception {
 
          // Create a new JNDIRealm instance
         JNDIRealm realm = new JNDIRealm();
 
         // Add the new instance to its parent component
-        ObjectName pname = new ObjectName(parent);
-        Container container = getParentContainerFromParent(pname);
-        // Add the new instance to its parent component
-        container.setRealm(realm);
-        // Return the corresponding MBean name
-        ObjectName oname = realm.getObjectName();
-
-        if (oname != null) {
-            return (oname.toString());
-        } else {
-            return null;
-        }
-
-
+        return addRealmToParent(parent, realm);
     }
 
 
@@ -391,25 +377,13 @@ public class MBeanFactory {
      *
      * @exception Exception if an MBean cannot be created or registered
      */
-    public String createMemoryRealm(String parent)
-        throws Exception {
+    public String createMemoryRealm(String parent) throws Exception {
 
          // Create a new MemoryRealm instance
         MemoryRealm realm = new MemoryRealm();
 
         // Add the new instance to its parent component
-        ObjectName pname = new ObjectName(parent);
-        Container container = getParentContainerFromParent(pname);
-        // Add the new instance to its parent component
-        container.setRealm(realm);
-        // Return the corresponding MBean name
-        ObjectName oname = realm.getObjectName();
-        if (oname != null) {
-            return (oname.toString());
-        } else {
-            return null;
-        }
-
+        return addRealmToParent(parent, realm);
     }
 
 
@@ -628,21 +602,7 @@ public class MBeanFactory {
         realm.setResourceName(resourceName);
 
         // Add the new instance to its parent component
-        ObjectName pname = new ObjectName(parent);
-        Container container = getParentContainerFromParent(pname);
-        // Add the new instance to its parent component
-        container.setRealm(realm);
-        // Return the corresponding MBean name
-        ObjectName oname = realm.getObjectName();
-        // FIXME getObjectName() returns null
-        //ObjectName oname =
-        //    MBeanUtils.createObjectName(pname.getDomain(), realm);
-        if (oname != null) {
-            return (oname.toString());
-        } else {
-            return null;
-        }
-
+        return addRealmToParent(parent, realm);
     }
 
 



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