Search in sources :

Example 1 with FacesBehavior

use of jakarta.faces.component.behavior.FacesBehavior in project myfaces by apache.

the class AnnotationConfigurator method handleFacesBehavior.

private void handleFacesBehavior(FacesConfigImpl facesConfig, Set<Class<?>> classes) {
    for (Class<?> clazz : classes) {
        FacesBehavior facesBehavior = (FacesBehavior) clazz.getAnnotation(FacesBehavior.class);
        if (facesBehavior != null) {
            // Can only apply @FacesBehavior to Behavior implementors.
            if (!jakarta.faces.component.behavior.Behavior.class.isAssignableFrom(clazz)) {
                // Just log this.  We'll catch it later in the runtime.
                if (log.isLoggable(Level.WARNING)) {
                    log.warning(clazz.getName() + " is annotated with @jakarta.faces.component.behavior.FacesBehavior, " + "but does not implement jakarta.faces.component.behavior.Behavior");
                }
            }
            if (log.isLoggable(Level.FINEST)) {
                log.finest("addBehavior(" + facesBehavior.value() + ", " + clazz.getName() + ')');
            }
            BehaviorImpl behavior = new BehaviorImpl();
            behavior.setBehaviorId(facesBehavior.value());
            behavior.setBehaviorClass(clazz.getName());
            facesConfig.addBehavior(behavior);
        }
    }
}
Also used : BehaviorImpl(org.apache.myfaces.config.impl.element.BehaviorImpl) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior)

Example 2 with FacesBehavior

use of jakarta.faces.component.behavior.FacesBehavior in project myfaces by apache.

the class ApplicationImpl method createBehavior.

@Override
public Behavior createBehavior(String behaviorId) throws FacesException {
    Assert.notEmpty(behaviorId, "behaviorId");
    final Class<? extends Behavior> behaviorClass = getObjectFromClassMap(behaviorId, _behaviorClassMap);
    if (behaviorClass == null) {
        throw new FacesException("Could not find any registered behavior-class for behaviorId : " + behaviorId);
    }
    try {
        if (!_cdiManagedBehaviorMap.containsKey(behaviorClass)) {
            FacesBehavior annotation = behaviorClass.getAnnotation(FacesBehavior.class);
            if (annotation != null && annotation.managed()) {
                _cdiManagedBehaviorMap.put(behaviorClass, true);
            } else {
                _cdiManagedBehaviorMap.put(behaviorClass, false);
            }
        }
        boolean managed = _cdiManagedBehaviorMap.get(behaviorClass);
        Behavior behavior = null;
        if (managed) {
            if (ClientBehavior.class.isAssignableFrom(behaviorClass)) {
                behavior = new FacesClientBehaviorCDIWrapper((Class<ClientBehavior>) behaviorClass, behaviorId);
            } else {
                behavior = new FacesBehaviorCDIWrapper(behaviorClass, behaviorId);
            }
            Behavior innerBehavior = ((FacesWrapper<Behavior>) behavior).getWrapped();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, innerBehavior);
            if (innerBehavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) innerBehavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        } else {
            behavior = behaviorClass.newInstance();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, behavior);
            if (behavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) behavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        }
        return behavior;
    } catch (Exception e) {
        log.log(Level.SEVERE, "Could not instantiate behavior " + behaviorClass, e);
        throw new FacesException("Could not instantiate behavior: " + behaviorClass, e);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FacesBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper) FacesWrapper(jakarta.faces.FacesWrapper) FacesException(jakarta.faces.FacesException) MissingResourceException(java.util.MissingResourceException) NamingException(javax.naming.NamingException) ELException(jakarta.el.ELException) FacesException(jakarta.faces.FacesException) ClientBehaviorRenderer(jakarta.faces.render.ClientBehaviorRenderer) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase) FacesClientBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesClientBehaviorCDIWrapper) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior) Behavior(jakarta.faces.component.behavior.Behavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior)

Example 3 with FacesBehavior

use of jakarta.faces.component.behavior.FacesBehavior in project mojarra by eclipse-ee4j.

the class CdiUtils method createBehavior.

/**
 * Create a behavior using the FacesBehavior value attribute.
 *
 * @param beanManager the bean manager.
 * @param value the value attribute.
 * @return the behavior, or null if we could not match one.
 */
public static Behavior createBehavior(BeanManager beanManager, String value) {
    Behavior delegatingBehavior = null;
    Behavior managedBehavior = getBeanReference(beanManager, Behavior.class, FacesBehavior.Literal.of(value, true));
    if (managedBehavior != null) {
        delegatingBehavior = new CdiBehavior(value, managedBehavior);
    }
    return delegatingBehavior;
}
Also used : FacesBehavior(jakarta.faces.component.behavior.FacesBehavior) Behavior(jakarta.faces.component.behavior.Behavior)

Aggregations

FacesBehavior (jakarta.faces.component.behavior.FacesBehavior)3 Behavior (jakarta.faces.component.behavior.Behavior)2 ELException (jakarta.el.ELException)1 FacesException (jakarta.faces.FacesException)1 FacesWrapper (jakarta.faces.FacesWrapper)1 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)1 ClientBehaviorBase (jakarta.faces.component.behavior.ClientBehaviorBase)1 FacesContext (jakarta.faces.context.FacesContext)1 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)1 MissingResourceException (java.util.MissingResourceException)1 NamingException (javax.naming.NamingException)1 FacesBehaviorCDIWrapper (org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper)1 FacesClientBehaviorCDIWrapper (org.apache.myfaces.cdi.wrapper.FacesClientBehaviorCDIWrapper)1 BehaviorImpl (org.apache.myfaces.config.impl.element.BehaviorImpl)1