<commit_before>package org.cache2k.jcache.provider;

/*
 * #%L
 * cache2k JCache provider
 * %%
 * Copyright (C) 2000 - 2017 headissue GmbH, Munich
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.cache2k.Cache;
import org.cache2k.CacheManager;
import org.cache2k.core.spi.CacheLifeCycleListener;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * @author <NAME>; created: 2015-04-29
 */
@SuppressWarnings("WeakerAccess")
public class JCacheJmxSupport implements CacheLifeCycleListener {

  private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

  @Override
  public void cacheCreated(Cache c) {
  }

  @Override
  public void cacheDestroyed(Cache c) {
    disableStatistics(c);
    disableConfiguration(c);
  }

  public void enableStatistics(JCacheAdapter c) {
    MBeanServer mbs = mBeanServer;
    String _name = createStatisticsObjectName(c.cache);
    try {
       mbs.registerMBean(
         new CacheJmxStatistics(c),
         new ObjectName(_name));
    } catch (Exception e) {
      throw new IllegalStateException("Error registering JMX bean, name='" + _name + "'", e);
    }
  }

  public void disableStatistics(Cache c) {
    MBeanServer mbs = mBeanServer;
    String _name = createStatisticsObjectName(c);
    try {
      mbs.unregisterMBean(new ObjectName(_name));
    } catch (InstanceNotFoundException ignore) {
    } catch (Exception e) {
      throw new IllegalStateException("Error unregister JMX bean, name='" + _name + "'", e);
    }
  }

  public void enableConfiguration(Cache c, javax.cache.Cache ca) {
    MBeanServer mbs = mBeanServer;
    String _name = createConfigurationObjectName(c);
    try {
       mbs.registerMBean(new CacheJmxConfiguration(ca), new ObjectName(_name));
    } catch (Exception e) {
      throw new IllegalStateException("Error register JMX bean, name='" + _name + "'", e);
    }
  }

  public void disableConfiguration(Cache c) {
    MBeanServer mbs = mBeanServer;
    String _name = createConfigurationObjectName(c);
    try {
      mbs.unregisterMBean(new ObjectName(_name));
    } catch (InstanceNotFoundException ignore) {
    } catch (Exception e) {
      throw new IllegalStateException("Error unregister JMX bean, name='" + _name + "'", e);
    }
  }

  public String createStatisticsObjectName(Cache cache) {
    return "javax.cache:type=CacheStatistics," +
          "CacheManager=" + sanitizeName(cache.getCacheManager().getName()) +
          ",Cache=" + sanitizeName(cache.getName());
  }

  public String createConfigurationObjectName(Cache cache) {
    return "javax.cache:type=CacheConfiguration," +
          "CacheManager=" + sanitizeName(cache.getCacheManager().getName()) +
          ",Cache=" + sanitizeName(cache.getName());
  }

  /**
   * Filter illegal chars, same rule as in TCK or RI?
   */
  public static String sanitizeName(String string) {
    return string == null ? "" : string.replaceAll(":|=|\n|,", ".");
  }

}
<commit_msg>Fix unused imports<commit_after>package org.cache2k.jcache.provider;

/*
 * #%L
 * cache2k JCache provider
 * %%
 * Copyright (C) 2000 - 2017 headissue GmbH, Munich
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.cache2k.Cache;
import org.cache2k.core.spi.CacheLifeCycleListener;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * @author <NAME>; created: 2015-04-29
 */
@SuppressWarnings("WeakerAccess")
public class JCacheJmxSupport implements CacheLifeCycleListener {

  private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

  @Override
  public void cacheCreated(Cache c) {
  }

  @Override
  public void cacheDestroyed(Cache c) {
    disableStatistics(c);
    disableConfiguration(c);
  }

  public void enableStatistics(JCacheAdapter c) {
    MBeanServer mbs = mBeanServer;
    String _name = createStatisticsObjectName(c.cache);
    try {
       mbs.registerMBean(
         new CacheJmxStatistics(c),
         new ObjectName(_name));
    } catch (Exception e) {
      throw new IllegalStateException("Error registering JMX bean, name='" + _name + "'", e);
    }
  }

  public void disableStatistics(Cache c) {
    MBeanServer mbs = mBeanServer;
    String _name = createStatisticsObjectName(c);
    try {
      mbs.unregisterMBean(new ObjectName(_name));
    } catch (InstanceNotFoundException ignore) {
    } catch (Exception e) {
      throw new IllegalStateException("Error unregister JMX bean, name='" + _name + "'", e);
    }
  }

  public void enableConfiguration(Cache c, javax.cache.Cache ca) {
    MBeanServer mbs = mBeanServer;
    String _name = createConfigurationObjectName(c);
    try {
       mbs.registerMBean(new CacheJmxConfiguration(ca), new ObjectName(_name));
    } catch (Exception e) {
      throw new IllegalStateException("Error register JMX bean, name='" + _name + "'", e);
    }
  }

  public void disableConfiguration(Cache c) {
    MBeanServer mbs = mBeanServer;
    String _name = createConfigurationObjectName(c);
    try {
      mbs.unregisterMBean(new ObjectName(_name));
    } catch (InstanceNotFoundException ignore) {
    } catch (Exception e) {
      throw new IllegalStateException("Error unregister JMX bean, name='" + _name + "'", e);
    }
  }

  public String createStatisticsObjectName(Cache cache) {
    return "javax.cache:type=CacheStatistics," +
          "CacheManager=" + sanitizeName(cache.getCacheManager().getName()) +
          ",Cache=" + sanitizeName(cache.getName());
  }

  public String createConfigurationObjectName(Cache cache) {
    return "javax.cache:type=CacheConfiguration," +
          "CacheManager=" + sanitizeName(cache.getCacheManager().getName()) +
          ",Cache=" + sanitizeName(cache.getName());
  }

  /**
   * Filter illegal chars, same rule as in TCK or RI?
   */
  public static String sanitizeName(String string) {
    return string == null ? "" : string.replaceAll(":|=|\n|,", ".");
  }

}

{'fraction_non_alphanumeric': 0.10954356846473029, 'fraction_numerical': 0.00677731673582296, 'mean_word_length': 3.779246530072703, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3959475', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2044, 'n_words': 572}