Search in sources :

Example 6 with ObjectCreator

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"));
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) ServiceDef(org.apache.tapestry5.ioc.def.ServiceDef) ServletContext(javax.servlet.ServletContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ServiceBuilderResources(org.apache.tapestry5.ioc.ServiceBuilderResources) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator) Test(org.testng.annotations.Test)

Example 7 with ObjectCreator

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;
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) PersistenceContext(javax.persistence.PersistenceContext) PlasticProxyFactory(org.apache.tapestry5.commons.services.PlasticProxyFactory) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Example 8 with ObjectCreator

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);
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Example 9 with ObjectCreator

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() + ")>");
}
Also used : ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Example 10 with ObjectCreator

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);
}
Also used : Constructor(java.lang.reflect.Constructor) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectStreamException(java.io.ObjectStreamException) JustInTimeObjectCreator(org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)

Aggregations

ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)5 Constructor (java.lang.reflect.Constructor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)3 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)3 ObjectStreamException (java.io.ObjectStreamException)2 ServiceBuilderResources (org.apache.tapestry5.ioc.ServiceBuilderResources)2 JustInTimeObjectCreator (org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)2 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)2 EntityManager (javax.persistence.EntityManager)1 PersistenceContext (javax.persistence.PersistenceContext)1 ServletContext (javax.servlet.ServletContext)1 Location (org.apache.tapestry5.commons.Location)1 StringLocation (org.apache.tapestry5.commons.internal.services.StringLocation)1 PlasticProxyFactory (org.apache.tapestry5.commons.services.PlasticProxyFactory)1 Invokable (org.apache.tapestry5.ioc.Invokable)1 ServiceLifecycle2 (org.apache.tapestry5.ioc.ServiceLifecycle2)1 ServiceDef (org.apache.tapestry5.ioc.def.ServiceDef)1 DelegatingInjectionResources (org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources)1 WrongConfigurationTypeGuard (org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard)1