View Javadoc
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  
21  import javax.management.ListenerNotFoundException;
22  import javax.management.MBeanNotificationInfo;
23  import javax.management.MBeanServer;
24  import javax.management.MBeanServerBuilder;
25  import javax.management.MBeanServerDelegate;
26  import javax.management.Notification;
27  import javax.management.NotificationFilter;
28  import javax.management.NotificationListener;
29  import java.util.concurrent.ConcurrentHashMap;
30  import java.util.concurrent.ConcurrentMap;
31  
32  public class ConfigurableMBeanServerIdBuilder extends MBeanServerBuilder
33  {
34      private static final ConcurrentMap<Key, MBeanServer> JVM_SINGLETONS = new ConcurrentHashMap<>();
35  
36      private static class Key
37      {
38          private final String domain;
39          private final MBeanServer outer;
40  
41          private Key(final String domain, final MBeanServer outer)
42          {
43              this.domain = domain;
44              this.outer = outer;
45          }
46  
47          @Override
48          public boolean equals(final Object o)
49          {
50              if (this == o) {
51                  return true;
52              }
53              if (o == null || getClass() != o.getClass()) {
54                  return false;
55              }
56  
57              final Key key = Key.class.cast(o);
58              return !(domain != null ? !domain.equals(key.domain) : key.domain != null)
59                      && !(outer != null ? !outer.equals(key.outer) : key.outer != null);
60  
61          }
62  
63          @Override
64          public int hashCode()
65          {
66              int result = domain != null ? domain.hashCode() : 0;
67              result = 31 * result + (outer != null ? outer.hashCode() : 0);
68              return result;
69          }
70      }
71  
72      @Override
73      public MBeanServer newMBeanServer(final String defaultDomain, final MBeanServer outer, final MBeanServerDelegate delegate)
74      {
75          final Key key = new Key(defaultDomain, outer);
76          MBeanServer server = JVM_SINGLETONS.get(key);
77          if (server == null)
78          {
79              server = super.newMBeanServer(defaultDomain, outer, new ForceIdMBeanServerDelegate(delegate));
80              final MBeanServer existing = JVM_SINGLETONS.putIfAbsent(key, server);
81              if (existing != null)
82              {
83                  server = existing;
84              }
85          }
86          return server;
87      }
88  
89      private class ForceIdMBeanServerDelegate extends MBeanServerDelegate
90      {
91          private final MBeanServerDelegate delegate;
92  
93          public ForceIdMBeanServerDelegate(final MBeanServerDelegate delegate)
94          {
95              this.delegate = delegate;
96          }
97  
98          @Override
99          public String getMBeanServerId()
100         {
101             return System.getProperty("org.jsr107.tck.management.agentId", delegate.getMBeanServerId());
102         }
103 
104         @Override
105         public String getSpecificationName()
106         {
107             return delegate.getSpecificationName();
108         }
109 
110         @Override
111         public String getSpecificationVersion()
112         {
113             return delegate.getSpecificationVersion();
114         }
115 
116         @Override
117         public String getSpecificationVendor()
118         {
119             return delegate.getSpecificationVendor();
120         }
121 
122         @Override
123         public String getImplementationName()
124         {
125             return delegate.getImplementationName();
126         }
127 
128         @Override
129         public String getImplementationVersion()
130         {
131             return delegate.getImplementationVersion();
132         }
133 
134         @Override
135         public String getImplementationVendor()
136         {
137             return delegate.getImplementationVendor();
138         }
139 
140         @Override
141         public MBeanNotificationInfo[] getNotificationInfo()
142         {
143             return delegate.getNotificationInfo();
144         }
145 
146         @Override
147         public void addNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback)
148                 throws IllegalArgumentException
149         {
150             delegate.addNotificationListener(listener, filter, handback);
151         }
152 
153         @Override
154         public void removeNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback)
155                 throws ListenerNotFoundException
156         {
157             delegate.removeNotificationListener(listener, filter, handback);
158         }
159 
160         @Override
161         public void removeNotificationListener(final NotificationListener listener) throws ListenerNotFoundException
162         {
163             delegate.removeNotificationListener(listener);
164         }
165 
166         @Override
167         public void sendNotification(final Notification notification)
168         {
169             delegate.sendNotification(notification);
170         }
171     }
172 }