Reduce JSR305 proliferation
[controller.git] / opendaylight / md-sal / sal-common-util / src / main / java / org / opendaylight / controller / md / sal / common / util / jmx / ThreadExecutorStatsMXBeanImpl.java
index b67855d7312c697b2a7a0c1049a3b628701f4e08..2323e2d541c26bddf72cbe6a0155c2e159df4aaf 100644 (file)
@@ -5,17 +5,19 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.md.sal.common.util.jmx;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Executor;
 import java.util.concurrent.RejectedExecutionHandler;
 import java.util.concurrent.ThreadPoolExecutor;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.util.concurrent.CountingRejectedExecutionHandler;
 import org.opendaylight.yangtools.util.concurrent.TrackingLinkedBlockingQueue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * MXBean implementation of the ThreadExecutorStatsMXBean interface that retrieves statistics
@@ -25,25 +27,78 @@ import org.opendaylight.yangtools.util.concurrent.TrackingLinkedBlockingQueue;
  */
 public class ThreadExecutorStatsMXBeanImpl extends AbstractMXBean
                                            implements ThreadExecutorStatsMXBean {
-
+    private static final Logger LOG = LoggerFactory.getLogger(ThreadExecutorStatsMXBeanImpl.class);
     private final ThreadPoolExecutor executor;
 
     /**
      * Constructs an instance for the given {@link Executor}.
      *
      * @param executor the backing {@link Executor}
-     * @param mBeanName Used as the <code>name</code> property in the bean's ObjectName.
-     * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
-     * @param mBeanCategory Used as the <code>Category</code> property in the bean's ObjectName.
+     * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
+     * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
+     * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
      */
-    public ThreadExecutorStatsMXBeanImpl(Executor executor, String mBeanName,
-            String mBeanType, @Nullable String mBeanCategory) {
-        super(mBeanName, mBeanType, mBeanCategory);
+    public ThreadExecutorStatsMXBeanImpl(final ThreadPoolExecutor executor, final String beanName,
+            final String beanType, final @Nullable String beanCategory) {
+        super(beanName, beanType, beanCategory);
+        this.executor = requireNonNull(executor);
+    }
+
+    private static ThreadExecutorStatsMXBeanImpl createInternal(final Executor executor,
+            final String beanName, final String beanType, final String beanCategory) {
+        if (executor instanceof ThreadPoolExecutor) {
+            final ThreadExecutorStatsMXBeanImpl ret = new ThreadExecutorStatsMXBeanImpl(
+                    (ThreadPoolExecutor) executor, beanName, beanType, beanCategory);
+            return ret;
+        }
+
+        LOG.info("Executor {} is not supported", executor);
+        return null;
+    }
 
-        Preconditions.checkArgument(executor instanceof ThreadPoolExecutor,
-                "The ExecutorService of type {} is not an instanceof ThreadPoolExecutor",
-                executor.getClass());
-        this.executor = (ThreadPoolExecutor)executor;
+    /**
+     * Creates a new bean if the backing executor is a ThreadPoolExecutor and registers it.
+     *
+     * @param executor the backing {@link Executor}
+     * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
+     * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
+     * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
+     * @return a registered ThreadExecutorStatsMXBeanImpl instance if the backing executor
+     *         is a ThreadPoolExecutor, otherwise null.
+     */
+    public static ThreadExecutorStatsMXBeanImpl create(final Executor executor, final String beanName,
+            final String beanType, final @Nullable String beanCategory) {
+        ThreadExecutorStatsMXBeanImpl ret = createInternal(executor, beanName, beanType, beanCategory);
+        if (ret != null) {
+            ret.registerMBean();
+        }
+
+        return ret;
+    }
+
+    /**
+     * Creates a new bean if the backing executor is a ThreadPoolExecutor and registers it.
+     *
+     * @param executor the backing {@link Executor}
+     * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
+     * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
+     * @return a registered ThreadExecutorStatsMXBeanImpl instance if the backing executor
+     *         is a ThreadPoolExecutor, otherwise null.
+     */
+    public static ThreadExecutorStatsMXBeanImpl create(final Executor executor, final String beanName,
+            final String beanType) {
+        return create(executor, beanName, beanType, null);
+    }
+
+    /**
+     * Creates a new bean if the backing executor is a ThreadPoolExecutor.
+     *
+     * @param executor the backing {@link Executor}
+     * @return a ThreadExecutorStatsMXBeanImpl instance if the backing executor
+     *         is a ThreadPoolExecutor, otherwise null.
+     */
+    public static ThreadExecutorStatsMXBeanImpl create(final Executor executor) {
+        return createInternal(executor, "", "", null);
     }
 
     @Override
@@ -69,7 +124,7 @@ public class ThreadExecutorStatsMXBeanImpl extends AbstractMXBean
     @Override
     public Long getLargestQueueSize() {
         BlockingQueue<Runnable> queue = executor.getQueue();
-        if(queue instanceof TrackingLinkedBlockingQueue) {
+        if (queue instanceof TrackingLinkedBlockingQueue) {
             return Long.valueOf(((TrackingLinkedBlockingQueue<?>)queue).getLargestQueueSize());
         }
 
@@ -100,7 +155,7 @@ public class ThreadExecutorStatsMXBeanImpl extends AbstractMXBean
     @Override
     public Long getRejectedTaskCount() {
         RejectedExecutionHandler rejectedHandler = executor.getRejectedExecutionHandler();
-        if(rejectedHandler instanceof CountingRejectedExecutionHandler) {
+        if (rejectedHandler instanceof CountingRejectedExecutionHandler) {
             return Long.valueOf(((CountingRejectedExecutionHandler)rejectedHandler)
                                                                      .getRejectedTaskCount());
         }