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();
}
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();
}
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;
}
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);
}
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);
}
}
}
Aggregations