Search in sources :

Example 1 with ReadOnlyComponentFieldConduit

use of org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit in project tapestry-5 by apache.

the class BlockInjectionProvider method createConduit.

private FieldConduit<Object> createConduit(PlasticField field, final String blockId) {
    final String className = field.getPlasticClass().getClassName();
    final String fieldName = field.getName();
    return new ReadOnlyComponentFieldConduit(className, fieldName) {

        public Object get(Object instance, InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            return resources.getBlock(blockId);
        }
    };
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 2 with ReadOnlyComponentFieldConduit

use of org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit in project tapestry-5 by apache.

the class MixinWorker method createMixinFieldProvider.

private ComputedValue<FieldConduit<Object>> createMixinFieldProvider(final String fieldName, final String mixinClassName) {
    return new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            final InternalComponentResources icr = (InternalComponentResources) resources;
            return new ReadOnlyComponentFieldConduit(resources, fieldName) {

                public Object get(Object instance, InstanceContext context) {
                    return icr.getMixinByClassName(mixinClassName);
                }
            };
        }
    };
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 3 with ReadOnlyComponentFieldConduit

use of org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit in project tapestry-5 by apache.

the class InjectContainerWorker method createFieldValueConduitProvider.

private ComputedValue<FieldConduit<Object>> createFieldValueConduitProvider(PlasticField field) {
    final String fieldName = field.getName();
    final String fieldTypeName = field.getTypeName();
    return new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            final Class fieldType = cache.forName(fieldTypeName);
            final ComponentResources resources = context.get(ComponentResources.class);
            return new ReadOnlyComponentFieldConduit(resources, fieldName) {

                public Object get(Object instance, InstanceContext context) {
                    Component container = resources.getContainer();
                    if (!fieldType.isInstance(container)) {
                        String message = String.format("Component %s (type %s) is not assignable to field %s.%s (of type %s).", container.getComponentResources().getCompleteId(), container.getClass().getName(), resources.getComponentModel().getComponentClassName(), fieldName, fieldTypeName);
                        throw new RuntimeException(message);
                    }
                    return container;
                }
            };
        }
    };
}
Also used : Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 4 with ReadOnlyComponentFieldConduit

use of org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit in project tapestry-5 by apache.

the class PersistenceContextWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (final PlasticField field : plasticClass.getFieldsWithAnnotation(PersistenceContext.class)) {
        final PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
        field.claim(annotation);
        field.setConduit(new ReadOnlyComponentFieldConduit(plasticClass.getClassName(), field.getName()) {

            @Override
            public Object get(Object instance, InstanceContext context) {
                return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
            }
        });
    }
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) PersistenceContext(javax.persistence.PersistenceContext)

Example 5 with ReadOnlyComponentFieldConduit

use of org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit in project tapestry-5 by apache.

the class ApplicationStateWorker method transform.

@SuppressWarnings("unchecked")
private void transform(PlasticClass transformation, PlasticField field, final boolean create) {
    final Class fieldClass = componentClassCache.forName(field.getTypeName());
    field.setConduit(new FieldConduit() {

        public void set(Object instance, InstanceContext context, Object newValue) {
            applicationStateManager.set(fieldClass, newValue);
        }

        public Object get(Object instance, InstanceContext context) {
            return create ? applicationStateManager.get(fieldClass) : applicationStateManager.getIfExists(fieldClass);
        }
    });
    final String expectedName = field.getName() + "Exists";
    List<PlasticField> fields = F.flow(transformation.getAllFields()).filter(new Predicate<PlasticField>() {

        public boolean accept(PlasticField field) {
            return field.getTypeName().equals("boolean") && field.getName().equalsIgnoreCase(expectedName);
        }
    }).toList();
    for (PlasticField existsField : fields) {
        existsField.claim(this);
        final String className = transformation.getClassName();
        final String fieldName = existsField.getName();
        existsField.setConduit(new ReadOnlyComponentFieldConduit(className, fieldName) {

            public Object get(Object instance, InstanceContext context) {
                return applicationStateManager.exists(fieldClass);
            }
        });
    }
}
Also used : FieldConduit(org.apache.tapestry5.plastic.FieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) Predicate(org.apache.tapestry5.func.Predicate)

Aggregations

ComponentResources (org.apache.tapestry5.ComponentResources)3 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)3 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)2 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 PersistenceContext (javax.persistence.PersistenceContext)1 Predicate (org.apache.tapestry5.func.Predicate)1 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)1 FieldConduit (org.apache.tapestry5.plastic.FieldConduit)1 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)1 Component (org.apache.tapestry5.runtime.Component)1