KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > InspectorSet


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import com.pavelvlasov.config.ConfigurationException;
37 import com.pavelvlasov.logging.Logger;
38
39 /**
40  * @author Pavel Vlasov
41  * @version $Revision: 1.8 $
42  */

43 public class InspectorSet {
44     private Map JavaDoc descriptors=new HashMap JavaDoc();
45     private Collection JavaDoc inspectors;
46     private Logger logger;
47     private InspectorContextFactory contextFactory;
48     private Collection JavaDoc inspectorSourceInfos=new ArrayList JavaDoc();
49     
50     public InspectorSet(InspectorContextFactory contextFactory, Logger logger) {
51         this.contextFactory=contextFactory;
52         this.logger=logger;
53     }
54     
55     public void addDescriptors(Collection JavaDoc descriptors) throws ConfigurationException {
56         Iterator JavaDoc it=descriptors.iterator();
57         while (it.hasNext()) {
58             addDescriptor((InspectorDescriptor) it.next());
59         }
60     }
61     
62     public void addInspectorSourceInfo(InspectorSourceInfo info) {
63         inspectorSourceInfos.add(info);
64     }
65     
66     public Collection JavaDoc getInspectorSourceInfos() {
67         return inspectorSourceInfos;
68     }
69     
70     public InspectorDescriptor getDescriptor(String JavaDoc name) {
71         return (InspectorDescriptor) descriptors.get(name);
72     }
73     
74     public Collection JavaDoc getDescriptors() {
75         return descriptors.values();
76     }
77     
78     public void addDescriptor(InspectorDescriptor descriptor) throws ConfigurationException {
79         SelfDescribingInspectorProxy sdrp=new SelfDescribingInspectorProxy(descriptor);
80         String JavaDoc inspectorName=descriptor.getName();
81         if (inspectorName==null) {
82             inspectorName=sdrp.getName();
83         }
84         
85         if (inspectorName==null) {
86             throw new ConfigurationException("Unnamed inspector");
87         }
88         
89         InspectorDescriptorStack rds=(InspectorDescriptorStack) descriptors.get(inspectorName);
90         if (rds==null) {
91             rds=new InspectorDescriptorStack();
92             descriptors.put(inspectorName, rds);
93         }
94         
95         rds.addFirst(sdrp);
96         rds.addFirst(descriptor);
97     }
98     
99     public Collection JavaDoc getInspectors() throws ConfigurationException, HammurapiException {
100         if (inspectors==null) {
101             assignOrders();
102             inspectors=new LinkedList JavaDoc();
103             Iterator JavaDoc it=descriptors.values().iterator();
104             while (it.hasNext()) {
105                 InspectorDescriptor inspectorDescriptor=(InspectorDescriptor) it.next();
106                 InspectorContext ic=contextFactory.newContext(inspectorDescriptor, logger);
107                 if (Boolean.TRUE.equals(inspectorDescriptor.isEnabled())) {
108                     Inspector inspector=inspectorDescriptor.getInspector();
109                     if (inspector!=null) {
110                         inspector.setContext(ic);
111                         inspectors.add(inspector);
112
113                         if (inspector instanceof FilteringInspector) {
114                             Collection JavaDoc fid=inspectorDescriptor.getFilteredInspectorDesriptors(this, null);
115                             if (fid!=null) {
116                                 Iterator JavaDoc dit=fid.iterator();
117                                 while (dit.hasNext()) {
118                                     ((FilteringInspector) inspector).addTarget(((InspectorDescriptor) dit.next()).getInspector());
119                                 }
120                             }
121                         }
122                     }
123                 }
124             }
125         }
126         return inspectors;
127     }
128     
129     /**
130      * Invokes init() for all inspectors in the set
131      * @throws HammurapiException
132      * @throws ConfigurationException
133      */

134     public void initInspectors() throws ConfigurationException, HammurapiException {
135         Iterator JavaDoc it=getInspectors().iterator();
136         while (it.hasNext()) {
137             ((Inspector) it.next()).init();
138         }
139     }
140     
141     private void assignOrders() throws HammurapiException {
142         final Map JavaDoc dependencyMap=new HashMap JavaDoc();
143         Iterator JavaDoc it=descriptors.values().iterator();
144         while (it.hasNext()) {
145             InspectorDescriptor inspectorDescriptor=(InspectorDescriptor) it.next();
146             if (Boolean.TRUE.equals(inspectorDescriptor.isEnabled())) {
147                 Collection JavaDoc inspectorsToOrder=new ArrayList JavaDoc();
148                 Collection JavaDoc waivedInspectorNames = inspectorDescriptor.getWaivedInspectorNames();
149                 if (waivedInspectorNames!=null) {
150                     inspectorsToOrder.addAll(waivedInspectorNames);
151                 }
152                 
153                 Collection JavaDoc filteredInspectors = inspectorDescriptor.getFilteredInspectorDesriptors(this, null);
154                 if (filteredInspectors!=null && !filteredInspectors.isEmpty()) {
155                     Iterator JavaDoc fit=filteredInspectors.iterator();
156                     while (fit.hasNext()) {
157                         inspectorsToOrder.add(((InspectorDescriptor) fit.next()).getName());
158                     }
159                 }
160
161                 Collection JavaDoc afterInspectors = inspectorDescriptor.getAfterInspectorNames();
162                 if (afterInspectors!=null) {
163                     inspectorsToOrder.addAll(afterInspectors);
164                 }
165
166                 dependencyMap.put(inspectorDescriptor.getName(), inspectorsToOrder);
167             }
168         }
169         
170         class GraphEntry implements Comparable JavaDoc {
171             private static final String JavaDoc CICRULAR_REFERENCE_MSG = "Circular <waives> or <filter> reference in inspector '";
172             String JavaDoc name;
173             private Set JavaDoc dependents=new HashSet JavaDoc();
174             private boolean ready=false;
175             private int level;
176             
177             public String JavaDoc toString() {
178                 StringBuffer JavaDoc ret=new StringBuffer JavaDoc(getClass().getName());
179                 ret.append("[").append(name);
180                 if (!dependents.isEmpty()) {
181                     ret.append(" <- ");
182                     Iterator JavaDoc it=dependents.iterator();
183                     while (it.hasNext()) {
184                         ret.append(it.next());
185                         if (it.hasNext()) {
186                             ret.append(", ");
187                         }
188                     }
189                 }
190                 ret.append("]");
191                 return ret.toString();
192             }
193         
194             GraphEntry(String JavaDoc name) throws HammurapiException {
195                 this.name=name;
196                 Iterator JavaDoc it=((Collection JavaDoc) dependencyMap.get(name)).iterator();
197                 while (it.hasNext()) {
198                     String JavaDoc dependent=(String JavaDoc) it.next();
199                     if (name.equals(dependent)) {
200                         throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'");
201                     }
202                     dependents.add(dependent);
203                     
204                     Object JavaDoc o=dependencyMap.get(dependent);
205                     if (o instanceof Collection JavaDoc) {
206                         o=new GraphEntry(dependent);
207                         level=Math.max(level, ((GraphEntry) o).level+1);
208                     }
209                     
210                     if (o!=null) {
211                         Iterator JavaDoc dit=((GraphEntry) o).getDependents().iterator();
212                         while (dit.hasNext()) {
213                             String JavaDoc ddependent=(String JavaDoc) it.next();
214                             if (name.equals(ddependent)) {
215                                 throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'");
216                             }
217                             
218                             dependents.add(ddependent);
219                         }
220                     }
221                 }
222                 ready=true;
223                 dependencyMap.put(name, this);
224             }
225
226             Set JavaDoc getDependents() throws HammurapiException {
227                 if (ready) {
228                     return dependents;
229                 }
230                 
231                 throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'");
232             }
233
234             public int compareTo(Object JavaDoc o) {
235                 if (o==this) {
236                     return 0;
237                 } else if (o instanceof GraphEntry) {
238                     GraphEntry ge=(GraphEntry) o;
239                     
240                     try {
241                         if (getDependents().contains(ge.name)) {
242                             return -1;
243                         } else if (ge.getDependents().contains(name)){
244                             return 1;
245                         } else if (level<ge.level) {
246                             return 1;
247                         } else if (level>ge.level) {
248                             return -1;
249                         } else {
250                             return name.compareTo(ge.name);
251                         }
252                     } catch (HammurapiException e) {
253                         throw new HammurapiRuntimeException(e);
254                     }
255                 } else {
256                     return -1;
257                 }
258             }
259             
260             public int hashCode() {
261                 return name.hashCode();
262             }
263         }
264         
265         Iterator JavaDoc kit=dependencyMap.keySet().iterator();
266         while (kit.hasNext()) {
267             String JavaDoc key=(String JavaDoc) kit.next();
268             if (dependencyMap.get(key) instanceof Collection JavaDoc) {
269                 new GraphEntry(key);
270             }
271         }
272         
273         List JavaDoc inspectors=new ArrayList JavaDoc(dependencyMap.values());
274         Collections.sort(inspectors);
275         
276         int counter=0;
277         Integer JavaDoc start=null;
278         Iterator JavaDoc wit=inspectors.iterator();
279         while (wit.hasNext()) {
280             String JavaDoc iName=((GraphEntry) wit.next()).name;
281             InspectorDescriptor id=(InspectorDescriptor) descriptors.get(iName);
282             Integer JavaDoc order = id.getOrder();
283             if (order!=null) {
284                 start=new Integer JavaDoc(order.intValue()-counter);
285                 break;
286             }
287             counter++;
288         }
289         
290         counter = start==null ? 0 : start.intValue();
291         wit=inspectors.iterator();
292         while (wit.hasNext()) {
293             String JavaDoc iName=((GraphEntry) wit.next()).name;
294             InspectorDescriptor id=(InspectorDescriptor) descriptors.get(iName);
295             Integer JavaDoc order = id.getOrder();
296             if (order!=null) {
297                 if (order.intValue()<counter) {
298                     throw new HammurapiException("Order "+order.intValue()+" conflicts with automatically assigned order "+counter+" for inspector "+iName);
299                 }
300                 
301                 counter=order.intValue();
302             } else {
303                 final Integer JavaDoc iOrder=new Integer JavaDoc(counter);
304                 descriptors.put(id.getName(), new InspectorDescriptorFilter(id) {
305                     public Integer JavaDoc getOrder() {
306                         return iOrder;
307                     }
308                 });
309             }
310             counter++;
311         }
312     }
313     
314     int size() {
315         return descriptors.size();
316     }
317     
318     /**
319      * Calls destroy() for all inspectors
320      */

321     public void destroy() {
322         if (inspectors!=null) {
323             Iterator JavaDoc it=inspectors.iterator();
324             while (it.hasNext()) {
325                 ((Inspector) it.next()).destroy();
326             }
327         }
328     }
329 }
330
Popular Tags