Search in sources :

Example 1 with PropertyAccess

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

the class ComponentDefaultProviderImplTest method no_matching_property_for_default.

@Test
public void no_matching_property_for_default() {
    String parameterName = "myparam";
    String id = "mycomponentid";
    ComponentResources resources = mockComponentResources();
    Component container = mockComponent();
    PropertyAccess access = mockPropertyAccess();
    ClassPropertyAdapter classPropertyAdapter = mockClassPropertyAdapter();
    BindingSource bindingSource = mockBindingSource();
    train_getId(resources, id);
    train_getContainer(resources, container);
    train_getAdapter(access, container, classPropertyAdapter);
    train_getPropertyAdapter(classPropertyAdapter, id, null);
    replay();
    ComponentDefaultProvider source = new ComponentDefaultProviderImpl(access, bindingSource, null, null, null);
    assertNull(source.defaultBinding(parameterName, resources));
    verify();
}
Also used : BindingSource(org.apache.tapestry5.services.BindingSource) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources) PropertyAccess(org.apache.tapestry5.commons.services.PropertyAccess) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 2 with PropertyAccess

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

the class ComponentDefaultProviderImplTest method default_property_exists.

@Test
public void default_property_exists() {
    String parameterName = "myparam";
    String id = "mycomponentid";
    ComponentResources resources = mockComponentResources();
    Component container = mockComponent();
    PropertyAccess access = mockPropertyAccess();
    ClassPropertyAdapter classPropertyAdapter = mockClassPropertyAdapter();
    PropertyAdapter propertyAdapter = mockPropertyAdapter();
    BindingSource bindingSource = mockBindingSource();
    Binding binding = mockBinding();
    ComponentResources containerResources = mockComponentResources();
    train_getId(resources, id);
    train_getContainer(resources, container);
    train_getAdapter(access, container, classPropertyAdapter);
    train_getPropertyAdapter(classPropertyAdapter, id, propertyAdapter);
    train_getContainerResources(resources, containerResources);
    train_newBinding(bindingSource, "default myparam", containerResources, BindingConstants.PROP, id, binding);
    replay();
    ComponentDefaultProvider source = new ComponentDefaultProviderImpl(access, bindingSource, null, null, null);
    assertSame(source.defaultBinding(parameterName, resources), binding);
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) BindingSource(org.apache.tapestry5.services.BindingSource) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources) PropertyAccess(org.apache.tapestry5.commons.services.PropertyAccess) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 3 with PropertyAccess

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

the class ExceptionUtils method findCause.

/**
 * Locates a particular type of exception, working its way down via any property that returns some type of Exception.
 * This is more expensive, but more accurate, than {@link #findCause(Throwable, Class)} as it works with older exceptions
 * that do not properly implement the (relatively new) {@linkplain Throwable#getCause() cause property}.
 *
 * @param t      the outermost exception
 * @param type   the type of exception to search for
 * @param access used to access properties
 * @return the first exception of the given type, if found, or null
 */
public static <T extends Throwable> T findCause(Throwable t, Class<T> type, PropertyAccess access) {
    Throwable current = t;
    while (current != null) {
        if (type.isInstance(current)) {
            return type.cast(current);
        }
        Throwable next = null;
        ClassPropertyAdapter adapter = access.getAdapter(current);
        for (String name : adapter.getPropertyNames()) {
            Object value = adapter.getPropertyAdapter(name).get(current);
            if (value != null && value != current && value instanceof Throwable) {
                next = (Throwable) value;
                break;
            }
        }
        current = next;
    }
    return null;
}
Also used : ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter)

Example 4 with PropertyAccess

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

the class TapestryModule method contributeApplicationInitializer.

/**
 * Adds a listener to the {@link org.apache.tapestry5.internal.services.ComponentInstantiatorSource} that clears the
 * {@link PropertyAccess} and {@link TypeCoercer} caches on
 * a class loader invalidation. In addition, forces the
 * realization of {@link ComponentClassResolver} at startup.
 */
public void contributeApplicationInitializer(OrderedConfiguration<ApplicationInitializerFilter> configuration, final TypeCoercer typeCoercer, final ComponentClassResolver componentClassResolver, @ComponentClasses final InvalidationEventHub invalidationEventHub, @Autobuild final RestoreDirtySessionObjects restoreDirtySessionObjects) {
    final Runnable callback = new Runnable() {

        public void run() {
            propertyAccess.clearCache();
            typeCoercer.clearCache();
        }
    };
    ApplicationInitializerFilter clearCaches = new ApplicationInitializerFilter() {

        public void initializeApplication(Context context, ApplicationInitializer initializer) {
            // Snuck in here is the logic to clear the PropertyAccess
            // service's cache whenever
            // the component class loader is invalidated.
            invalidationEventHub.addInvalidationCallback(callback);
            endOfRequestEventHub.addEndOfRequestListener(restoreDirtySessionObjects);
            // Perform other pending initialization
            initializer.initializeApplication(context);
            // We don't care about the result, but this forces a load of the
            // service
            // at application startup, rather than on first request.
            componentClassResolver.isPageName("ForceLoadAtStartup");
        }
    };
    configuration.add("ClearCachesOnInvalidation", clearCaches);
}
Also used : PageActivationContext(org.apache.tapestry5.annotations.PageActivationContext) EventContext(org.apache.tapestry5.EventContext) Context(org.apache.tapestry5.http.services.Context) ApplicationInitializer(org.apache.tapestry5.http.services.ApplicationInitializer) ApplicationInitializerFilter(org.apache.tapestry5.http.services.ApplicationInitializerFilter)

Example 5 with PropertyAccess

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

the class HibernateModule method contributeValueEncoderSource.

/**
 * Contributes {@link ValueEncoderFactory}s for all registered Hibernate entity classes. Encoding and decoding are
 * based on the id property value of the entity using type coercion. Hence, if the id can be coerced to a String and
 * back then the entity can be coerced.
 */
@SuppressWarnings("unchecked")
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(HibernateSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) boolean provideEncoders, final HibernateSessionSource sessionSource, final Session session, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    Set<EntityType<?>> entities = sessionSource.getSessionFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : entities) {
        Class<?> entityClass = entityType.getJavaType();
        if (entityClass != null) {
            SingularAttribute<?, ?> id = entityType.getId(entityType.getIdType().getJavaType());
            final String idenfierPropertyName = id.getName();
            ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(Class type) {
                    return new HibernateEntityValueEncoder(entityClass, idenfierPropertyName, session, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass));
                }
            };
            configuration.add(entityClass, factory);
        }
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory)

Aggregations

ClassPropertyAdapter (org.apache.tapestry5.commons.services.ClassPropertyAdapter)3 ComponentResources (org.apache.tapestry5.ComponentResources)2 PropertyAccess (org.apache.tapestry5.commons.services.PropertyAccess)2 Component (org.apache.tapestry5.runtime.Component)2 BindingSource (org.apache.tapestry5.services.BindingSource)2 ComponentDefaultProvider (org.apache.tapestry5.services.ComponentDefaultProvider)2 ValueEncoderFactory (org.apache.tapestry5.services.ValueEncoderFactory)2 Test (org.testng.annotations.Test)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityType (javax.persistence.metamodel.EntityType)1 Metamodel (javax.persistence.metamodel.Metamodel)1 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)1 Binding (org.apache.tapestry5.Binding)1 EventContext (org.apache.tapestry5.EventContext)1 PageActivationContext (org.apache.tapestry5.annotations.PageActivationContext)1 PropertyAdapter (org.apache.tapestry5.commons.services.PropertyAdapter)1 HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)1 ApplicationInitializer (org.apache.tapestry5.http.services.ApplicationInitializer)1 ApplicationInitializerFilter (org.apache.tapestry5.http.services.ApplicationInitializerFilter)1 Context (org.apache.tapestry5.http.services.Context)1