BUG-650: remove executor abstraction
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / jmx / mbeans / shard / ShardMBeanFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
9
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author Basheeruddin syedbahm@cisco.com
19  *
20  */
21 public class ShardMBeanFactory {
22
23     private static final Logger LOG = LoggerFactory.getLogger(ShardMBeanFactory.class);
24
25     private static final Cache<String,ShardStats> shardMBeansCache =
26                                       CacheBuilder.newBuilder().weakValues().build();
27
28     public static ShardStats getShardStatsMBean(final String shardName, final String mxBeanType) {
29         final String finalMXBeanType = mxBeanType != null ? mxBeanType : "DistDataStore";
30         try {
31             return shardMBeansCache.get(shardName, new Callable<ShardStats>() {
32                 @Override
33                 public ShardStats call() throws Exception {
34                     ShardStats shardStatsMBeanImpl = new ShardStats(shardName, finalMXBeanType);
35                     shardStatsMBeanImpl.registerMBean();
36                     return shardStatsMBeanImpl;
37                 }
38             });
39         } catch(ExecutionException e) {
40             LOG.error(String.format("Could not create MXBean for shard: %s", shardName), e);
41             // Just return an instance that isn't registered.
42             return new ShardStats(shardName, finalMXBeanType);
43         }
44     }
45 }