use of org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper 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);
}
}
Aggregations