/*** PropKit. PropKit can load properties file from CLASSPATH or File object.*/
public classPropKit {private static Prop prop = null;private static final Map map = new ConcurrentHashMap();privatePropKit() {}/*** Using the properties file. It will loading the properties file if not loading.
*@see#use(String, String)*/
public staticProp use(String fileName) {returnuse(fileName, Const.DEFAULT_ENCODING);
}/*** Using the properties file. It will loading the properties file if not loading.
*
* Example:
* PropKit.use("config.txt", "UTF-8");
* PropKit.use("other_config.txt", "UTF-8");
* String userName = PropKit.get("userName");
* String password = PropKit.get("password");
*
* userName = PropKit.use("other_config.txt").get("userName");
* password = PropKit.use("other_config.txt").get("password");
*
* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
*
*@paramfileName the properties file's name in classpath or the sub directory of classpath
*@paramencoding the encoding*/
public staticProp use(String fileName, String encoding) {
Prop result=map.get(fileName);if (result == null) {
result= newProp(fileName, encoding);
map.put(fileName, result);if (PropKit.prop == null)
PropKit.prop=result;
}returnresult;
}/*** Using the properties file bye File object. It will loading the properties file if not loading.
*@see#use(File, String)*/
public staticProp use(File file) {returnuse(file, Const.DEFAULT_ENCODING);
}/*** Using the properties file bye File object. It will loading the properties file if not loading.
*
* Example:
* PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");
* Strig userName = PropKit.use("my_config.txt").get("userName");
*
*@paramfile the properties File object
*@paramencoding the encoding*/
public staticProp use(File file, String encoding) {
Prop result=map.get(file.getName());if (result == null) {
result= newProp(file, encoding);
map.put(file.getName(), result);if (PropKit.prop == null)
PropKit.prop=result;
}returnresult;
}public staticProp useless(String fileName) {
Prop previous=map.remove(fileName);if (PropKit.prop ==previous)
PropKit.prop= null;returnprevious;
}public static voidclear() {
prop= null;
map.clear();
}public staticProp getProp() {if (prop == null)throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");returnprop;
}public staticProp getProp(String fileName) {returnmap.get(fileName);
}public staticString get(String key) {returngetProp().get(key);
}public staticString get(String key, String defaultValue) {returngetProp().get(key, defaultValue);
}public staticInteger getInt(String key) {returngetProp().getInt(key);
}public staticInteger getInt(String key, Integer defaultValue) {returngetProp().getInt(key, defaultValue);
}public staticLong getLong(String key) {returngetProp().getLong(key);
}public staticLong getLong(String key, Long defaultValue) {returngetProp().getLong(key, defaultValue);
}public staticBoolean getBoolean(String key) {returngetProp().getBoolean(key);
}public staticBoolean getBoolean(String key, Boolean defaultValue) {returngetProp().getBoolean(key, defaultValue);
}public static booleancontainsKey(String key) {returngetProp().containsKey(key);
}
}