use of org.apache.tapestry5.ioc.ServiceBuilderResources in project tapestry-5 by apache.
the class SpringModuleDef method constructObjectCreatorForApplicationContext.
private ObjectCreator constructObjectCreatorForApplicationContext(final ServiceBuilderResources resources, @Primary ApplicationContextCustomizer customizer) {
final CustomizingContextLoader loader = new CustomizingContextLoader(customizer);
final Runnable shutdownListener = new Runnable() {
@Override
public void run() {
loader.closeWebApplicationContext(servletContext);
}
};
final RegistryShutdownHub shutdownHub = resources.getService(RegistryShutdownHub.class);
return new ObjectCreator() {
@Override
public Object createObject() {
return resources.getTracker().invoke("Creating Spring ApplicationContext via ContextLoader", new Invokable<Object>() {
@Override
public Object invoke() {
resources.getLogger().info(String.format("Starting Spring (version %s)", SpringVersion.getVersion()));
WebApplicationContext context = loader.initWebApplicationContext(servletContext);
shutdownHub.addRegistryShutdownListener(shutdownListener);
applicationContextCreated.set(true);
return context;
}
});
}
@Override
public String toString() {
return "ObjectCreator for Spring ApplicationContext";
}
};
}
use of org.apache.tapestry5.ioc.ServiceBuilderResources in project tapestry-5 by apache.
the class SpringModuleDefTest method load_application_context_externally.
@Test
public void load_application_context_externally() {
ServletContext servletContext = mockServletContext();
ConfigurableListableBeanFactory beanFactory = newMock(ConfigurableListableBeanFactory.class);
ConfigurableWebApplicationContext ac = newMock(ConfigurableWebApplicationContext.class);
Runnable fred = mockRunnable();
Runnable barney = mockRunnable();
Runnable arnold = mockRunnable();
BeanDefinition fredBeanDef = newMock(BeanDefinition.class);
BeanDefinition barneyBeanDef = newMock(BeanDefinition.class);
BeanDefinition arnoldBeanDef = newMock(BeanDefinition.class);
ServiceBuilderResources resources = mockServiceBuilderResources();
train_getInitParameter(servletContext, SpringConstants.USE_EXTERNAL_SPRING_CONTEXT, "true");
train_getAttribute(servletContext, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
expect(ac.getBeanFactory()).andReturn(beanFactory);
expect(ac.getBeanDefinitionNames()).andReturn(new String[] { "fred", "&barney", "arnold" });
expect(fredBeanDef.isAbstract()).andReturn(false);
expect(barneyBeanDef.isAbstract()).andReturn(false);
expect(arnoldBeanDef.isAbstract()).andReturn(true);
expect(beanFactory.getBeanDefinition("fred")).andReturn(fredBeanDef);
expect(beanFactory.getBeanDefinition("&barney")).andReturn(barneyBeanDef);
expect(beanFactory.getBeanDefinition("arnold")).andReturn(arnoldBeanDef);
replay();
SpringModuleDef moduleDef = new SpringModuleDef(servletContext);
ServiceDef serviceDef = moduleDef.getServiceDef(SpringModuleDef.SERVICE_ID);
ObjectCreator serviceCreator = serviceDef.createServiceCreator(resources);
assertSame(serviceCreator.createObject(), ac);
verify();
// Now, let's test for some of the services.
ServiceDef sd = moduleDef.getServiceDef("ApplicationContext");
assertEquals(sd.getServiceInterface(), ac.getClass());
assertEquals(sd.createServiceCreator(null).toString(), "<ObjectCreator for externally configured Spring ApplicationContext>");
expect((Class) ac.getType("fred")).andReturn(Runnable.class);
expect(ac.getBean("fred")).andReturn(fred);
sd = moduleDef.getServiceDef("fred");
replay();
assertEquals(sd.getServiceId(), "fred");
assertEquals(sd.getServiceInterface(), Runnable.class);
assertEquals(sd.getServiceScope(), ScopeConstants.DEFAULT);
assertSame(sd.createServiceCreator(null).createObject(), fred);
assertTrue(sd.getMarkers().isEmpty());
assertFalse(sd.isEagerLoad());
assertEquals(sd.createServiceCreator(null).toString(), "ObjectCreator<Spring Bean 'fred'>");
verify();
expect((Class) ac.getType("barney")).andReturn(Runnable.class);
expect(ac.getBean("barney")).andReturn(barney);
replay();
sd = moduleDef.getServiceDef("barney");
assertSame(sd.createServiceCreator(null).createObject(), barney);
assertNull(moduleDef.getServiceDef("arnold"));
}
use of org.apache.tapestry5.ioc.ServiceBuilderResources in project tapestry-5 by apache.
the class IOCTestCase method mockServiceBuilderResources.
protected final ServiceBuilderResources mockServiceBuilderResources(OperationTracker tracker) {
ServiceBuilderResources resources = mockServiceBuilderResources();
train_getTracker(resources, tracker);
return resources;
}
use of org.apache.tapestry5.ioc.ServiceBuilderResources in project tapestry-5 by apache.
the class ModuleImpl method create.
/**
* Creates the service and updates the cache of created services.
*
* @param eagerLoadProxies a list into which any eager loaded proxies should be added
*/
private Object create(final ServiceDef3 def, final Collection<EagerLoadServiceProxy> eagerLoadProxies) {
final String serviceId = def.getServiceId();
final Logger logger = registry.getServiceLogger(serviceId);
final Class serviceInterface = def.getServiceInterface();
final boolean canBeProxied = canBeProxiedPredicate.test(serviceInterface);
String description = String.format("Creating %s service %s", canBeProxied ? "proxy for" : "non-proxied instance of", serviceId);
if (logger.isDebugEnabled())
logger.debug(description);
final Module module = this;
Invokable operation = new Invokable() {
@Override
public Object invoke() {
try {
ServiceBuilderResources resources = new ServiceResourcesImpl(registry, module, def, proxyFactory, logger);
// Build up a stack of operations that will be needed to realize the service
// (by the proxy, at a later date).
ObjectCreator creator = def.createServiceCreator(resources);
// For non-proxyable services, we immediately create the service implementation
// and return it. There's no interface to proxy, which throws out the possibility of
// deferred instantiation, service lifecycles, and decorators.
ServiceLifecycle2 lifecycle = registry.getServiceLifecycle(def.getServiceScope());
if (!canBeProxied) {
if (lifecycle.requiresProxy())
throw new IllegalArgumentException(String.format("Service scope '%s' requires a proxy, but the service does not have a service interface (necessary to create a proxy). Provide a service interface or select a different service scope.", def.getServiceScope()));
return creator.createObject();
}
creator = new OperationTrackingObjectCreator(registry, String.format("Instantiating service %s implementation via %s", serviceId, creator), creator);
creator = new LifecycleWrappedServiceCreator(lifecycle, resources, creator);
// Marked services (or services inside marked modules) are not decorated.
// TapestryIOCModule prevents decoration of its services. Note that all decorators will decorate
// around the aspect interceptor, which wraps around the core service implementation.
boolean allowDecoration = !def.isPreventDecoration();
if (allowDecoration) {
creator = new AdvisorStackBuilder(def, creator, getAspectDecorator(), registry);
creator = new InterceptorStackBuilder(def, creator, registry);
}
// Add a wrapper that checks for recursion.
creator = new RecursiveServiceCreationCheckWrapper(def, creator, logger);
creator = new OperationTrackingObjectCreator(registry, "Realizing service " + serviceId, creator);
JustInTimeObjectCreator delegate = new JustInTimeObjectCreator(tracker, creator, serviceId);
Object proxy = createProxy(resources, delegate, def.isPreventDecoration());
registry.addRegistryShutdownListener(delegate);
if (def.isEagerLoad() && eagerLoadProxies != null)
eagerLoadProxies.add(delegate);
tracker.setStatus(serviceId, Status.VIRTUAL);
return proxy;
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(IOCMessages.errorBuildingService(serviceId, def, ex), ex);
}
}
};
return registry.invoke(description, operation);
}
Aggregations