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.util;
20  
21  import org.apache.myfaces.extensions.validator.core.storage.FacesInformationStorage;
22  import org.apache.myfaces.extensions.validator.internal.UsageCategory;
23  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
24  
25  import javax.faces.FactoryFinder;
26  import javax.faces.application.FacesMessage;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.PhaseId;
29  import javax.faces.event.PhaseListener;
30  import javax.faces.lifecycle.Lifecycle;
31  import javax.faces.lifecycle.LifecycleFactory;
32  import java.util.Iterator;
33  import java.util.MissingResourceException;
34  import java.util.ResourceBundle;
35  
36  
37  /**
38   * @since 1.x.1
39   */
40  @UsageInformation(UsageCategory.INTERNAL)
41  public class JsfUtils
42  {
43      public static void deregisterPhaseListener(PhaseListener phaseListener)
44      {
45          LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
46  
47          String currentId;
48          Lifecycle currentLifecycle;
49          Iterator lifecycleIds = lifecycleFactory.getLifecycleIds();
50          while (lifecycleIds.hasNext())
51          {
52              currentId = (String) lifecycleIds.next();
53              currentLifecycle = lifecycleFactory.getLifecycle(currentId);
54              currentLifecycle.removePhaseListener(phaseListener);
55          }
56      }
57  
58      public static void registerPhaseListener(PhaseListener phaseListener)
59      {
60          LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
61  
62          String currentId;
63          Lifecycle currentLifecycle;
64          Iterator lifecycleIds = lifecycleFactory.getLifecycleIds();
65          while (lifecycleIds.hasNext())
66          {
67              currentId = (String) lifecycleIds.next();
68              currentLifecycle = lifecycleFactory.getLifecycle(currentId);
69              currentLifecycle.addPhaseListener(phaseListener);
70          }
71      }
72  
73      public static ResourceBundle getDefaultFacesMessageBundle()
74      {
75          FacesContext facesContext = FacesContext.getCurrentInstance();
76  
77          return ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, facesContext.getViewRoot().getLocale());
78      }
79  
80      public static ResourceBundle getCustomFacesMessageBundle()
81      {
82          FacesContext facesContext = FacesContext.getCurrentInstance();
83          String bundleName = facesContext.getApplication().getMessageBundle();
84  
85          if (bundleName == null)
86          {
87              return null;
88          }
89  
90          return ResourceBundle.getBundle(bundleName, facesContext.getViewRoot().getLocale());
91      }
92  
93      public static String getMessageFromApplicationMessageBundle(String messageKey)
94      {
95          ResourceBundle customResourceBundle = getCustomFacesMessageBundle();
96  
97          try
98          {
99              if (customResourceBundle != null)
100             {
101                 return customResourceBundle.getString(messageKey);
102             }
103         }
104         catch (MissingResourceException e)
105         {
106             //no custom message is available - do nothing
107         }
108 
109         return getDefaultFacesMessageBundle().getString(messageKey);
110     }
111 
112     public static boolean isRenderResponsePhase()
113     {
114         return PhaseId.RENDER_RESPONSE.equals(getFacesInformationStorage().getCurrentPhaseId());
115     }
116 
117     public static PhaseId getCurrentPhaseId()
118     {
119         return getFacesInformationStorage().getCurrentPhaseId();
120     }
121 
122     /**
123      * simple test for early config in case of mojarra
124      * @return true if the jsf impl. is initialized and it's possible to use it as expected
125      */
126     public static boolean isApplicationInitialized()
127     {
128         FacesContext facesContext = FacesContext.getCurrentInstance();
129         return facesContext != null &&
130                 (facesContext.getClass().getName().startsWith("org.apache.myfaces") ||
131                         facesContext.getExternalContext().getRequestMap() != null &&
132                                 !facesContext.getExternalContext().getRequestMap().isEmpty());
133     }
134 
135     private static FacesInformationStorage getFacesInformationStorage()
136     {
137         return ExtValUtils.getStorage(FacesInformationStorage.class, FacesInformationStorage.class.getName());
138     }
139 }