    001    // Copyright 2009, 2010, 2011 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.beanvalidator;
    015
    016    import org.apache.tapestry5.internal.beanvalidator.*;
    017    import org.apache.tapestry5.ioc.Configuration;
    018    import org.apache.tapestry5.ioc.MappedConfiguration;
    019    import org.apache.tapestry5.ioc.OrderedConfiguration;
    020    import org.apache.tapestry5.ioc.ServiceBinder;
    021    import org.apache.tapestry5.ioc.annotations.Contribute;
    022    import org.apache.tapestry5.ioc.annotations.Local;
    023    import org.apache.tapestry5.ioc.annotations.Primary;
    024    import org.apache.tapestry5.ioc.services.PropertyShadowBuilder;
    025    import org.apache.tapestry5.ioc.services.ThreadLocale;
    026    import org.apache.tapestry5.services.FieldValidatorDefaultSource;
    027    import org.apache.tapestry5.services.javascript.JavaScriptStack;
    028    import org.apache.tapestry5.services.javascript.JavaScriptStackSource;
    029    import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
    030
    031    import javax.validation.MessageInterpolator;
    032    import javax.validation.Validator;
    033    import javax.validation.ValidatorFactory;
    034    import javax.validation.constraints.*;
    035    import javax.validation.groups.Default;
    036
    037    /**
    038     * Module for JSR-303 services.
    039     *
    040     * @since 5.2.0.0
    041     */
    042    public class BeanValidatorModule
    043    {
    044        public static void bind(final ServiceBinder binder)
    045        {
    046            binder.bind(FieldValidatorDefaultSource.class, BeanFieldValidatorDefaultSource.class).withSimpleId();
    047            binder.bind(BeanValidatorGroupSource.class, BeanValidationGroupSourceImpl.class);
    048            binder.bind(BeanValidatorSource.class, BeanValidatorSourceImpl.class);
    049            binder.bind(ClientConstraintDescriptorSource.class, ClientConstraintDescriptorImpl.class);
    050        }
    051
    052        public static void contributeServiceOverride(
    053                MappedConfiguration<Class, Object> configuration,
    054                @Local FieldValidatorDefaultSource source)
    055        {
    056            configuration.add(FieldValidatorDefaultSource.class, source);
    057        }
    058
    059        public static Validator buildBeanValidator(ValidatorFactory validatorFactory, PropertyShadowBuilder propertyShadowBuilder)
    060        {
    061            return propertyShadowBuilder.build(validatorFactory, "validator", Validator.class);
    062        }
    063
    064
    065        public static ValidatorFactory buildValidatorFactory(BeanValidatorSource beanValidatorSource, PropertyShadowBuilder propertyShadowBuilder)
    066        {
    067            return propertyShadowBuilder.build(beanValidatorSource, "validatorFactory", ValidatorFactory.class);
    068        }
    069
    070        public static void contributeBeanValidatorGroupSource(
    071                final Configuration<Class> configuration)
    072        {
    073            configuration.add(Default.class);
    074        }
    075
    076        public static void contributeBeanValidatorSource(
    077                final OrderedConfiguration<BeanValidatorConfigurer> configuration, final ThreadLocale threadLocale)
    078        {
    079            configuration.add("LocaleAwareMessageInterpolator", new BeanValidatorConfigurer()
    080            {
    081                public void configure(javax.validation.Configuration<?> configuration)
    082                {
    083                    MessageInterpolator defaultInterpolator = configuration.getDefaultMessageInterpolator();
    084
    085                    configuration.messageInterpolator(new MessageInterpolatorImpl(defaultInterpolator, threadLocale));
    086                }
    087            });
    088        }
    089
    090        public static void contributeClientConstraintDescriptorSource(
    091                final Configuration<ClientConstraintDescriptor> configuration)
    092        {
    093            configuration.add(new ClientConstraintDescriptor(Max.class, "maxnumber", "value"));
    094            configuration.add(new ClientConstraintDescriptor(Min.class, "minnumber", "value"));
    095            configuration.add(new ClientConstraintDescriptor(NotNull.class, "notnull"));
    096            configuration.add(new ClientConstraintDescriptor(Null.class, "isnull"));
    097            configuration.add(new ClientConstraintDescriptor(Pattern.class, "pattern", "regexp"));
    098            configuration.add(new ClientConstraintDescriptor(Size.class, "size", "min", "max"));
    099        }
    100
    101        @Contribute(JavaScriptStackSource.class)
    102        public static void addBeanValidationStack(MappedConfiguration<String, JavaScriptStack> configuration)
    103        {
    104            configuration.addInstance(BeanValidatorStack.STACK_ID, BeanValidatorStack.class);
    105        }
    106
    107        @Contribute(ComponentClassTransformWorker2.class)
    108        @Primary
    109        public static void addWorker(OrderedConfiguration<ComponentClassTransformWorker2> configuration)
    110        {
    111            configuration.addInstance("FeebboFormResourcesInclusionWorker", FormResourcesInclusionWorker.class);
    112        }
    113
    114    }