001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
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 java.util.*;
021    import org.objectweb.jac.core.*;
022    import org.objectweb.jac.util.Log;
023    import org.objectweb.jac.wrappers.*;
024    
025    /**
026     * This sample aspect component traces the calls on all the objects of
027     * the program.
028     *
029     * <p>To be active this aspect component must be configured with the
030     * <code>addTrace</code> method.
031     *
032     * @see org.objectweb.jac.wrappers.VerboseWrapper */
033    
034    public class TracingAC extends AspectComponent implements TracingConf  {
035    
036        VerboseWrapper timedWrapper;
037        VerboseWrapper namedWrapper;
038        VerboseWrapper stackWrapper;
039        VerboseWrapper wrappersWrapper;
040    
041        public TracingAC () {
042            timedWrapper = new VerboseWrapper(this,VerboseWrapper.TIMED);
043            namedWrapper = new VerboseWrapper(this,VerboseWrapper.NAMED);
044            stackWrapper = new VerboseWrapper(this,VerboseWrapper.STACK);
045            wrappersWrapper = new VerboseWrapper(this,VerboseWrapper.WRAPPERS);
046        }
047        
048        public void addTrace(String wrappeeExpr, 
049                             String wrappeeClassExpr, 
050                             String wrappeeMethodExpr) {
051            pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
052                     timedWrapper, null);
053        }
054    
055        // TODO: Handle other types of traces
056    
057        public void addNamedTrace(String wrappeeExpr, 
058                                  String wrappeeClassExpr, 
059                                  String wrappeeMethodExpr) {
060            pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
061                     namedWrapper, null);
062        }
063    
064        public void addStackTrace(String wrappeeExpr, 
065                                  String wrappeeClassExpr, 
066                                  String wrappeeMethodExpr) {
067    
068            pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
069                     stackWrapper, null);
070        }
071    
072        public void addWrappersTrace(String wrappeeExpr, 
073                                     String wrappeeClassExpr, 
074                                     String wrappeeMethodExpr) {
075            pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
076                     wrappersWrapper,null);
077        }
078    
079        public void addRecording(String wrappeeExpr, 
080                                 String wrappeeClassExpr, 
081                                 String wrappeeMethodExpr) {
082    
083            pointcut(wrappeeExpr+" && !recorder0",
084                     wrappeeClassExpr,
085                     wrappeeMethodExpr,
086                     RecordingWrapper.class.getName(), null, false);
087        }
088    
089        Hashtable counters = new Hashtable();
090    
091        public void addCounter(String name,
092                               String wrappeeExpr,
093                               String wrappeeClassExpr, 
094                               String wrappeeMethodExpr) {
095    
096            Counter c = (Counter) counters.get(name);
097            if (c == null) {
098                c = new Counter();
099                counters.put(name, c);
100            }
101            pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
102                     new SimpleCountingWrapper(this,c), null);
103        }
104    
105        public void addOptimizedCounter(String name,
106                                        String wrappeeExpr, 
107                                        String wrappeeClassExpr, 
108                                        String wrappeeMethodExpr,
109                                        String fieldName,
110                                        String argNumber) {
111          
112            Counter c = (Counter) counters.get(name);
113            if (c == null) {
114                c = new Counter();
115                counters.put(name,c);
116            }
117            if (!fieldName.equals("")) {
118                pointcut(wrappeeExpr, wrappeeClassExpr, 
119                         wrappeeMethodExpr, 
120                         new OptimizedCountingWrapper(this,c,fieldName), 
121                         null);
122            }
123            if (!argNumber.equals("")) {
124                pointcut(wrappeeExpr, wrappeeClassExpr, 
125                         wrappeeMethodExpr, 
126                         new OptimizedCountingWrapper(
127                             this, c, (new Integer(argNumber)).intValue()), 
128                         null );
129            }
130        }
131    
132        /**
133         * Skips the counting wrapper if this call is part of a global
134         * conting optimization performed by an
135         * <code>OptimizedCountingWrapper</code>
136         *
137         * @param wrapper the wrapper 
138         * @param wrappingMethod the wrapping method that is about to be
139         * run
140         * @see OptimizedCountingWrapper */
141    
142        public boolean beforeRunningWrapper (Wrapper wrapper, 
143                                             String wrappingMethod) {
144            if (attr("tracing.globalIncr")!=null) 
145                return false;
146            return true;
147        }
148    
149    
150    }