Coverage Report - org.kuali.student.enrollment.lpr.service.aspect.ServiceAspectLayering
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceAspectLayering
0%
0/26
0%
0/16
2.5
 
 1  
 package org.kuali.student.enrollment.lpr.service.aspect;
 2  
 
 3  
 import java.lang.reflect.Method;
 4  
 
 5  
 import java.util.ArrayList;
 6  
 import java.util.List;
 7  
 
 8  
 import org.aspectj.lang.JoinPoint;
 9  
 import org.aspectj.lang.ProceedingJoinPoint;
 10  
 import org.kuali.student.common.exceptions.OperationFailedException;
 11  
 
 12  
 
 13  
 
 14  
 
 15  
 
 16  
 
 17  
 /**
 18  
  * 
 19  
  * @author sambit
 20  
  * 
 21  
  * @param <T> the service this aspect is operating on
 22  
  */
 23  0
 public class ServiceAspectLayering<T,P>   {
 24  
 
 25  
         List <T>  serviceImplObjs; 
 26  
 
 27  
         List<Throwable> includeThrowableClassList ;
 28  
 
 29  
         List <P> servicePostProcessClasses;
 30  
 
 31  
         public void setServicePostProcessClasses(List<P> servicePostProcessClasses) {
 32  0
                 this.servicePostProcessClasses = servicePostProcessClasses;
 33  0
         }
 34  
         public void setServiceImpls(List<T> serviceImplObjs) {
 35  0
                 this.serviceImplObjs = serviceImplObjs;
 36  0
         }
 37  
         public void setIncludeThrowableClassList(List <Throwable> serviceExceptionTypes) {
 38  0
                 this.includeThrowableClassList = serviceExceptionTypes;
 39  0
         }
 40  
         /**
 41  
          * Aspect method, this is the controller which invokes other impl method using reflection. 
 42  
          * One of this method could be defined per service or there could just be one generic method for the services in a package. 
 43  
          * @param call
 44  
          * @throws Throwable
 45  
          */
 46  
         public Object performLayeringCalls(ProceedingJoinPoint call) throws Throwable{
 47  
 
 48  0
                 for (Object service : serviceImplObjs){
 49  
 
 50  0
                         for (Method m : service.getClass().getMethods()){
 51  0
                                 if (m.getName().equals(call.getSignature().getName())) {
 52  0
                                         m.invoke(service, call.getArgs());
 53  0
                                         break;
 54  
                                 }
 55  
                         }
 56  
                 }
 57  0
                 return call.proceed();
 58  
         }
 59  
 
 60  
         /**
 61  
          * 
 62  
          * @param ex
 63  
          * @return
 64  
          * @throws Throwable
 65  
          */
 66  
         public void handleExceptions(Throwable ex) throws OperationFailedException{
 67  
 
 68  0
                 if (!includeThrowableClassList.contains(ex)) {
 69  0
                         throw new OperationFailedException(ex.getMessage());
 70  
                 }
 71  0
         }
 72  
 
 73  
 
 74  
 
 75  
         /**
 76  
          * The advice used after return 
 77  
          */
 78  
 
 79  
         public void afterReturning( JoinPoint call, Object returnValue) throws Throwable {
 80  
 
 81  0
                 for (Object servicePostProcessClass : servicePostProcessClasses){
 82  
 
 83  0
                         if ( servicePostProcessClass.getClass().getName().contains(call.getTarget().getClass().getName())){        
 84  
 
 85  0
                                 for (Method m : servicePostProcessClass.getClass().getMethods()){
 86  0
                                         if (m.getName().equals(call.getSignature().getName())) {
 87  0
                                                 List  <Object> argsList = new ArrayList<Object>();
 88  0
                                                 argsList.add(0,returnValue);
 89  0
                                                 argsList.add (1,call.getArgs());        
 90  0
                                                 m.invoke( servicePostProcessClass, argsList.toArray());
 91  0
                                                 break;
 92  
                                         }
 93  
                                 }
 94  
                         }
 95  
                 }
 96  0
         }
 97  
 
 98  
 }
 99