Search in sources :

Example 11 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.

the class ValueObjectProvider method provide.

@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
    Value annotation = annotationProvider.getAnnotation(Value.class);
    if (annotation == null)
        return null;
    String value = annotation.value();
    Object expanded = symbolSource.expandSymbols(value);
    IntermediateType intermediate = annotationProvider.getAnnotation(IntermediateType.class);
    if (intermediate != null)
        expanded = typeCoercer.coerce(expanded, intermediate.value());
    return typeCoercer.coerce(expanded, objectType);
}
Also used : IntermediateType(org.apache.tapestry5.ioc.annotations.IntermediateType) Value(org.apache.tapestry5.ioc.annotations.Value)

Example 12 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator 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)

Example 13 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.

the class RegistryImpl method getOrderedConfiguration.

@Override
@SuppressWarnings("unchecked")
public <T> List<T> getOrderedConfiguration(ServiceDef3 serviceDef, Class<T> objectType) {
    lock.check();
    String serviceId = serviceDef.getServiceId();
    Logger logger = getServiceLogger(serviceId);
    Orderer<T> orderer = new Orderer<T>(logger);
    Map<String, OrderedConfigurationOverride<T>> overrides = CollectionFactory.newCaseInsensitiveMap();
    // TAP5-2129. NOTICE: if someday an ordering between modules is added, this should be reverted
    // or a notice added to the documentation.
    List<Module> modules = new ArrayList<Module>(moduleToServiceDefs.keySet());
    Collections.sort(modules, new ModuleComparator());
    for (Module m : modules) addToOrderedConfiguration(orderer, overrides, objectType, serviceDef, m);
    if (serviceId.equals("MasterObjectProvider")) {
        ObjectProvider contribution = new ObjectProvider() {

            @Override
            public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                return findServiceByMarkerAndType(objectType, annotationProvider, null);
            }
        };
        orderer.add("ServiceByMarker", (T) contribution);
    }
    for (OrderedConfigurationOverride<T> override : overrides.values()) override.apply();
    final List<T> result = orderer.getOrdered();
    if (!isServiceConfigurationListenerServiceDef(serviceDef)) {
        serviceConfigurationListener.onOrderedConfiguration(serviceDef, result);
    }
    return result;
}
Also used : NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) Logger(org.slf4j.Logger) MasterObjectProvider(org.apache.tapestry5.ioc.services.MasterObjectProvider) TapestryIOCModule(org.apache.tapestry5.ioc.modules.TapestryIOCModule) Orderer(org.apache.tapestry5.ioc.internal.util.Orderer)

Example 14 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.

the class ServiceAnnotationObjectProviderTest method annotation_present.

@SuppressWarnings("unchecked")
@Test
public void annotation_present() {
    Class objectType = Runnable.class;
    AnnotationProvider provider = mockAnnotationProvider();
    ObjectLocator locator = mockObjectLocator();
    Service service = newMock(Service.class);
    String serviceId = "JiffyPop";
    Runnable instance = mockRunnable();
    train_getAnnotation(provider, Service.class, service);
    expect(service.value()).andReturn(serviceId);
    train_getService(locator, serviceId, objectType, instance);
    replay();
    ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
    assertSame(objectProvider.provide(objectType, provider, locator), instance);
    verify();
}
Also used : AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) ObjectLocator(org.apache.tapestry5.commons.ObjectLocator) Service(org.apache.tapestry5.annotations.Service) ObjectProvider(org.apache.tapestry5.commons.ObjectProvider) Test(org.testng.annotations.Test)

Example 15 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.

the class ServiceAnnotationObjectProviderTest method no_annotation.

@SuppressWarnings("unchecked")
@Test
public void no_annotation() {
    Class objectType = Runnable.class;
    AnnotationProvider provider = mockAnnotationProvider();
    ObjectLocator locator = mockObjectLocator();
    train_getAnnotation(provider, Service.class, null);
    replay();
    ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
    assertNull(objectProvider.provide(objectType, provider, locator));
    verify();
}
Also used : AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) ObjectLocator(org.apache.tapestry5.commons.ObjectLocator) ObjectProvider(org.apache.tapestry5.commons.ObjectProvider) Test(org.testng.annotations.Test)

Aggregations

Asset (org.apache.tapestry5.Asset)5 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)5 ObjectLocator (org.apache.tapestry5.commons.ObjectLocator)5 Test (org.testng.annotations.Test)5 ObjectProvider (org.apache.tapestry5.commons.ObjectProvider)4 Path (org.apache.tapestry5.annotations.Path)3 Map (java.util.Map)2 ComponentResources (org.apache.tapestry5.ComponentResources)2 NullAnnotationProvider (org.apache.tapestry5.commons.internal.NullAnnotationProvider)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 IntermediateType (org.apache.tapestry5.ioc.annotations.IntermediateType)2 MasterObjectProvider (org.apache.tapestry5.ioc.services.MasterObjectProvider)2 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)2 AssetSource (org.apache.tapestry5.services.AssetSource)2 ObjectStreamException (java.io.ObjectStreamException)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DateFormat (java.text.DateFormat)1