Search in sources :

Example 1 with AnnotationProvider

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

the class DefaultInjectionProvider method provideInjection.

public boolean provideInjection(final PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) {
    Class fieldType = classCache.forName(field.getTypeName());
    Object injectionValue = masterObjectProvider.provide(fieldType, new AnnotationProvider() {

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return field.getAnnotation(annotationClass);
        }
    }, this.locator, false);
    if (injectionValue != null) {
        field.inject(injectionValue);
        return true;
    }
    return false;
}
Also used : AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider)

Example 2 with AnnotationProvider

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

the class RegistryImpl method getObject.

@Override
public <T> T getObject(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator, Module localModule) {
    lock.check();
    AnnotationProvider effectiveProvider = annotationProvider != null ? annotationProvider : new NullAnnotationProvider();
    // We do a check here for known marker/type combinations, so that you can use a marker
    // annotation
    // to inject into a contribution method that contributes to MasterObjectProvider.
    // We also force a contribution into MasterObjectProvider to accomplish the same thing.
    T result = findServiceByMarkerAndType(objectType, annotationProvider, localModule);
    if (result != null)
        return result;
    MasterObjectProvider masterProvider = getService(IOCConstants.MASTER_OBJECT_PROVIDER_SERVICE_ID, MasterObjectProvider.class);
    return masterProvider.provide(objectType, effectiveProvider, locator, true);
}
Also used : NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) MasterObjectProvider(org.apache.tapestry5.ioc.services.MasterObjectProvider)

Example 3 with AnnotationProvider

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

the class TapestryBeanFactory method resolveDependency.

@Override
public Object resolveDependency(final DependencyDescriptor descriptor, String beanName, Set autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
    Class objectType = descriptor.getDependencyType();
    final Annotation[] annotations = descriptor.getAnnotations();
    if (annotations != null) {
        AnnotationProvider provider = new AnnotationProvider() {

            @Override
            public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
                for (Object a : annotations) {
                    if (annotationClass.isInstance(a))
                        return annotationClass.cast(a);
                }
                return null;
            }
        };
        if (provider.getAnnotation(Inject.class) != null || provider.getAnnotation(InjectService.class) != null)
            return registry.getObject(objectType, provider);
    }
    return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
}
Also used : AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) Annotation(java.lang.annotation.Annotation)

Example 4 with AnnotationProvider

use of org.apache.tapestry5.commons.AnnotationProvider 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 5 with AnnotationProvider

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

the class FieldTranslatorSourceImplTest method create_default_translator_with_name_and_null_key.

@Test
public void create_default_translator_with_name_and_null_key() {
    Field field = mockField();
    Messages messages = mockMessages();
    Locale locale = Locale.ENGLISH;
    Class propertyType = Map.class;
    TranslatorSource ts = mockTranslatorSource();
    FormSupport fs = mockFormSupport();
    Translator translator = mockTranslator("maptrans", Map.class);
    Messages globalMessages = mockMessages();
    MessageFormatter formatter = mockMessageFormatter();
    MarkupWriter writer = mockMarkupWriter();
    String label = "Field Label";
    String message = "Woops, did it again.";
    AnnotationProvider ap = mockAnnotationProvider(null);
    train_findByType(ts, propertyType, translator);
    train_getFormValidationId(fs, "myform");
    train_contains(messages, "myform-myfield-maptrans-message", false);
    train_contains(messages, "myfield-maptrans-message", false);
    train_getMessageKey(translator, null);
    train_getMessageFormatter(globalMessages, "maptrans-message", formatter);
    train_getLabel(field, label);
    train_format(formatter, message, label);
    translator.render(field, message, writer, fs);
    replay();
    FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, globalMessages, fs);
    FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
    assertEquals(ft.getType(), Map.class);
    ft.render(writer);
    verify();
}
Also used : Locale(java.util.Locale) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldTranslator(org.apache.tapestry5.FieldTranslator) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) MarkupWriter(org.apache.tapestry5.MarkupWriter) FormSupport(org.apache.tapestry5.services.FormSupport) TranslatorSource(org.apache.tapestry5.services.TranslatorSource) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Field(org.apache.tapestry5.Field) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Map(java.util.Map) Test(org.testng.annotations.Test)

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