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