1 |
| package com.sourceforge.jpatterns.utils; |
2 |
| |
3 |
| import com.sourceforge.jpatterns.core.JPConstants; |
4 |
| import com.sourceforge.jpatterns.core.configuration.IPropertiesManager; |
5 |
| import com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl; |
6 |
| import com.zmicer.utils.LoggingUtils; |
7 |
| import com.zmicer.utils.ReflexionUtils; |
8 |
| import com.zmicer.utils.ResourceUtils; |
9 |
| import com.zmicer.utils.ZmicerUtilsConstants; |
10 |
| import org.apache.commons.lang.StringUtils; |
11 |
| import org.apache.log4j.Logger; |
12 |
| |
13 |
| import java.util.ResourceBundle; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class JPatternsPropsUtils |
23 |
| { |
24 |
| |
25 |
| |
26 |
| |
27 |
| final public static Logger LOG = Logger.getLogger(ZmicerUtilsConstants.LoggingCategory.PROPERTIES_RELATED.toString()); |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public static ResourceBundle defaultBundle; |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public static ResourceBundle customBundle; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
174
| public static String getDefaultBundleName()
|
44 |
| { |
45 |
174
| final String jvmDefautlt = System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM);
|
46 |
174
| if (null != jvmDefautlt && !"".equals(jvmDefautlt.trim()))
|
47 |
| { |
48 |
90
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Please be noticed the JVM provided value for the default props is used [" +
|
49 |
| jvmDefautlt + "]"); |
50 |
90
| return jvmDefautlt;
|
51 |
| } |
52 |
84
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The default (not JVM based) value is used [" +
|
53 |
| JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME + "]"); |
54 |
84
| return JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME;
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
174
| public static String getCustomBundleName()
|
62 |
| { |
63 |
174
| final String jvmCustom = System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM);
|
64 |
174
| if (null != jvmCustom && !"".equals(jvmCustom.trim()))
|
65 |
| { |
66 |
72
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Please be noticed the JVM param value is used for custom props [" +
|
67 |
| jvmCustom + "]"); |
68 |
72
| return jvmCustom;
|
69 |
| } |
70 |
102
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The default (not JVM based value is used for custom props file name [" +
|
71 |
| JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME + "]"); |
72 |
102
| return JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME;
|
73 |
| } |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
6
| public static IPropertiesManager getPropertiesManagerImplementation()
|
86 |
| { |
87 |
6
| final String defaultProps = JPatternsPropsUtils.getDefaultBundleName();
|
88 |
6
| final String customProps = JPatternsPropsUtils.getCustomBundleName();
|
89 |
6
| if (null == defaultProps)
|
90 |
| { |
91 |
0
| throw new IllegalStateException("The name of default configuration can not be null.");
|
92 |
| } |
93 |
6
| defaultBundle = ResourceUtils.getResourceBundle(defaultProps);
|
94 |
6
| customBundle = ResourceUtils.getResourceBundle(customProps);
|
95 |
6
| String className = null;
|
96 |
6
| if (null != customBundle)
|
97 |
| { |
98 |
3
| try
|
99 |
| { |
100 |
3
| className = customBundle.getString(ReflexionUtils.getBaseName(IPropertiesManager.class));
|
101 |
| } |
102 |
| catch (Exception ex) |
103 |
| { |
104 |
| |
105 |
| } |
106 |
3
| if (null != className)
|
107 |
| { |
108 |
2
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The implementation of IPropertiesManager was found at the custom " +
|
109 |
| "properties file [" + customProps + "], implementation is [" + className + "]"); |
110 |
| } |
111 |
| } |
112 |
6
| if (StringUtils.isBlank(className) && null != defaultBundle)
|
113 |
| { |
114 |
4
| try
|
115 |
| { |
116 |
4
| className = defaultBundle.getString(ReflexionUtils.getBaseName(IPropertiesManager.class));
|
117 |
| } |
118 |
| catch (Exception ex) |
119 |
| { |
120 |
| |
121 |
| } |
122 |
4
| if (null != className)
|
123 |
| { |
124 |
3
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The implementation of IPropertiesManager was found at the default " +
|
125 |
| "properties file [" + defaultProps + "]," + "implementation is [" + className + "]"); |
126 |
| } |
127 |
| } |
128 |
6
| if (StringUtils.isBlank(className))
|
129 |
| { |
130 |
1
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The default PropertiesManagerImpl implementation of the " +
|
131 |
| "IPropertiesManager is used [" + ReflexionUtils.getBaseName(PropertiesManagerImpl.class) + "]"); |
132 |
1
| return new PropertiesManagerImpl();
|
133 |
| } |
134 |
| else |
135 |
| { |
136 |
5
| final Object object = ReflexionUtils.reflectObject(className, true);
|
137 |
4
| if (null == object || !(object instanceof IPropertiesManager))
|
138 |
| { |
139 |
0
| LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Can not instantiate the implementation of the IPropertiesManager " +
|
140 |
| "using class name [" + className + "]. The returned object type [" + |
141 |
0
| ((null != object) ? object.getClass().getName() : "null was returned") + "]");
|
142 |
0
| return null;
|
143 |
| } |
144 |
| else |
145 |
| { |
146 |
4
| return (IPropertiesManager) object;
|
147 |
| } |
148 |
| } |
149 |
| } |
150 |
| |
151 |
| |
152 |
7
| public static ResourceBundle getDefaultBundle()
|
153 |
| { |
154 |
7
| return defaultBundle;
|
155 |
| } |
156 |
| |
157 |
2
| public static void setDefaultBundle(ResourceBundle defaultBundle)
|
158 |
| { |
159 |
2
| JPatternsPropsUtils.defaultBundle = defaultBundle;
|
160 |
| } |
161 |
| |
162 |
2
| public static ResourceBundle getCustomBundle()
|
163 |
| { |
164 |
2
| return customBundle;
|
165 |
| } |
166 |
| |
167 |
2
| public static void setCustomBundle(ResourceBundle customBundle)
|
168 |
| { |
169 |
2
| JPatternsPropsUtils.customBundle = customBundle;
|
170 |
| } |
171 |
| } |