| /** |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * 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; |
| } |
| |
| } |