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