All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ehcache.management.service.impl.ConstrainableEntityBuilderSupportV2 Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * All content copyright (c) 2003-2012 Terracotta, Inc., except as may otherwise be noted in a separate copyright
 * notice. All rights reserved.
 */
package net.sf.ehcache.management.service.impl;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.sf.ehcache.management.service.AccessorPrefix;
import net.sf.ehcache.util.ManagementAttribute;
import net.sf.ehcache.util.counter.Counter;
import net.sf.ehcache.util.counter.sampled.SampledCounter;

import org.slf4j.Logger;

/**
* @author brandony
*/
abstract class ConstrainableEntityBuilderSupportV2 {

  private static final Set SIZE_ATTRIBUTE_NAMES =
      Collections.unmodifiableSet(new HashSet(Arrays.asList("Size", "SizeSample", "RemoteSizeSample")));

  private Set constraints;

  abstract Logger getLog();

  protected void addConstraints(Set constraints) {
    if (constraints == null) throw new IllegalArgumentException("constraints == null");

    if (this.constraints == null) {
      this.constraints = constraints;
    } else {
      this.constraints.addAll(constraints);
    }
  }

  protected Set getAttributeConstraints() {
    return constraints;
  }

  protected void buildAttributeMapByAttribute(Class api,
                                   SAMPLER sampler,
                                   Map attributeMap,
                                   Collection attributes,
                                   String nameAccessor) {
    Set excludedNames = getExcludedAttributeNames(sampler);

    for (String attribute : attributes) {
      Method method = null;
      for (AccessorPrefix prefix : AccessorPrefix.values()) {
        try {
          method = api.getMethod(prefix + attribute);
          break;
        } catch (NoSuchMethodException e) {
          //This is not the accessor you were looking for....move along
        }
      }

      if (method != null && !nameAccessor.equals(method.getName())) {
        if (excludedNames.contains(attribute)) {
          attributeMap.put(attribute, 0);
          continue;
        }

        addAttribute(sampler, attributeMap, attribute, method);
      }
    }
  }

  protected void buildAttributeMapByApi(Class api,
                                   SAMPLER sampler,
                                   Map attributeMap,
                                   Collection attributes,
                                   String nameAccessor) {
    Set excludedNames = getExcludedAttributeNames(sampler);
    boolean haveAttributes = attributeMap != null && attributeMap.size() > 0;

    for (Method method : api.getMethods()) {
      if (!haveAttributes && method.isAnnotationPresent(ManagementAttribute.class)) {
        String name = method.getName();
        String trimmedName = AccessorPrefix.trimPrefix(name);
        if (!nameAccessor.equals(name) && AccessorPrefix.isAccessor(name) && (attributes == null || attributes.contains(
            trimmedName))) {
  
          if (excludedNames.contains(trimmedName)) {
            attributeMap.put(trimmedName, 0);
            continue;
          }
  
          addAttribute(sampler, attributeMap, trimmedName, method);
        }
      }
    }
  }

  protected abstract Set getExcludedAttributeNames(SAMPLER sampler);

  protected Set getUnsignedIntAttributeNames(SAMPLER sampler) {
    return SIZE_ATTRIBUTE_NAMES;
  }

  private void addAttribute(SAMPLER sampler,
                            Map attributeMap,
                            String attribute,
                            Method method) {
    Object value = null;
    try {
      value = method.invoke(sampler);

      // stats reflection "helper" code
      if (value instanceof SampledCounter) {
        value = ((SampledCounter)value).getMostRecentSample().getCounterValue();
      } else if (value instanceof Counter) {
        value = ((Counter)value).getValue();
      }

      if (getUnsignedIntAttributeNames(sampler).contains(attribute) && value instanceof Number) {
        value = coerceUnsignedIntToLong(((Number)value).intValue());
      }
    } catch (Exception e) {
      value = null;
      String msg = String.format("Failed to invoke method %s while constructing entity.", method.getName());
      getLog().warn(msg);
      getLog().debug(msg, e);
    } finally {
      attributeMap.put(attribute, value);
    }
  }

  private static long coerceUnsignedIntToLong(int value) {
    return value < 0 ? ((long)Integer.MAX_VALUE) + (value - Integer.MIN_VALUE + 1) : value;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy