/projects/myfaces_core-2.1.10/src/myfaces-impl-2.1.10-sources/org/apache/myfaces/shared/util/WebConfigParamUtils.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 709 lines · 315 code · 48 blank · 346 comment · 81 complexity · fe15916db93710e87c2289b82520c109 MD5 · raw file

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