KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > SynchronizationInServletsRule


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.inspectors;
24
25 import org.hammurapi.InspectorBase;
26
27 import com.pavelvlasov.jsel.JselException;
28 import com.pavelvlasov.jsel.LanguageElement;
29 import com.pavelvlasov.jsel.Method;
30 import com.pavelvlasov.jsel.Operation;
31 import com.pavelvlasov.jsel.Parameter;
32 import com.pavelvlasov.jsel.statements.SynchronizedStatement;
33 import com.pavelvlasov.util.Visitor;
34
35
36 /**
37  * ER-103
38  * Minimize synchronization in Servlets
39  * @author Pavel Vlasov
40  * @version $Revision: 1.4 $
41  */

42 public class SynchronizationInServletsRule extends InspectorBase {
43     
44     private static final String JavaDoc JAVAX_SERVLET_SERVLETRESPONSE = "javax.servlet.ServletResponse";
45     private static final String JavaDoc JAVAX_SERVLET_SERVLETREQUEST = "javax.servlet.ServletRequest";
46     private static final String JavaDoc JAVAX_SERVLET_SERVLET = "javax.servlet.Servlet";
47     
48     public void visit(SynchronizedStatement element) {
49         if (((LanguageElement) element).getEnclosingCode() instanceof Method) {
50             checkCallers((Method) ((LanguageElement) element).getEnclosingCode());
51         }
52     }
53     
54     private boolean j2eeChecked=false;
55     
56     public void visit(Method element) {
57         if (element.getModifiers().contains("synchronized")) {
58             checkCallers(element);
59         }
60     }
61
62     /**
63      * @param method
64      */

65     private void checkCallers(Method method) {
66         if (!j2eeChecked) {
67             j2eeChecked=true;
68             try {
69                 method.getRepository().loadClass(JAVAX_SERVLET_SERVLET);
70             } catch (ClassNotFoundException JavaDoc e) {
71                 disable(e.getMessage());
72                 return;
73             }
74         }
75         
76         try {
77             /* this buffer will accumulate list of servlet
78              * methods invoking this method directly or indirectly
79              */

80             final StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
81             
82             // Check whether this method is invoked from servlet
83
// methods with 2 parameters of type ServletRequest and ServletResponse.
84
method.visitCallers(new Visitor() {
85
86                 public boolean visit(Object JavaDoc target) {
87                     Operation.Invocation inv=(Operation.Invocation) target;
88                     if (inv.getCaller() instanceof Method) {
89                         Method caller=(Method) inv.getCaller();
90                         try {
91                             if (caller.getEnclosingType().isKindOf(JAVAX_SERVLET_SERVLET)
92                                     && caller.getParameters().size()==2
93                                     && ((Parameter) caller.getParameters().get(0)).isTypeOf(JAVAX_SERVLET_SERVLETREQUEST)
94                                     && ((Parameter) caller.getParameters().get(0)).isTypeOf(JAVAX_SERVLET_SERVLETRESPONSE)) {
95                                 if (sb.length()>0) {
96                                     sb.append(", ");
97                                 }
98                                 sb.append(caller.getEnclosingType().getFcn());
99                                 sb.append(".");
100                                 sb.append(caller.getOperationSignature());
101                             }
102                         } catch (JselException e) {
103                             context.warn(caller, e);
104                         }
105                     }
106                     return true;
107                 }
108                 
109             });
110             
111             if (sb.length()>0) {
112                 context.reportViolation(
113                         method,
114                         context.getDescriptor().getMessage()
115                         +". Synchronized method "
116                         +method.getOperationSignature()
117                         + " is invoked from servlet method(s) "
118                         + sb.toString());
119             }
120         } catch (JselException e) {
121             context.warn(method, e);
122         }
123     }
124 }
125
Popular Tags