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 public static String getDefaultBundleName()
44 {
45 final String jvmDefautlt = System.getProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM);
46 if (null != jvmDefautlt && !"".equals(jvmDefautlt.trim()))
47 {
48 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Please be noticed the JVM provided value for the default props is used [" +
49 jvmDefautlt + "]");
50 return jvmDefautlt;
51 }
52 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The default (not JVM based) value is used [" +
53 JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME + "]");
54 return JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME;
55 }
56
57
58
59
60
61 public static String getCustomBundleName()
62 {
63 final String jvmCustom = System.getProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM);
64 if (null != jvmCustom && !"".equals(jvmCustom.trim()))
65 {
66 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Please be noticed the JVM param value is used for custom props [" +
67 jvmCustom + "]");
68 return jvmCustom;
69 }
70 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 return JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public static IPropertiesManager getPropertiesManagerImplementation()
86 {
87 final String defaultProps = JPatternsPropsUtils.getDefaultBundleName();
88 final String customProps = JPatternsPropsUtils.getCustomBundleName();
89 if (null == defaultProps)
90 {
91 throw new IllegalStateException("The name of default configuration can not be null.");
92 }
93 defaultBundle = ResourceUtils.getResourceBundle(defaultProps);
94 customBundle = ResourceUtils.getResourceBundle(customProps);
95 String className = null;
96 if (null != customBundle)
97 {
98 try
99 {
100 className = customBundle.getString(ReflexionUtils.getBaseName(IPropertiesManager.class));
101 }
102 catch (Exception ex)
103 {
104
105 }
106 if (null != className)
107 {
108 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The implementation of IPropertiesManager was found at the custom " +
109 "properties file [" + customProps + "], implementation is [" + className + "]");
110 }
111 }
112 if (StringUtils.isBlank(className) && null != defaultBundle)
113 {
114 try
115 {
116 className = defaultBundle.getString(ReflexionUtils.getBaseName(IPropertiesManager.class));
117 }
118 catch (Exception ex)
119 {
120
121 }
122 if (null != className)
123 {
124 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The implementation of IPropertiesManager was found at the default " +
125 "properties file [" + defaultProps + "]," + "implementation is [" + className + "]");
126 }
127 }
128 if (StringUtils.isBlank(className))
129 {
130 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "The default PropertiesManagerImpl implementation of the " +
131 "IPropertiesManager is used [" + ReflexionUtils.getBaseName(PropertiesManagerImpl.class) + "]");
132 return new PropertiesManagerImpl();
133 }
134 else
135 {
136 final Object object = ReflexionUtils.reflectObject(className, true);
137 if (null == object || !(object instanceof IPropertiesManager))
138 {
139 LoggingUtils.debug(LOG, JPatternsPropsUtils.class, "Can not instantiate the implementation of the IPropertiesManager " +
140 "using class name [" + className + "]. The returned object type [" +
141 ((null != object) ? object.getClass().getName() : "null was returned") + "]");
142 return null;
143 }
144 else
145 {
146 return (IPropertiesManager) object;
147 }
148 }
149 }
150
151
152 public static ResourceBundle getDefaultBundle()
153 {
154 return defaultBundle;
155 }
156
157 public static void setDefaultBundle(ResourceBundle defaultBundle)
158 {
159 JPatternsPropsUtils.defaultBundle = defaultBundle;
160 }
161
162 public static ResourceBundle getCustomBundle()
163 {
164 return customBundle;
165 }
166
167 public static void setCustomBundle(ResourceBundle customBundle)
168 {
169 JPatternsPropsUtils.customBundle = customBundle;
170 }
171 }