Coverage Report - org.jtheque.core.spring.aspect.PhasesCounterAndLoggingAspect
 
Classes in this File Line Coverage Branch Coverage Complexity
PhasesCounterAndLoggingAspect
0 %
0/14
0 %
0/2
1.25
 
 1  
 package org.jtheque.core.spring.aspect;
 2  
 
 3  
 import org.apache.log4j.Logger;
 4  
 import org.aspectj.lang.JoinPoint;
 5  
 import org.aspectj.lang.annotation.After;
 6  
 import org.aspectj.lang.annotation.Aspect;
 7  
 import org.aspectj.lang.annotation.Before;
 8  
 import org.aspectj.lang.annotation.Pointcut;
 9  
 import org.jtheque.core.managers.Managers;
 10  
 import org.jtheque.core.managers.lifecycle.JThequeCoreTimer;
 11  
 import org.jtheque.core.managers.log.ILoggingManager;
 12  
 
 13  
 import java.util.HashMap;
 14  
 import java.util.Map;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * An AspectJ aspect to log the phases and calc the time needed.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  
 @Aspect
 38  0
 public final class PhasesCounterAndLoggingAspect {
 39  0
     private final Map<String, Long> startTimes = new HashMap<String, Long>(3);
 40  
 
 41  
     /**
 42  
      * A point cut to the run method of the Phase.
 43  
      */
 44  
     @Pointcut("execution(* org.jtheque.core.managers.lifecycle.phases.*Phase.run())")
 45  0
     public void run() { /* Point cut */}
 46  
 
 47  
     /**
 48  
      * Start the counter before the run method.
 49  
      *
 50  
      * @param joinPoint The join point of the advised method.
 51  
      */
 52  
     @Before("run()")
 53  
     public void startCounter(JoinPoint joinPoint) {
 54  0
         String phase = getPhase(joinPoint);
 55  
 
 56  0
         Logger.getLogger(getClass()).trace("Phase " + phase + " started");
 57  
 
 58  0
         startTimes.put(phase, System.currentTimeMillis());
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Stop the counter after the run method.
 63  
      *
 64  
      * @param joinPoint The join point of the advised method.
 65  
      */
 66  
     @After("run()")
 67  
     public void stopCounter(JoinPoint joinPoint) {
 68  0
         String phase = getPhase(joinPoint);
 69  
 
 70  0
         long time = System.currentTimeMillis() - startTimes.get(phase);
 71  
 
 72  0
         Managers.getManager(ILoggingManager.class).getLogger(getClass()).trace("Phase {} finished in {}ms", phase, time);
 73  
 
 74  0
         if ("Third".equals(phase)) {
 75  0
             JThequeCoreTimer.stop();
 76  
         }
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Return the phase
 81  
      *
 82  
      * @param joinPoint The join point of the advised method.
 83  
      * @return The phase of the join point.
 84  
      */
 85  
     private static String getPhase(JoinPoint joinPoint) {
 86  0
         return joinPoint.getSourceLocation().getWithinType().getSimpleName().replace("Phase", "");
 87  
     }
 88  
 }