001// Copyright 2009 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014package org.apache.tapestry5.internal.beanvalidator; 015 016import java.util.List; 017 018import javax.validation.Configuration; 019import javax.validation.Validation; 020import javax.validation.Validator; 021import javax.validation.ValidatorFactory; 022 023import org.apache.tapestry5.beanvalidator.BeanValidatorConfigurer; 024import org.apache.tapestry5.beanvalidator.BeanValidatorSource; 025 026public class BeanValidatorSourceImpl implements BeanValidatorSource 027{ 028 private final ValidatorFactory validatorFactory; 029 030 public BeanValidatorSourceImpl(final List<BeanValidatorConfigurer> contribution) 031 { 032 final Configuration<?> configuration = Validation.byDefaultProvider().configure(); 033 034 for (final BeanValidatorConfigurer configurer : contribution) 035 { 036 configurer.configure(configuration); 037 } 038 039 this.validatorFactory = configuration.buildValidatorFactory(); 040 } 041 042 @Override 043 public Validator create() 044 { 045 return this.validatorFactory.getValidator(); 046 } 047 048 @Override 049 public ValidatorFactory getValidatorFactory() 050 { 051 return this.validatorFactory; 052 } 053 054}