001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak 
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.tracing;
019    
020    import org.aopalliance.intercept.ConstructorInvocation;
021    import org.aopalliance.intercept.MethodInvocation;
022    import org.objectweb.jac.core.*;
023    
024    /**
025     * This simple counter must wrap the methods of which calls have to be
026     * counted.
027     *
028     * <p>In some cases, optimizations can be achieved by grouping
029     * counts. See <code>OptimizedCountingWrapper</code>.
030     *
031     * @see OptimizedCountingWrapper */
032    
033    public class SimpleCountingWrapper extends Wrapper {
034    
035        /** Stores the counter. */
036        Counter counter = null;
037    
038        /**
039         * Creates a new wrapper that uses the given counter.
040         *
041         * @param counter the counter */
042    
043        public SimpleCountingWrapper(AspectComponent ac, Counter counter) {
044            super(ac);
045            this.counter = counter;
046        }
047    
048        /**
049         * This wrapping method increments the counter when the wrapped
050         * method is called.
051         *
052         * @return the return value of the wrapped method */
053    
054        public Object incr(Interaction interaction) {
055            Object ret = proceed(interaction);
056            counter.incr(1);
057            printCounter();
058            return ret;
059        }
060    
061        /** Role method: set the counter value.
062         *
063         * @param value the new counter value
064         * @see #getCounter()
065         * @see #incr(Interaction) */
066    
067        public void setCounter(int value) {
068            counter.set(value);
069        }
070       
071        /** Role method: get the counter value.
072         *
073         * @return the counter value
074         * @see #setCounter(int)
075         * @see #incr(Interaction) */
076    
077        public int getCounter() {
078            return counter.get();
079        }
080    
081        /**
082         * Prints the counter in <code>System.out</code>.
083         */
084        public void printCounter() {
085            System.out.println("<<< Counting aspect says : " + 
086                               counter.get() + 
087                               " line(s) printed. >>>");
088        }
089    
090        public Object invoke(MethodInvocation invocation) throws Throwable {
091            return incr((Interaction)invocation);
092        }
093    
094        public Object construct(ConstructorInvocation invocation) throws Throwable {
095            return incr((Interaction)invocation);
096        }  
097    
098    }
099    
100    
101