Search in sources :

Example 26 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField 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 27 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField in project tapestry-5 by apache.

the class TestInjectTransformer method transform.

@Override
public void transform(PlasticClass plasticClass) {
    for (PlasticField f : plasticClass.getFieldsWithAnnotation(TestInject.class)) {
        if (f.getTypeName().equals(className)) {
            f.inject(fieldValue);
            f.claim(this);
        }
    }
}
Also used : PlasticField(org.apache.tapestry5.plastic.PlasticField)

Example 28 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField in project tapestry-5 by apache.

the class CachedWorker method adviseMethod.

private void adviseMethod(PlasticClass plasticClass, PlasticMethod method) {
    // Every instance of the clas srequires its own per-thread value. This handles the case of multiple
    // pages containing the component, or the same page containing the component multiple times.
    PlasticField cacheField = plasticClass.introduceField(PerThreadValue.class, "cache$" + method.getDescription().methodName);
    cacheField.injectComputed(new ComputedValue<PerThreadValue>() {

        public PerThreadValue get(InstanceContext context) {
            // Each instance will get a new PerThreadValue
            return perThreadManager.createValue();
        }
    });
    Cached annotation = method.getAnnotation(Cached.class);
    MethodResultCacheFactory factory = createFactory(plasticClass, annotation.watch(), method);
    MethodAdvice advice = createAdvice(cacheField, factory);
    method.addAdvice(advice);
}
Also used : Cached(org.apache.tapestry5.annotations.Cached) PerThreadValue(org.apache.tapestry5.ioc.services.PerThreadValue)

Example 29 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField in project tapestry-5 by apache.

the class ComponentWorker method transformField.

private void transformField(PlasticClass transformation, MutableComponentModel model, PlasticField field) {
    Component annotation = field.getAnnotation(Component.class);
    field.claim(annotation);
    String annotationId = annotation.id();
    String fieldName = field.getName();
    String id = InternalUtils.isNonBlank(annotationId) ? annotationId : InternalUtils.stripMemberName(fieldName);
    String type = field.getTypeName();
    Location location = new StringLocation(String.format("%s.%s", transformation.getClassName(), fieldName), 0);
    MutableEmbeddedComponentModel embedded = model.addEmbeddedComponent(id, annotation.type(), type, annotation.inheritInformalParameters(), location);
    addParameters(embedded, annotation.parameters());
    updateModelWithPublishedParameters(embedded, annotation);
    convertAccessToField(field, id);
    addMixinClasses(field, embedded);
    addMixinTypes(field, embedded);
}
Also used : StringLocation(org.apache.tapestry5.commons.internal.services.StringLocation) Component(org.apache.tapestry5.annotations.Component) MutableEmbeddedComponentModel(org.apache.tapestry5.model.MutableEmbeddedComponentModel) StringLocation(org.apache.tapestry5.commons.internal.services.StringLocation) Location(org.apache.tapestry5.commons.Location)

Example 30 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField in project tapestry-5 by apache.

the class ComponentWorker method addMixinClasses.

private void addMixinClasses(PlasticField field, MutableEmbeddedComponentModel model) {
    MixinClasses annotation = field.getAnnotation(MixinClasses.class);
    if (annotation == null)
        return;
    boolean orderEmpty = annotation.order().length == 0;
    if (!orderEmpty && annotation.order().length != annotation.value().length)
        throw new TapestryException(String.format("%d mixins defined via @MixinClasses on field '%s', but %d ordering constraints \\\n" + " specified (expected 0 or %1$d).", annotation.value().length, field.getName(), annotation.order().length), model, null);
    for (int i = 0; i < annotation.value().length; i++) {
        String[] constraints = orderEmpty ? CommonsUtils.EMPTY_STRING_ARRAY : TapestryInternalUtils.splitMixinConstraints(annotation.order()[i]);
        model.addMixin(annotation.value()[i].getName(), constraints);
    }
}
Also used : MixinClasses(org.apache.tapestry5.annotations.MixinClasses) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

PlasticField (org.apache.tapestry5.plastic.PlasticField)15 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)7 ComponentResources (org.apache.tapestry5.ComponentResources)5 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)5 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)5 Method (java.lang.reflect.Method)4 InstructionBuilder (org.apache.tapestry5.plastic.InstructionBuilder)4 InstructionBuilderCallback (org.apache.tapestry5.plastic.InstructionBuilderCallback)4 PlasticClassTransformer (org.apache.tapestry5.plastic.PlasticClassTransformer)3 PageActivationContext (org.apache.tapestry5.annotations.PageActivationContext)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)2 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)2 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)2 IFn (clojure.lang.IFn)1 Symbol (clojure.lang.Symbol)1 CDI (com.flowlogix.web.services.annotations.CDI)1 Stateful (com.flowlogix.web.services.annotations.Stateful)1 Annotation (java.lang.annotation.Annotation)1 EJB (javax.ejb.EJB)1