• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JmxRegistration.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.jcache.management;
17

18
import static java.util.Locale.US;
19

20
import java.lang.management.ManagementFactory;
21

22
import javax.cache.Cache;
23
import javax.cache.CacheException;
24
import javax.management.InstanceAlreadyExistsException;
25
import javax.management.InstanceNotFoundException;
26
import javax.management.MBeanRegistrationException;
27
import javax.management.MBeanServer;
28
import javax.management.MalformedObjectNameException;
29
import javax.management.NotCompliantMBeanException;
30
import javax.management.ObjectName;
31

32
/**
33
 * Jmx cache utilities.
34
 *
35
 * @author ben.manes@gmail.com (Ben Manes)
36
 */
37
public final class JmxRegistration {
38

39
  private JmxRegistration() {}
40

41
  /**
42
   * Registers the JMX management bean for the cache.
43
   *
44
   * @param cache the cache to register
45
   * @param mxbean the management bean
46
   * @param type the mxbean type
47
   */
48
  public static void registerMxBean(Cache<?, ?> cache, Object mxbean, MBeanType type) {
49
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
×
50
    ObjectName objectName = getObjectName(cache, type);
×
51
    register(server, objectName, mxbean);
×
52
  }
×
53

54
  /**
55
   * Unregisters the JMX management bean for the cache.
56
   *
57
   * @param cache the cache to unregister
58
   * @param type the mxbean type
59
   */
60
  public static void unregisterMxBean(Cache<?, ?> cache, MBeanType type) {
61
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
×
62
    ObjectName objectName = getObjectName(cache, type);
×
63
    unregister(server, objectName);
×
64
  }
×
65

66
  /** Registers the management bean with the given object name. */
67
  static void register(MBeanServer server, ObjectName objectName, Object mbean) {
68
    try {
69
      if (!server.isRegistered(objectName)) {
×
70
        server.registerMBean(mbean, objectName);
×
71
      }
72
    } catch (InstanceAlreadyExistsException
×
73
        | MBeanRegistrationException | NotCompliantMBeanException e) {
74
      throw new CacheException("Error registering " + objectName, e);
×
75
    }
×
76
  }
×
77

78
  /** Unregisters the management bean(s) with the given object name. */
79
  static void unregister(MBeanServer server, ObjectName objectName) {
80
    try {
81
      for (ObjectName name : server.queryNames(objectName, null)) {
×
82
        server.unregisterMBean(name);
×
83
      }
×
84
    } catch (MBeanRegistrationException | InstanceNotFoundException e) {
×
85
      throw new CacheException("Error unregistering " + objectName, e);
×
86
    }
×
87
  }
×
88

89
  /** Returns the object name of the management bean. */
90
  static ObjectName getObjectName(Cache<?, ?> cache, MBeanType type) {
91
    String cacheManagerName = sanitize(cache.getCacheManager().getURI().toString());
×
92
    String cacheName = sanitize(cache.getName());
×
93
    String name = String.format(US, "javax.cache:type=Cache%s,CacheManager=%s,Cache=%s",
×
94
        type.formatted(), cacheManagerName, cacheName);
×
95
    return newObjectName(name);
×
96
  }
97

98
  static ObjectName newObjectName(String name) {
99
    try {
100
      return new ObjectName(name);
×
101
    } catch (MalformedObjectNameException e) {
×
102
      String msg = "Illegal ObjectName: " + name;
×
103
      throw new CacheException(msg, e);
×
104
    }
105
  }
106

107
  /** Returns a sanitized string for use as a management bean name. */
108
  static String sanitize(String name) {
109
    return (name == null) ? "" : name.replaceAll("[,:=\n]", ".");
×
110
  }
111

112
  public enum MBeanType {
×
113
    CONFIGURATION, STATISTICS;
×
114

115
    private String formatted() {
116
      return Character.toUpperCase(name().charAt(0)) + name().toLowerCase(US).substring(1);
×
117
    }
118
  }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc