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