View Javadoc

1   package org.apache.onami.validation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static com.google.inject.matcher.Matchers.annotatedWith;
23  import static com.google.inject.matcher.Matchers.any;
24  
25  import javax.inject.Singleton;
26  import javax.validation.ConstraintValidatorFactory;
27  import javax.validation.MessageInterpolator;
28  import javax.validation.TraversableResolver;
29  import javax.validation.Validator;
30  import javax.validation.ValidatorFactory;
31  import javax.validation.spi.ConfigurationState;
32  import javax.validation.spi.ValidationProvider;
33  
34  import org.aopalliance.intercept.MethodInterceptor;
35  import org.apache.bval.jsr303.ApacheValidationProvider;
36  import org.apache.bval.jsr303.DefaultMessageInterpolator;
37  import org.apache.bval.jsr303.resolver.DefaultTraversableResolver;
38  import org.kohsuke.MetaInfServices;
39  
40  import com.google.inject.AbstractModule;
41  import com.google.inject.Module;
42  import com.google.inject.TypeLiteral;
43  
44  /**
45   * The Google-Guice Validation module.
46   */
47  @MetaInfServices( Module.class )
48  public final class ValidationModule
49      extends AbstractModule
50  {
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      protected void configure()
57      {
58          // apache bval bootstrap
59          bind( MessageInterpolator.class ).to( DefaultMessageInterpolator.class ).in( Singleton.class );
60          bind( TraversableResolver.class ).to( DefaultTraversableResolver.class ).in( Singleton.class );
61          bind( ConstraintValidatorFactory.class ).to( GuiceAwareConstraintValidatorFactory.class );
62          bind( new TypeLiteral<ValidationProvider<?>>(){} ).to( ApacheValidationProvider.class ).in( Singleton.class );
63          bind( ConfigurationState.class ).toProvider( ConfigurationStateProvider.class ).in( Singleton.class );
64          bind( ValidatorFactory.class ).toProvider( ValidatorFactoryProvider.class ).in( Singleton.class );
65          bind( Validator.class ).toProvider( ValidatorProvider.class );
66  
67          // AOP stuff
68          MethodInterceptor validateMethodInterceptor = new ValidateMethodInterceptor();
69          requestInjection( validateMethodInterceptor );
70          bindInterceptor( any(), annotatedWith( Validate.class ), validateMethodInterceptor );
71      }
72  
73  }