use of org.apache.tapestry5.ioc.ModuleBuilderSource in project tapestry-5 by apache.
the class ContributionDefImpl method invokeMethod.
private <T> void invokeMethod(ModuleBuilderSource source, ServiceResources resources, Class<T> parameterType, T parameterValue) {
Map<Class, Object> resourceMap = CollectionFactory.newMap();
resourceMap.put(parameterType, parameterValue);
resourceMap.put(ObjectLocator.class, resources);
resourceMap.put(Logger.class, resources.getLogger());
InjectionResources injectionResources = new MapInjectionResources(resourceMap);
for (Class t : CONFIGURATION_TYPES) {
if (parameterType != t) {
injectionResources = new DelegatingInjectionResources(new WrongConfigurationTypeGuard(resources.getServiceId(), t, parameterType), injectionResources);
}
}
Throwable fail = null;
Object moduleInstance = InternalUtils.isStatic(contributorMethod) ? null : source.getModuleBuilder();
try {
ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(contributorMethod, resources, injectionResources, resources.getTracker());
contributorMethod.invoke(moduleInstance, InternalUtils.realizeObjects(parameters));
} catch (InvocationTargetException ex) {
fail = ex.getTargetException();
} catch (Exception ex) {
fail = ex;
}
if (fail != null)
throw new RuntimeException(IOCMessages.contributionMethodError(contributorMethod, fail), fail);
}
use of org.apache.tapestry5.ioc.ModuleBuilderSource in project tapestry-5 by apache.
the class SpringModuleDef method createContributionToMasterObjectProvider.
private ContributionDef createContributionToMasterObjectProvider() {
ContributionDef def = new AbstractContributionDef() {
@Override
public String getServiceId() {
return "MasterObjectProvider";
}
@Override
public void contribute(ModuleBuilderSource moduleSource, ServiceResources resources, OrderedConfiguration configuration) {
final OperationTracker tracker = resources.getTracker();
final ApplicationContext context = resources.getService(SERVICE_ID, ApplicationContext.class);
final ObjectProvider springBeanProvider = new ObjectProvider() {
@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
Map beanMap = context.getBeansOfType(objectType);
switch(beanMap.size()) {
case 0:
return null;
case 1:
Object bean = beanMap.values().iterator().next();
return objectType.cast(bean);
default:
String message = String.format("Spring context contains %d beans assignable to type %s: %s.", beanMap.size(), PlasticUtils.toTypeName(objectType), InternalUtils.joinSorted(beanMap.keySet()));
throw new IllegalArgumentException(message);
}
}
};
final ObjectProvider springBeanProviderInvoker = new ObjectProvider() {
@Override
public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider, final ObjectLocator locator) {
return tracker.invoke("Resolving dependency by searching Spring ApplicationContext", new Invokable<T>() {
@Override
public T invoke() {
return springBeanProvider.provide(objectType, annotationProvider, locator);
}
});
}
};
ObjectProvider outerCheck = new ObjectProvider() {
@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
if (!applicationContextCreated.get())
return null;
return springBeanProviderInvoker.provide(objectType, annotationProvider, locator);
}
};
configuration.add("SpringBean", outerCheck, "after:AnnotationBasedContributions", "after:ServiceOverride");
}
};
return def;
}
Aggregations