1 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 31 public class PipelineAssembler extends BaseLocatable 32 { 33 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 66 public PipelineAssembler(ErrorLog errorLog, String serviceId, Class serviceInterface, 67 Class filterInterface, ClassFactory classFactory, 68 DefaultImplementationBuilder defaultBuilder) 69 { 70 _errorLog = errorLog; 71 _serviceId = serviceId; 72 _serviceInterface = serviceInterface; 73 _filterInterface = filterInterface; 74 _classFactory = classFactory; 75 _defaultBuilder = defaultBuilder; 76 77 _orderer = new Orderer(_errorLog, "filter"); 78 79 } 80 81 public void addFilter(String name, String prereqs, String postreqs, Object filter, 82 Location location) 83 { 84 if (!checkInterface(_filterInterface, filter, location)) 85 return; 86 87 FilterHolder holder = new FilterHolder(filter, location); 88 89 _orderer.add(holder, name, prereqs, postreqs); 90 } 91 92 public void setTerminator(Object terminator, Location terminatorLocation) 93 { 94 if (_terminator != null) 95 { 96 _errorLog.error(PipelineMessages.duplicateTerminator( 97 terminator, 98 _serviceId, 99 _terminator, 100 _terminatorLocation), terminatorLocation, null); 101 return; 102 } 103 104 if (!checkInterface(_serviceInterface, terminator, terminatorLocation)) 105 return; 106 107 _terminator = terminator; 108 _terminatorLocation = terminatorLocation; 109 } 110 111 113 Object getTerminator() 114 { 115 return _terminator; 116 } 117 118 private boolean checkInterface(Class interfaceType, Object instance, Location location) 119 { 120 if (interfaceType.isAssignableFrom(instance.getClass())) 121 return true; 122 123 _errorLog.error( 124 PipelineMessages.incorrectInterface(instance, interfaceType, _serviceId), 125 location, 126 null); 127 128 return false; 129 } 130 131 135 public Object createPipeline() 136 { 137 List filterHolders = _orderer.getOrderedObjects(); 138 int count = filterHolders.size(); 139 140 BridgeBuilder bb = (count == 0) ? null : new BridgeBuilder(_errorLog, _serviceId, 141 _serviceInterface, _filterInterface, _classFactory); 142 143 Object next = _terminator != null ? _terminator : _defaultBuilder 144 .buildDefaultImplementation(_serviceInterface); 145 146 148 for (int i = count - 1; i >= 0; i--) 149 { 150 FilterHolder h = (FilterHolder) filterHolders.get(i); 151 Object filter = h.getFilter(); 152 153 next = bb.instantiateBridge(next, filter); 154 } 155 156 return next; 157 } 158 } | Popular Tags |