KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > ArchitecturalLayerInspector


1   /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Johannes Bellert
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.pavelvlasov.com/pv/content/menu.show?id=products.jtaste
21  * e-Mail: Johannes.Bellert@gmail.com
22  */

23 package org.hammurapi.inspectors.metrics;
24
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 import org.hammurapi.HammurapiException;
40 import org.hammurapi.InspectorBase;
41 import org.hammurapi.results.AnnotationContext;
42 import org.hammurapi.results.LinkedAnnotation;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45
46 import com.pavelvlasov.config.ConfigurationException;
47 import com.pavelvlasov.config.Parameterizable;
48 import com.pavelvlasov.jsel.Interface;
49 import com.pavelvlasov.jsel.JselException;
50 import com.pavelvlasov.jsel.Method;
51 import com.pavelvlasov.jsel.Parameter;
52 import com.pavelvlasov.jsel.Repository;
53 import com.pavelvlasov.jsel.TypeBody;
54 import com.pavelvlasov.jsel.TypeDefinition;
55 import com.pavelvlasov.jsel.TypeSpecification;
56 import com.pavelvlasov.jsel.VariableDefinition;
57 import com.pavelvlasov.jsel.impl.ClassTypeSpecificationImpl;
58 import com.pavelvlasov.render.RenderRequest;
59 import com.pavelvlasov.render.RenderingException;
60 import com.pavelvlasov.render.dom.AbstractRenderer;
61 import com.pavelvlasov.review.SourceMarker;
62
63 public class ArchitecturalLayerInspector
64     extends InspectorBase
65     implements ArchitecturalLayerConstants, Parameterizable {
66
67     private static final String JavaDoc CONFIG_ERROR_MESSAGE = "'mapping-configuration-file', " +
68             "'mapping-configuration-resource', and 'mapping-configuration-url' " +
69             "parameters are mutually exclusive";
70     
71     private ArchitecturalLayerMappingTable architecturalLayerMappingTable = null;
72     private ArchitecturalLayerExtensionsMap extensionMap = new ArchitecturalLayerExtensionsMap();
73     private Hashtable JavaDoc allCategorizedPackages = new Hashtable JavaDoc();
74     private Hashtable JavaDoc allCategorizedClasses = new Hashtable JavaDoc();
75     private Hashtable JavaDoc allGetterSetterCounterForClasses = new Hashtable JavaDoc();
76     private ListOfLayers listOfLayers = null;
77     private Hashtable JavaDoc allTechStackEntitiesTable = new Hashtable JavaDoc();
78     private ArchitecturalComplexityMappingTable complexityMappingTable = null;
79     private HashSet JavaDoc unknownVariableList = new HashSet JavaDoc();
80
81     private Coupling coupling = new Coupling();
82     private CodeMetric projectMetric = new CodeMetric();
83     String JavaDoc projectBaseDir = "";
84     String JavaDoc xmlResourceName=this.getClass().getName().replace('.','/')+ "XmlPretty.xsl";
85     Repository repository = null;
86     
87     private String JavaDoc mappingConfigurationPath;
88     private String JavaDoc mappingConfigurationResource;
89     private String JavaDoc mappingConfigurationUrl;
90                 
91     public void init() throws HammurapiException {
92         
93         try {
94             InputStream JavaDoc inspectorStream;
95             if (mappingConfigurationPath!=null) {
96                 inspectorStream=new FileInputStream JavaDoc(this.mappingConfigurationPath);
97             } else if (mappingConfigurationUrl!=null) {
98                 inspectorStream=new URL JavaDoc(mappingConfigurationUrl).openStream();
99             } else if (mappingConfigurationResource!=null) {
100                 inspectorStream=getClass().getClassLoader().getResourceAsStream(mappingConfigurationResource);
101                 if (inspectorStream==null) {
102                     throw new HammurapiException("Resource not found: "+mappingConfigurationResource);
103                 }
104             } else {
105                 String JavaDoc className = getClass().getName();
106                 int idx=className.lastIndexOf('.');
107                 String JavaDoc rName = (idx==-1 ? className : className.substring(idx+1))+".xml";
108                 inspectorStream=getClass().getResourceAsStream(rName);
109                 if (inspectorStream==null) {
110                     throw new HammurapiException("Default mappig not found: "+rName);
111                 }
112             }
113     
114             DomArchitecturalMappingSource source=new DomArchitecturalMappingSource(inspectorStream);
115             this.architecturalLayerMappingTable = source.loadLayerMappings();
116             this.complexityMappingTable = source.loadComplexityMapping();
117             Enumeration JavaDoc enumAlm = architecturalLayerMappingTable.elements();
118             
119                 while( enumAlm.hasMoreElements()){
120                     ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)enumAlm.nextElement();
121                     
122                         Enumeration JavaDoc stackEnum = alm.getTechStackEntityList().elements();
123             
124                 while( stackEnum.hasMoreElements()){
125                     TechStackEntity te = (TechStackEntity)stackEnum.nextElement();
126                     for( int i=0; i< te.getExtensionMapping().size(); i++){
127                         // System.out.println( (String)te.getExtensionMapping().elementAt(i) + " >-> "+ te.getName());
128
this.extensionMap.put( (String JavaDoc)te.getExtensionMapping().elementAt(i), te.getName());
129                     }
130                 }
131                 }
132                 Collection JavaDoc almList = architecturalLayerMappingTable.values();
133                 Iterator JavaDoc it = almList.iterator();
134                 
135                 
136                  while(it.hasNext()){
137                 //for( int i=0; i<almList.size(); i++){
138
// ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)architecturalLayerMappingTable.get((String)it.next());
139
ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)it.next();
140                     
141                     // allTechStackEntitiesSet.addAll(alm.getTechStackEntityListValues());
142
Collection JavaDoc clte = alm.getTechStackEntityListValues();
143                     Iterator JavaDoc itClte = clte.iterator();
144                     while(itClte.hasNext()){
145                         TechStackEntity te = (TechStackEntity)itClte.next();
146                         allTechStackEntitiesTable.put( te.getName(), te);
147                     }
148                  }
149                 
150         } catch (IOException JavaDoc e) {
151             throw new HammurapiException("Cannot load mapping configuration: "+e, e);
152         }
153     }
154     
155     public void visit(com.pavelvlasov.jsel.Package p) {
156         if ( "".equals( p.getName()) ){
157             allCategorizedPackages.put( "<default>", new ListOfPackageCategories( p.getName() ));
158         }else {
159             allCategorizedPackages.put(p.getName(), new ListOfPackageCategories( p.getName() ));
160             
161         }
162     }
163
164     public void visit(TypeDefinition p) throws JselException, ClassNotFoundException JavaDoc {
165         
166         this.coupling.classCouplingMetric.put( p.getFcn(), new CouplingMetricOfClass(p));
167         allCategorizedClasses.put(p.getFcn(), new ListOfCategories( p.getFcn(), ((SourceMarker)p).getSourceURL(), ((SourceMarker)p).getLine(), ((SourceMarker)p).getColumn() ));
168         this.identifyCategories(p);
169
170     }
171
172     public void visit(Interface p) throws JselException, ClassNotFoundException JavaDoc {
173         this.coupling.classCouplingMetric.put( p.getFcn(), new CouplingMetricOfClass(p));
174         allCategorizedClasses.put(p.getFcn(), new ListOfCategories( p.getFcn(), ((SourceMarker)p).getSourceURL(),((SourceMarker)p).getLine(), ((SourceMarker)p).getColumn() ));
175         addCategory("Interface", p.getName(), p);
176     }
177
178     
179     //!! job: read from XML descriptor
180
public void checkExtentType( TypeDefinition type )throws JselException, ClassNotFoundException JavaDoc {
181
182         Enumeration JavaDoc keys = extensionMap.keys();
183         while ( keys.hasMoreElements() ){
184             String JavaDoc key = (String JavaDoc)keys.nextElement();
185             String JavaDoc category = extensionMap.getProperty(key);
186             checkExtendTypeHelper( key, category, type);
187         }
188     }
189
190     //!! job: remember tokens which raise a ClassNotFounds and remove from list, for performance optimization
191
private void checkExtendTypeHelper (String JavaDoc _loadClass, String JavaDoc category, TypeDefinition type) {
192         try {
193             if ( type.isKindOf(_loadClass) ) {
194                 addCategory(category, _loadClass, type);
195             }
196         } catch (JselException e) {
197             // TODO Auto-generated catch block
198
e.printStackTrace();
199         }
200     }
201
202
203     /*
204      *
205      * Constant Identifier missing
206      * 23public class Constants {
207 24 //~ Static fields/initializers ---------------------------------------------
208 25
209 26 public static final int PROCESS_STATUS_NOT_SUBMITTED = 1;
210 27 public static final int PROCESS_STATUS_OPEN = 2;
211
212      *
213      *
214      */

215     public void checkVariableType(TypeSpecification typDef, TypeDefinition declarationType ) throws JselException, ClassNotFoundException JavaDoc {
216         boolean foundAnyCategory = false;
217         Iterator JavaDoc it = this.architecturalLayerMappingTable.values().iterator();
218         
219         while (it.hasNext() && !foundAnyCategory){
220             
221             ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)it.next();
222             Iterator JavaDoc stackEnum = alm.getTechStackEntityListValues().iterator();
223             while (stackEnum.hasNext() && !foundAnyCategory){
224                 TechStackEntity tse = (TechStackEntity)stackEnum.next();
225                 foundAnyCategory = iterateThroughVarDefSearchStrings(tse.getVariableMapping(), typDef, tse.getName(), declarationType);
226             }
227         }
228         if ( foundAnyCategory ){
229             // I have a dam clue about this var
230
} else {
231             if ( nonTrivialTypeFilter( typDef ) ){
232             unknownVariableList.add( typDef.getName() );
233         }
234         }
235
236     }
237     public void checkVariableType(TypeSpecification typDef, TypeBody declarationType ) throws JselException, ClassNotFoundException JavaDoc {
238         boolean foundAnyCategory = false;
239         Iterator JavaDoc it = this.architecturalLayerMappingTable.values().iterator();
240     
241         while (it.hasNext() && !foundAnyCategory){
242             
243             ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)it.next();
244             Iterator JavaDoc stackEnum = alm.getTechStackEntityListValues().iterator();
245             while (stackEnum.hasNext() && !foundAnyCategory){
246                 TechStackEntity tse = (TechStackEntity)stackEnum.next();
247                 foundAnyCategory = iterateThroughVarDefSearchStrings(tse.getVariableMapping(), typDef, tse.getName(), declarationType);
248         }
249         }
250         if ( foundAnyCategory ){
251             // THIS means, I have a dam clue about this var
252
} else {
253             if ( nonTrivialTypeFilter( typDef ) ) {
254             unknownVariableList.add( typDef.getName() );
255         }
256         }
257
258     }
259
260     public boolean iterateThroughVarDefSearchStrings( Vector JavaDoc searchList, TypeSpecification typeDef, String JavaDoc category, TypeDefinition declarationType)
261     throws JselException {
262     
263         boolean foundAnyCategory = false;
264         for ( int i = 0; i < searchList.size(); i++){
265         String JavaDoc searchStr = (String JavaDoc)searchList.elementAt(i);
266
267         if("*".equals(searchStr.substring(searchStr.length()-1) ) ){
268             String JavaDoc packageStr = searchStr.substring(0, searchStr.length()-1);
269
270             if ( typeDef.getName().startsWith(packageStr ) ){
271                 addCategory(category, typeDef.getName(), typeDef, declarationType);
272                 foundAnyCategory = true;
273             }
274         }else if (searchStr.equals(typeDef.getName())) {
275             addCategory(category, searchStr, typeDef, declarationType);
276             foundAnyCategory = true;
277         }
278         }
279     return foundAnyCategory;
280     }
281     
282     public boolean iterateThroughVarDefSearchStrings( Vector JavaDoc searchList, TypeSpecification typeDef, String JavaDoc category, TypeBody declarationType)
283     throws JselException {
284     
285         boolean foundAnyCategory = false;
286
287     for ( int i = 0; i < searchList.size(); i++){
288         String JavaDoc searchStr = (String JavaDoc)searchList.elementAt(i);
289         if("*".equals(searchStr.substring(searchStr.length()-1) ) ){
290             String JavaDoc packageStr = searchStr.substring(0,searchStr.length()-1);
291
292             if ( typeDef.getName().startsWith(packageStr ) ){
293                 addCategory(category, typeDef.getName(), typeDef, declarationType);
294                 foundAnyCategory = true;
295             }
296         }else if (searchStr.equals(typeDef.getName())) {
297             addCategory(category, searchStr, typeDef, declarationType);
298             foundAnyCategory = true;
299         }
300         }
301     return foundAnyCategory;
302     }
303
304     public boolean nonTrivialTypeFilter( TypeSpecification typeDef )throws JselException{
305             //!! should be xml configured
306
if ( typeDef.getName().startsWith("java." ) ||
307                 typeDef.getName().startsWith("com.ge." ) ||
308                 typeDef.getName().startsWith("ge.geia." ) ||
309                 typeDef.getName().startsWith("com.erc." ) ||
310                 typeDef.getName().startsWith("com.gefra." ) ||
311                 typeDef.getName().startsWith("com.geerc." ) ||
312                 typeDef.getName().startsWith("com.gecapital." ) ||
313                 typeDef.getName().startsWith("erc." ) ||
314                 typeDef.getName().startsWith("ercrio.rio." ) ||
315                 typeDef.getName().startsWith("com.iri." )||
316                 typeDef.getName().startsWith("com.igate." )
317                 ){
318             return false;
319         }
320         return true;
321     }
322     
323     public void visit(VariableDefinition variableDefinition) {
324         
325         // JDepend
326
this.coupling.incrementVariableCoupling(variableDefinition) ;
327         
328         // Repository initialization
329
if (repository == null ){
330             repository = variableDefinition.getCompilationUnit().getPackage().getRepository();
331         }
332         try {
333             if (variableDefinition.getTypeSpecification() instanceof ClassTypeSpecificationImpl ) {
334                 try {
335                     
336                     TypeDefinition typeDef = variableDefinition.getRootEnclosingType();
337                     checkVariableType(variableDefinition.getTypeSpecification(), typeDef);
338                 } catch (JselException e) {
339                     context.warn(variableDefinition, e);
340                 }
341             } else {
342                 // System.out.println( " OO " + variableDefinition.getTypeSpecification().getClass().toString() );
343
}
344         } catch (ClassNotFoundException JavaDoc e) {
345             context.warn(variableDefinition, e);
346         }
347     }
348
349     public void visit(Parameter parameter) {
350
351         // JDepend
352
this.coupling.incrementVariableCoupling(parameter) ;
353
354         if (repository == null ){
355             repository = parameter.getCompilationUnit().getPackage().getRepository();
356         }
357         
358         try {
359             if (parameter.getTypeSpecification() instanceof ClassTypeSpecificationImpl ) {
360                 try {
361                     TypeSpecification ts = parameter.getTypeSpecification();
362                     checkVariableType(ts, parameter.getParent().getEnclosingType());
363                 } catch (JselException e) {
364                     context.warn(parameter, e);
365                 }
366             }
367         } catch (ClassNotFoundException JavaDoc e) {
368             context.warn(parameter, e);
369         }
370             
371     }
372     
373     /*
374     public void addCategory(String category, String variableIdent, VariableDefinition element, TypeDefinition declarationType) {
375         ListOfCategories typeLoc = null;
376         typeLoc = (ListOfCategories) allCategorizedClasses.get(element.getEnclosingType().getFcn());
377         typeLoc.add(new ArchitecturalCategory(
378                 ((SourceMarker)element).getSourceURL(),
379                 ((SourceMarker)element).getLine(),
380                 ((SourceMarker)element).getColumn(),
381                  variableIdent, category));
382     }
383     */

384     /*
385     public void addCategory(String category, String variableIdent, VariableDefinition element, TypeBody declarationType) {
386         ListOfCategories typeLoc = null;
387         typeLoc = (ListOfCategories) allCategorizedClasses.get(element.getEnclosingType().getFcn());
388         typeLoc.add(new ArchitecturalCategory(
389                 ((SourceMarker)element).getSourceURL(),
390                 ((SourceMarker)element).getLine(),
391                 ((SourceMarker)element).getColumn(),
392                  variableIdent, category));
393     }
394     */

395 /*
396     public void addCategory(String category, String variableIdent, Interface element, TypeDefinition declarationType) {
397         ListOfCategories typeLoc = null;
398         typeLoc = (ListOfCategories) allCategorizedClasses.get(element.getFcn());
399         typeLoc.add(new ArchitecturalCategory(
400                 ((SourceMarker)element).getSourceURL(),
401                 ((SourceMarker)element).getLine(),
402                 ((SourceMarker)element).getColumn(),
403                  variableIdent, category));
404     }
405 */

406     public void addCategory(String JavaDoc category, String JavaDoc variableIdent, TypeSpecification element, TypeDefinition declarationType) throws JselException {
407         ListOfCategories typeLoc = null;
408         if(declarationType != null ){
409             typeLoc = (ListOfCategories) allCategorizedClasses.get(declarationType.getFcn());
410         
411         //!! job: what happens with missing categories??
412
if( typeLoc == null){
413     // typeLoc = new ListOfCategories( declarationType.getFcn( );
414
// allCategorizedClasses.put( declarationType.getFcn(), typeLoc );
415
new Exception JavaDoc().printStackTrace();
416         }else{
417             typeLoc.add(new ArchitecturalCategory(
418                     ((SourceMarker)element).getSourceURL(),
419                     ((SourceMarker)element).getLine(),
420                     ((SourceMarker)element).getColumn(),
421                      variableIdent, category));
422             }
423         }
424     }
425     
426     public void addCategory(String JavaDoc category, String JavaDoc variableIdent, TypeSpecification element, TypeBody declarationType) throws JselException {
427         
428         
429         ListOfCategories typeLoc = null;
430         
431         if(declarationType != null ){
432             typeLoc = (ListOfCategories) allCategorizedClasses.get(declarationType.getFcn());
433         
434         //!! job: what happens with missing categories??
435
if( typeLoc == null){
436     // typeLoc = new ListOfCategories( declarationType.getFcn( );
437
// allCategorizedClasses.put( declarationType.getFcn(), typeLoc );
438
}else{
439             
440             //!! put in here (and all category constructors a complexity helper method
441
typeLoc.add(new ArchitecturalCategory(
442                     ((SourceMarker)element).getSourceURL(),
443                     ((SourceMarker)element).getLine(),
444                     ((SourceMarker)element).getColumn(),
445                      variableIdent, category));
446             }
447         }
448     }
449
450     public void addCategory(String JavaDoc category, String JavaDoc variableIdent, TypeDefinition element) throws JselException {
451         ListOfCategories typeLoc = null;
452         typeLoc = (ListOfCategories) allCategorizedClasses.get(element.getFcn());
453
454         typeLoc.add(new ArchitecturalCategory(
455                 ((SourceMarker)element).getSourceURL(),
456                 ((SourceMarker)element).getLine(),
457                 ((SourceMarker)element).getColumn(),
458                 variableIdent, category));
459     }
460 /*
461     public void addCategory(ListOfCategories typeLoc, String category, String variableIdent, LanguageElement element) {
462         try {
463             typeLoc.add(new ArchitecturalCategory(
464                     ((SourceMarker)element).getSourceURL(),
465                     ((SourceMarker)element).getLine(),
466                     ((SourceMarker)element).getColumn(),
467                      variableIdent, category));
468         } catch (Exception e) {
469             context.warn(element, e);
470         }
471     }
472 */

473     public void addCategory(ListOfCategories typeLoc, String JavaDoc category, String JavaDoc variableIdent, String JavaDoc fcn) {
474             typeLoc.add(new ArchitecturalCategory(fcn, 0,0, variableIdent, category));
475             }
476     
477     public void visit(Method p) {
478         //!! detect Connection >> prepareCall( )
479

480         int start = p.getAst().getFirstToken().getLine();
481         int end = p.getAst().getLastToken().getLine();
482         TypeBody enclosingType = p.getEnclosingType();
483         final String JavaDoc fcn = enclosingType.getFcn();
484
485         if (((end - start) < 3)
486                 && (p.getName().toString().startsWith("get") || p.getName()
487                         .toString().startsWith("set"))) {
488             try {
489                 String JavaDoc keyGetterSetter = fcn + GETTERSETTER;
490                 Integer JavaDoc getterCounter = (Integer JavaDoc) allGetterSetterCounterForClasses
491                         .get(keyGetterSetter);
492                 if (getterCounter == null) {
493                     allGetterSetterCounterForClasses.put(keyGetterSetter, new Integer JavaDoc(1));
494                     ListOfCategories typeLoc = (ListOfCategories) allCategorizedClasses.get(fcn);
495                     if (typeLoc!=null) {
496                         typeLoc.setType(fcn);
497                     }
498                 } else {
499                     allGetterSetterCounterForClasses.put(keyGetterSetter, new Integer JavaDoc(getterCounter.intValue() + 1));
500                 }
501             } catch (Exception JavaDoc e) {
502                 e.printStackTrace();
503             }
504
505         } else {
506             try {
507                 Integer JavaDoc getterCounter = (Integer JavaDoc) allGetterSetterCounterForClasses.get(fcn);
508                 if (getterCounter == null) {
509                     allGetterSetterCounterForClasses.put(fcn, new Integer JavaDoc(1));
510                     ListOfCategories typeLoc = (ListOfCategories) allCategorizedClasses.get(fcn);
511                     if (typeLoc!=null) {
512                         typeLoc.setType(fcn);
513                     }
514                 } else {
515                     allGetterSetterCounterForClasses.put(fcn, new Integer JavaDoc(getterCounter.intValue() + 1));
516                 }
517             } catch (Exception JavaDoc e) {
518                 e.printStackTrace();
519             }
520         }
521     }
522     
523     public void identifyCategories(TypeDefinition c)
524         throws JselException, ClassNotFoundException JavaDoc {
525         this.checkExtentType(c);
526         return;
527     }
528
529
530     public void aggregate(){
531         // create Getter-Setter Categories
532
checkGetterSetter();
533         
534         //-- compress to package level
535
compressClassesToPackageLevel();
536
537         determineLayerPerPackage();
538         extractLayer2PackageRelations();
539     }
540
541
542     private void extractLayer2PackageRelations(){
543      listOfLayers = new ListOfLayers( this.allCategorizedPackages );
544     }
545
546     private void compressClassesToPackageLevel(){
547
548     Enumeration JavaDoc classesEnum = this.allCategorizedClasses.keys();
549     while (classesEnum.hasMoreElements()){
550         String JavaDoc key = (String JavaDoc)classesEnum.nextElement();
551         ListOfCategories loc =(ListOfCategories)allCategorizedClasses.get(key);
552
553         ListOfPackageCategories lopc = (ListOfPackageCategories)this.allCategorizedPackages.get( ListOfPackageCategories.getWithClassFcn(key));
554         
555         if (loc.size() == 0) {
556             ArchitecturalCategoryPackage pnac = (ArchitecturalCategoryPackage) lopc.get("NOT CATEGORIZED");
557             if (pnac != null) {
558                 pnac.occurance++;
559             } else {
560                 // logger.error(" No ArchitecturalCategory for <NOT CATEGORIZED> initialized. ");
561
}
562         }
563
564             for (int i = 0; i < loc.size(); i++) {
565                 try {
566                     ArchitecturalCategory ac = (ArchitecturalCategory) loc.elementAt(i);
567                     // System.out.println ("*** " +ac.categoryType );
568
ArchitecturalCategoryPackage pac = lopc.get(ac.categoryType);
569                     if (pac == null) {
570                         lopc.put( ac.categoryType, new ArchitecturalCategoryPackage( key ,ac.categoryType,ac.source_url ));
571                     } else {
572                         pac.occurance++;
573                     }
574                 } catch (RuntimeException JavaDoc e) {
575                     // TODO Auto-generated catch block
576
}
577             }
578     }
579 }
580
581     private void checkGetterSetter(){
582     //!!
583
double ratio = 0;
584         Enumeration JavaDoc propertyEnum = allGetterSetterCounterForClasses.keys();
585         while (propertyEnum.hasMoreElements()){
586             String JavaDoc key = (String JavaDoc)propertyEnum.nextElement();
587             Integer JavaDoc numGetterSetter = (Integer JavaDoc)allGetterSetterCounterForClasses.get( key+GETTERSETTER );
588             Integer JavaDoc num = (Integer JavaDoc)allGetterSetterCounterForClasses.get( key );
589             if( numGetterSetter != null && num != null){
590                  ratio = new Double JavaDoc(num.intValue()).doubleValue() / new Double JavaDoc(numGetterSetter.intValue()).doubleValue();
591                 } else if ( numGetterSetter != null && num == null){
592                     ratio = -1 ;
593                 } else if ( numGetterSetter == null && num != null){
594                     ratio = 1;
595                 }
596                 //System.out.println( "§§ " + num +" / "+ numGetterSetter + " = "+ ratio );
597
if( ratio < 0.3){
598                 ListOfCategories typeLoc = (ListOfCategories) allCategorizedClasses.get(key);
599                 if (typeLoc!=null) {
600                     BigDecimal JavaDoc bg =new BigDecimal JavaDoc( ratio);
601                     addCategory( typeLoc, VALUE_OBJECT, "Getter/Setter Count = " + numGetterSetter
602                             + " Ratio: " + bg.divide(new BigDecimal JavaDoc("1.0"),2,BigDecimal.ROUND_HALF_UP).toString(),
603                             typeLoc.getSourceURL());
604                 }
605             }
606         }
607     }
608
609     private void determineLayerPerPackage(){
610
611         Enumeration JavaDoc enumKey = this.allCategorizedPackages.keys();
612         while(enumKey.hasMoreElements()){
613             String JavaDoc key = (String JavaDoc)enumKey.nextElement() ;
614             ListOfPackageCategories loc = (ListOfPackageCategories)this.allCategorizedPackages.get( key );
615             loc.setLayers( this.determineLayerInListOfCategories(loc) );
616         }
617
618         return;
619     }
620
621     private HashSet JavaDoc determineLayerInListOfCategories(ListOfPackageCategories loc) {
622         HashSet JavaDoc layers = new HashSet JavaDoc();
623         // Enumeration keys = architecturalLayerMapping.getMappings().keys();
624
Enumeration JavaDoc keys = architecturalLayerMappingTable.keys();
625         
626         while (keys.hasMoreElements()) {
627             String JavaDoc layer = (String JavaDoc) keys.nextElement();
628              // System.out.print ( "layer " + layer );
629
ArchitecturalLayerMapping alMapping = architecturalLayerMappingTable.get(layer);
630 Iterator JavaDoc itAlTe = alMapping.getTechStackEntityListValues().iterator();
631              while ( itAlTe.hasNext() ){
632                 
633                 TechStackEntity tse = (TechStackEntity)itAlTe.next();
634                         
635             if (loc.contains(tse.getName())) {
636                 // layers.add((String) architecturalLayerMapping.getMappings().get(key));
637
layers.add( layer );
638                 
639                  // layers.add((String) architecturalLayerMappingTable.get(key));
640
// System.out.println ( " ..added" );
641
} else {
642                  // System.out.println ( " ..failed" );
643
}
644              }
645         }
646         return layers;
647     }
648
649     public void leave(Repository repository) {
650         aggregate();
651     
652         context.annotate( new LinkedAnnotation() {
653             private String JavaDoc path;
654
655             public String JavaDoc getName() {
656                 return getContext().getDescriptor().getName();
657             }
658
659             public String JavaDoc getPath() {
660                 return path;
661             }
662
663             public Properties JavaDoc getProperties() {
664                 Properties JavaDoc ret=new Properties JavaDoc();
665                 ret.setProperty("left-panel", "yes");
666                 ret.setProperty("target", "Architectual Layers");
667                 return ret;
668             }
669
670                 public void render(AnnotationContext context) throws HammurapiException {
671                 String JavaDoc errorCausingFile = "";
672                 projectMetric.setName ( "Project" );
673                 projectMetric.setDescriptonEntity ( projectMetric.getName() );
674
675                 // Output images here. See AnnotationTest for details.
676

677                 class ArchitecturalLayerInspectorRenderer
678                     extends AbstractRenderer {
679                     
680                     ArchitecturalLayerInspectorRenderer() {
681                         
682                         super(new RenderRequest(ArchitecturalLayerInspector.this));
683                     }
684
685                     public Element JavaDoc render(Document JavaDoc document) throws RenderingException {
686                         HashSet JavaDoc hs = new HashSet JavaDoc();
687
688                         ArchitecturalLayerInspector myInspector=(ArchitecturalLayerInspector) request.getRenderee(); ;
689                         Element JavaDoc entities=document.createElement("Entities");
690                         Element JavaDoc inspectorElement=document.createElement("ArchitecturalLayers");
691
692                         Enumeration JavaDoc enumC = allCategorizedClasses.elements();
693                         while(enumC.hasMoreElements()){
694                             ListOfCategories loc = (ListOfCategories)enumC.nextElement();
695                             inspectorElement.appendChild( loc.toDom(document) );
696                             for ( int i=0; i<loc.size(); i++){
697                             hs.add( ((ArchitecturalCategory)loc.get(i)).categoryType);
698                             }
699                         }
700                         
701                         Element JavaDoc tsel=document.createElement("TechStackEntityList");
702                         inspectorElement.appendChild( tsel );
703                         
704                         Collection JavaDoc almList = architecturalLayerMappingTable.values();
705                         Iterator JavaDoc it = almList.iterator();
706                     
707                          while(it.hasNext()){
708                             ArchitecturalLayerMapping alm = (ArchitecturalLayerMapping)it.next();
709                             
710                             Collection JavaDoc clte = alm.getTechStackEntityListValues();
711                             Iterator JavaDoc itClte = clte.iterator();
712                              tsel.appendChild( alm.toDom(document) );
713                          }
714                             Element JavaDoc tselU=document.createElement("UsedTechStackEntityList");
715                             inspectorElement.appendChild( tselU );
716
717                         Vector JavaDoc usedTechStackEntityList = new Vector JavaDoc();
718                          Iterator JavaDoc itHs = hs.iterator();
719                          while(itHs.hasNext()){
720                             TechStackEntity tse = (TechStackEntity)allTechStackEntitiesTable.get((String JavaDoc)itHs.next());
721                             if (tse != null){
722                                  tselU.appendChild( tse.toDom(document) );
723                                  usedTechStackEntityList.add(tse);
724                                 }
725                          }
726                          //!! markUsedJarFiles
727
JarFileList jarFileList = new JarFileLookup().parseClasspath();
728                         jarFileList.markUsedJarFiles(usedTechStackEntityList, allTechStackEntitiesTable);
729                         inspectorElement.appendChild( jarFileList.toDom(document) );
730                         
731                         //-------------------------------------------------------------------------
732

733                         
734                         Element JavaDoc projSumE=document.createElement("ProjectSummary");
735                         projSumE.setAttribute("number", String.valueOf( "" ) );
736                         inspectorElement.appendChild(projSumE);
737                         inspectorElement.appendChild(listOfLayers.toDom(document));
738                         Enumeration JavaDoc enump = allCategorizedPackages.elements();
739                         while(enump.hasMoreElements()){
740                             ListOfPackageCategories loc = (ListOfPackageCategories)enump.nextElement();
741                             inspectorElement.appendChild(loc.toDom(document));
742                         }
743                         Element JavaDoc tselr=document.createElement("TechStackEntityRating");
744                         inspectorElement.appendChild( tselr );
745                         Enumeration JavaDoc enumalmt = architecturalLayerMappingTable.elements();
746                         while(enumalmt.hasMoreElements()){
747                             ArchitecturalLayerMapping almx = (ArchitecturalLayerMapping)enumalmt.nextElement();
748                             Enumeration JavaDoc enumte = almx.getTechStackEntityList().elements();
749                             while(enumte.hasMoreElements()){
750                                 TechStackEntity tse = (TechStackEntity)enumte.nextElement();
751                                 tselr.appendChild( tse.toDom(document) );
752                             }
753                         }
754                         inspectorElement.appendChild(new ArchitecturalLayerMapping().toDom(document));
755                         inspectorElement.appendChild(extensionMap.toDom(document) );
756                         inspectorElement.appendChild(complexityMappingTable.toDom(document) );
757                         
758                         Element JavaDoc urantvlst=document.createElement("UnResolvedAndNonTrivialVariableList");
759                         Iterator JavaDoc ituvl = unknownVariableList.iterator();
760                         while( ituvl.hasNext()){
761                             Element JavaDoc urantv=document.createElement("UnResolvedAndNonTrivialVariable");
762                             urantv.setAttribute("name", (String JavaDoc)ituvl.next() );
763                             urantvlst.appendChild( urantv );
764                         }
765                         inspectorElement.appendChild( urantvlst );
766                         inspectorElement.appendChild( coupling.toDom(document) );
767                         return inspectorElement;
768                     }
769                 } //-- end class Renderer
770

771                 AnnotationContext.FileEntry fileEntry=context.getNextFile(context.getExtension());
772                 path=fileEntry.getPath();
773                 AnnotationContext.FileEntry fileEntryXml=context.getNextFile(".xml");
774                 try {
775                     ArchitecturalLayerInspectorRenderer renderer=new ArchitecturalLayerInspectorRenderer();
776                     FileOutputStream JavaDoc out=new FileOutputStream JavaDoc(fileEntry.getFile());
777
778                     renderer.setEmbeddedStyle(context.isEmbeddedStyle());
779                     try {
780                         errorCausingFile = fileEntry.getFile().getAbsolutePath();
781                     renderer.render(
782                                 context.getParameter("style")==null
783                                     ? null
784                                     : new FileInputStream JavaDoc(context.getParameter("style").toString()), out);
785
786                     } finally {
787                         out.close();
788                     }
789                     FileOutputStream JavaDoc outXml=new FileOutputStream JavaDoc(fileEntryXml.getFile());
790                     try {
791                         errorCausingFile = fileEntryXml.getFile().getAbsolutePath();
792                         //-- write a XML file for other XSL usage
793
renderer.setEmbeddedStyle(false);
794                         renderer.render(outXml);
795 // renderer.setPrettyPrint( true );
796
// InputStream inStream=getClass().getClassLoader().getResourceAsStream(xmlResourceName);
797
// renderer.render(inStream, outXml);
798

799                     } finally {
800                         outXml.close();
801                     }
802                 } catch (Exception JavaDoc e) {
803                     throw new HammurapiException("Can't save "+ errorCausingFile +". " +e.getMessage() );
804                 }
805             }
806         });
807
808     }
809
810
811        /**
812      * Configures the rule. Reads in the values of the parameters XXX and
813      * class-max-complexity.
814      *
815      * @param name the name of the parameter being loaded from Hammurapi configuration
816      * @param value the value of the parameter being loaded from Hammurapi configuration
817      * @exception ConfigurationException in case of a not supported parameter
818      */

819     public boolean setParameter(String JavaDoc name, Object JavaDoc value)
820         throws ConfigurationException {
821     
822          if ("mapping-configuration-file".equals(name)) {
823             if (mappingConfigurationResource!=null) {
824                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
825             }
826             if (mappingConfigurationUrl!=null) {
827                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
828             }
829             mappingConfigurationPath=value.toString();
830          } else if ("mapping-configuration-resource".equals(name)) {
831             if (mappingConfigurationPath!=null) {
832                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
833             }
834             if (mappingConfigurationUrl!=null) {
835                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
836             }
837             mappingConfigurationResource=value.toString();
838          } else if ("mapping-configuration-url".equals(name)) {
839             if (mappingConfigurationPath!=null) {
840                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
841             }
842             if (mappingConfigurationResource!=null) {
843                 throw new ConfigurationException(CONFIG_ERROR_MESSAGE);
844             }
845             mappingConfigurationUrl=value.toString();
846         } else {
847             throw new ConfigurationException("Parameter '"+name+"' is not supported");
848         }
849             return true;
850     }
851
852 }
853
Popular Tags