KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > ConfigAttributor


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
//
20

21 package gov.nasa.jpf.jvm;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import org.apache.bcel.classfile.JavaClass;
32 import org.apache.bcel.classfile.Method;
33
34 /**
35  * A configuration file-driven attributor so that we can tailor JPF's
36  * attributor based on the application under test.
37  *
38  * The input file looks like:
39  * # This is a comment
40  * [atomic]
41  * <class regex> <method regex> (true|false)
42  * ...
43  * [relevance]
44  * <class regex> <method regex> (always|never|runnables|sync)
45  * ...
46  *
47  * @author Owen O'Malley
48  */

49 public class ConfigAttributor extends DefaultAttributor {
50
51   public ConfigAttributor() {
52     try {
53       parse("jpf-attributes");
54     } catch (FileNotFoundException JavaDoc e) {
55     } catch (IOException JavaDoc e) {
56       System.err.println("IO exception reading attribute file: " + e);
57       System.err.println("Ignoring file");
58       relevance_rules.clear();
59       atomic_rules.clear();
60     }
61   }
62
63   public boolean isMethodAtomic (JavaClass jc, Method mth, String JavaDoc uniqueName) {
64     String JavaDoc cls = jc.getClassName();
65     Iterator JavaDoc itr = atomic_rules.iterator();
66     while (itr.hasNext()) {
67       NameRule rule = (NameRule) itr.next();
68       if (rule.isMatch(cls, uniqueName)) {
69         System.out.println("Found atomic rule for " + cls + ":" +
70                              uniqueName);
71         return rule.getResult() == 1;
72       }
73     }
74     // if no rules matched, use the default rules
75
return super.isMethodAtomic(jc, mth, uniqueName);
76   }
77
78   /**
79    * is calling a certain method in the context of multiple runnable threads
80    * scheduling relevant (i.e. has to be considered as a step boundary
81    */

82   public int getSchedulingRelevance (JavaClass jc, Method mth, String JavaDoc uniqueName) {
83     String JavaDoc cls = jc.getClassName();
84     Iterator JavaDoc itr = relevance_rules.iterator();
85     while (itr.hasNext()) {
86       NameRule rule = (NameRule) itr.next();
87       if (rule.isMatch(cls, uniqueName)) {
88         int result = rule.getResult();
89         System.out.println("Found scheduling rule for " + cls + ":" +
90                              uniqueName + " = " + result);
91         return result;
92       }
93     }
94     // if no rules matched, use the default rules
95
return super.getSchedulingRelevance(jc, mth, uniqueName);
96   }
97
98   private void parse(String JavaDoc filename
99                     ) throws FileNotFoundException JavaDoc, IOException JavaDoc {
100     BufferedReader JavaDoc config_file = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
101     System.out.println("Using JPF attributes from " + filename);
102     String JavaDoc line = config_file.readLine();
103     String JavaDoc current_section = null;
104     while (line != null) {
105       line = line.trim();
106       if (line.length() != 0 && line.charAt(0) != '#') {
107         if (line.charAt(0) == '[') {
108           current_section = parseSection(line);
109         } else if ("relevance".equals(current_section)) {
110           parseRelevanceRule(line);
111         } else if ("atomic".equals(current_section)) {
112           parseAtomicRule(line);
113         } else {
114           System.err.println("Rules found in section " + current_section +
115                                ", which is unknown.");
116         }
117       }
118       line = config_file.readLine();
119     }
120   }
121
122   private static final Pattern JavaDoc section_pattern =
123     Pattern.compile("\\[([a-zA-Z]+)\\]");
124
125   private String JavaDoc parseSection(String JavaDoc line) {
126     Matcher JavaDoc match = section_pattern.matcher(line);
127     if (match.matches()) {
128       return match.group(1);
129     }
130     System.err.println("Problem parsing section name " + line);
131     return null;
132   }
133
134   private static final Pattern JavaDoc relevance_rule_pattern =
135     Pattern.compile("([^ ]+) +([^ ]+) +(always|never|neverbody|runnables|sync)");
136
137   private void parseRelevanceRule(String JavaDoc line) {
138     Matcher JavaDoc match = relevance_rule_pattern.matcher(line);
139     if (match.matches()) {
140       int result = 0;
141       String JavaDoc value = match.group(3);
142       if ("always".equals(value)) {
143         result = MethodInfo.SR_ALWAYS;
144       } else if ("never".equals(value)) {
145         result = MethodInfo.SR_NEVER;
146       } else if ("neverbody".equals(value)) {
147         result = MethodInfo.SR_NEVERBODY;
148       } else if ("runnables".equals(value)) {
149         result = MethodInfo.SR_RUNNABLES;
150       } else if ("sync".equals(value)) {
151         result = MethodInfo.SR_SYNC;
152       } else {
153         System.err.println("Unknown relevance value: " + value);
154         return;
155       }
156       relevance_rules.add(new NameRule(match.group(1), match.group(2), result));
157     } else {
158       System.err.println("Relevance rule parse error on line: " + line);
159     }
160   }
161
162   private static final Pattern JavaDoc atomic_rule_pattern =
163     Pattern.compile("([^ ]+) +([^ ]+) +(true|false)");
164
165   private void parseAtomicRule(String JavaDoc line) {
166     Matcher JavaDoc match = atomic_rule_pattern.matcher(line);
167     if (match.matches()) {
168       int result = 0;
169       String JavaDoc value = match.group(3);
170       if ("true".equals(value)) {
171         result = 1;
172       } else if ("false".equals(value)) {
173         result = 0;
174       } else {
175         System.err.println("Unknown atomic value: " + value);
176         return;
177       }
178       atomic_rules.add(new NameRule(match.group(1), match.group(2), result));
179     } else {
180       System.err.println("Atomic rule parse error on line: " + line);
181     }
182   }
183
184   /**
185    * A rule that contains two regular expressions that will take two strings
186    * and report if they match the rule.
187    * It also stores the result that needs to be returned if this rule matches.
188    */

189   private static class NameRule {
190     NameRule(String JavaDoc class_pattern_string,
191              String JavaDoc method_pattern_string,
192              int value) {
193       class_pattern = Pattern.compile(class_pattern_string);
194       method_pattern = Pattern.compile(method_pattern_string);
195       result = value;
196     }
197
198     boolean isMatch(String JavaDoc class_name, String JavaDoc method_name) {
199       Matcher JavaDoc class_match = class_pattern.matcher(class_name);
200       if (class_match.matches()) {
201         return method_pattern.matcher(method_name).matches();
202       }
203       return false;
204     }
205
206     int getResult() {
207       return result;
208     }
209
210     Pattern JavaDoc class_pattern;
211     Pattern JavaDoc method_pattern;
212     int result;
213   }
214
215   private List JavaDoc relevance_rules = new ArrayList JavaDoc();
216   private List JavaDoc atomic_rules = new ArrayList JavaDoc();
217 }
218
219
Popular Tags