blob: 906e33c92a9fab8d2a5e4453739c5071e8861506 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.runtime.jsr303.validation.common;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.eclipse.osbp.runtime.common.validation.IStatus;
import org.eclipse.osbp.runtime.common.validation.IValidationParticipant;
import org.eclipse.osbp.runtime.common.validation.ValidationKind;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
@Component(immediate = true)
public class JSR303ValidationParticipant implements IValidationParticipant {
private ValidatorFactory valFactory;
@SuppressWarnings("rawtypes")
@Override
public Set<IStatus> validate(Object object, ValidationKind kind,
Map<String, Object> properties) {
if (valFactory == null) {
throw new IllegalArgumentException(
"No ValidatorFactory bound to service");
}
Validator validator = valFactory.getValidator();
Set<ConstraintViolation<Object>> tempResult = validator.validate(
object, new Class[0]);
Set<IStatus> result = ValidationUtil.getStatus(getClass(),
new HashSet<ConstraintViolation>(tempResult));
return result;
}
/**
* Binds the validator factory to validate contraints placed on the dto
* class.
*
* @param valFactory
* the service
*/
@Reference(cardinality = ReferenceCardinality.MANDATORY, unbind = "unbindValidatorFactory", policy = ReferencePolicy.DYNAMIC)
protected void bindValidatorFactory(final ValidatorFactory valFactory) {
this.valFactory = valFactory;
}
/**
* Unbinds the service from this component. <br>
*
* @param valFactory
* the service
*/
protected void unbindValidatorFactory(final ValidatorFactory valFactory) {
this.valFactory = null;
}
}