001package org.atteo.moonshine.hibernate;
002
003import javax.validation.ConstraintValidator;
004import javax.validation.ConstraintValidatorFactory;
005
006import com.google.inject.Inject;
007import com.google.inject.Injector;
008
009public class GuiceConstraintValidatorFactory implements ConstraintValidatorFactory {
010
011    private final Injector injector;
012    private ConstraintValidatorFactory defaultFactory;
013
014    @Inject
015    public GuiceConstraintValidatorFactory(final Injector injector) {
016        this.injector = injector;
017    }
018
019    public void setDefaultFactory(ConstraintValidatorFactory defaultFactory) {
020        this.defaultFactory = defaultFactory;
021    }
022
023    @Override
024    public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
025        T validator = defaultFactory.getInstance(key);
026        injector.injectMembers(validator);
027        return validator;
028    }
029
030    @Override
031    public void releaseInstance(ConstraintValidator<?, ?> cv) {
032    }
033}