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 * @param deprecatedName 176 * the init parameter's deprecated name. 177 * @param defaultValue 178 * the default value to return in case the parameter was not set 179 * 180 * @return the init parameter value as a boolean 181 * 182 * @throws NullPointerException 183 * if context or name is <code>null</code> 184 */ 185 public static boolean getBooleanInitParameter(ExternalContext context, String name) 186 { 187 return getBooleanInitParameter(context, name, false); 188 } 189 190 /** 191 * Gets the boolean init parameter value from the specified context. If the parameter was not specified, the default 192 * value is used instead. 193 * 194 * @param context 195 * the application's external context 196 * @param name 197 * the init parameter's name 198 * @param deprecatedName 199 * the init parameter's deprecated name. 200 * @param defaultValue 201 * the default value to return in case the parameter was not set 202 * 203 * @return the init parameter value as a boolean 204 * 205 * @throws NullPointerException 206 * if context or name is <code>null</code> 207 */ 208 public static boolean getBooleanInitParameter(ExternalContext context, String name, boolean defaultValue) 209 { 210 if (name == null) 211 { 212 throw new NullPointerException(); 213 } 214 215 String param = getStringInitParameter(context, name); 216 if (param == null) 217 { 218 return defaultValue; 219 } 220 else 221 { 222 return Boolean.parseBoolean(param.toLowerCase()); 223 } 224 } 225 226 /** 227 * Gets the boolean init parameter value from the specified context. If the parameter 228 * was not specified, the default 229 * value is used instead. 230 * 231 * @param context 232 * the application's external context 233 * @param name 234 * the init parameter's name 235 * @param deprecatedName 236 * the init parameter's deprecated name. 237 * @param defaultValue 238 * the default value to return in case the parameter was not set 239 * @param valuesIgnoreCase 240 * an array of valid values to match 241 * @param returnOnValueEqualsIgnoreCase 242 * the value to return in case the parameter match with valuesIgnoreCase 243 * 244 * @return the init parameter value as a boolean 245 * 246 * @throws NullPointerException 247 * if context or name is <code>null</code> 248 */ 249 public static boolean getBooleanInitParameter(ExternalContext context, String name, 250 boolean defaultValue, String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase) 251 { 252 if (name == null) 253 { 254 throw new NullPointerException(); 255 } 256 257 String param = getStringInitParameter(context, name); 258 if (param == null) 259 { 260 return defaultValue; 261 } 262 else 263 { 264 if (valuesIgnoreCase != null) 265 { 266 for (String trueValue : valuesIgnoreCase) 267 { 268 if (trueValue.equalsIgnoreCase(param)) 269 { 270 return returnOnValueEqualsIgnoreCase; 271 } 272 } 273 return defaultValue; 274 } 275 else 276 { 277 return Boolean.parseBoolean(param.toLowerCase()); 278 } 279 } 280 } 281 282 /** 283 * Gets the boolean init parameter value from the specified context. If the parameter was not specified, 284 * the default value is used instead. 285 * 286 * @param context 287 * the application's external context 288 * @param names 289 * the init parameter's names 290 * 291 * @return the init parameter value as a boolean 292 * 293 * @throws NullPointerException 294 * if context or name is <code>null</code> 295 */ 296 297 public static boolean getBooleanInitParameter(ExternalContext context, String[] names) 298 { 299 return getBooleanInitParameter(context, names, false); 300 } 301 302 /** 303 * Gets the boolean init parameter value from the specified context. If the parameter was not specified, 304 * the default value is used instead. 305 * 306 * @param context 307 * the application's external context 308 * @param names 309 * the init parameter's names 310 * @param defaultValue 311 * the default value to return in case the parameter was not set 312 * 313 * @return the init parameter value as a boolean 314 * 315 * @throws NullPointerException 316 * if context or name is <code>null</code> 317 */ 318 public static boolean getBooleanInitParameter(ExternalContext context, String[] names, boolean defaultValue) 319 { 320 if (names == null) 321 { 322 throw new NullPointerException(); 323 } 324 325 String param = null; 326 for (String name : names) 327 { 328 if (name == null) 329 { 330 throw new NullPointerException(); 331 } 332 333 param = getStringInitParameter(context, name); 334 if (param != null) 335 { 336 break; 337 } 338 } 339 if (param == null) 340 { 341 return defaultValue; 342 } 343 else 344 { 345 return Boolean.parseBoolean(param.toLowerCase()); 346 } 347 } 348 349 /** 350 * Gets the boolean init parameter value from the specified context. If the parameter was not specified, 351 * the default value is used instead. 352 * 353 * @param context 354 * the application's external context 355 * @param names 356 * the init parameter's names 357 * @param defaultValue 358 * the default value to return in case the parameter was not set 359 * @param valuesIgnoreCase 360 * an array of valid values to match 361 * @param returnOnValueEqualsIgnoreCase 362 * the value to return in case the parameter match with valuesIgnoreCase 363 * 364 * @return the init parameter value as a boolean 365 * 366 * @throws NullPointerException 367 * if context or name is <code>null</code> 368 */ 369 370 public static boolean getBooleanInitParameter(ExternalContext context, String[] names, boolean defaultValue, 371 String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase) 372 { 373 if (names == null) 374 { 375 throw new NullPointerException(); 376 } 377 378 String param = null; 379 for (String name : names) 380 { 381 if (name == null) 382 { 383 throw new NullPointerException(); 384 } 385 386 param = getStringInitParameter(context, name); 387 if (param != null) 388 { 389 break; 390 } 391 } 392 if (param == null) 393 { 394 return defaultValue; 395 } 396 else 397 { 398 if (valuesIgnoreCase != null) 399 { 400 for (String trueValue : valuesIgnoreCase) 401 { 402 if (trueValue.equalsIgnoreCase(param)) 403 { 404 return returnOnValueEqualsIgnoreCase; 405 } 406 } 407 return defaultValue; 408 } 409 else 410 { 411 return Boolean.parseBoolean(param.toLowerCase()); 412 } 413 } 414 } 415 416 /** 417 * Gets the int init parameter value from the specified context. If the parameter was not 418 * specified, the default 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) 435 { 436 return getIntegerInitParameter(context, name, 0); 437 } 438 439 /** 440 * Gets the int init parameter value from the specified context. If the parameter was not specified, 441 * the default value is used instead. 442 * 443 * @param context 444 * the application's external context 445 * @param name 446 * the init parameter's name 447 * @param deprecatedName 448 * the init parameter's deprecated name. 449 * @param defaultValue 450 * the default value to return in case the parameter was not set 451 * 452 * @return the init parameter value as a int 453 * 454 * @throws NullPointerException 455 * if context or name is <code>null</code> 456 */ 457 public static int getIntegerInitParameter(ExternalContext context, String name, int defaultValue) 458 { 459 if (name == null) 460 { 461 throw new NullPointerException(); 462 } 463 464 String param = getStringInitParameter(context, name); 465 if (param == null) 466 { 467 return defaultValue; 468 } 469 else 470 { 471 return Integer.parseInt(param.toLowerCase()); 472 } 473 } 474 475 /** 476 * Gets the int init parameter value from the specified context. If the parameter was not specified, 477 * the default value is used instead. 478 * 479 * @param context 480 * the application's external context 481 * @param names 482 * the init parameter's names 483 * 484 * @return the init parameter value as a int 485 * 486 * @throws NullPointerException 487 * if context or name is <code>null</code> 488 */ 489 public static int getIntegerInitParameter(ExternalContext context, String[] names) 490 { 491 return getIntegerInitParameter(context, names, 0); 492 } 493 494 /** 495 * Gets the int init parameter value from the specified context. If the parameter was not specified, the default 496 * value is used instead. 497 * 498 * @param context 499 * the application's external context 500 * @param names 501 * the init parameter's names 502 * @param defaultValue 503 * the default value to return in case the parameter was not set 504 * 505 * @return the init parameter value as a int 506 * 507 * @throws NullPointerException 508 * if context or name is <code>null</code> 509 */ 510 511 public static int getIntegerInitParameter(ExternalContext context, String[] names, int defaultValue) 512 { 513 if (names == null) 514 { 515 throw new NullPointerException(); 516 } 517 518 String param = null; 519 for (String name : names) 520 { 521 if (name == null) 522 { 523 throw new NullPointerException(); 524 } 525 526 param = getStringInitParameter(context, name); 527 if (param != null) 528 { 529 break; 530 } 531 } 532 if (param == null) 533 { 534 return defaultValue; 535 } 536 else 537 { 538 return Integer.parseInt(param.toLowerCase()); 539 } 540 } 541 542 /** 543 * Gets the long init parameter value from the specified context. If the parameter was not specified, the default 544 * value is used instead. 545 * 546 * @param context 547 * the application's external context 548 * @param name 549 * the init parameter's name 550 * @param deprecatedName 551 * the init parameter's deprecated name. 552 * @param defaultValue 553 * the default value to return in case the parameter was not set 554 * 555 * @return the init parameter value as a long 556 * 557 * @throws NullPointerException 558 * if context or name is <code>null</code> 559 */ 560 public static long getLongInitParameter(ExternalContext context, String name) 561 { 562 return getLongInitParameter(context, name, 0); 563 } 564 565 /** 566 * Gets the long init parameter value from the specified context. If the parameter was not specified, the default 567 * value is used instead. 568 * 569 * @param context 570 * the application's external context 571 * @param name 572 * the init parameter's name 573 * @param deprecatedName 574 * the init parameter's deprecated name. 575 * @param defaultValue 576 * the default value to return in case the parameter was not set 577 * 578 * @return the init parameter value as a long 579 * 580 * @throws NullPointerException 581 * if context or name is <code>null</code> 582 */ 583 public static long getLongInitParameter(ExternalContext context, String name, long defaultValue) 584 { 585 if (name == null) 586 { 587 throw new NullPointerException(); 588 } 589 590 String param = getStringInitParameter(context, name); 591 if (param == null) 592 { 593 return defaultValue; 594 } 595 else 596 { 597 return Long.parseLong(param.toLowerCase()); 598 } 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 * 610 * @return the init parameter value as a long 611 * 612 * @throws NullPointerException 613 * if context or name is <code>null</code> 614 */ 615 616 public static long getLongInitParameter(ExternalContext context, String[] names) 617 { 618 return getLongInitParameter(context, names, 0); 619 } 620 621 /** 622 * Gets the long init parameter value from the specified context. If the parameter was not specified, the default 623 * value is used instead. 624 * 625 * @param context 626 * the application's external context 627 * @param names 628 * the init parameter's names 629 * @param defaultValue 630 * the default value to return in case the parameter was not set 631 * 632 * @return the init parameter value as a long 633 * 634 * @throws NullPointerException 635 * if context or name is <code>null</code> 636 */ 637 638 public static long getLongInitParameter(ExternalContext context, String[] names, long defaultValue) 639 { 640 if (names == null) 641 { 642 throw new NullPointerException(); 643 } 644 645 String param = null; 646 for (String name : names) 647 { 648 if (name == null) 649 { 650 throw new NullPointerException(); 651 } 652 653 param = getStringInitParameter(context, name); 654 if (param != null) 655 { 656 break; 657 } 658 } 659 if (param == null) 660 { 661 return defaultValue; 662 } 663 else 664 { 665 return Long.parseLong(param.toLowerCase()); 666 } 667 } 668 669 /** 670 * Gets the init parameter value from the specified context and instanciate it. 671 * If the parameter was not specified, the default value is used instead. 672 * 673 * @param context 674 * the application's external context 675 * @param name 676 * the init parameter's name 677 * @param deprecatedName 678 * the init parameter's deprecated name. 679 * @param defaultValue 680 * the default value to return in case the parameter was not set 681 * 682 * @return the init parameter value as an object instance 683 * 684 * @throws NullPointerException 685 * if context or name is <code>null</code> 686 */ 687 @SuppressWarnings("unchecked") 688 public static <T> T getInstanceInitParameter(ExternalContext context, String name, 689 String deprecatedName, T defaultValue) 690 { 691 String param = getStringInitParameter(context, name, deprecatedName); 692 if (param == null) 693 { 694 return defaultValue; 695 } 696 else 697 { 698 try 699 { 700 return (T) ClassUtils.classForName(param).newInstance(); 701 } 702 catch (Exception e) 703 { 704 throw new FacesException("Error Initializing Object[" + param + "]", e); 705 } 706 } 707 } 708 }