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