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

me.prettyprint.hom.cache.AnonymousParserValidator Maven / Gradle / Ivy

There is a newer version: 3.0-04
Show newest version
package me.prettyprint.hom.cache;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hom.CFMappingDef;
import me.prettyprint.hom.ClassCacheMgr;
import me.prettyprint.hom.annotations.AnonymousPropertyHandling;

/**
 * Parse, validate, and set defaults if needed for Inheritance functionality.
 * 
 * @author bburruss
 */
public class AnonymousParserValidator implements ParserValidator {

  @Override
  public  void parse(ClassCacheMgr cacheMgr, Annotation anno, CFMappingDef cfMapDef) {
    if (anno instanceof AnonymousPropertyHandling) {
      parseInheritanceAnnotation((AnonymousPropertyHandling) anno, cfMapDef);
    } else {
      throw new HectorObjectMapperException("This class cannot parse annotation, "
          + anno.getClass().getSimpleName());
    }
  }

  private  void parseInheritanceAnnotation(AnonymousPropertyHandling anno,
      CFMappingDef cfMapDef) {
    cfMapDef.setAnonymousValueType(anno.type());
    try {
      cfMapDef.setAnonymousValueSerializer(instantiateSerializer(anno.serializer()));
    } catch (Throwable e) {
      throw new HectorObjectMapperException(
          "exception while instantiating anonymous converter for class, " + cfMapDef.getRealClass()
              + ", with converter type, " + anno.serializer(), e);
    }

    Method addMeth;
    try {
      addMeth = cfMapDef.getRealClass().getMethod(anno.adder(), String.class, anno.type());
    } catch (NoSuchMethodException e) {
      throw new HectorObjectMapperException("Could not find anonymous add handler for class, "
          + cfMapDef.getRealClass() + ", with anonymous value type, " + anno.type());
    } catch (Throwable e) {
      throw new HectorObjectMapperException(
          "exception while finding anonymous add handler for class, " + cfMapDef.getRealClass()
              + ", with anonymous value type, " + anno.type(), e);
    }

    Method getMeth;
    try {
      getMeth = cfMapDef.getRealClass().getMethod(anno.getter());
    } catch (NoSuchMethodException e) {
      throw new HectorObjectMapperException("Could not find anonymous get handler for class, "
          + cfMapDef.getRealClass());
    } catch (Throwable e) {
      throw new HectorObjectMapperException(
          "exception while finding anonymous get handler for class, " + cfMapDef.getRealClass());
    }

    cfMapDef.setAnonymousPropertyAddHandler(addMeth);
    cfMapDef.setAnonymousPropertyGetHandler(getMeth);
  }

  @Override
  public  void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
//    Serializer ser = cfMapDef.getAnonymousValueSerializer();
//    if ( null == ser) {
//      return;
//    }
//    
//    try {
//      ser.getClass().getMethod("toByteBuffer", cfMapDef.getAnonymousValueType());
//    } catch (NoSuchMethodException e) {
//      throw new HectorObjectMapperException("Anonymous serializer, "
//          + cfMapDef.getAnonymousValueSerializer().getClass().getName() + ", cannot handle type, "
//          + cfMapDef.getAnonymousValueType().getClass().getName());
//    } catch (Throwable e) {
//      throw new HectorObjectMapperException(
//          "exception while validating anonymous configuration for class, "
//              + cfMapDef.getAnonymousValueSerializer().getClass().getName());
//    }
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  private Serializer instantiateSerializer(Class ser) {
    try {
      return (Serializer) ser.getMethod("get").invoke(null, (Object[]) null);
    } catch (NoSuchMethodException e) {
      try {
        return  (Serializer) ser.newInstance();
      } catch (Throwable e1) {
        throw new HectorObjectMapperException("exception while instantiating Hector serializer, "
            + ser.getName());
      }
    } catch (Throwable e) {
      throw new HectorObjectMapperException("exception while instantiating Hector serializer, "
          + ser.getName());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy