1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
99
100
101
102
103
104
105
106
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 }