org.newhart.japi.aspect.AuditAspect.java Source code

Java tutorial

Introduction

Here is the source code for org.newhart.japi.aspect.AuditAspect.java

Source

/**
 * Copyright 2012-2013 Clint Combs
 *
 * 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 org.newhart.japi.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.newhart.mongodb.AuditStore;
import java.net.UnknownHostException;
import java.util.List;
import java.util.LinkedList;
import org.newhart.japi.annotation.AuditData;
import com.mongodb.Mongo;
import com.mongodb.casbah.MongoConnection;

@Aspect
public class AuditAspect {

    private static AuditStore store;

    @Pointcut("@annotation(org.newhart.japi.annotation.Audit) && execution(* *(..))")
    public void auditMethod() {
    }

    @Before(value = "auditMethod()")
    public void beforeAuditMethod(final JoinPoint joinPoint) {
        System.out.println("data arguments: " + getDataArgs(joinPoint));
    }

    private List<Object> getDataArgs(final JoinPoint joinPoint) {
        final List<Object> dataArgs = new LinkedList<Object>();

        final Object[] args = joinPoint.getArgs();
        final Signature sig = joinPoint.getSignature();
        final MethodSignature methodSig = (sig instanceof MethodSignature) ? (MethodSignature) sig : null;

        //System.out.println("beforeAuditMethod(" + joinPoint + ", " + joinPoint.getKind()+ ")... args " + (args == null?"is null":"has length " + args.length) + ", signature=" + joinPoint.getSignature());
        if (methodSig != null) {
            final Method method = methodSig.getMethod();
            final Annotation annotations[][] = method.getParameterAnnotations();
            int i = 0;
            for (Annotation parameter[] : annotations) {
                boolean isDataParameter = false;
                for (Annotation a : parameter) {
                    if (a instanceof AuditData) {
                        isDataParameter = true;
                    }
                }
                if (isDataParameter) {
                    dataArgs.add(args[i]);
                }
                i++;
            }
        }

        return dataArgs;
    }

    @AfterReturning(pointcut = "auditMethod()", returning = "result")
    public void afterAuditMethodReturning(final JoinPoint joinPoint, final Object result) {
        // audit result
        System.out.println("afterAuditMethodReturning(" + joinPoint + ", " + result + ")");
    }

    @AfterThrowing(pointcut = "auditMethod()", throwing = "e")
    public void afterAuditMethodThrowing(final JoinPoint joinPoint, final Exception e) {
        // audit exception
        System.out.println("afterAuditMethodThrowing(" + joinPoint + ", " + e + ")");
    }
}