KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MetricsFactory


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.lang.reflect.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39 import org.apache.oro.text.perl.*;
40
41 public class MetricsFactory {
42     private static final Perl5Util perl = new Perl5Util();
43
44     private String JavaDoc projectName;
45     private MetricsConfiguration configuration;
46
47     private Map projects = new HashMap();
48     private Map groups = new HashMap();
49     private Map classes = new HashMap();
50     private Map methods = new HashMap();
51
52     private Map includedProjects = new HashMap();
53     private Map includedGroups = new HashMap();
54     private Map includedClasses = new HashMap();
55     private Map includedMethods = new HashMap();
56     
57     public MetricsFactory(String JavaDoc projectName, MetricsConfiguration configuration) {
58         this.projectName = projectName;
59         this.configuration = configuration;
60     }
61
62     public String JavaDoc getProjectName() {
63         return projectName;
64     }
65
66     public MetricsConfiguration getConfiguration() {
67         return configuration;
68     }
69     
70     public Metrics createProjectMetrics() {
71         return createProjectMetrics(getProjectName());
72     }
73     
74     public Metrics createProjectMetrics(String JavaDoc name) {
75         Metrics result = (Metrics) projects.get(name);
76
77         if (result == null) {
78             result = buildProjectMetrics(name);
79             projects.put(name, result);
80         }
81         
82         return result;
83     }
84
85     private Metrics buildProjectMetrics(String JavaDoc name) {
86         Metrics result = new Metrics(name);
87
88         populateMetrics(result, getConfiguration().getProjectMeasurements());
89         
90         return result;
91     }
92     
93     public void includeProjectMetrics(Metrics metrics) {
94         includedProjects.put(metrics.getName(), metrics);
95     }
96
97     public Collection getProjectNames() {
98         return Collections.unmodifiableCollection(includedProjects.keySet());
99     }
100     
101     public Collection getProjectMetrics() {
102         return Collections.unmodifiableCollection(includedProjects.values());
103     }
104     
105     public Collection getAllProjectNames() {
106         return Collections.unmodifiableCollection(projects.keySet());
107     }
108     
109     public Collection getAllProjectMetrics() {
110         return Collections.unmodifiableCollection(projects.values());
111     }
112
113     public Metrics createGroupMetrics(String JavaDoc name) {
114         Metrics result = (Metrics) groups.get(name);
115
116         if (result == null) {
117             result = buildGroupMetrics(name);
118             groups.put(name, result);
119         }
120         
121         return result;
122     }
123
124     private Metrics buildGroupMetrics(String JavaDoc name) {
125         Metrics projectMetrics = createProjectMetrics();
126         Metrics result = new Metrics(projectMetrics, name);
127
128         populateMetrics(result, getConfiguration().getGroupMeasurements());
129
130         return result;
131     }
132     
133     public void includeGroupMetrics(Metrics metrics) {
134         includedGroups.put(metrics.getName(), metrics);
135         metrics.getParent().addSubMetrics(metrics);
136         includeProjectMetrics(metrics.getParent());
137     }
138
139     public Collection getGroupNames() {
140         return Collections.unmodifiableCollection(includedGroups.keySet());
141     }
142
143     public Collection getGroupMetrics() {
144         return Collections.unmodifiableCollection(includedGroups.values());
145     }
146
147     public Collection getAllGroupNames() {
148         return Collections.unmodifiableCollection(groups.keySet());
149     }
150
151     public Collection getAllGroupMetrics() {
152         return Collections.unmodifiableCollection(groups.values());
153     }
154
155     public Metrics createClassMetrics(String JavaDoc name) {
156         Metrics result = (Metrics) classes.get(name);
157
158         if (result == null) {
159             result = buildClassMetrics(name);
160             classes.put(name, result);
161         }
162         
163         return result;
164     }
165
166     private Metrics buildClassMetrics(String JavaDoc name) {
167         String JavaDoc packageName = "";
168         int pos = name.lastIndexOf('.');
169         if (pos != -1) {
170             packageName = name.substring(0, pos);
171         }
172         Metrics packageMetrics = createGroupMetrics(packageName);
173         Metrics result = new Metrics(packageMetrics, name);
174         
175         populateMetrics(result, getConfiguration().getClassMeasurements());
176
177         return result;
178     }
179     
180     public void includeClassMetrics(Metrics metrics) {
181         includedClasses.put(metrics.getName(), metrics);
182         metrics.getParent().addSubMetrics(metrics);
183         includeGroupMetrics(metrics.getParent());
184
185         Iterator i = getConfiguration().getGroups(metrics.getName()).iterator();
186         while (i.hasNext()) {
187             Metrics groupMetrics = createGroupMetrics((String JavaDoc) i.next());
188             groupMetrics.addSubMetrics(metrics);
189             includeGroupMetrics(groupMetrics);
190         }
191     }
192
193     public Collection getClassNames() {
194         return Collections.unmodifiableCollection(includedClasses.keySet());
195     }
196
197     public Collection getClassMetrics() {
198         return Collections.unmodifiableCollection(includedClasses.values());
199     }
200
201     public Collection getAllClassNames() {
202         return Collections.unmodifiableCollection(classes.keySet());
203     }
204     
205     public Collection getAllClassMetrics() {
206         return Collections.unmodifiableCollection(classes.values());
207     }
208
209     public Metrics createMethodMetrics(String JavaDoc name) {
210         Metrics result = (Metrics) methods.get(name);
211
212         if (result == null) {
213             result = buildMethodMetrics(name);
214             methods.put(name, result);
215         }
216         
217         return result;
218     }
219
220     private Metrics buildMethodMetrics(String JavaDoc name) {
221         String JavaDoc className = "";
222         if (perl.match("/^(.*)\\.[^\\.]*\\(.*\\)$/", name)) {
223             className = perl.group(1);
224         } else if (perl.match("/^(.*)\\.static {}$/", name)) {
225             className = perl.group(1);
226         } else if (perl.match("/^(.*)\\.[\\^.]*$/", name)) {
227             className = perl.group(1);
228         }
229         Metrics classMetrics = createClassMetrics(className);
230         Metrics result = new Metrics(classMetrics, name);
231         classMetrics.addSubMetrics(result);
232
233         populateMetrics(result, getConfiguration().getMethodMeasurements());
234
235         return result;
236     }
237
238     public void includeMethodMetrics(Metrics metrics) {
239         includedMethods.put(metrics.getName(), metrics);
240         metrics.getParent().addSubMetrics(metrics);
241         includeClassMetrics(metrics.getParent());
242     }
243     
244     public Collection getMethodNames() {
245         return Collections.unmodifiableCollection(includedMethods.keySet());
246     }
247
248     public Collection getMethodMetrics() {
249         return Collections.unmodifiableCollection(includedMethods.values());
250     }
251     
252     public Collection getAllMethodNames() {
253         return Collections.unmodifiableCollection(methods.keySet());
254     }
255
256     public Collection getAllMethodMetrics() {
257         return Collections.unmodifiableCollection(methods.values());
258     }
259
260     public void clear() {
261         projects.clear();
262         groups.clear();
263         classes.clear();
264         methods.clear();
265         
266         includedProjects.clear();
267         includedGroups.clear();
268         includedClasses.clear();
269         includedMethods.clear();
270     }
271     
272     private void populateMetrics(Metrics metrics, Collection descriptors) {
273         Iterator i = descriptors.iterator();
274         while (i.hasNext()) {
275             MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next();
276             try {
277                 metrics.track(descriptor.createMeasurement(metrics));
278             } catch (InstantiationException JavaDoc ex) {
279                 Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex);
280             } catch (IllegalAccessException JavaDoc ex) {
281                 Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex);
282             } catch (NoSuchMethodException JavaDoc ex) {
283                 Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex);
284             } catch (InvocationTargetException ex) {
285                 Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex);
286             }
287         }
288     }
289
290     public String JavaDoc toString() {
291         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
292
293         result.append("Factory for project \"").append(getProjectName()).append("\"").append(System.getProperty("line.separator", "\n"));
294
295         Iterator i;
296         
297         result.append("projects:").append(System.getProperty("line.separator", "\n"));
298         i = projects.entrySet().iterator();
299         while (i.hasNext()) {
300             Map.Entry entry = (Map.Entry) i.next();
301             result.append(" ").append(entry.getKey()).append(" -> ").append(((Metrics) entry.getValue()).getName()).append("").append(System.getProperty("line.separator", "\n"));
302         }
303         
304         result.append("groups:").append(System.getProperty("line.separator", "\n"));
305         i = groups.entrySet().iterator();
306         while (i.hasNext()) {
307             Map.Entry entry = (Map.Entry) i.next();
308             result.append(" ").append(entry.getKey()).append(" -> ").append(((Metrics) entry.getValue()).getName()).append("").append(System.getProperty("line.separator", "\n"));
309         }
310         
311         result.append("classes:").append(System.getProperty("line.separator", "\n"));
312         i = classes.entrySet().iterator();
313         while (i.hasNext()) {
314             Map.Entry entry = (Map.Entry) i.next();
315             result.append(" ").append(entry.getKey()).append(" -> ").append(((Metrics) entry.getValue()).getName()).append("").append(System.getProperty("line.separator", "\n"));
316         }
317         
318         result.append("methods:").append(System.getProperty("line.separator", "\n"));
319         i = methods.entrySet().iterator();
320         while (i.hasNext()) {
321             Map.Entry entry = (Map.Entry) i.next();
322             result.append(" ").append(entry.getKey()).append(" -> ").append(((Metrics) entry.getValue()).getName()).append("").append(System.getProperty("line.separator", "\n"));
323         }
324         
325         return result.toString();
326     }
327 }
328
Popular Tags