1 package net.trajano.twiff.internal.util;
2
3 import static net.trajano.twiff.internal.TwiffInternal.TWIFF_DEFAULTS;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6 import javax.servlet.ServletConfig;
7 import javax.servlet.ServletContext;
8 import net.trajano.twiff.MissingInitialParameterException;
9
10 /***
11 * Utility class to get data for initialization parameters.
12 *
13 * @author Archimedes Trajano
14 */
15 public class InitParamUtil {
16 /***
17 * Resource bundle containing the default values.
18 */
19 private static ResourceBundle defaults = ResourceBundle.getBundle(TWIFF_DEFAULTS);
20
21 /***
22 * Gets the initialization parameter. It first tries to look for it in the
23 * servlet config, if it is not found it will look in the servlet context,
24 * then the defaults, if it is not found it will throw a
25 * {@link MissingInitialParameterException}.
26 *
27 * @param servletConfig
28 * the servlet config to check for the initialization parameter.
29 * @param name
30 * the name of the initialization parameter
31 * @return the initialization value
32 */
33 public static String getInitParam(final ServletConfig servletConfig, final String name) {
34 String initialParameter = safeGetInitParam(servletConfig, name);
35 if (initialParameter == null) {
36 throw new MissingInitialParameterException(name);
37 }
38 return initialParameter;
39 }
40
41 /***
42 * Gets the initialization parameter. It first tries to look for it in the
43 * servlet config, if it is not found it will look in the servlet context,
44 * then the defaults. If it is not found, it returns null.
45 *
46 * @param servletConfig
47 * the servlet config to check for the initialization parameter.
48 * @param name
49 * the name of the initialization parameter
50 * @return the initialization value
51 */
52 public static String safeGetInitParam(final ServletConfig servletConfig, final String name) {
53 try {
54 return ObjectUtils.defaultIfNull(servletConfig.getInitParameter(name), servletConfig.getServletContext().getInitParameter(name), defaults.getString(name));
55 } catch (MissingResourceException e) {
56 return (String) ObjectUtils.defaultIfNull(servletConfig.getInitParameter(name), servletConfig.getServletContext().getInitParameter(name));
57 }
58 }
59
60 /***
61 * Gets the initialization parameter. It first tries to look for it in the
62 * servlet context, then the defaults if it is not found it will throw a
63 * {@link MissingInitialParameterException}.
64 *
65 * @param servletContext
66 * the servlet context to check for the initialization parameter.
67 * @param name
68 * the name of the initialization parameter
69 * @return the initialization value
70 */
71 public static String getInitParam(final ServletContext servletContext, final String name) {
72 String initialParameter = safeGetInitParam(servletContext, name);
73 if (initialParameter == null) {
74 throw new MissingInitialParameterException(name);
75 }
76 return initialParameter;
77 }
78
79 /***
80 * Gets the initialization parameter. It first tries to look for it in the
81 * servlet context, then the defaults. If it is not found, it returns null.
82 *
83 * @param servletContext
84 * the servlet context to check for the initialization parameter.
85 * @param name
86 * the name of the initialization parameter
87 * @return the initialization value
88 */
89 public static String safeGetInitParam(final ServletContext servletContext, final String name) {
90 try {
91 return (String) ObjectUtils.defaultIfNull(servletContext.getInitParameter(name), defaults.getString(name));
92 } catch (MissingResourceException e) {
93 return servletContext.getInitParameter(name);
94 }
95 }
96
97 /***
98 * Private constructor to prevent instantiation of this utility class.
99 */
100 private InitParamUtil() {
101 }
102 }