Search in sources :

Example 26 with AnnotationProvider

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

the class AssetObjectProviderTest method no_path_annotation.

@Test
public void no_path_annotation() {
    AssetSource source = mockAssetSource();
    ObjectLocator locator = mockObjectLocator();
    AnnotationProvider annotationProvider = mockAnnotationProvider();
    TypeCoercer typeCoercer = mockTypeCoercer();
    SymbolSource symbolSource = mockSymbolSource();
    train_getAnnotation(annotationProvider, Path.class, null);
    replay();
    ObjectProvider provider = new AssetObjectProvider(source, typeCoercer, symbolSource);
    assertNull(provider.provide(Asset.class, annotationProvider, locator));
    verify();
}
Also used : AssetSource(org.apache.tapestry5.services.AssetSource) SymbolSource(org.apache.tapestry5.ioc.services.SymbolSource) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) ObjectLocator(org.apache.tapestry5.commons.ObjectLocator) Asset(org.apache.tapestry5.Asset) ObjectProvider(org.apache.tapestry5.commons.ObjectProvider) Test(org.testng.annotations.Test)

Example 27 with AnnotationProvider

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

the class AssetObjectProviderTest method normal_conversion.

@Test
public void normal_conversion() {
    AssetSource source = mockAssetSource();
    ObjectLocator locator = mockObjectLocator();
    Asset asset = mockAsset();
    String path = "${foo}";
    String expanded = "foo/bar/baz.gif";
    AnnotationProvider annotationProvider = mockAnnotationProvider();
    TypeCoercer typeCoercer = mockTypeCoercer();
    Path pathAnnotation = mockPath();
    SymbolSource symbolSource = mockSymbolSource();
    train_getAnnotation(annotationProvider, Path.class, pathAnnotation);
    train_value(pathAnnotation, path);
    train_expandSymbols(symbolSource, path, expanded);
    train_getAsset(source, null, expanded, null, asset);
    train_coerce(typeCoercer, asset, Asset.class, asset);
    replay();
    ObjectProvider provider = new AssetObjectProvider(source, typeCoercer, symbolSource);
    Asset result = provider.provide(Asset.class, annotationProvider, locator);
    assertSame(result, asset);
    verify();
}
Also used : AssetSource(org.apache.tapestry5.services.AssetSource) Path(org.apache.tapestry5.annotations.Path) SymbolSource(org.apache.tapestry5.ioc.services.SymbolSource) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) ObjectLocator(org.apache.tapestry5.commons.ObjectLocator) Asset(org.apache.tapestry5.Asset) ObjectProvider(org.apache.tapestry5.commons.ObjectProvider) Test(org.testng.annotations.Test)

Example 28 with AnnotationProvider

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

the class CDIInjectionProvider method provideInjection.

/* (non-Javadoc)
	 * @see org.apache.tapestry5.services.transform.InjectionProvider2#provideInjection(org.apache.tapestry5.plastic.PlasticField, org.apache.tapestry5.ioc.ObjectLocator, org.apache.tapestry5.model.MutableComponentModel)
	 */
@SuppressWarnings("rawtypes")
public boolean provideInjection(final PlasticField field, final ObjectLocator locator, final MutableComponentModel componentModel) {
    Class type = cache.forName(field.getTypeName());
    if (InternalUtils.isManagedByTapestry(type, new AnnotationProvider() {

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return field.getAnnotation(annotationClass);
        }
    }, locator)) {
        logger.debug("Field " + field.getName() + " of type " + field.getTypeName() + " is managed by Tapestry");
        return false;
    }
    logger.debug("Field " + field.getName() + " of type " + field.getTypeName() + " will be managed by CDI");
    final Class<?> fieldClass = load(field.getTypeName());
    final Annotation[] qualifiers = InternalUtils.getFieldQualifiers(type, new AnnotationProvider() {

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return field.getAnnotation(annotationClass);
        }
    });
    logger.debug("[" + field.getName() + "][" + componentModel.getComponentClassName() + "] Qualifiers : ");
    for (Annotation annotation : qualifiers) {
        logger.debug("==> " + annotation.toString());
    }
    try {
        final BeanInstance instance = getInstance(fieldClass, qualifiers);
        final boolean resolved = instance != null && instance.isResolved();
        if (resolved) {
            field.inject(instance.getBean());
        }
        if (instance != null && instance.isReleasable()) {
            synchronized (instancesToRelease) {
                instancesToRelease.add(instance);
            }
        }
        logger.debug("Is field " + field.getName() + " of type " + field.getTypeName() + " has been succesfully managed by CDI ? " + resolved);
        return resolved;
    } catch (IllegalStateException isa) {
        logger.debug("CDI failed to manage the field " + field.getName() + " of type " + field.getTypeName());
        return false;
    }
}
Also used : AnnotationProvider(org.apache.tapestry5.ioc.AnnotationProvider) Annotation(java.lang.annotation.Annotation)

Example 29 with AnnotationProvider

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

the class TapestryModule method decorateFieldValidatorDefaultSource.

/**
 * Decorate FieldValidatorDefaultSource to setup the EnvironmentMessages
 * object and place it in the environment.
 * Although this could have been implemented directly in the default
 * implementation of the service, doing it
 * as service decoration ensures that the environment will be properly setup
 * even if a user overrides the default
 * service implementation.
 *
 * @param defaultSource
 *         The service to decorate
 * @param environment
 */
public static FieldValidatorDefaultSource decorateFieldValidatorDefaultSource(final FieldValidatorDefaultSource defaultSource, final Environment environment) {
    return new FieldValidatorDefaultSource() {

        public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) {
            environment.push(EnvironmentMessages.class, new EnvironmentMessages(overrideMessages, overrideId));
            FieldValidator fieldValidator = defaultSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
            environment.pop(EnvironmentMessages.class);
            return fieldValidator;
        }

        public FieldValidator createDefaultValidator(ComponentResources resources, String parameterName) {
            EnvironmentMessages em = new EnvironmentMessages(resources.getContainerMessages(), resources.getId());
            environment.push(EnvironmentMessages.class, em);
            FieldValidator fieldValidator = defaultSource.createDefaultValidator(resources, parameterName);
            environment.pop(EnvironmentMessages.class);
            return fieldValidator;
        }
    };
}
Also used : PersistentLocale(org.apache.tapestry5.services.PersistentLocale) Locale(java.util.Locale) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Field(org.apache.tapestry5.Field) ComponentMessages(org.apache.tapestry5.services.ComponentMessages) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) FieldValidator(org.apache.tapestry5.FieldValidator) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 30 with AnnotationProvider

use of org.apache.tapestry5.commons.AnnotationProvider 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;
}
Also used : OperationTracker(org.apache.tapestry5.ioc.OperationTracker) ModuleBuilderSource(org.apache.tapestry5.ioc.ModuleBuilderSource) ContributionDef(org.apache.tapestry5.ioc.def.ContributionDef) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ServiceResources(org.apache.tapestry5.ioc.ServiceResources) Map(java.util.Map)

Aggregations

AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)17 Test (org.testng.annotations.Test)13 Messages (org.apache.tapestry5.commons.Messages)10 Locale (java.util.Locale)9 Field (org.apache.tapestry5.Field)9 Map (java.util.Map)7 FieldValidator (org.apache.tapestry5.FieldValidator)6 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)6 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)6 FieldTranslator (org.apache.tapestry5.FieldTranslator)5 Translator (org.apache.tapestry5.Translator)5 MarkupWriter (org.apache.tapestry5.MarkupWriter)4 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)4 ObjectLocator (org.apache.tapestry5.commons.ObjectLocator)4 ObjectProvider (org.apache.tapestry5.commons.ObjectProvider)4 FieldValidatorDefaultSource (org.apache.tapestry5.services.FieldValidatorDefaultSource)4 Asset (org.apache.tapestry5.Asset)3 NullAnnotationProvider (org.apache.tapestry5.commons.internal.NullAnnotationProvider)3 MasterObjectProvider (org.apache.tapestry5.ioc.services.MasterObjectProvider)3 FormSupport (org.apache.tapestry5.services.FormSupport)3