/*
* Copyright 2010 Nabil Ben Said.
*
* 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.
*/
package net.greencoding.thysdrus.circuitbreaker.aspectj;
import net.greencoding.thysdrus.circuitbreaker.annotation.MonitoredByCircuitBreaker;
import net.greencoding.thysdrus.circuitbreaker.core.handler.CircuitBreakerHandler;
import net.greencoding.thysdrus.circuitbreaker.core.handler.DefaultCircuitBreakerHandler;
import net.greencoding.thysdrus.circuitbreaker.core.handler.MethodInvocationResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Nabil Ben Said (nabil.bensaid@gmail.com)
*
*/
@Aspect
public class CircuitBreakerAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
//private final static String ANNOTATION_SIGNATURE = MonitoredByCircuitBreaker.class.getName();
private final static String ANNOTATION_SIGNATURE = "net.greencoding.thysdrus.circuitbreaker.annotation.MonitoredByCircuitBreaker";
private final static String POINTCUT = "execution(@"+ ANNOTATION_SIGNATURE + " * * (..)) && @annotation(annotation)";
private AnnotationDiscoverer annotationDiscoverer = new CircuitBreakerAnnotationDiscoverer();
public CircuitBreakerAspect() {
annotationDiscoverer.discover();
}
private CircuitBreakerHandler cbHandler = DefaultCircuitBreakerHandler.getSingleton() ;
@Around(POINTCUT)
public Object breakCircuit(final ProceedingJoinPoint pjp, final MonitoredByCircuitBreaker annotation) throws Throwable {
String circuitBreakerKey = getCircuitBreakerKey(pjp.getSignature().toLongString(), annotation);
logger.info("CB: {}", circuitBreakerKey);
MethodInvocationResult methodInvocationResult = cbHandler.handleMethodInvocation(pjp, circuitBreakerKey);
if (methodInvocationResult.getReturnObject() != null) {
return methodInvocationResult.getReturnObject();
} else if (methodInvocationResult.getCause() != null){
throw methodInvocationResult.getCause();
} else {
return null;
}
}
private String getCircuitBreakerKey(final String methodSignature, final MonitoredByCircuitBreaker cbAnnotation) {
return cbAnnotation.groupId().isEmpty() ? methodSignature : cbAnnotation.groupId();
}
}