Java Code Examples for org.aopalliance.aop.Advice

The following examples show how to use org.aopalliance.aop.Advice. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public String toString() {
	StringBuilder sb = new StringBuilder();
	Advice advice = this.advisor.getAdvice();
	sb.append(ClassUtils.getShortName(advice.getClass()));
	sb.append(": ");
	if (this.advisor instanceof Ordered) {
		sb.append("order ").append(((Ordered) this.advisor).getOrder()).append(", ");
	}
	if (advice instanceof AbstractAspectJAdvice) {
		AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice;
		sb.append(ajAdvice.getAspectName());
		sb.append(", declaration order ");
		sb.append(ajAdvice.getDeclarationOrder());
	}
	return sb.toString();
}
 
Example 2
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
	List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
	Advice advice = advisor.getAdvice();
	if (advice instanceof MethodInterceptor) {
		interceptors.add((MethodInterceptor) advice);
	}
	for (AdvisorAdapter adapter : this.adapters) {
		if (adapter.supportsAdvice(advice)) {
			interceptors.add(adapter.getInterceptor(advisor));
		}
	}
	if (interceptors.isEmpty()) {
		throw new UnknownAdviceTypeException(advisor.getAdvice());
	}
	return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
 
Example 3
private Advice[] getAdviceChainOrAddInterceptorToChain(Advice... existingAdviceChain) {
  if (existingAdviceChain == null) {
    return new Advice[] {rabbitMqReceiveTracingInterceptor};
  }

  for (Advice advice : existingAdviceChain) {
    if (advice instanceof RabbitMqReceiveTracingInterceptor) {
      return existingAdviceChain;
    }
  }

  Advice[] newChain = new Advice[existingAdviceChain.length + 1];
  System.arraycopy(existingAdviceChain, 0, newChain, 0, existingAdviceChain.length);
  newChain[existingAdviceChain.length] = rabbitMqReceiveTracingInterceptor;

  return newChain;
}
 
Example 4
Source Project: BlogManagePlatform   Source File: DurationLogAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 检测出耗时过高的方法调用并在日志中告警
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @throws Throwable
	 * @date 2018-12-21
	 */
	return (MethodInterceptor) invocation -> {
		String name = ReflectUtil.fullName(invocation.getMethod());
		long threshold = thresholdCache.get(name);
		long count = System.nanoTime();
		Object result = invocation.proceed();
		count = System.nanoTime() - count;
		if (count > threshold) {
			log.warn("{}方法耗时{}毫秒,触发超时警告!", name, count / times);
		}
		return result;
	};
}
 
Example 5
Source Project: BlogManagePlatform   Source File: RepeatAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 在请求前判断是否存在正在执行的请求
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @throws Throwable
	 * @date 2018-12-21
	 */
	return (MethodInterceptor) invocation -> {
		HttpServletRequest request = MVCUtil.request();
		String key = KeyGenerator.servletKey(ReflectUtil.fullName(invocation.getMethod()), request);
		try {
			if (checker.check(key)) {
				log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));
				return Result.repeatRequest();
			}
			checker.lock(key);
			return invocation.proceed();
		} finally {
			checker.free(key);
		}
	};
}
 
Example 6
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
	List<MethodInterceptor> interceptors = new ArrayList<>(3);
	Advice advice = advisor.getAdvice();
	if (advice instanceof MethodInterceptor) {
		interceptors.add((MethodInterceptor) advice);
	}
	for (AdvisorAdapter adapter : this.adapters) {
		if (adapter.supportsAdvice(advice)) {
			interceptors.add(adapter.getInterceptor(advisor));
		}
	}
	if (interceptors.isEmpty()) {
		throw new UnknownAdviceTypeException(advisor.getAdvice());
	}
	return interceptors.toArray(new MethodInterceptor[0]);
}
 
Example 7
Source Project: BlogManagePlatform   Source File: AsyncTimeoutAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 在一定时间段内拦截重复请求
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @date 2018-12-21
	 */
	return (MethodInterceptor) invocation -> {
		HttpServletRequest request = MVCUtil.request();
		String name = ReflectUtil.fullName(invocation.getMethod());
		String key = KeyGenerator.servletKey(name, request);
		if (checker.check(key)) {
			log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));
			return Result.repeatRequest().async();
		}
		checker.lock(key, timeoutCache.get(name));
		return invocation.proceed();
	};
}
 
Example 8
Source Project: phone   Source File: AopProxyUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
    if(!AopUtils.isAopProxy(proxy)) {
        return false;
    }
    ProxyFactory proxyFactory = null;
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if(AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if(advisors == null || advisors.length == 0) {
        return false;
    }

    for(Advisor advisor : advisors) {
        if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source Project: BlogManagePlatform   Source File: ParamLogAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 打印参数到日志
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @date 2019-01-12
	 */
	return (MethodBeforeAdvice) (method, args, target) -> {
		Parameter[] parameters = method.getParameters();
		Map<String, Object> paramMap = new HashMap<>(parameters.length);
		for (int i = 0; i < parameters.length; ++i) {
			paramMap.put(parameters[i].getName(), args[i]);
		}
		log.info("{} 请求参数:{}", ReflectUtil.fullName(method), JSONUtil.string(paramMap));
	};
}
 
Example 10
Source Project: BlogManagePlatform   Source File: LimitUserAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 请求限流
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @date 2018-12-21
	 */
	return (MethodInterceptor) invocation -> {
		Pair<RateLimiter, Long> pair = limitCache.get(ReflectUtil.fullName(invocation.getMethod()));
		if (!pair.getKey().tryAcquire(pair.getValue(), DefTime.UNIT)) {
			return Result.busy();
		}
		return invocation.proceed();
	};
}
 
Example 11
Source Project: BlogManagePlatform   Source File: ResultLogAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 打印返回值到日志
	 * @param JoinPoint AOP切点
	 * @author Frodez
	 * @date 2019-01-12
	 */
	return (AfterReturningAdvice) (returnValue, method, args, target) -> log.info("{} 返回值:{}", ReflectUtil.fullName(method), JSONUtil
		.string(returnValue));
}
 
Example 12
Source Project: BlogManagePlatform   Source File: ValidationAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 对参数进行验证
	 * @author Frodez
	 * @date 2019-05-10
	 */
	return (MethodInterceptor) invocation -> {
		String msg = ValidationUtil.validateParam(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
		return msg == null ? invocation.proceed() : Result.errorRequest(msg);
	};
}
 
Example 13
Source Project: elasticactors   Source File: ExecutorBeanPostProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
    ProxyFactoryBean factory = new ProxyFactoryBean();
    factory.setProxyTargetClass(cglibProxy);
    factory.addAdvice(advice);
    factory.setTarget(bean);
    return getObject(factory);
}
 
Example 14
@Override
public int getOrder() {
	if (this.order != null) {
		return this.order;
	}
	Advice advice = getAdvice();
	if (advice instanceof Ordered) {
		return ((Ordered) advice).getOrder();
	}
	return Ordered.LOWEST_PRECEDENCE;
}
 
Example 15
Source Project: BlogManagePlatform   Source File: AsyncValidationAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * AOP切点
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public Advice getAdvice() {
	/**
	 * 对参数进行验证
	 * @author Frodez
	 * @date 2019-05-10
	 */
	return (MethodInterceptor) invocation -> {
		String msg = ValidationUtil.validateParam(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
		return msg == null ? invocation.proceed() : Result.errorRequest(msg).async();
	};
}
 
Example 16
Source Project: spring4-understanding   Source File: AdvisedSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int indexOf(Advice advice) {
	Assert.notNull(advice, "Advice must not be null");
	for (int i = 0; i < this.advisors.size(); i++) {
		Advisor advisor = this.advisors.get(i);
		if (advisor.getAdvice() == advice) {
			return i;
		}
	}
	return -1;
}
 
Example 17
Source Project: spring4-understanding   Source File: AdvisedSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeAdvice(Advice advice) throws AopConfigException {
	int index = indexOf(advice);
	if (index == -1) {
		return false;
	}
	else {
		removeAdvisor(index);
		return true;
	}
}
 
Example 18
Source Project: spring4-understanding   Source File: AdvisedSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Is the given advice included in any advisor within this proxy configuration?
 * @param advice the advice to check inclusion of
 * @return whether this advice instance is included
 */
public boolean adviceIncluded(Advice advice) {
	if (advice != null) {
		for (Advisor advisor : this.advisors) {
			if (advisor.getAdvice() == advice) {
				return true;
			}
		}
	}
	return false;
}
 
Example 19
Source Project: lams   Source File: AdvisedSupport.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public int indexOf(Advice advice) {
	Assert.notNull(advice, "Advice must not be null");
	for (int i = 0; i < this.advisors.size(); i++) {
		Advisor advisor = this.advisors.get(i);
		if (advisor.getAdvice() == advice) {
			return i;
		}
	}
	return -1;
}
 
Example 20
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
	ProxyFactoryBean factory = new ProxyFactoryBean();
	factory.setProxyTargetClass(cglibProxy);
	factory.addAdvice(advice);
	factory.setTarget(bean);
	return getObject(factory);
}
 
Example 21
Source Project: BlogManagePlatform   Source File: CatchAndReturnAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Advice getAdvice() {
	return (MethodInterceptor) invocation -> {
		try {
			return invocation.proceed();
		} catch (Exception e) {
			log.error(StrUtil.concat("[", ReflectUtil.fullName(invocation.getMethod()), "]"), e);
			return Result.errorService();
		}
	};
}
 
Example 22
Source Project: sinavi-jfw   Source File: ExceptionQueueContextConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * リトライインタセプタ実行のアドバイスを指定し、DIコンテナに登録します。
 * @return {@link #statefulRetryOperationsInterceptor()}
 */
@Bean
public Advice[] advice() {
    return new Advice[] {
        statefulRetryOperationsInterceptor()
    };
}
 
Example 23
Source Project: spring4-understanding   Source File: ProxyFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look at bean factory metadata to work out whether this bean name,
 * which concludes the interceptorNames list, is an Advisor or Advice,
 * or may be a target.
 * @param beanName bean name to check
 * @return {@code true} if it's an Advisor or Advice
 */
private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) {
	Class<?> namedBeanClass = this.beanFactory.getType(beanName);
	if (namedBeanClass != null) {
		return (Advisor.class.isAssignableFrom(namedBeanClass) || Advice.class.isAssignableFrom(namedBeanClass));
	}
	// Treat it as an target bean if we can't tell.
	if (logger.isDebugEnabled()) {
		logger.debug("Could not determine type of bean with name '" + beanName +
				"' - assuming it is neither an Advisor nor an Advice");
	}
	return false;
}
 
Example 24
Source Project: spring4-understanding   Source File: AspectJAopUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the AspectJPrecedenceInformation provided by this advisor or its advice.
 * If neither the advisor nor the advice have precedence information, this method
 * will return {@code null}.
 */
public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) {
	if (anAdvisor instanceof AspectJPrecedenceInformation) {
		return (AspectJPrecedenceInformation) anAdvisor;
	}
	Advice advice = anAdvisor.getAdvice();
	if (advice instanceof AspectJPrecedenceInformation) {
		return (AspectJPrecedenceInformation) advice;
	}
	return null;
}
 
Example 25
Source Project: java-technology-stack   Source File: AdvisedSupport.java    License: MIT License 5 votes vote down vote up
@Override
public int indexOf(Advice advice) {
	Assert.notNull(advice, "Advice must not be null");
	for (int i = 0; i < this.advisors.size(); i++) {
		Advisor advisor = this.advisors.get(i);
		if (advisor.getAdvice() == advice) {
			return i;
		}
	}
	return -1;
}
 
Example 26
Source Project: brave   Source File: SpringRabbitTracing.java    License: Apache License 2.0 5 votes vote down vote up
/** Instruments an existing {@linkplain SimpleRabbitListenerContainerFactory} */
public SimpleRabbitListenerContainerFactory decorateSimpleRabbitListenerContainerFactory(
  SimpleRabbitListenerContainerFactory factory
) {
  Advice[] chain = factory.getAdviceChain();

  TracingRabbitListenerAdvice tracingAdvice = new TracingRabbitListenerAdvice(this);
  // If there are no existing advice, return only the tracing one
  if (chain == null) {
    factory.setAdviceChain(tracingAdvice);
    return factory;
  }

  // If there is an existing tracing advice return
  for (Advice advice : chain) {
    if (advice instanceof TracingRabbitListenerAdvice) {
      return factory;
    }
  }

  // Otherwise, add ours and return
  Advice[] newChain = new Advice[chain.length + 1];
  newChain[0] = tracingAdvice;
  System.arraycopy(chain, 0, newChain, 1, chain.length);
  factory.setAdviceChain(newChain);
  return factory;
}
 
Example 27
@Override
public Advice getAdvice() {
	return this.advice;
}
 
Example 28
@Override
public boolean supportsAdvice(Advice advice) {
	return (advice instanceof SimpleBeforeAdvice);
}
 
Example 29
private Advice instantiateAdvice(AspectJExpressionPointcut pointcut) {
	Advice advice = this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pointcut,
			this.aspectInstanceFactory, this.declarationOrder, this.aspectName);
	return (advice != null ? advice : EMPTY_ADVICE);
}
 
Example 30
@Override
public Advice getAdvice() {
	return this.interceptor;
}