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 javax.faces.convert;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.FacesException;
23  import javax.faces.application.FacesMessage;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  
27  import java.security.AccessController;
28  import java.security.PrivilegedActionException;
29  import java.security.PrivilegedExceptionAction;
30  import java.text.MessageFormat;
31  import java.util.Locale;
32  import java.util.MissingResourceException;
33  import java.util.ResourceBundle;
34  
35  /**
36   * @author Manfred Geiler (latest modification by $Author: bommel $)
37   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
38   */
39  class _MessageUtils
40  {
41      private static final String DETAIL_SUFFIX = "_detail";
42  
43      static FacesMessage getErrorMessage(FacesContext facesContext,
44                                          String messageId,
45                                          Object args[])
46      {
47          return getMessage(facesContext,
48                            facesContext.getViewRoot().getLocale(),
49                            FacesMessage.SEVERITY_ERROR,
50                            messageId,
51                            args);
52      }
53  
54      static FacesMessage getMessage(FacesContext facesContext,
55                                     Locale locale,
56                                     FacesMessage.Severity severity,
57                                     String messageId,
58                                     Object args[])
59      {
60          ResourceBundle appBundle;
61          ResourceBundle defBundle;
62          String summary;
63          String detail;
64  
65          appBundle = getApplicationBundle(facesContext, locale);
66          summary = getBundleString(appBundle, messageId);
67          if (summary != null)
68          {
69              detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
70          }
71          else
72          {
73              defBundle = getDefaultBundle(facesContext, locale);
74              summary = getBundleString(defBundle, messageId);
75              if (summary != null)
76              {
77                  detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
78              }
79              else
80              {
81                  //Try to find detail alone
82                  detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
83                  if (detail != null)
84                  {
85                      summary = null;
86                  }
87                  else
88                  {
89                      detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
90                      if (detail != null)
91                      {
92                          summary = null;
93                      }
94                      else
95                      {
96                          //Neither detail nor summary found
97                          facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
98                          return new FacesMessage(severity, messageId, null);
99                      }
100                 }
101             }
102         }
103 
104         if (args != null && args.length > 0)
105         {
106             return new _ParametrizableFacesMessage(severity, summary, detail, args, locale);
107         }
108         else
109         {
110             return new FacesMessage(severity, summary, detail);
111         }
112     }
113 
114     private static String getBundleString(ResourceBundle bundle, String key)
115     {
116         try
117         {
118             return bundle == null ? null : bundle.getString(key);
119         }
120         catch (MissingResourceException e)
121         {
122             return null;
123         }
124     }
125 
126 
127     private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
128     {
129         String bundleName = facesContext.getApplication().getMessageBundle();
130         return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
131     }
132 
133     private static ResourceBundle getDefaultBundle(FacesContext facesContext,
134                                                    Locale locale)
135     {
136         return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
137     }
138 
139     private static ResourceBundle getBundle(FacesContext facesContext,
140                                             Locale locale,
141                                             String bundleName)
142     {
143         try
144         {
145             //First we try the JSF implementation class loader
146             return ResourceBundle.getBundle(bundleName,
147                                             locale,
148                                             facesContext.getClass().getClassLoader());
149         }
150         catch (MissingResourceException ignore1)
151         {
152             try
153             {
154                 //Next we try the JSF API class loader
155                 return ResourceBundle.getBundle(bundleName,
156                                                 locale,
157                                                 _MessageUtils.class.getClassLoader());
158             }
159             catch (MissingResourceException ignore2)
160             {
161                 try
162                 {
163                     //Last resort is the context class loader
164                     if (System.getSecurityManager() != null) {
165                         Object cl = AccessController.doPrivileged(new PrivilegedExceptionAction() {
166                             public Object run() throws PrivilegedActionException {
167                                 return Thread.currentThread().getContextClassLoader();
168                             }
169                         });
170                         return ResourceBundle.getBundle(bundleName,locale,(ClassLoader)cl);
171 
172                     }else{
173                         return ResourceBundle.getBundle(bundleName,locale, Thread.currentThread().getContextClassLoader()); 
174                     }                   
175                 }catch(PrivilegedActionException pae){
176                     throw new FacesException(pae);
177                 }catch (MissingResourceException damned){
178                     facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
179                     return null;
180                 }
181             }
182         }
183     }
184     
185     static Object getLabel(FacesContext facesContext, UIComponent component) {
186         Object label = component.getAttributes().get("label");
187         if(label != null)
188             return label;
189         
190         ValueExpression expression = component.getValueExpression("label");
191         if(expression != null)
192             return expression;
193         
194         //If no label is not specified, use clientId
195         return component.getClientId( facesContext );
196     }
197 }