Search in sources :

Example 1 with UnexpectedTypeException

use of javax.validation.UnexpectedTypeException in project Payara by payara.

the class ReferenceValidator method isValid.

@Override
public boolean isValid(ConfigBeanProxy config, ConstraintValidatorContext cvc) throws UnexpectedTypeException {
    if (config == null) {
        return true;
    }
    Dom dom = Dom.unwrap(config);
    if (rc.skipDuringCreation() && dom.getKey() == null) {
        // During creation the coresponding DOM is not fully loaded.
        return true;
    }
    Collection<RemoteKeyInfo> remoteKeys = findRemoteKeys(config);
    if (remoteKeys != null && !remoteKeys.isEmpty()) {
        ServiceLocator habitat = dom.getHabitat();
        boolean result = true;
        boolean disableGlobalMessage = true;
        for (RemoteKeyInfo remoteKeyInfo : remoteKeys) {
            if (remoteKeyInfo.method.getParameterTypes().length > 0) {
                throw new UnexpectedTypeException(localStrings.getLocalString("referenceValidator.not.getter", "The RemoteKey annotation must be on a getter method."));
            }
            try {
                Object value = remoteKeyInfo.method.invoke(config);
                if (value instanceof String) {
                    String key = (String) value;
                    ConfigBeanProxy component = habitat.getService(remoteKeyInfo.annotation.type(), key);
                    if (component == null) {
                        result = false;
                        if (remoteKeyInfo.annotation.message().isEmpty()) {
                            disableGlobalMessage = false;
                        } else {
                            cvc.buildConstraintViolationWithTemplate(remoteKeyInfo.annotation.message()).addNode(Dom.convertName(remoteKeyInfo.method.getName())).addConstraintViolation();
                        }
                    }
                } else {
                    throw new UnexpectedTypeException(localStrings.getLocalString("referenceValidator.not.string", "The RemoteKey annotation must identify a method that returns a String."));
                }
            } catch (Exception ex) {
                return false;
            }
        }
        if (!result && disableGlobalMessage) {
            cvc.disableDefaultConstraintViolation();
        }
        return result;
    }
    return true;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Dom(org.jvnet.hk2.config.Dom) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) UnexpectedTypeException(javax.validation.UnexpectedTypeException) UnexpectedTypeException(javax.validation.UnexpectedTypeException)

Aggregations

UnexpectedTypeException (javax.validation.UnexpectedTypeException)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)1 Dom (org.jvnet.hk2.config.Dom)1