Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 225   Methods: 8
NCLOC: 146   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
LoggingInterceptorFactory.java 90% 97.6% 100% 96.4%
coverage coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.service.impl;
 16   
 
 17   
 import java.lang.reflect.Constructor;
 18   
 import java.lang.reflect.Method;
 19   
 import java.lang.reflect.Modifier;
 20   
 import java.util.Iterator;
 21   
 import java.util.List;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.commons.logging.LogFactory;
 25   
 import org.apache.hivemind.InterceptorStack;
 26   
 import org.apache.hivemind.methodmatch.MethodMatcher;
 27   
 import org.apache.hivemind.service.BodyBuilder;
 28   
 import org.apache.hivemind.service.ClassFab;
 29   
 import org.apache.hivemind.service.ClassFabUtils;
 30   
 import org.apache.hivemind.service.MethodContribution;
 31   
 import org.apache.hivemind.service.MethodFab;
 32   
 import org.apache.hivemind.service.MethodSignature;
 33   
 
 34   
 /**
 35   
  * An interceptor factory that adds logging capability to a service.
 36   
  * The logging is based upon the Jakarta 
 37   
  * <a href="http://jakarta.apache.org/commons/logging.html">commons-logging</a> toolkit, 
 38   
  * which makes
 39   
  * it very transportable.
 40   
  * 
 41   
  * <p>
 42   
  * The interceptor will log entry to each method and exit from the method
 43   
  * (with return value), plus log any exceptions thrown by the method.
 44   
  * The logger used is the <em>id of the service</em>, which is not necessarily
 45   
  * the name of the implementing class.  Logging occurs at the debug level.
 46   
  *
 47   
  * @author Howard Lewis Ship
 48   
  */
 49   
 public class LoggingInterceptorFactory extends AbstractServiceInterceptorFactory
 50   
 {
 51  7
     protected void createInfrastructure(InterceptorStack stack, ClassFab classFab, List parameters)
 52   
     {
 53  7
         Class topClass = stack.peek().getClass();
 54   
 
 55  7
         classFab.addField("_inner", topClass);
 56   
 
 57  7
         classFab.addConstructor(
 58   
             new Class[] { Log.class, topClass },
 59   
             null,
 60   
             "{ super($1); _inner = $2; }");
 61   
     }
 62   
 
 63  9
     protected Object instantiateInterceptor(
 64   
         InterceptorStack stack,
 65   
         Class interceptorClass,
 66   
         List parameters)
 67   
         throws Exception
 68   
     {
 69  9
         Object stackTop = stack.peek();
 70   
 
 71  9
         Log log = LogFactory.getLog(stack.getServiceExtensionPointId());
 72   
 
 73  9
         Constructor c = interceptorClass.getConstructors()[0];
 74   
 
 75  9
         return c.newInstance(new Object[] { log, stackTop });
 76   
     }
 77   
 
 78  1
     private void addPassThruMethodImplementation(ClassFab classFab, MethodSignature sig)
 79   
     {
 80  1
         BodyBuilder builder = new BodyBuilder();
 81  1
         builder.begin();
 82   
 
 83  1
         if (sig.getReturnType() != void.class)
 84  0
             builder.add("return ");
 85   
 
 86  1
         builder.add("_inner.");
 87  1
         builder.add(sig.getName());
 88  1
         builder.addln("($$);");
 89   
 
 90  1
         builder.end();
 91   
 
 92  1
         classFab.addMethod(Modifier.PUBLIC, sig, builder.toString());
 93   
     }
 94   
 
 95  13
     protected void addServiceMethodImplementation(ClassFab classFab, MethodSignature sig)
 96   
     {
 97  13
         Class returnType = sig.getReturnType();
 98  13
         String methodName = sig.getName();
 99   
 
 100  13
         boolean isVoid = (returnType == void.class);
 101   
 
 102  13
         BodyBuilder builder = new BodyBuilder();
 103   
 
 104  13
         builder.begin();
 105  13
         builder.addln("boolean debug = _isDebugEnabled();");
 106   
 
 107  13
         builder.addln("if (debug)");
 108  13
         builder.add("  _logEntry(");
 109  13
         builder.addQuoted(methodName);
 110  13
         builder.addln(", $args);");
 111   
 
 112  13
         if (!isVoid)
 113   
         {
 114  7
             builder.add(ClassFabUtils.getJavaClassName(returnType));
 115  7
             builder.add(" result = ");
 116   
         }
 117   
 
 118  13
         builder.add("_inner.");
 119  13
         builder.add(methodName);
 120  13
         builder.addln("($$);");
 121   
 
 122  13
         if (isVoid)
 123   
         {
 124  6
             builder.addln("if (debug)");
 125  6
             builder.add("  _logVoidExit(");
 126  6
             builder.addQuoted(methodName);
 127  6
             builder.addln(");");
 128   
         }
 129   
         else
 130   
         {
 131  7
             builder.addln("if (debug)");
 132  7
             builder.add("  _logExit(");
 133  7
             builder.addQuoted(methodName);
 134  7
             builder.addln(", ($w)result);");
 135  7
             builder.addln("return result;");
 136   
         }
 137   
 
 138  13
         builder.end();
 139   
 
 140  13
         MethodFab methodFab = classFab.addMethod(Modifier.PUBLIC, sig, builder.toString());
 141   
 
 142  13
         builder.clear();
 143   
 
 144  13
         builder.begin();
 145  13
         builder.add("_logException(");
 146  13
         builder.addQuoted(methodName);
 147  13
         builder.addln(", $e);");
 148  13
         builder.addln("throw $e;");
 149  13
         builder.end();
 150   
 
 151  13
         String body = builder.toString();
 152   
 
 153  13
         Class[] exceptions = sig.getExceptionTypes();
 154   
 
 155  13
         int count = exceptions.length;
 156   
 
 157  13
         for (int i = 0; i < count; i++)
 158   
         {
 159  0
             methodFab.addCatch(exceptions[i], body);
 160   
         }
 161   
 
 162   
         // Catch and log any runtime exceptions, in addition to the
 163   
         // checked exceptions.
 164   
 
 165  13
         methodFab.addCatch(RuntimeException.class, body);
 166   
     }
 167   
 
 168  7
     protected Class getInterceptorSuperclass()
 169   
     {
 170  7
         return AbstractLoggingInterceptor.class;
 171   
     }
 172   
 
 173  7
     protected void addServiceMethods(InterceptorStack stack, ClassFab fab, List parameters)
 174   
     {
 175  7
         boolean toString = false;
 176  7
         Method[] methods = stack.getServiceInterface().getMethods();
 177   
 
 178  7
         MethodMatcher matcher = buildMethodMatcher(parameters);
 179   
 
 180  7
         for (int i = 0; i < methods.length; i++)
 181   
         {
 182  14
             Method m = methods[i];
 183  14
             MethodSignature sig = new MethodSignature(m);
 184   
 
 185  14
             if (includeMethod(matcher, sig))
 186  13
                 addServiceMethodImplementation(fab, sig);
 187   
             else
 188  1
                 addPassThruMethodImplementation(fab, sig);
 189   
 
 190  14
             toString |= ClassFabUtils.isToString(m);
 191   
         }
 192   
 
 193  7
         if (!toString)
 194  6
             addToStringMethod(stack, fab);
 195   
     }
 196   
 
 197  7
     private MethodMatcher buildMethodMatcher(List parameters)
 198   
     {
 199  7
         MethodMatcher result = null;
 200   
 
 201  7
         Iterator i = parameters.iterator();
 202  7
         while (i.hasNext())
 203   
         {
 204  2
             MethodContribution mc = (MethodContribution) i.next();
 205   
 
 206  2
             if (result == null)
 207  1
                 result = new MethodMatcher();
 208   
 
 209  2
             result.put(mc.getMethodPattern(), mc);
 210   
         }
 211   
 
 212  7
         return result;
 213   
     }
 214   
 
 215  14
     private boolean includeMethod(MethodMatcher matcher, MethodSignature sig)
 216   
     {
 217  14
         if (matcher == null)
 218  11
             return true;
 219   
 
 220  3
         MethodContribution mc = (MethodContribution) matcher.get(sig);
 221   
 
 222  3
         return mc == null || mc.getInclude();
 223   
     }
 224   
 }
 225