<commit_before>package com.trivago.triava.tcache.util;

import java.util.Set;

import javax.cache.CacheException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.trivago.triava.tcache.eviction.Cache;
import com.trivago.triava.tcache.eviction.TCacheJSR107;

/**
 * Utility functions to register MBean objects. Concrete implementations for Configuration and
 * Statistics are excluded and are implemented in subclasses.
 * <p>
 * Implementation note: Modeled after the JSR107 RI
 * 
 * @author cesken
 *
 */
public abstract class TCacheMBean
{
	private final static MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer();
	
	abstract public String objectNameType();
	abstract public Object getMBean(TCacheJSR107<?, ?> jsr107cache);

	public void register(Cache<?,?> cache)
	{
		TCacheJSR107<?, ?> jsr107cache = cache.jsr107cache();
		// these can change during runtime, so always look it up
		ObjectName registeredObjectName = calculateObjectName(jsr107cache, objectNameType());
		if (isRegistered(registeredObjectName))
		{
			// Do not register twice. Actually this contains a race condition due to a
			// check-then-act "isRegistered() => registerMBean()" action, but it should not be
			// a problem for us. Either it happens during 
			return;
		}
		
		try
		{
			mBeanServer.registerMBean(getMBean(jsr107cache), registeredObjectName);
		}
		catch (Exception e)
		{
			throw new CacheException("Error registering cache MXBeans for CacheManager " + registeredObjectName
					+ " . Error was " + e.getMessage(), e);
		}
	}

	
	public void unregister(Cache<?, ?> cache)<commit_msg>SRT-28657 Register to platform MBeanServer instead of new MBeanServer<commit_after>package com.trivago.triava.tcache.util;

import java.lang.management.ManagementFactory;
import java.util.Set;

import javax.cache.CacheException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.trivago.triava.tcache.eviction.Cache;
import com.trivago.triava.tcache.eviction.TCacheJSR107;

/**
 * Utility functions to register MBean objects. Concrete implementations for Configuration and
 * Statistics are excluded and are implemented in subclasses.
 * <p>
 * Implementation note: Modeled after the JSR107 RI
 * 
 * @author cesken
 *
 */
public abstract class TCacheMBean
{
// JSR107 RI creates its own MBeanServer. There was a discussion on the JSR107 ML about this.
// At the moment TCache will register on the PlatformMBeanServer
//	private final static MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer();
	private final static MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
	
	abstract public String objectNameType();
	abstract public Object getMBean(TCacheJSR107<?, ?> jsr107cache);

	public void register(Cache<?,?> cache)
	{
		TCacheJSR107<?, ?> jsr107cache = cache.jsr107cache();
		// these can change during runtime, so always look it up
		ObjectName registeredObjectName = calculateObjectName(jsr107cache, objectNameType());
		if (isRegistered(registeredObjectName))
		{
			// Do not register twice. Actually this contains a race condition due to a
			// check-then-act "isRegistered() => registerMBean()" action, but it should not be
			// a problem for us. Either it happens during 
			return;
		}
		
		try
		{
			mBeanServer.registerMBean(getMBean(jsr107cache), registeredObjectName);
		}
		catch (Exception e)
		{
			throw new CacheException("Error registering cache MXBeans for CacheManager " + registeredObjectName
					+ " . Error was " + e.getMessage(), e);
		}
	}

	
	public void unregister(Cache<?, ?> cache)
{'fraction_non_alphanumeric': 0.09054699946893255, 'fraction_numerical': 0.01725969198088157, 'mean_word_length': 4.716236722306525, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1790784', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1076, 'n_words': 324}