Java tutorial
/* * Copyright 2014 Alibaba.com All right reserved. This software is the * confidential and proprietary information of Alibaba.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Alibaba.com. */ package com.alibaba.china.plugin.cache.spring.aop; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import com.alibaba.china.plugin.cache.spring.annotation.Cached; import com.alibaba.china.plugin.cache.spring.annotation.Key; import com.alibaba.china.plugin.cache.spring.annotation.KeyCodedBy; import com.alibaba.china.plugin.cache.spring.annotation.Value; import com.alibaba.china.plugin.cache.spring.annotation.ValueCodedBy; import com.alibaba.china.plugin.cache.spring.annotation.VersionedBy; import com.alibaba.china.plugin.nova.cache.configuration.Configuration; import com.alibaba.china.plugin.nova.cache.core.CacheTemplate; import com.alibaba.china.plugin.nova.cache.core.KeyCoder; import com.alibaba.china.plugin.nova.cache.core.OperationResult; import com.alibaba.china.plugin.nova.cache.core.ValueCoder; import com.alibaba.china.plugin.nova.cache.core.Versioner; import com.alibaba.china.plugin.nova.cache.exception.CacheException; /** * CacheAopSupport.java??TODO ?? * * @author liuzhijun.pt 2014130 ?11:05:36 */ @Aspect public class CacheAopSupport { private CacheTemplate cacheTemplate; @Around("@annotation(cached)") public Object cacheAop(ProceedingJoinPoint joinPoint, Cached cached) { Configuration configuration = retriveConfiguration(joinPoint, cached); Object key = retriveKey(joinPoint); // Object value = retriveValue(joinPoint); switch (cached.cacheMethod()) { case PUT: break; case GET: dealGet(joinPoint, configuration, key); break; case GETBATCH: break; case REMOVE: break; case REMOVEBATCH: break; default: break; } return cached; } private Object dealPut(Configuration configuration, Object key, Object value) { return null; } private Object dealGet(ProceedingJoinPoint joinPoint, Configuration configuration, Object key) { cacheTemplate.setConfiguration(configuration); if (key == null) { key = configuration.getDefaultKey(); } OperationResult opResult = cacheTemplate.get(key); if (!opResult.isSuccess()) { throw opResult.getExcetpion(); } Object resultObj = opResult.getValue(); if (resultObj == null) { resultObj = proceedJoinPoint(joinPoint); cacheTemplate.put(key, resultObj); } return resultObj; } private Object dealGetBatch(Configuration configuration, Object key, Object value) { return null; } private Object dealRemove(Configuration configuration, Object key, Object value) { return null; } private Object dealRemoveBatch(Configuration configuration, Object key, Object value) { return null; } private Object proceedJoinPoint(ProceedingJoinPoint joinPoint) { try { return joinPoint.proceed(); } catch (Throwable e) { throw new RuntimeException(e); } } private Configuration retriveConfiguration(ProceedingJoinPoint joinPoint, Cached cached) { Configuration configuration = new Configuration(); configuration.setExpireTime(cached.expireTime()); configuration.setZip(cached.isZip()); configuration.setStrategy(cached.strategy()); configuration.setNamespace(cached.namespace()); configuration.setDefaultKey(cached.defaultKey()); configuration.setDefaultValue(cached.defaultValue()); Method method = (Method) joinPoint.getTarget(); try { Versioner versioner = getVersioner(method); configuration.setVersioner(versioner); KeyCoder keyCoder = getKeyCoder(method); configuration.setKeyCoder(keyCoder); ValueCoder valueCoder = getValueCoder(method); configuration.setValueCoder(valueCoder); } catch (Exception e) { throw new CacheException("", e); } return configuration; } /** * @param method * @return * @throws NoSuchMethodException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ private ValueCoder getValueCoder(Method method) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { ValueCodedBy valueCodedBy = method.getAnnotation(ValueCodedBy.class); String[] args = valueCodedBy.args(); Class<?>[] classType = new Class<?>[args == null ? 0 : args.length]; Constructor<? extends ValueCoder> constructor = valueCodedBy.coder().getConstructor(classType); ValueCoder valueCoder = constructor.newInstance(args); return valueCoder; } /** * @param method * @return * @throws NoSuchMethodException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ private Versioner getVersioner(Method method) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { VersionedBy versionedBy = method.getAnnotation(VersionedBy.class); String[] args = versionedBy.args(); Class<?>[] classType = new Class<?>[args == null ? 0 : args.length]; Constructor<? extends Versioner> constructor = versionedBy.versioner().getConstructor(classType); Versioner versioner = constructor.newInstance(args); return versioner; } /** * @param method * @return * @throws NoSuchMethodException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ private KeyCoder getKeyCoder(Method method) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { KeyCodedBy keyCodedBy = method.getAnnotation(KeyCodedBy.class); String[] args = keyCodedBy.args(); Class<?>[] classType = new Class<?>[args == null ? 0 : args.length]; Constructor<? extends KeyCoder> constructor = keyCodedBy.coder().getConstructor(classType); KeyCoder keyCoder = constructor.newInstance(args); return keyCoder; } private Object retriveKey(ProceedingJoinPoint joinPoint) { Method method = (Method) joinPoint.getTarget(); method.getAnnotation(VersionedBy.class); Object[] args = joinPoint.getArgs(); if (args == null || args.length == 0) { return null; } Annotation[][] paramAnnotations = method.getParameterAnnotations(); List<Object> keyList = new ArrayList<Object>(); for (Annotation[] annos : paramAnnotations) { for (Annotation a : annos) { if (a instanceof Key) { Key keyAnno = (Key) a; int index = keyAnno.value(); if (index >= 0 && index < args.length) { keyList.add(args[index]); } } } } if (keyList.size() == 0) { return null; } else if (keyList.size() == 1) { return keyList.get(0); } return keyList.toArray(); } private Object retriveValue(ProceedingJoinPoint joinPoint) { Method method = (Method) joinPoint.getTarget(); method.getAnnotation(VersionedBy.class); Object[] args = joinPoint.getArgs(); if (args == null || args.length == 0) { return null; } Annotation[][] paramAnnotations = method.getParameterAnnotations(); List<Object> valueList = new ArrayList<Object>(); for (Annotation[] annos : paramAnnotations) { for (Annotation a : annos) { if (a instanceof Value) { Value valueAnno = (Value) a; int index = valueAnno.value(); if (index >= 0 && index < args.length) { valueList.add(args[index]); } } } } if (valueList.size() == 0) { return null; } else if (valueList.size() == 1) { return valueList.get(0); } return valueList.toArray(); } }