Coverage Report - org.seasar.cubby.tomcat55.valves.StaticReferenceCleanerValve
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticReferenceCleanerValve
0%
0/55
0%
0/16
2.429
 
 1  
 package org.seasar.cubby.tomcat55.valves;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.lang.reflect.Field;
 5  
 import java.lang.reflect.Method;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Collections;
 8  
 import java.util.Iterator;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 import java.util.StringTokenizer;
 12  
 
 13  
 import javax.servlet.ServletException;
 14  
 
 15  
 import org.apache.catalina.connector.Request;
 16  
 import org.apache.catalina.connector.Response;
 17  
 import org.apache.catalina.util.StringManager;
 18  
 import org.apache.catalina.valves.ValveBase;
 19  
 import org.apache.commons.logging.Log;
 20  
 
 21  0
 public class StaticReferenceCleanerValve extends ValveBase {
 22  
 
 23  
         private static final String info = "org.seasar.cubby.tomcat55.valves.StaticReferenceCleanerValve/1.0";
 24  
 
 25  0
         private static StringManager sm = StringManager
 26  
                         .getManager(Constants.Package);
 27  
 
 28  
         private String methods;
 29  
 
 30  
         private String maps;
 31  
 
 32  
         public String getInfo() {
 33  0
                 return info;
 34  
         }
 35  
 
 36  
         public String getMethods() {
 37  0
                 return methods;
 38  
         }
 39  
 
 40  
         public void setMethods(final String methods) {
 41  0
                 this.methods = methods;
 42  
 
 43  0
                 this.methodNames = new ArrayList();
 44  0
                 final StringTokenizer tokenizer = new StringTokenizer(methods, ",");
 45  0
                 while (tokenizer.hasMoreTokens()) {
 46  0
                         this.methodNames.add(tokenizer.nextToken());
 47  
                 }
 48  0
         }
 49  
 
 50  
         public String getMaps() {
 51  0
                 return maps;
 52  
         }
 53  
 
 54  
         public void setMaps(final String maps) {
 55  0
                 this.maps = maps;
 56  
 
 57  0
                 this.mapNames = new ArrayList();
 58  0
                 final StringTokenizer tokenizer = new StringTokenizer(maps, ",");
 59  0
                 while (tokenizer.hasMoreTokens()) {
 60  0
                         this.mapNames.add(tokenizer.nextToken());
 61  
                 }
 62  0
         }
 63  
 
 64  0
         private List methodNames = Collections.EMPTY_LIST;
 65  
 
 66  0
         private List mapNames = Collections.EMPTY_LIST;
 67  
 
 68  
         public void invoke(final Request request, final Response response)
 69  
                         throws IOException, ServletException {
 70  
                 try {
 71  0
                         getNext().invoke(request, response);
 72  
                 } finally {
 73  0
                         clean();
 74  0
                 }
 75  0
         }
 76  
 
 77  
         private void clean() {
 78  0
                 final Log log = getContainer().getLogger();
 79  0
                 for (final Iterator i = methodNames.iterator(); i.hasNext();) {
 80  0
                         final StringTokenizer tokenizer = new StringTokenizer((String) i
 81  
                                         .next(), "#");
 82  0
                         final String className = tokenizer.nextToken();
 83  0
                         final String methodName = tokenizer.nextToken();
 84  
 
 85  
                         try {
 86  0
                                 final Class clazz = Class.forName(className);
 87  0
                                 final Method method = clazz.getMethod(methodName, new Class[0]);
 88  0
                                 method.invoke(null, null);
 89  0
                                 if (log.isDebugEnabled()) {
 90  0
                                         log.debug(sm.getString(
 91  
                                                         "staticReferenceCleanerValve.methodInvokeSucceed",
 92  
                                                         method));
 93  
                                 }
 94  0
                         } catch (final Exception e) {
 95  0
                                 if (log.isWarnEnabled()) {
 96  0
                                         log.warn(sm.getString(
 97  
                                                         "staticReferenceCleanerValve.methodInvokeFailed",
 98  
                                                         className, methodName), e);
 99  
                                 }
 100  0
                         }
 101  0
                 }
 102  
 
 103  0
                 for (final Iterator i = mapNames.iterator(); i.hasNext();) {
 104  0
                         final StringTokenizer tokenizer = new StringTokenizer((String) i
 105  
                                         .next(), "#");
 106  0
                         final String className = tokenizer.nextToken();
 107  0
                         final String mapFieldName = tokenizer.nextToken();
 108  
 
 109  
                         try {
 110  0
                                 final Class clazz = Class.forName(className);
 111  0
                                 final Field field = clazz.getDeclaredField(mapFieldName);
 112  0
                                 field.setAccessible(true);
 113  0
                                 final Map map = (Map) field.get(null);
 114  0
                                 map.clear();
 115  0
                                 if (log.isDebugEnabled()) {
 116  0
                                         log.debug(sm.getString(
 117  
                                                         "staticReferenceCleanerValve.mapFieldClearSucceed",
 118  
                                                         field));
 119  
                                 }
 120  0
                         } catch (final Exception e) {
 121  0
                                 if (log.isWarnEnabled()) {
 122  0
                                         log.warn(sm.getString(
 123  
                                                         "staticReferenceCleanerValve.mapFieldClearFailed",
 124  
                                                         className, maps), e);
 125  
                                 }
 126  0
                         }
 127  0
                 }
 128  
 
 129  0
         }
 130  
 
 131  
 }