View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.validatebeanbehavior;
20  
21  import javax.faces.context.FacesContext;
22  import javax.validation.MessageInterpolator;
23  import javax.validation.ValidatorFactory;
24  import java.util.Locale;
25  
26  /**
27   * Holder class to prevent NoClassDefFoundError in environments without Bean Validation.
28   * <p/>
29   * This is needed, because holder classes are loaded lazily. This means that when it's not
30   * used, it will not be loaded, parsed and initialized. The BeanValidator class is used always,
31   * so the MessageInterpolator references need to be in this separate class.
32   */
33  final class FacesMessageInterpolatorHolder {
34  
35      // Needs to be volatile.
36      private static volatile FacesMessageInterpolator instance;
37  
38      /**
39       * Helper method for initializing the FacesMessageInterpolator.
40       * <p/>
41       * It uses the "Single Check Idiom" as described in Joshua Bloch's Effective Java 2nd Edition.
42       *
43       * @param validatorFactory Used to obtain the MessageInterpolator.
44       * @return The instantiated MessageInterpolator for BeanValidator.
45       */
46      static MessageInterpolator get(final ValidatorFactory validatorFactory) {
47          FacesMessageInterpolatorHolder.FacesMessageInterpolator ret = instance;
48          if (ret == null) {
49              final MessageInterpolator interpolator = validatorFactory.getMessageInterpolator();
50              instance = ret = new FacesMessageInterpolator(interpolator);
51          }
52          return ret;
53      }
54  
55      /**
56       * Standard MessageInterpolator, as described in the JSR-314 spec.
57       */
58      private static class FacesMessageInterpolator implements MessageInterpolator {
59          private final MessageInterpolator interpolator;
60  
61          FacesMessageInterpolator(final MessageInterpolator interpolator) {
62              this.interpolator = interpolator;
63          }
64  
65          public String interpolate(final String s, final Context context) {
66              final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
67              return interpolator.interpolate(s, context, locale);
68          }
69  
70          public String interpolate(final String s, final Context context, final Locale locale) {
71              return interpolator.interpolate(s, context, locale);
72          }
73      }
74  }