Clover coverage report - Code Coverage for hivemind-lib release 1.1-alpha-2
Coverage timestamp: Wed Feb 23 2005 10:00:02 EST
file stats: LOC: 158   Methods: 6
NCLOC: 85   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
PipelineAssembler.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 2004, 2005 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.util.List;
 18   
 
 19   
 import org.apache.hivemind.ErrorLog;
 20   
 import org.apache.hivemind.Location;
 21   
 import org.apache.hivemind.impl.BaseLocatable;
 22   
 import org.apache.hivemind.lib.DefaultImplementationBuilder;
 23   
 import org.apache.hivemind.order.Orderer;
 24   
 import org.apache.hivemind.service.ClassFactory;
 25   
 
 26   
 /**
 27   
  * Used by the {@link org.apache.hivemind.lib.pipeline.PipelineFactory}to assemble the pipeline.
 28   
  * 
 29   
  * @author Howard Lewis Ship
 30   
  */
 31   
 public class PipelineAssembler extends BaseLocatable
 32   
 {
 33   
     /** @since 1.1 */
 34   
     private ErrorLog _errorLog;
 35   
 
 36   
     private String _serviceId;
 37   
 
 38   
     private Class _serviceInterface;
 39   
 
 40   
     private Class _filterInterface;
 41   
 
 42   
     private ClassFactory _classFactory;
 43   
 
 44   
     private DefaultImplementationBuilder _defaultBuilder;
 45   
 
 46   
     private Orderer _orderer;
 47   
 
 48   
     private Object _terminator;
 49   
 
 50   
     private Location _terminatorLocation;
 51   
 
 52   
     /**
 53   
      * @param errorLog
 54   
      *            used for reporting recoverable errors
 55   
      * @param serviceInterface
 56   
      *            the main interface
 57   
      * @param filterInterface
 58   
      *            the interface for filters
 59   
      * @param classFactory
 60   
      *            for creating new classes
 61   
      * @param defaultBuilder
 62   
      *            used to provide a placeholder terminator if no real terminator is supplied
 63   
      * @param servceId
 64   
      *            of the service being assembled
 65   
      */
 66  9
     public PipelineAssembler(ErrorLog errorLog, String serviceId, Class serviceInterface,
 67   
             Class filterInterface, ClassFactory classFactory,
 68   
             DefaultImplementationBuilder defaultBuilder)
 69   
     {
 70  9
         _errorLog = errorLog;
 71  9
         _serviceId = serviceId;
 72  9
         _serviceInterface = serviceInterface;
 73  9
         _filterInterface = filterInterface;
 74  9
         _classFactory = classFactory;
 75  9
         _defaultBuilder = defaultBuilder;
 76   
 
 77  9
         _orderer = new Orderer(_errorLog, "filter");
 78   
 
 79   
     }
 80   
 
 81  8
     public void addFilter(String name, String prereqs, String postreqs, Object filter,
 82   
             Location location)
 83   
     {
 84  8
         if (!checkInterface(_filterInterface, filter, location))
 85  1
             return;
 86   
 
 87  7
         FilterHolder holder = new FilterHolder(filter, location);
 88   
 
 89  7
         _orderer.add(holder, name, prereqs, postreqs);
 90   
     }
 91   
 
 92  8
     public void setTerminator(Object terminator, Location terminatorLocation)
 93   
     {
 94  8
         if (_terminator != null)
 95   
         {
 96  1
             _errorLog.error(PipelineMessages.duplicateTerminator(
 97   
                     terminator,
 98   
                     _serviceId,
 99   
                     _terminator,
 100   
                     _terminatorLocation), terminatorLocation, null);
 101  1
             return;
 102   
         }
 103   
 
 104  7
         if (!checkInterface(_serviceInterface, terminator, terminatorLocation))
 105  1
             return;
 106   
 
 107  6
         _terminator = terminator;
 108  6
         _terminatorLocation = terminatorLocation;
 109   
     }
 110   
 
 111   
     // For testing
 112   
 
 113  2
     Object getTerminator()
 114   
     {
 115  2
         return _terminator;
 116   
     }
 117   
 
 118  15
     private boolean checkInterface(Class interfaceType, Object instance, Location location)
 119   
     {
 120  15
         if (interfaceType.isAssignableFrom(instance.getClass()))
 121  13
             return true;
 122   
 
 123  2
         _errorLog.error(
 124   
                 PipelineMessages.incorrectInterface(instance, interfaceType, _serviceId),
 125   
                 location,
 126   
                 null);
 127   
 
 128  2
         return false;
 129   
     }
 130   
 
 131   
     /**
 132   
      * Returns an object that implements the service interface, and integrates any filters for the
 133   
      * pipeline with the
 134   
      */
 135  6
     public Object createPipeline()
 136   
     {
 137  6
         List filterHolders = _orderer.getOrderedObjects();
 138  6
         int count = filterHolders.size();
 139   
 
 140  6
         BridgeBuilder bb = (count == 0) ? null : new BridgeBuilder(_errorLog, _serviceId,
 141   
                 _serviceInterface, _filterInterface, _classFactory);
 142   
 
 143  6
         Object next = _terminator != null ? _terminator : _defaultBuilder
 144   
                 .buildDefaultImplementation(_serviceInterface);
 145   
 
 146   
         // Like service interceptors, we work deepest (last) to shallowest (first).
 147   
 
 148  6
         for (int i = count - 1; i >= 0; i--)
 149   
         {
 150  7
             FilterHolder h = (FilterHolder) filterHolders.get(i);
 151  7
             Object filter = h.getFilter();
 152   
 
 153  7
             next = bb.instantiateBridge(next, filter);
 154   
         }
 155   
 
 156  6
         return next;
 157   
     }
 158   
 }