List of usage examples for org.aspectj.lang.reflect MethodSignature getMethod
Method getMethod();
From source file:com.vmware.bdd.aop.tx.RetryTransactionAdvice.java
License:Open Source License
public void retry(ProceedingJoinPoint pjp) throws Throwable { logger.info("retry transaction"); int retriesLeft = 5; String methodName = pjp.getSignature().getName(); MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); if (method.getDeclaringClass().isInterface()) { method = pjp.getTarget().getClass().getDeclaredMethod(methodName, method.getParameterTypes()); }/*from w w w . j a v a2 s . c o m*/ Annotation[] annotations = method.getDeclaredAnnotations(); for (Annotation a : annotations) { if (a instanceof RetryTransaction) { RetryTransaction retryAnno = (RetryTransaction) a; retriesLeft = retryAnno.value(); } } RetryTransaction retryTrx = AnnotationUtils.findAnnotation(method, RetryTransaction.class); retriesLeft = retryTrx.value(); Throwable rootCause = null; boolean success = false; while (!success) { try { pjp.proceed(); success = true; } catch (Throwable ex) { rootCause = (ex instanceof BddException) ? ex.getCause() : ex; if (isRetryable(rootCause)) { if (retriesLeft > 0) { retriesLeft--; } else { throw TxRetryException.wrap(rootCause, false); } } else if (isUniqViolation(rootCause)) { throw UniqueConstraintViolationException.wrap((ConstraintViolationException) rootCause); } else { throw BddException.wrapIfNeeded(ex, "Exception in a DAL transaction."); } } } if (!success) { if (rootCause != null) { logger.warn("retry transaction failed.", rootCause); throw rootCause; } else { logger.warn("retry transction failed."); throw new Exception("retry transaction failed"); } } else { if (rootCause != null) { logger.warn("retry transaction completed. Failure root cause:" + rootCause.getMessage()); } else { logger.info("normal operation"); } } }
From source file:com.vsct.supervision.notification.log.LoggingAspect.java
License:Open Source License
@Around(value = "@within(com.vsct.supervision.notification.log.Loggable) || @annotation(com.vsct.supervision.notification.log.Loggable)") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { final MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature(); final Method method = signature.getMethod(); final Class clazz = signature.getClass(); final Loggable loggableMethod = method.getAnnotation(Loggable.class); final Loggable loggableClass = proceedingJoinPoint.getTarget().getClass().getAnnotation(Loggable.class); //get current log level final LogLevel logLevel = loggableMethod != null ? loggableMethod.value() : loggableClass.value(); final String service = StringUtils.isNotBlank(loggableClass.service()) ? loggableClass.service() : clazz.getName();//from ww w . j a v a 2 s . c o m final String methodName = StringUtils.isNotBlank(loggableClass.method()) ? loggableClass.method() : method.getName(); final String star = "**********"; //before LogWriter.write(proceedingJoinPoint.getTarget().getClass(), logLevel, star + service + "." + methodName + "() start execution" + star); //show traceParams final boolean showParams = loggableMethod != null ? loggableMethod.traceParams() : loggableClass.traceParams(); if (showParams) { if (proceedingJoinPoint.getArgs() != null && proceedingJoinPoint.getArgs().length > 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < proceedingJoinPoint.getArgs().length; i++) { sb.append(method.getParameterTypes()[i].getName() + ":" + proceedingJoinPoint.getArgs()[i]); if (i < proceedingJoinPoint.getArgs().length - 1) sb.append(", "); } LogWriter.write(proceedingJoinPoint.getTarget().getClass(), logLevel, service + "." + methodName + "() args " + sb); } } final long startTime = System.currentTimeMillis(); //start method execution final Object result = proceedingJoinPoint.proceed(); final long endTime = System.currentTimeMillis(); //show results if (result != null) { boolean showResults = loggableMethod != null ? loggableMethod.traceResult() : loggableClass.traceResult(); if (showResults) { LogWriter.write(proceedingJoinPoint.getTarget().getClass(), logLevel, service + "." + methodName + "() Result : " + result); } } //show after LogWriter.write(proceedingJoinPoint.getTarget().getClass(), logLevel, star + service + "." + methodName + "() finished execution and takes " + (endTime - startTime) + " millis time to execute " + star); return result; }
From source file:com.wuxiansen.beehive.core.security.SecurityAspect.java
License:Open Source License
public Object execute(ProceedingJoinPoint pjp) throws Throwable { // ?/*from w w w. j a v a2s. c om*/ MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); Method method = methodSignature.getMethod(); // if (method.isAnnotationPresent(IgnoreSecurity.class)) { return pjp.proceed(); } // request header ?? token String token = WebContext.getRequest().getHeader(tokenName); // token if (!tokenManager.checkToken(token)) { String message = String.format("token [%s] is invalid", token); throw new TokenException(message); } // return pjp.proceed(); }
From source file:com.zh.snmp.snmpcore.aop.BaseInterceptor.java
License:Open Source License
/** * Visszaadja a hvott metdust, mint reflexis objektum. * * @param joinPoint kapcsoldsi pont arra a metdusra, amit az aspektus figyel * @return//from ww w. ja v a2 s. co m */ protected Method retrieveTargetMethodFrom(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); return methodSignature.getMethod(); }
From source file:de.codecentric.capturereplay.CaptureReplayAdvice.java
License:Apache License
@Around("execution(@de.codecentric.capturereplay.Capturable * *(..))") public Object aroundCapturableMethod(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); if (Mode.CAPTURE.equals(mode)) { Object returnValue = pjp.proceed(); dataMapper.writeCapturedData(signature, returnValue, pjp.getArgs()); return returnValue; } else if (Mode.REPLAY.equals(mode)) { return dataMapper.getCapturedData(signature.getMethod().getName(), pjp.getArgs()); } else if (Mode.DISABLED.equals(mode)) { return pjp.proceed(); } else {//from w ww . ja v a 2 s . c om throw new IllegalCaptureReplayUsageException( String.format("Capturing/replaying is switched off. You should not use %s directly.", this.getClass().getSimpleName())); } }
From source file:de.codecentric.capturereplay.data.JsonDataMapper.java
License:Apache License
@Override public void writeCapturedData(MethodSignature signature, Object returnValue, Object[] arguments) throws DataMappingException { Method method = signature.getMethod(); String captureFileName = getCaptureFileName(method, arguments); try {//from www.j ava2 s. c o m File captureFile = captureFileProvider.getCaptureFile(captureFileName); enableTypeSupport(); objectMapper.writeValue(captureFile, new TypeWrapper(returnValue.getClass().getCanonicalName(), returnValue)); } catch (Exception e) { throw new DataMappingException(String.format("Could not write test data to file %s.", captureFileName), e); } }
From source file:de.codecentric.spring.boot.chaos.monkey.watcher.ChaosMonkeyBaseAspect.java
License:Apache License
String createSignature(MethodSignature signature) { return signature.getDeclaringTypeName() + "." + signature.getMethod().getName(); }
From source file:de.dan_nrw.caching.CachingAspect.java
License:Open Source License
@Around("cachablePointcut()") public Object aroundCachable(final ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Cachable cachableAnnotation = (Cachable) methodSignature.getMethod().getAnnotation(Cachable.class); Cache cache = Cache.getCurrent();/*from w w w.ja va2s . c om*/ try { if (cache.containsKey(cachableAnnotation.key())) { // if key is already in cache and data is not expired return cache.get(cachableAnnotation.key()); } } catch (IllegalArgumentException ex) { // if key does not exists, data must be retrieved } catch (CachedDataExpiredException ex) { // if data stored in cache is expired, it must be retrieved } // determinig data Object data = joinPoint.proceed(); // cache is filled if (data != null) { cache.put(cachableAnnotation.key(), data, cachableAnnotation.durability()); } // returning result return data; }
From source file:de.escidoc.core.aa.security.aop.SecurityInterceptor.java
License:Open Source License
/** * Around advice to perform the authorization of the current request. * <p/>//from w w w . ja v a 2s . c o m * This method is called every time the Interceptor is intercepting a method call. * <p/> * It does the following steps: <ul> <li>Fetch the credentials (techUser, handle) of the current user from class * {@code UserContext}.</li> <li>Checks the technical username. Has to be either <ul> * <li>{@code ShibbolethUser}, which means that the service has been invoked from via a webservice, </li> * <li>{@code internal}, which means that the service has been called internally from another component and * {@code INTERNAL_INTERCEPTION} is turned off, or</li> <li>{@code authorization}, which means that the * service has been called internally from the authorization component.</li> </ul> <li>In case the technical * username is {@code internal}, no further security checks are done, the intercepted method is invoked and its * return value is returned to the originally invoking method.</li> <li>In case the technical username is * {@code ShibbolethUser}, the following steps are executed.</li> <li>The private method * {@code doAuthentication} is called, which returns the "real" username for the handle fetched from * {@code UserContext}.</li> <li>The private method {@code doAuthorisation} is called, which calls the * XACML engine with the current input parameters in order to decide whether invoking the intercepted method is * permitted or denied. In case of denial, an exception is thrown.</li> <li>The intercepted method is invoked, * returning some return values.</li> <li>If the return values are a list of objects, these have to filtered before * returned to the invoking service. For this the private method {@code doFiltering} is called, which returns * the (filtered) return value of the intercepted method.</li> <li>The (filtered) return value of the intercepted * method is returned back to the invoking service.</li> </ul> * * @param joinPoint The current {@link ProceedingJoinPoint}. * @throws Throwable Thrown in case of an error. * @return */ @Around("execution(public * de.escidoc.core.*.service.*.*(..))" + " && !within(de.escidoc.core.aa.service.EscidocUserDetailsService)" + " && !within(de.escidoc.core.common.util.aop..*)") public Object authorize(final ProceedingJoinPoint joinPoint) throws Throwable { final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); final Method calledMethod = methodSignature.getMethod(); final String target = getTargetInterface(joinPoint); final String methodName = calledMethod.getName(); final String handle = UserContext.getHandle(); // ------------------- // --- Preparation --- // ------------------- if (LOGGER.isDebugEnabled()) { LOGGER.debug(StringUtility.concatenateWithColonToString("The callee", target)); LOGGER.debug(StringUtility.concatenateWithColonToString("Method name", methodName)); LOGGER.debug(StringUtility.concatenateWithColonToString("The handle/password", handle)); } final Object[] arguments = joinPoint.getArgs(); if (LOGGER.isDebugEnabled()) { if (arguments.length > 0) { LOGGER.debug(StringUtility.concatenateWithColon("First Argument", arguments[0]).toString()); } else { LOGGER.debug("Method called without arguments."); } } // --------------------- // --- Authorization --- // --------------------- // authorization is not performed if the current request is executed as // an internal user. Only external users are authorized. if (!UserContext.isInternalUser()) { // Calls from the authorization component to other components run // with privileges of the internal authorization user (superuser). // They will not be further intercepted. UserContext.runAsInternalUser(); doAuthorisation(target, methodName, arguments); // -------------------- // --- Continuation --- // -------------------- // if everything is fine, finally call the method. // This method runs with privileges of an internal user that will // not be // further intercepted, as the access to the resource has been // granted, // now. } try { return proceed(joinPoint); } catch (final ResourceNotFoundException e) { // see issue 475, 500 // this exception may be thrown if the user tries to access // a versionized resource without providing the version number. // If the access is denied for the latest version, the business // logic is asked to retrieve the latest release. If no release // exists, a Resource not found exception is thrown containing // an error message indicating the missing release. // As this is an authorization failure, this kind of // ResourceNotFoundException must be caught and a // AuthorizationException has to be thrown, instead if (UserContext.isRetrieveRestrictedToReleased() && ERR_MSG_LATEST_RELEASE_NOT_FOUND.equals(e.getMessage())) { throw createAuthorizationException(target, methodName, arguments); } else { throw e; } } }
From source file:de.escidoc.core.common.util.aop.ParameterCheckInterceptor.java
License:Open Source License
@Before("execution(public * de.escidoc.core.*.service.*.*(..))" + " && !execution(* de.escidoc.core..*.PolicyDecisionPoint*.evaluateRoles(..))" + " && !execution(* de.escidoc.core..*.PolicyDecisionPoint*.getRoleUserWhereClause(..))" + " && !execution(* de.escidoc.core..*.PolicyDecisionPoint*.findAttribute(..))" + " && (!execution(* de.escidoc.core..*.*HandlerInterface.retrieve*List(String))" + " || execution(* de.escidoc.core.aa..*.*HandlerInterface.retrieve*List(String)))" + " && !execution(* de.escidoc.core..*.Fedora*Handler*.*(..))") public void checkParameters(final JoinPoint joinPoint) throws MissingMethodParameterException { final Object[] arguments = joinPoint.getArgs(); final int length = arguments.length; for (int i = 0; i < length; ++i) { if (arguments[i] == null || "".equals(arguments[i])) { final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); final Method calledMethod = methodSignature.getMethod(); final String target = methodSignature.getDeclaringTypeName(); throw new MissingMethodParameterException( StringUtility.format("The parameter at position " + (i + 1) + " must be provided", target + '.' + calledMethod.getName())); }/*from ww w . j a v a 2 s . c o m*/ } }