use of org.apache.tapestry5.commons.ObjectCreator 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.commons.ObjectCreator in project tapestry-5 by apache.
the class EntityManagerObjectProvider method getOrCreateProxy.
@SuppressWarnings({ "unchecked", "rawtypes" })
private EntityManager getOrCreateProxy(final AnnotationProvider annotationProvider, final ObjectLocator objectLocator) {
final PersistenceContext annotation = annotationProvider.getAnnotation(PersistenceContext.class);
final String unitName = annotation == null ? null : annotation.unitName();
EntityManager proxy = emProxyByName.get(unitName);
if (proxy == null)
synchronized (this) {
final PlasticProxyFactory proxyFactory = objectLocator.getService("PlasticProxyFactory", PlasticProxyFactory.class);
proxy = proxyFactory.createProxy(EntityManager.class, new ObjectCreator() {
@Override
public Object createObject() {
final EntityManagerManager entityManagerManager = objectLocator.getService(EntityManagerManager.class);
return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
}
}, "<EntityManagerProxy>");
emProxyByName.put(unitName, proxy);
}
return proxy;
}
use of org.apache.tapestry5.commons.ObjectCreator in project tapestry-5 by apache.
the class LazyAdvisorImpl method addAdvice.
private void addAdvice(Method method, MethodAdviceReceiver receiver) {
final Class thunkType = method.getReturnType();
final String description = String.format("<%s Thunk for %s>", thunkType.getName(), InternalUtils.asString(method));
MethodAdvice advice = new MethodAdvice() {
/**
* When the method is invoked, we don't immediately proceed. Instead, we return a thunk instance
* that defers its behavior to the lazily invoked invocation.
*/
@Override
public void advise(final MethodInvocation invocation) {
ObjectCreator deferred = new ObjectCreator() {
@Override
public Object createObject() {
invocation.proceed();
return invocation.getReturnValue();
}
};
ObjectCreator cachingObjectCreator = new CachingObjectCreator(deferred);
Object thunk = thunkCreator.createThunk(thunkType, cachingObjectCreator, description);
invocation.setReturnValue(thunk);
}
};
receiver.adviseMethod(method, advice);
}
use of org.apache.tapestry5.commons.ObjectCreator in project tapestry-5 by apache.
the class PerThreadServiceLifecycle method createService.
@Override
public Object createService(ServiceResources resources, ObjectCreator creator) {
ObjectCreator perThreadCreator = perthreadManager.createValue(creator);
Class serviceInterface = resources.getServiceInterface();
return proxyFactory.createProxy(serviceInterface, perThreadCreator, "<PerThread Proxy for " + resources.getServiceId() + "(" + serviceInterface.getName() + ")>");
}
use of org.apache.tapestry5.commons.ObjectCreator in project tapestry-5 by apache.
the class ModuleImpl method instantiateModuleInstance.
private Object instantiateModuleInstance() {
Class moduleClass = moduleDef.getBuilderClass();
Constructor[] constructors = moduleClass.getConstructors();
if (constructors.length == 0)
throw new RuntimeException(IOCMessages.noPublicConstructors(moduleClass));
if (constructors.length > 1) {
// Sort the constructors ascending by number of parameters (descending); this is really
// just to allow the test suite to work properly across different JVMs (which will
// often order the constructors differently).
Comparator<Constructor> comparator = new Comparator<Constructor>() {
@Override
public int compare(Constructor c1, Constructor c2) {
return c2.getParameterTypes().length - c1.getParameterTypes().length;
}
};
Arrays.sort(constructors, comparator);
logger.warn(IOCMessages.tooManyPublicConstructors(moduleClass, constructors[0]));
}
Constructor constructor = constructors[0];
if (insideConstructor)
throw new RuntimeException(IOCMessages.recursiveModuleConstructor(moduleClass, constructor));
ObjectLocator locator = new ObjectLocatorImpl(registry, this);
Map<Class, Object> resourcesMap = CollectionFactory.newMap();
resourcesMap.put(Logger.class, logger);
resourcesMap.put(ObjectLocator.class, locator);
resourcesMap.put(OperationTracker.class, registry);
InjectionResources resources = new MapInjectionResources(resourcesMap);
Throwable fail = null;
try {
insideConstructor = true;
ObjectCreator[] parameterValues = InternalUtils.calculateParameters(locator, resources, constructor.getParameterTypes(), constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), registry);
Object[] realized = InternalUtils.realizeObjects(parameterValues);
Object result = constructor.newInstance(realized);
InternalUtils.injectIntoFields(result, locator, resources, registry);
return result;
} catch (InvocationTargetException ex) {
fail = ex.getTargetException();
} catch (Exception ex) {
fail = ex;
} finally {
insideConstructor = false;
}
throw new RuntimeException(IOCMessages.instantiateBuilderError(moduleClass, fail), fail);
}
Aggregations