Clover coverage report - Code Coverage for hivemind-lib release 1.0-rc-2
Coverage timestamp: Sat Sep 11 2004 09:10:14 EDT
file stats: LOC: 262   Methods: 7
NCLOC: 159   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
BridgeBuilder.java 96.2% 98.7% 100% 98.1%
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.lib.pipeline;
 16   
 
 17   
 import java.lang.reflect.Constructor;
 18   
 import java.lang.reflect.Modifier;
 19   
 import java.util.ArrayList;
 20   
 import java.util.Iterator;
 21   
 import java.util.List;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.hivemind.ApplicationRuntimeException;
 25   
 import org.apache.hivemind.internal.Module;
 26   
 import org.apache.hivemind.service.BodyBuilder;
 27   
 import org.apache.hivemind.service.ClassFab;
 28   
 import org.apache.hivemind.service.ClassFabUtils;
 29   
 import org.apache.hivemind.service.ClassFactory;
 30   
 import org.apache.hivemind.service.MethodIterator;
 31   
 import org.apache.hivemind.service.MethodSignature;
 32   
 
 33   
 /**
 34   
  * Used by the {@link org.apache.hivemind.lib.pipeline.PipelineAssembler}
 35   
  * class to create bridge classes and to create instances of briddge classes.
 36   
  *
 37   
  * @author Howard Lewis Ship
 38   
  */
 39   
 class BridgeBuilder
 40   
 {
 41   
     private Log _log;
 42   
     private String _serviceId;
 43   
     private Class _serviceInterface;
 44   
     private Class _filterInterface;
 45   
 
 46   
     private ClassFab _classFab;
 47   
     private FilterMethodAnalyzer _filterMethodAnalyzer;
 48   
 
 49   
     private Constructor _constructor;
 50   
 
 51  10
     BridgeBuilder(
 52   
         Log log,
 53   
         String serviceId,
 54   
         Class serviceInterface,
 55   
         Class filterInterface,
 56   
         ClassFactory classFactory,
 57   
         Module module)
 58   
     {
 59  10
         _log = log;
 60  10
         _serviceId = serviceId;
 61  10
         _serviceInterface = serviceInterface;
 62  10
         _filterInterface = filterInterface;
 63   
 
 64  10
         String name = ClassFabUtils.generateClassName("PipelineBridge");
 65   
 
 66  10
         _classFab =
 67   
             classFactory.newClass(name, Object.class, module.getClassResolver().getClassLoader());
 68   
 
 69  10
         _filterMethodAnalyzer = new FilterMethodAnalyzer(serviceInterface);
 70   
     }
 71   
 
 72  10
     private void createClass()
 73   
     {
 74  10
         List serviceMethods = new ArrayList();
 75  10
         List filterMethods = new ArrayList();
 76   
 
 77  10
         createInfrastructure();
 78   
 
 79  10
         MethodIterator mi = new MethodIterator(_serviceInterface);
 80   
 
 81  10
         while (mi.hasNext())
 82   
         {
 83  9
             serviceMethods.add(mi.next());
 84   
         }
 85   
 
 86  10
         boolean toStringMethodExists = mi.getToString();
 87   
 
 88  10
         mi = new MethodIterator(_filterInterface);
 89   
 
 90  10
         while (mi.hasNext())
 91   
         {
 92  9
             filterMethods.add(mi.next());
 93   
         }
 94   
 
 95  10
         while (!serviceMethods.isEmpty())
 96   
         {
 97  9
             MethodSignature ms = (MethodSignature) serviceMethods.remove(0);
 98   
 
 99  9
             addBridgeMethod(ms, filterMethods);
 100   
         }
 101   
 
 102  10
         reportExtraFilterMethods(filterMethods);
 103   
 
 104  10
         if (!toStringMethodExists)
 105  9
             ClassFabUtils.addToStringMethod(
 106   
                 _classFab,
 107   
                 PipelineMessages.bridgeInstanceDescription(_serviceId, _serviceInterface));
 108   
 
 109  10
         Class bridgeClass = _classFab.createClass();
 110   
 
 111  10
         _constructor = bridgeClass.getConstructors()[0];
 112   
     }
 113   
 
 114  10
     private void createInfrastructure()
 115   
     {
 116  10
         _classFab.addField("_next", _serviceInterface);
 117  10
         _classFab.addField("_filter", _filterInterface);
 118   
 
 119  10
         _classFab.addConstructor(
 120   
             new Class[] { _serviceInterface, _filterInterface },
 121   
             null,
 122   
             "{ _next = $1; _filter = $2; }");
 123   
 
 124  10
         _classFab.addInterface(_serviceInterface);
 125   
     }
 126   
 
 127   
     /**
 128   
      * Instantiates a bridge object.
 129   
      * 
 130   
      * @param nextBridge the next Bridge object in the pipeline, or the terminator service
 131   
      * @param filter the filter object for this step of the pipeline
 132   
      */
 133  12
     public Object instantiateBridge(Object nextBridge, Object filter)
 134   
     {
 135  12
         if (_constructor == null)
 136  10
             createClass();
 137   
 
 138  12
         try
 139   
         {
 140  12
             return _constructor.newInstance(new Object[] { nextBridge, filter });
 141   
         }
 142   
         catch (Exception ex)
 143   
         {
 144  0
             throw new ApplicationRuntimeException(ex);
 145   
         }
 146   
     }
 147   
 
 148  10
     private void reportExtraFilterMethods(List filterMethods)
 149   
     {
 150  10
         Iterator i = filterMethods.iterator();
 151   
 
 152  10
         while (i.hasNext())
 153   
         {
 154  1
             MethodSignature ms = (MethodSignature) i.next();
 155   
 
 156  1
             _log.error(
 157   
                 PipelineMessages.extraFilterMethod(
 158   
                     ms,
 159   
                     _filterInterface,
 160   
                     _serviceInterface,
 161   
                     _serviceId));
 162   
         }
 163   
     }
 164   
 
 165   
     /**
 166   
      * Finds a matching method in filterMethods for the given service method.
 167   
      * A matching method has the same signature as the service interface method,
 168   
      * but with an additional parameter matching the service interface itself.
 169   
      * 
 170   
      * <p>
 171   
      * The matching method signature from the list of filterMethods is removed
 172   
      * and code generation strategies for making the two methods call each other are added. 
 173   
      */
 174  9
     private void addBridgeMethod(MethodSignature ms, List filterMethods)
 175   
     {
 176  9
         Iterator i = filterMethods.iterator();
 177   
 
 178  9
         while (i.hasNext())
 179   
         {
 180  8
             MethodSignature fms = (MethodSignature) i.next();
 181   
 
 182  8
             int position = _filterMethodAnalyzer.findServiceInterfacePosition(ms, fms);
 183   
 
 184  8
             if (position >= 0)
 185   
             {
 186  8
                 addBridgeMethod(position, ms, fms);
 187  8
                 i.remove();
 188  8
                 return;
 189   
             }
 190   
         }
 191   
 
 192  1
         String message = PipelineMessages.unmatchedServiceMethod(ms, _filterInterface);
 193   
 
 194  1
         _log.error(message);
 195   
 
 196  1
         BodyBuilder b = new BodyBuilder();
 197   
 
 198  1
         b.add("throw new org.apache.hivemind.ApplicationRuntimeException(");
 199  1
         b.addQuoted(message);
 200  1
         b.addln(");");
 201   
 
 202  1
         _classFab.addMethod(Modifier.PUBLIC, ms, b.toString());
 203   
     }
 204   
 
 205   
     /**
 206   
      * Adds a method to the class which bridges from the service method
 207   
      * to the corresponding method in the filter interface.  The next service
 208   
      * (either another Bridge, or the terminator at the end of the pipeline)
 209   
      * is passed to the filter).
 210   
      */
 211  8
     private void addBridgeMethod(int position, MethodSignature ms, MethodSignature fms)
 212   
     {
 213  8
         StringBuffer buffer = new StringBuffer(100);
 214   
 
 215  8
         if (ms.getReturnType() != void.class)
 216  7
             buffer.append("return ");
 217   
 
 218  8
         buffer.append("_filter.");
 219  8
         buffer.append(ms.getName());
 220  8
         buffer.append("(");
 221   
 
 222  8
         boolean comma = false;
 223  8
         int filterParameterCount = fms.getParameterTypes().length;
 224   
 
 225  8
         for (int i = 0; i < position; i++)
 226   
         {
 227  8
             if (comma)
 228  1
                 buffer.append(", ");
 229   
 
 230  8
             buffer.append("$");
 231   
             // Add one to the index to get the parameter symbol ($0 is the implicit
 232   
             // this parameter).
 233  8
             buffer.append(i + 1);
 234   
 
 235  8
             comma = true;
 236   
         }
 237   
 
 238  8
         if (comma)
 239  7
             buffer.append(", ");
 240   
 
 241   
         // _next is the variable in -this- Bridge that points to the -next- Bridge
 242   
         // or the terminator for the pipeline. The filter is expected to reinvoke the
 243   
         // method on the _next that's passed to it.
 244   
 
 245  8
         buffer.append("_next");
 246   
 
 247  8
         for (int i = position + 1; i < filterParameterCount; i++)
 248   
         {
 249  1
             buffer.append(", $");
 250  1
             buffer.append(i);
 251   
         }
 252   
 
 253  8
         buffer.append(");");
 254   
 
 255   
         // This should work, unless the exception types turn out to not be compatble. We still
 256   
         // don't do a check on that, and not sure that Javassist does either!
 257   
 
 258  8
         _classFab.addMethod(Modifier.PUBLIC, ms, buffer.toString());
 259   
     }
 260   
 
 261   
 }
 262