View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour;
22  
23  import net.sf.jour.filter.*;
24  import net.sf.jour.instrumentor.*;
25  import net.sf.jour.util.*;
26  
27  import java.util.*;
28  
29  import org.apache.log4j.Logger;
30  
31  /***
32   * @author michaellif  TODO To change the template for this generated type
33   *         comment go to Window - Preferences - Java - Code Style - Code
34   *         Templates
35   */
36  public class Config {
37  	
38  	protected static final Logger log = Logger.getLogger(Config.class);
39  
40      private static Config instance;
41  
42      public static final String CONFING_FILE = "jour.xml";
43  
44      private boolean isDebug;
45  
46      /***   Key - ClassFilter, value - Instrumentor  */
47      private HashMap instrumentors = new HashMap();
48  
49      private Config() throws InterceptorException {
50          initialize();
51      }
52  
53      public static Config getInstance() throws InterceptorException {
54          if (instance == null) {
55              instance = new Config();
56          }
57          return instance;
58      }
59  
60  	protected void initialize() throws InterceptorException {
61  	    net.sf.jour.config.Jour config = 
62  	       (net.sf.jour.config.Jour) ConfigFileUtil.unmarshalConfigFile(
63  	               CONFING_FILE, "net.sf.jour.config");
64          if (config != null) {
65              isDebug = config.isDebug();
66              List aspectList = config.getAspect();
67              for (Iterator i = aspectList.iterator(); i.hasNext();) {
68                  net.sf.jour.config.Aspect aspectCfg = (net.sf.jour.config.Aspect) i.next();
69                  if (aspectCfg.isEnabled()) {
70                      ClassFilter filter = createFilter(aspectCfg.getTypedef());
71                      //Pointcut[] pointcuts = createPointcuts(aspectCfg.getPointcut());
72                      PointcutListFilter pointcuts = new PointcutListFilter();
73                      pointcuts.readConfig(aspectCfg.getPointcut());
74                      Instrumentor instr = InstrumentorFactory.createInstrumentor(aspectCfg.getType(), pointcuts);
75                      instrumentors.put(filter, instr);
76                  }
77              }
78  
79          }
80      }
81  
82  	protected void checkUniqueAspect() throws ConfigException {
83          HashMap map = new HashMap();
84          Iterator iter = instrumentors.entrySet().iterator();
85          while (iter.hasNext()) {
86              String instrumentor = (String) iter.next();
87              map.put(instrumentor, "");
88          }
89          if (instrumentors.entrySet().size() > map.size()) {
90              throw new ConfigException("Duplicate aspects in jour.xml are not supported");
91          }
92  	}
93  
94  	protected ClassFilter createFilter(String typedef) {
95  	    return new ClassFilter(typedef);
96  	}
97  
98  	/* Removed: Use calss PointcutListFilter
99  	protected Pointcut [] createPointcuts(List pointcutList) {
100 	    ArrayList pointcutArray = new ArrayList();
101 	    Iterator iter = pointcutList.iterator();
102 	    while (iter.hasNext()) {
103 	        net.sf.jour.config.Pointcut element = (net.sf.jour.config.Pointcut) iter.next();
104 	        pointcutArray.add(new Pointcut(element.getExpr()));
105         }
106 	    return (Pointcut[])pointcutArray.toArray(new Pointcut[0]);
107 	}
108 	*/
109 
110     /***
111      * DOCUMENT ME!
112      *
113      * @param className DOCUMENT ME!
114      *
115      * @return DOCUMENT ME!
116      *
117      * @throws InterceptorException DOCUMENT ME!
118      */
119     public static Instrumentor[] getInstrumentors(String className)
120         throws InterceptorException {
121         ArrayList instrList = new ArrayList();
122         Iterator filters = getInstance().instrumentors.keySet().iterator();
123         while (filters.hasNext()) {
124             ClassFilter filter = (ClassFilter) filters.next();
125             if (filter.accept(className)) {
126                 instrList.add(instance.instrumentors.get(filter));
127             }
128 
129         }
130         return (Instrumentor[])instrList.toArray(new Instrumentor[0]);
131     }
132 }