ConfigurableMBeanServerIdBuilder.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache.jmx;

  20. import javax.management.ListenerNotFoundException;
  21. import javax.management.MBeanNotificationInfo;
  22. import javax.management.MBeanServer;
  23. import javax.management.MBeanServerBuilder;
  24. import javax.management.MBeanServerDelegate;
  25. import javax.management.Notification;
  26. import javax.management.NotificationFilter;
  27. import javax.management.NotificationListener;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.concurrent.ConcurrentMap;

  30. public class ConfigurableMBeanServerIdBuilder extends MBeanServerBuilder
  31. {
  32.     private static final ConcurrentMap<Key, MBeanServer> JVM_SINGLETONS = new ConcurrentHashMap<>();

  33.     private static class Key
  34.     {
  35.         private final String domain;
  36.         private final MBeanServer outer;

  37.         private Key(final String domain, final MBeanServer outer)
  38.         {
  39.             this.domain = domain;
  40.             this.outer = outer;
  41.         }

  42.         @Override
  43.         public boolean equals(final Object o)
  44.         {
  45.             if (this == o) {
  46.                 return true;
  47.             }
  48.             if (o == null || getClass() != o.getClass()) {
  49.                 return false;
  50.             }

  51.             final Key key = Key.class.cast(o);
  52.             return !(domain != null ? !domain.equals(key.domain) : key.domain != null)
  53.                     && !(outer != null ? !outer.equals(key.outer) : key.outer != null);

  54.         }

  55.         @Override
  56.         public int hashCode()
  57.         {
  58.             int result = domain != null ? domain.hashCode() : 0;
  59.             result = 31 * result + (outer != null ? outer.hashCode() : 0);
  60.             return result;
  61.         }
  62.     }

  63.     @Override
  64.     public MBeanServer newMBeanServer(final String defaultDomain, final MBeanServer outer, final MBeanServerDelegate delegate)
  65.     {
  66.         final Key key = new Key(defaultDomain, outer);
  67.         MBeanServer server = JVM_SINGLETONS.get(key);
  68.         if (server == null)
  69.         {
  70.             server = super.newMBeanServer(defaultDomain, outer, new ForceIdMBeanServerDelegate(delegate));
  71.             final MBeanServer existing = JVM_SINGLETONS.putIfAbsent(key, server);
  72.             if (existing != null)
  73.             {
  74.                 server = existing;
  75.             }
  76.         }
  77.         return server;
  78.     }

  79.     private class ForceIdMBeanServerDelegate extends MBeanServerDelegate
  80.     {
  81.         private final MBeanServerDelegate delegate;

  82.         public ForceIdMBeanServerDelegate(final MBeanServerDelegate delegate)
  83.         {
  84.             this.delegate = delegate;
  85.         }

  86.         @Override
  87.         public String getMBeanServerId()
  88.         {
  89.             return System.getProperty("org.jsr107.tck.management.agentId", delegate.getMBeanServerId());
  90.         }

  91.         @Override
  92.         public String getSpecificationName()
  93.         {
  94.             return delegate.getSpecificationName();
  95.         }

  96.         @Override
  97.         public String getSpecificationVersion()
  98.         {
  99.             return delegate.getSpecificationVersion();
  100.         }

  101.         @Override
  102.         public String getSpecificationVendor()
  103.         {
  104.             return delegate.getSpecificationVendor();
  105.         }

  106.         @Override
  107.         public String getImplementationName()
  108.         {
  109.             return delegate.getImplementationName();
  110.         }

  111.         @Override
  112.         public String getImplementationVersion()
  113.         {
  114.             return delegate.getImplementationVersion();
  115.         }

  116.         @Override
  117.         public String getImplementationVendor()
  118.         {
  119.             return delegate.getImplementationVendor();
  120.         }

  121.         @Override
  122.         public MBeanNotificationInfo[] getNotificationInfo()
  123.         {
  124.             return delegate.getNotificationInfo();
  125.         }

  126.         @Override
  127.         public void addNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback)
  128.                 throws IllegalArgumentException
  129.         {
  130.             delegate.addNotificationListener(listener, filter, handback);
  131.         }

  132.         @Override
  133.         public void removeNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback)
  134.                 throws ListenerNotFoundException
  135.         {
  136.             delegate.removeNotificationListener(listener, filter, handback);
  137.         }

  138.         @Override
  139.         public void removeNotificationListener(final NotificationListener listener) throws ListenerNotFoundException
  140.         {
  141.             delegate.removeNotificationListener(listener);
  142.         }

  143.         @Override
  144.         public void sendNotification(final Notification notification)
  145.         {
  146.             delegate.sendNotification(notification);
  147.         }
  148.     }
  149. }