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

ben-manes / caffeine / #5612

11 Jul 2026 03:47PM UTC coverage: 71.292% (-28.7%) from 100.0%
#5612

push

github

ben-manes
Preserve timestamps on a raced same-instance refresh

The automatic refreshIfNeeded path checked for an equal-value reload
before the ABA/ownership check and returned without preserving the
timestamps, so a reload completing after a concurrent put(k,
sameInstance) did a full update — shifting the entry's expiration
forward by the reload's in-flight duration and stomping the write.
Mirror the explicit-refresh fix: check ownership first. An owned
reload installs the value (refreshing metadata even for a same
instance), while a raced reload preserves the write's timestamps and
only notifies REPLACED when the value differs.

2957 of 4134 branches covered (71.53%)

3 of 3 new or added lines in 1 file covered. (100.0%)

2408 existing lines in 53 files now uncovered.

5980 of 8388 relevant lines covered (71.29%)

0.71 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 org.jspecify.annotations.Nullable;
19

20
import static java.util.Locale.US;
21

22
import java.lang.management.ManagementFactory;
23

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

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

41
  private JmxRegistration() {}
42

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

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

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

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

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

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

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

UNCOV
114
  public enum MBeanType {
×
UNCOV
115
    CONFIGURATION, STATISTICS;
×
116

117
    private String formatted() {
UNCOV
118
      return Character.toUpperCase(name().charAt(0)) + name().toLowerCase(US).substring(1);
×
119
    }
120
  }
121
}
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