001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.bean.validation;
018
019import java.util.function.Supplier;
020
021import javax.validation.Configuration;
022import javax.validation.MessageInterpolator;
023import javax.validation.Validation;
024import javax.validation.Validator;
025import javax.validation.ValidatorFactory;
026
027
028/**
029 * This is the default validator provider. It creates a validator instance with the default message
030 * interpolator wrapped inside a {@link SessionLocaleInterpolator} so it is aware of Wicket's
031 * locale. Only one instance of the {@link Validator} is created.
032 * 
033 * @author igor
034 * 
035 */
036public class DefaultValidatorProvider implements Supplier<Validator>
037{
038
039        private Validator validator;
040
041        @Override
042        public Validator get()
043        {
044                if (validator == null)
045                {
046                        Configuration<?> config = Validation.byDefaultProvider().configure();
047
048                        MessageInterpolator interpolator = config.getDefaultMessageInterpolator();
049                        interpolator = new SessionLocaleInterpolator(interpolator);
050
051                        ValidatorFactory factory = config.messageInterpolator(interpolator)
052                                .buildValidatorFactory();
053
054                        validator = factory.getValidator();
055                }
056                return validator;
057        }
058}