1 15 package org.apache.hivemind.impl; 16 17 import java.util.List ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ErrorLog; 21 import org.apache.hivemind.Location; 22 import org.apache.hivemind.ServiceImplementationFactory; 23 import org.apache.hivemind.ServiceImplementationFactoryParameters; 24 import org.apache.hivemind.definition.ImplementationConstructionContext; 25 import org.apache.hivemind.definition.Occurances; 26 import org.apache.hivemind.definition.ServicePointDefinition; 27 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 28 import org.apache.hivemind.internal.Module; 29 import org.apache.hivemind.internal.ServicePoint; 30 import org.apache.hivemind.schema.Schema; 31 import org.apache.hivemind.util.Defense; 32 import org.apache.hivemind.util.InstanceCreationUtils; 33 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl; 34 35 41 public final class InvokeFactoryServiceConstructor extends AbstractServiceImplementationConstructor 42 { 43 private String _factoryServiceId; 44 45 46 private List _parameters; 47 48 49 private ServiceImplementationFactory _factory; 50 51 52 private Object _convertedParameters; 53 54 public InvokeFactoryServiceConstructor(Location location, String contributingModuleId) 55 { 56 super(location); 57 } 58 59 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 60 { 61 setupFactoryAndParameters(context.getServicePoint(), context.getDefiningModule()); 62 63 try 64 { 65 ServiceImplementationFactoryParameters factoryParameters = new ServiceImplementationFactoryParametersImpl( 66 context.getServicePoint(), context.getDefiningModule(), _convertedParameters); 67 68 return _factory.createCoreServiceImplementation(factoryParameters); 69 } 70 catch (Exception ex) 71 { 72 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); 73 } 74 } 75 76 80 private synchronized void setupFactoryAndParameters(ServicePoint servicePoint, Module contributingModule) 81 { 82 if (_factory == null) 83 { 84 ServicePoint factoryPoint = contributingModule.getServicePoint(_factoryServiceId); 85 86 ErrorLog errorLog = servicePoint.getErrorLog(); 87 88 _factory = (ServiceImplementationFactory) factoryPoint 89 .getService(ServiceImplementationFactory.class); 90 91 ServicePointDefinition spd = factoryPoint.getServicePointDefinition(); 92 if (!(spd instanceof XmlServicePointDefinitionImpl)) { 93 throw new ApplicationRuntimeException("ServicePoint used as ServiceImplementationFactory must be of type XmlServicePointDefinitionImpl"); 95 } 96 XmlServicePointDefinitionImpl xmlServicePoint = (XmlServicePointDefinitionImpl) spd; 97 98 Schema schema = xmlServicePoint.getParametersSchema(); 99 if (schema != null) { 100 101 Occurances expected = xmlServicePoint.getParametersCount(); 102 checkParameterCounts(errorLog, expected); 103 104 SchemaProcessorImpl processor = new SchemaProcessorImpl(errorLog, schema); 105 106 _convertedParameters = constructParametersContainer(schema.getRootElementClassName(), factoryPoint.getModule()); 107 processor.process(_convertedParameters, _parameters, contributingModule); 108 } 109 } 110 } 111 112 public List getParameters() 113 { 114 return _parameters; 115 } 116 117 public void setParameters(List list) 118 { 119 _parameters = list; 120 } 121 122 public void setFactoryServiceId(String factoryServiceId) 123 { 124 _factoryServiceId = factoryServiceId; 125 } 126 127 public String getFactoryServiceId() 128 { 129 return _factoryServiceId; 130 } 131 132 public Object constructParametersContainer(String containerClassName, Module definingModule) 133 { 134 Defense.notNull(containerClassName, "containerClassName"); 135 136 return InstanceCreationUtils.createInstance( 137 definingModule, 138 containerClassName, 139 getLocation()); 140 } 141 142 145 private void checkParameterCounts(ErrorLog log, Occurances expected) 146 { 147 int actual = _parameters.size(); 148 149 if (expected.inRange(actual)) 150 return; 151 152 String message = XmlImplMessages.wrongNumberOfParameters(_factoryServiceId, actual, expected); 153 154 log.error(message, getLocation(), null); 155 } 156 157 158 } | Popular Tags |