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.extensions.validator.core;
20  
21  import org.apache.myfaces.extensions.validator.core.storage.ViolationSeverityInterpreterStorage;
22  import org.apache.myfaces.extensions.validator.core.validation.parameter.ViolationSeverityInterpreter;
23  import org.apache.myfaces.extensions.validator.internal.UsageCategory;
24  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
25  import org.apache.myfaces.extensions.validator.util.ClassUtils;
26  import org.apache.myfaces.extensions.validator.util.ExtValUtils;
27  
28  import javax.faces.context.FacesContext;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * @since x.x.3
35   */
36  @UsageInformation(UsageCategory.INTERNAL)
37  class ExtValContextInternals
38  {
39      boolean isComponentInitializationActivated()
40      {
41          return !ExtValCoreConfiguration.get().deactivateComponentInitialization();
42      }
43  
44      @SuppressWarnings({"unchecked"})
45      synchronized InformationProviderBean initInformationProviderBean(Map applicationMap)
46      {
47          List<String> informationProviderBeanClassNames = new ArrayList<String>();
48  
49          informationProviderBeanClassNames.add(ExtValCoreConfiguration.get().customInformationProviderBeanClassName());
50          informationProviderBeanClassNames.add(InformationProviderBean.CUSTOM_BEAN);
51  
52          InformationProviderBean informationProviderBean;
53          for (String className : informationProviderBeanClassNames)
54          {
55              informationProviderBean = (InformationProviderBean) ClassUtils.tryToInstantiateClassForName(className);
56  
57              if (informationProviderBean != null)
58              {
59                  applicationMap.put(InformationProviderBean.BEAN_NAME, informationProviderBean);
60                  return informationProviderBean;
61              }
62          }
63  
64          tryToInitCustomConfiguredInformationProviderBeanClassName(applicationMap);
65  
66          if (applicationMap.containsKey(InformationProviderBean.BEAN_NAME))
67          {
68              return (InformationProviderBean) applicationMap.get(InformationProviderBean.BEAN_NAME);
69          }
70          return new InformationProviderBean();
71      }
72  
73      @SuppressWarnings({"unchecked"})
74      synchronized void tryToInitCustomConfiguredInformationProviderBeanClassName(Map applicationMap)
75      {
76          InformationProviderBean bean = (InformationProviderBean) ExtValUtils.getELHelper()
77                  .getBean(InformationProviderBean.CUSTOM_BEAN.replace(".", "_"));
78  
79          if (bean != null)
80          {
81              applicationMap.put(InformationProviderBean.BEAN_NAME, bean);
82          }
83      }
84  
85      InformationProviderBean getInformationProviderBean()
86      {
87          Map applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
88          InformationProviderBean bean = (InformationProviderBean) applicationMap.get(InformationProviderBean.BEAN_NAME);
89  
90          if (bean == null)
91          {
92              return initInformationProviderBean(applicationMap);
93          }
94          return bean;
95      }
96  
97      ViolationSeverityInterpreter getRequestScopedViolationSeverityInterpreter()
98      {
99          return ExtValUtils.getStorage(
100                 ViolationSeverityInterpreterStorage.class, ViolationSeverityInterpreterStorage.class.getName())
101                 .getViolationSeverityInterpreter();
102     }
103 }