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.shared.util;
20  
21  import javax.faces.FacesException;
22  import javax.faces.context.ExternalContext;
23  
24  /**
25   * Utility class to handle web config parameters
26   * 
27   * @since 2.0.10 (4.0.4 in shared, 1.0.1 in commons)
28   */
29  public final class WebConfigParamUtils
30  {
31      public final static String[] COMMON_TRUE_VALUES = {"true", "on", "yes"};
32      public final static String[] COMMON_FALSE_VALUES = {"false", "off", "no"};
33  
34      /**
35       * Gets the String init parameter value from the specified context. If the parameter is an empty String or a String
36       * containing only white space, this method returns <code>null</code>
37       * 
38       * @param context
39       *            the application's external context
40       * @param name
41       *            the init parameter's name
42       *            
43       * @return the parameter if it was specified and was not empty, <code>null</code> otherwise
44       * 
45       * @throws NullPointerException
46       *             if context or name is <code>null</code>
47       */    
48      public static String getStringInitParameter(ExternalContext context, String name)
49      {
50          return getStringInitParameter(context,name,null);
51      }
52  
53      /**
54       * Gets the String init parameter value from the specified context. If the parameter is an empty String or a String
55       * containing only white space, this method returns <code>null</code>
56       * 
57       * @param context
58       *            the application's external context
59       * @param name
60       *            the init parameter's name
61       * @param defaultValue
62       *            the value by default if null or empty
63       *            
64       * @return the parameter if it was specified and was not empty, <code>null</code> otherwise
65       * 
66       * @throws NullPointerException
67       *             if context or name is <code>null</code>
68       */    
69      public static String getStringInitParameter(ExternalContext context, String name, String defaultValue)
70      {
71          if (name == null)
72          {
73              throw new NullPointerException();
74          }
75          
76          String param = context.getInitParameter(name);
77          
78          if (param == null)
79          {
80              return defaultValue;
81          }
82  
83          param = param.trim();
84          if (param.length() == 0)
85          {
86              return defaultValue;
87          }
88  
89          return param;
90      }
91      
92      /**
93       * Gets the String init parameter value from the specified context. If the parameter is an 
94       * empty String or a String
95       * containing only white space, this method returns <code>null</code>
96       * 
97       * @param context
98       *            the application's external context
99       * @param names
100      *            the init parameter's names, the first one is scanned first. Usually used when a 
101      *            param has multiple aliases
102      *            
103      * @return the parameter if it was specified and was not empty, <code>null</code> otherwise
104      * 
105      * @throws NullPointerException
106      *             if context or name is <code>null</code>
107      */    
108     public static String getStringInitParameter(ExternalContext context, String[] names)
109     {
110         return getStringInitParameter(context, names, null);
111     }
112     
113     /**
114      * Gets the String init parameter value from the specified context. If the parameter is an empty 
115      * String or a String containing only white space, this method returns <code>null</code>
116      * 
117      * @param context
118      *            the application's external context
119      * @param names
120      *            the init parameter's names, the first one is scanned first. Usually used when a param has 
121      *            multiple aliases
122      * @param defaultValue
123      *            the value by default if null or empty
124      *            
125      * @return the parameter if it was specified and was not empty, <code>null</code> otherwise
126      * 
127      * @throws NullPointerException
128      *             if context or name is <code>null</code>
129      */    
130     public static String getStringInitParameter(ExternalContext context, String[] names, String defaultValue)
131     {
132         if (names == null)
133         {
134             throw new NullPointerException();
135         }
136         
137         String param = null;
138         
139         for (String name : names)
140         {
141             if (name == null)
142             {
143                 throw new NullPointerException();
144             }
145             
146             param = context.getInitParameter(name);
147             if (param != null)
148             {
149                 break;
150             }
151         }
152         
153         if (param == null)
154         {
155             return defaultValue;
156         }
157 
158         param = param.trim();
159         if (param.length() == 0)
160         {
161             return defaultValue;
162         }
163 
164         return param;
165     }
166     
167     /**
168      * Gets the boolean init parameter value from the specified context. If the parameter was not specified, the default
169      * value is used instead.
170      * 
171      * @param context
172      *            the application's external context
173      * @param name
174      *            the init parameter's name
175      * 
176      * @return the init parameter value as a boolean
177      * 
178      * @throws NullPointerException
179      *             if context or name is <code>null</code>
180      */
181     public static boolean getBooleanInitParameter(ExternalContext context, String name)
182     {
183         return getBooleanInitParameter(context, name, false);
184     }
185     
186     /**
187      * Gets the boolean init parameter value from the specified context. If the parameter was not specified, the default
188      * value is used instead.
189      * 
190      * @param context
191      *            the application's external context
192      * @param name
193      *            the init parameter's name
194      * @param defaultValue
195      *            the default value to return in case the parameter was not set
196      * 
197      * @return the init parameter value as a boolean
198      * 
199      * @throws NullPointerException
200      *             if context or name is <code>null</code>
201      */
202     public static boolean getBooleanInitParameter(ExternalContext context, String name, boolean defaultValue)
203     {
204         if (name == null)
205         {
206             throw new NullPointerException();
207         }
208 
209         String param = getStringInitParameter(context, name);
210         if (param == null)
211         {
212             return defaultValue;
213         }
214         else
215         {
216             return Boolean.parseBoolean(param.toLowerCase());
217         }
218     }
219     
220     /**
221      * Gets the boolean init parameter value from the specified context. If the parameter 
222      * was not specified, the default
223      * value is used instead.
224      * 
225      * @param context
226      *            the application's external context
227      * @param name
228      *            the init parameter's name
229      * @param defaultValue
230      *            the default value to return in case the parameter was not set
231      * @param valuesIgnoreCase
232      *            an array of valid values to match
233      * @param returnOnValueEqualsIgnoreCase
234      *            the value to return in case the parameter match with valuesIgnoreCase
235      * 
236      * @return the init parameter value as a boolean
237      * 
238      * @throws NullPointerException
239      *             if context or name is <code>null</code>
240      */
241     public static boolean getBooleanInitParameter(ExternalContext context, String name, 
242             boolean defaultValue, String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase)
243     {
244         if (name == null)
245         {
246             throw new NullPointerException();
247         }
248 
249         String param = getStringInitParameter(context, name);
250         if (param == null)
251         {
252             return defaultValue;
253         }
254         else
255         {
256             if (valuesIgnoreCase != null)
257             {
258                 for (String trueValue : valuesIgnoreCase)
259                 {
260                     if (trueValue.equalsIgnoreCase(param))
261                     {
262                         return returnOnValueEqualsIgnoreCase;
263                     }
264                 }
265                 return defaultValue;
266             }
267             else 
268             {
269                 return Boolean.parseBoolean(param.toLowerCase());
270             }
271         }
272     }    
273 
274     /**
275      * Gets the boolean init parameter value from the specified context. If the parameter was not specified, 
276      * the default value is used instead.
277      * 
278      * @param context
279      *            the application's external context
280      * @param names
281      *            the init parameter's names
282      * 
283      * @return the init parameter value as a boolean
284      * 
285      * @throws NullPointerException
286      *             if context or name is <code>null</code>
287      */
288     
289     public static boolean getBooleanInitParameter(ExternalContext context, String[] names)
290     {
291         return getBooleanInitParameter(context, names, false);
292     }
293 
294     /**
295      * Gets the boolean init parameter value from the specified context. If the parameter was not specified,
296      * the default value is used instead.
297      * 
298      * @param context
299      *            the application's external context
300      * @param names
301      *            the init parameter's names
302      * @param defaultValue
303      *            the default value to return in case the parameter was not set
304      * 
305      * @return the init parameter value as a boolean
306      * 
307      * @throws NullPointerException
308      *             if context or name is <code>null</code>
309      */
310     public static boolean getBooleanInitParameter(ExternalContext context, String[] names, boolean defaultValue)
311     {
312         if (names == null)
313         {
314             throw new NullPointerException();
315         }
316         
317         String param = null;
318         for (String name : names)
319         {
320             if (name == null)
321             {
322                 throw new NullPointerException();
323             }
324             
325             param = getStringInitParameter(context, name);
326             if (param != null)
327             {
328                 break;
329             }
330         }
331         if (param == null)
332         {
333             return defaultValue;
334         }
335         else
336         {
337             return Boolean.parseBoolean(param.toLowerCase());
338         }
339     }
340     
341     /**
342      * Gets the boolean init parameter value from the specified context. If the parameter was not specified,
343      * the default value is used instead.
344      * 
345      * @param context
346      *            the application's external context
347      * @param names
348      *            the init parameter's names
349      * @param defaultValue
350      *            the default value to return in case the parameter was not set
351      * @param valuesIgnoreCase
352      *            an array of valid values to match
353      * @param returnOnValueEqualsIgnoreCase
354      *            the value to return in case the parameter match with valuesIgnoreCase
355      * 
356      * @return the init parameter value as a boolean
357      * 
358      * @throws NullPointerException
359      *             if context or name is <code>null</code>
360      */
361     
362     public static boolean getBooleanInitParameter(ExternalContext context, String[] names, boolean defaultValue,
363             String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase)
364     {
365         if (names == null)
366         {
367             throw new NullPointerException();
368         }
369         
370         String param = null;
371         for (String name : names)
372         {
373             if (name == null)
374             {
375                 throw new NullPointerException();
376             }
377             
378             param = getStringInitParameter(context, name);
379             if (param != null)
380             {
381                 break;
382             }
383         }
384         if (param == null)
385         {
386             return defaultValue;
387         }
388         else
389         {
390             if (valuesIgnoreCase != null)
391             {
392                 for (String trueValue : valuesIgnoreCase)
393                 {
394                     if (trueValue.equalsIgnoreCase(param))
395                     {
396                         return returnOnValueEqualsIgnoreCase;
397                     }
398                 }
399                 return defaultValue;
400             }
401             else 
402             {
403                 return Boolean.parseBoolean(param.toLowerCase());
404             }
405         }
406     }
407     
408     /**
409      * Gets the int init parameter value from the specified context. If the parameter was not 
410      * specified, the default value is used instead.
411      * 
412      * @param context
413      *            the application's external context
414      * @param name
415      *            the init parameter's name
416      * 
417      * @return the init parameter value as a int
418      * 
419      * @throws NullPointerException
420      *             if context or name is <code>null</code>
421      */
422     public static int getIntegerInitParameter(ExternalContext context, String name)
423     {
424         return getIntegerInitParameter(context, name, 0);
425     }
426     
427     /**
428      * Gets the int init parameter value from the specified context. If the parameter was not specified,
429      * the default value is used instead.
430      * 
431      * @param context
432      *            the application's external context
433      * @param name
434      *            the init parameter's name
435      * @param defaultValue
436      *            the default value to return in case the parameter was not set
437      * 
438      * @return the init parameter value as a int
439      * 
440      * @throws NullPointerException
441      *             if context or name is <code>null</code>
442      */
443     public static int getIntegerInitParameter(ExternalContext context, String name, int defaultValue)
444     {
445         if (name == null)
446         {
447             throw new NullPointerException();
448         }
449 
450         String param = getStringInitParameter(context, name);
451         if (param == null)
452         {
453             return defaultValue;
454         }
455         else
456         {
457             return Integer.parseInt(param.toLowerCase());
458         }
459     }
460 
461     /**
462      * Gets the int init parameter value from the specified context. If the parameter was not specified,
463      * the default value is used instead.
464      * 
465      * @param context
466      *            the application's external context
467      * @param names
468      *            the init parameter's names
469      * 
470      * @return the init parameter value as a int
471      * 
472      * @throws NullPointerException
473      *             if context or name is <code>null</code>
474      */
475     public static int getIntegerInitParameter(ExternalContext context, String[] names)
476     {
477         return getIntegerInitParameter(context, names, 0);
478     }
479 
480     /**
481      * Gets the int init parameter value from the specified context. If the parameter was not specified, the default
482      * value is used instead.
483      * 
484      * @param context
485      *            the application's external context
486      * @param names
487      *            the init parameter's names
488      * @param defaultValue
489      *            the default value to return in case the parameter was not set
490      * 
491      * @return the init parameter value as a int
492      * 
493      * @throws NullPointerException
494      *             if context or name is <code>null</code>
495      */
496     
497     public static int getIntegerInitParameter(ExternalContext context, String[] names, int defaultValue)
498     {
499         if (names == null)
500         {
501             throw new NullPointerException();
502         }
503         
504         String param = null;
505         for (String name : names)
506         {
507             if (name == null)
508             {
509                 throw new NullPointerException();
510             }
511             
512             param = getStringInitParameter(context, name);
513             if (param != null)
514             {
515                 break;
516             }
517         }
518         if (param == null)
519         {
520             return defaultValue;
521         }
522         else
523         {
524             return Integer.parseInt(param.toLowerCase());
525         }
526     }
527     
528     /**
529      * Gets the long init parameter value from the specified context. If the parameter was not specified, the default
530      * value is used instead.
531      * 
532      * @param context
533      *            the application's external context
534      * @param name
535      *            the init parameter's name
536      * 
537      * @return the init parameter value as a long
538      * 
539      * @throws NullPointerException
540      *             if context or name is <code>null</code>
541      */
542     public static long getLongInitParameter(ExternalContext context, String name)
543     {
544         return getLongInitParameter(context, name, 0);
545     }
546     
547     /**
548      * Gets the long init parameter value from the specified context. If the parameter was not specified, the default
549      * value is used instead.
550      * 
551      * @param context
552      *            the application's external context
553      * @param name
554      *            the init parameter's name
555      * @param defaultValue
556      *            the default value to return in case the parameter was not set
557      * 
558      * @return the init parameter value as a long
559      * 
560      * @throws NullPointerException
561      *             if context or name is <code>null</code>
562      */
563     public static long getLongInitParameter(ExternalContext context, String name, long defaultValue)
564     {
565         if (name == null)
566         {
567             throw new NullPointerException();
568         }
569 
570         String param = getStringInitParameter(context, name);
571         if (param == null)
572         {
573             return defaultValue;
574         }
575         else
576         {
577             return Long.parseLong(param.toLowerCase());
578         }
579     }
580 
581     /**
582      * Gets the long init parameter value from the specified context. If the parameter was not specified, the default
583      * value is used instead.
584      * 
585      * @param context
586      *            the application's external context
587      * @param names
588      *            the init parameter's names
589      * 
590      * @return the init parameter value as a long
591      * 
592      * @throws NullPointerException
593      *             if context or name is <code>null</code>
594      */
595     
596     public static long getLongInitParameter(ExternalContext context, String[] names)
597     {
598         return getLongInitParameter(context, names, 0);
599     }
600     
601     /**
602      * Gets the long init parameter value from the specified context. If the parameter was not specified, the default
603      * value is used instead.
604      * 
605      * @param context
606      *            the application's external context
607      * @param names
608      *            the init parameter's names
609      * @param defaultValue
610      *            the default value to return in case the parameter was not set
611      * 
612      * @return the init parameter value as a long
613      * 
614      * @throws NullPointerException
615      *             if context or name is <code>null</code>
616      */
617     
618     public static long getLongInitParameter(ExternalContext context, String[] names, long defaultValue)
619     {
620         if (names == null)
621         {
622             throw new NullPointerException();
623         }
624         
625         String param = null;
626         for (String name : names)
627         {
628             if (name == null)
629             {
630                 throw new NullPointerException();
631             }
632             
633             param = getStringInitParameter(context, name);
634             if (param != null)
635             {
636                 break;
637             }
638         }
639         if (param == null)
640         {
641             return defaultValue;
642         }
643         else
644         {
645             return Long.parseLong(param.toLowerCase());
646         }
647     }
648 
649     /**
650      * Gets the init parameter value from the specified context and instanciate it. 
651      * If the parameter was not specified, the default value is used instead.
652      * 
653      * @param context
654      *            the application's external context
655      * @param name
656      *            the init parameter's name
657      * @param deprecatedName
658      *            the init parameter's deprecated name.
659      * @param defaultValue
660      *            the default value to return in case the parameter was not set
661      * 
662      * @return the init parameter value as an object instance
663      * 
664      * @throws NullPointerException
665      *             if context or name is <code>null</code>
666      */
667     @SuppressWarnings("unchecked")
668     public static <T> T getInstanceInitParameter(ExternalContext context, String name, 
669             String deprecatedName, T defaultValue)
670     {
671         String param = getStringInitParameter(context, name, deprecatedName);
672         if (param == null)
673         {
674             return defaultValue;
675         }
676         else
677         {
678             try
679             {
680                 return (T) ClassUtils.classForName(param).newInstance();
681             }
682             catch (Exception e)
683             {
684                 throw new FacesException("Error Initializing Object[" + param + "]", e);
685             }
686         }
687     }
688 }