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. 014 package org.apache.tapestry5.internal.beanvalidator; 015 016 import java.util.List; 017 018 import javax.validation.Configuration; 019 import javax.validation.Validation; 020 import javax.validation.Validator; 021 import javax.validation.ValidatorFactory; 022 023 import org.apache.tapestry5.beanvalidator.BeanValidatorConfigurer; 024 import org.apache.tapestry5.beanvalidator.BeanValidatorSource; 025 026 public 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 public Validator create() 043 { 044 return this.validatorFactory.getValidator(); 045 } 046 047 public ValidatorFactory getValidatorFactory() 048 { 049 return this.validatorFactory; 050 } 051 052 }