You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/01/12 14:06:13 UTC
[05/50] [abbrv] directory-kerberos git commit: Renaming packages in
contrib projects, using "apache"
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java b/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
new file mode 100644
index 0000000..695bf45
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
@@ -0,0 +1,266 @@
+package org.apache.haox.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.*;
+
+public class Conf implements Config {
+ private static final Logger logger = LoggerFactory.getLogger(Conf.class);
+
+ private List<ConfigLoader> resourceConfigs;
+ private final ConfigImpl config;
+ private boolean needReload;
+
+ public Conf() {
+ this.resourceConfigs = new ArrayList<ConfigLoader>(1);
+ this.config = new ConfigImpl("Conf");
+ this.needReload = true;
+ }
+
+ public void addXmlConfig(File xmlFile) throws IOException {
+ addResource(Resource.createXmlResource(xmlFile));
+ }
+
+ public void addIniConfig(File iniFile) throws IOException {
+ addResource(Resource.createIniResource(iniFile));
+ }
+
+ public void addJsonConfig(File jsonFile) throws IOException {
+ addResource(Resource.createJsonResource(jsonFile));
+ }
+
+ public void addPropertiesConfig(File propertiesFile) throws IOException {
+ addResource(Resource.createPropertiesFileResource(propertiesFile));
+ }
+
+ public void addPropertiesConfig(Properties propertiesConfig) {
+ addResource(Resource.createPropertiesResource(propertiesConfig));
+ }
+
+ public void addMapConfig(Map<String, String> mapConfig) {
+ addResource(Resource.createMapResource(mapConfig));
+ }
+
+ public void addResource(Resource resource) {
+ ConfigLoader loader = getLoader(resource);
+ resourceConfigs.add(loader);
+ needReload = true;
+ }
+
+ private static ConfigLoader getLoader(Resource resource) {
+ ConfigLoader loader = null;
+
+ Class<? extends ConfigLoader> loaderClass = resource.getFormat().getLoaderClass();
+ try {
+ loader = loaderClass.newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to create org.haox.config loader for " + loaderClass.getName(), e);
+ }
+ loader.setResource(resource);
+ return loader;
+ }
+
+ private void checkAndLoad() {
+ if (needReload) {
+ reload();
+ needReload = false;
+ }
+ }
+
+ public void reload() {
+ config.reset();
+ if (resourceConfigs.size() == 1) {
+ ConfigLoader loader = resourceConfigs.get(0);
+ loader.setConfig(config);
+ loader.load();
+ } else {
+ for (ConfigLoader loader : resourceConfigs) {
+ Config loaded = loader.load();
+ config.set(loaded.getResource(), loaded);
+ }
+ }
+ }
+
+ @Override
+ public String getResource() {
+ checkAndLoad();
+ return config.getResource();
+ }
+
+ @Override
+ public Set<String> getNames() {
+ checkAndLoad();
+ return config.getNames();
+ }
+
+ @Override
+ public String getString(String name) {
+ checkAndLoad();
+ return config.getString(name);
+ }
+
+ @Override
+ public String getString(ConfigKey name) {
+ checkAndLoad();
+ return config.getString(name);
+ }
+
+ @Override
+ public String getString(String name, String defaultValue) {
+ checkAndLoad();
+ return config.getString(name, defaultValue);
+ }
+
+ @Override
+ public String getTrimmed(String name) {
+ checkAndLoad();
+ return config.getTrimmed(name);
+ }
+
+ @Override
+ public String getTrimmed(ConfigKey name) {
+ checkAndLoad();
+ return config.getTrimmed(name);
+ }
+
+ @Override
+ public Boolean getBoolean(String name) {
+ checkAndLoad();
+ return config.getBoolean(name);
+ }
+
+ @Override
+ public Boolean getBoolean(ConfigKey name) {
+ checkAndLoad();
+ return config.getBoolean(name);
+ }
+
+ @Override
+ public Boolean getBoolean(String name, boolean defaultValue) {
+ checkAndLoad();
+ return config.getBoolean(name, defaultValue);
+ }
+
+ @Override
+ public Integer getInt(String name) {
+ checkAndLoad();
+ return config.getInt(name);
+ }
+
+ @Override
+ public Integer getInt(ConfigKey name) {
+ checkAndLoad();
+ return config.getInt(name);
+ }
+
+ @Override
+ public Integer getInt(String name, int defaultValue) {
+ checkAndLoad();
+ return config.getInt(name, defaultValue);
+ }
+
+ @Override
+ public Long getLong(String name) {
+ checkAndLoad();
+ return config.getLong(name);
+ }
+
+ @Override
+ public Long getLong(ConfigKey name) {
+ checkAndLoad();
+ return config.getLong(name);
+ }
+
+ @Override
+ public Long getLong(String name, long defaultValue) {
+ checkAndLoad();
+ return config.getLong(name, defaultValue);
+ }
+
+ @Override
+ public Float getFloat(String name) {
+ checkAndLoad();
+ return config.getFloat(name);
+ }
+
+ @Override
+ public Float getFloat(ConfigKey name) {
+ checkAndLoad();
+ return config.getFloat(name);
+ }
+
+ @Override
+ public Float getFloat(String name, float defaultValue) {
+ checkAndLoad();
+ return config.getFloat(name, defaultValue);
+ }
+
+ @Override
+ public List<String> getList(String name) {
+ checkAndLoad();
+ return config.getList(name);
+ }
+
+ @Override
+ public List<String> getList(String name, String[] defaultValue) {
+ checkAndLoad();
+ return config.getList(name, defaultValue);
+ }
+
+ @Override
+ public List<String> getList(ConfigKey name) {
+ checkAndLoad();
+ return config.getList(name);
+ }
+
+ @Override
+ public Config getConfig(String name) {
+ checkAndLoad();
+ return config.getConfig(name);
+ }
+
+ @Override
+ public Config getConfig(ConfigKey name) {
+ checkAndLoad();
+ return config.getConfig(name);
+ }
+
+ @Override
+ public Class<?> getClass(String name) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getClass(name);
+ }
+
+ @Override
+ public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getClass(name, defaultValue);
+ }
+
+ @Override
+ public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getClass(name);
+ }
+
+ @Override
+ public <T> T getInstance(String name) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getInstance(name);
+ }
+
+ @Override
+ public <T> T getInstance(ConfigKey name) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getInstance(name);
+ }
+
+ @Override
+ public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException {
+ checkAndLoad();
+ return config.getInstance(name, xface);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/Config.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/Config.java b/contrib/haox-config/src/main/java/org/apache/haox/config/Config.java
new file mode 100644
index 0000000..8d4e413
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/Config.java
@@ -0,0 +1,39 @@
+package org.apache.haox.config;
+
+import java.util.List;
+import java.util.Set;
+
+public interface Config {
+ public String getResource();
+ public Set<String> getNames();
+
+ public String getString(String name);
+ public String getString(ConfigKey name);
+ public String getString(String name, String defaultValue);
+ public String getTrimmed(String name);
+ public String getTrimmed(ConfigKey name);
+ public Boolean getBoolean(String name);
+ public Boolean getBoolean(ConfigKey name);
+ public Boolean getBoolean(String name, boolean defaultValue);
+ public Integer getInt(String name);
+ public Integer getInt(ConfigKey name);
+ public Integer getInt(String name, int defaultValue);
+ public Long getLong(String name);
+ public Long getLong(ConfigKey name);
+ public Long getLong(String name, long defaultValue);
+ public Float getFloat(String name);
+ public Float getFloat(ConfigKey name);
+ public Float getFloat(String name, float defaultValue);
+ public List<String> getList(String name);
+ public List<String> getList(String name, String[] defaultValue);
+ public List<String> getList(ConfigKey name);
+ public Config getConfig(String name);
+ public Config getConfig(ConfigKey name);
+
+ public Class<?> getClass(String name) throws ClassNotFoundException;
+ public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException;
+ public Class<?> getClass(ConfigKey name) throws ClassNotFoundException;
+ public <T> T getInstance(String name) throws ClassNotFoundException;
+ public <T> T getInstance(ConfigKey name) throws ClassNotFoundException;
+ public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException;
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
new file mode 100644
index 0000000..87d825f
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
@@ -0,0 +1,325 @@
+package org.apache.haox.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+public class ConfigImpl implements Config {
+ private static final Logger logger = LoggerFactory.getLogger(Config.class);
+
+ private String resource;
+ private Map<String, ConfigObject> properties;
+ private List<Config> subConfigs;
+
+ private Set<String> propNames;
+
+ protected ConfigImpl(String resource) {
+ this.resource = resource;
+ this.properties = new HashMap<String, ConfigObject>();
+ this.subConfigs = new ArrayList<Config>(0);
+ }
+
+ protected void reset() {
+ this.properties.clear();
+ this.subConfigs.clear();
+ }
+
+ @Override
+ public String getResource() {
+ return resource;
+ }
+
+ @Override
+ public Set<String> getNames() {
+ reloadNames();
+ return propNames;
+ }
+
+ @Override
+ public String getString(String name) {
+ String result = null;
+
+ ConfigObject co = properties.get(name);
+ if (co != null) {
+ result = co.getPropertyValue();
+ }
+
+ if (result == null) {
+ for (Config sub : subConfigs) {
+ result = sub.getString(name);
+ if (result != null) break;
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ public String getString(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getString(name.getPropertyKey(), (String) name.getDefaultValue());
+ }
+ return getString(name.getPropertyKey());
+ }
+
+ @Override
+ public String getString(String name, String defaultValue) {
+ String result = getString(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public String getTrimmed(String name) {
+ String result = getString(name);
+ if (null != result) {
+ result = result.trim();
+ }
+ return result;
+ }
+
+ @Override
+ public String getTrimmed(ConfigKey name) {
+ return getTrimmed(name.getPropertyKey());
+ }
+
+ @Override
+ public Integer getInt(String name) {
+ Integer result = null;
+ String value = getTrimmed(name);
+ if (value != null) {
+ result = Integer.valueOf(value);
+ }
+ return result;
+ }
+
+ @Override
+ public Integer getInt(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getInt(name.getPropertyKey(), (Integer) name.getDefaultValue());
+ }
+ return getInt(name.getPropertyKey());
+ }
+
+ @Override
+ public Integer getInt(String name, int defaultValue) {
+ Integer result = getInt(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public Long getLong(String name) {
+ Long result = null;
+ String value = getTrimmed(name);
+ if (value != null) {
+ result = Long.valueOf(value);
+ }
+ return result;
+ }
+
+ @Override
+ public Long getLong(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getLong(name.getPropertyKey(), (Long) name.getDefaultValue());
+ }
+ return getLong(name.getPropertyKey());
+ }
+
+ @Override
+ public Long getLong(String name, long defaultValue) {
+ Long result = getLong(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public Float getFloat(String name) {
+ Float result = null;
+ String value = getTrimmed(name);
+ if (value != null) {
+ result = Float.valueOf(value);
+ }
+ return result;
+ }
+
+ @Override
+ public Float getFloat(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getFloat(name.getPropertyKey(), (Float) name.getDefaultValue());
+ }
+ return getFloat(name.getPropertyKey());
+ }
+
+ @Override
+ public Float getFloat(String name, float defaultValue) {
+ Float result = getFloat(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public Boolean getBoolean(String name) {
+ Boolean result = null;
+ String value = getTrimmed(name);
+ if (value != null) {
+ result = Boolean.valueOf(value);
+ }
+ return result;
+ }
+
+ @Override
+ public Boolean getBoolean(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getBoolean(name.getPropertyKey(), (Boolean) name.getDefaultValue());
+ }
+ return getBoolean(name.getPropertyKey());
+ }
+
+ @Override
+ public Boolean getBoolean(String name, boolean defaultValue) {
+ Boolean result = getBoolean(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public List<String> getList(String name) {
+ List<String> results = null;
+ ConfigObject co = properties.get(name);
+ if (co != null) {
+ results = co.getListValues();
+ }
+ return results;
+ }
+
+ @Override
+ public List<String> getList(String name, String[] defaultValue) {
+ List<String> results = getList(name);
+ if (results == null) {
+ results = Arrays.asList(defaultValue);
+ }
+ return results;
+ }
+
+ @Override
+ public List<String> getList(ConfigKey name) {
+ if (name.getDefaultValue() != null) {
+ return getList(name.getPropertyKey(), (String[]) name.getDefaultValue());
+ }
+ return getList(name.getPropertyKey());
+ }
+
+ @Override
+ public Config getConfig(String name) {
+ Config result = null;
+ ConfigObject co = properties.get(name);
+ if (co != null) {
+ result = co.getConfigValue();
+ }
+ return result;
+ }
+
+ @Override
+ public Config getConfig(ConfigKey name) {
+ return getConfig(name.getPropertyKey());
+ }
+
+ @Override
+ public Class<?> getClass(String name) throws ClassNotFoundException {
+ Class<?> result = null;
+
+ String valueString = getString(name);
+ if (valueString != null) {
+ Class<?> cls = Class.forName(name);
+ result = cls;
+ }
+
+ return result;
+ }
+
+ @Override
+ public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException {
+ Class<?> result = getClass(name);
+ if (result == null) {
+ result = defaultValue;
+ }
+ return result;
+ }
+
+ @Override
+ public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
+ if (name.getDefaultValue() != null) {
+ return getClass(name.getPropertyKey(), (Class<?>) name.getDefaultValue());
+ }
+ return getClass(name.getPropertyKey());
+ }
+
+ @Override
+ public <T> T getInstance(String name) throws ClassNotFoundException {
+ return getInstance(name, null);
+ }
+
+ @Override
+ public <T> T getInstance(ConfigKey name) throws ClassNotFoundException {
+ return getInstance(name.getPropertyKey());
+ }
+
+ @Override
+ public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException {
+ T result = null;
+
+ Class<?> cls = getClass(name, null);
+ if (xface != null && !xface.isAssignableFrom(cls)) {
+ throw new RuntimeException(cls + " does not implement " + xface);
+ }
+ try {
+ result = (T) cls.newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to create instance with class " + cls.getName());
+ }
+
+ return result;
+ }
+
+ protected void set(String name, String value) {
+ ConfigObject co = new ConfigObject(value);
+ set(name, co);
+ }
+
+ protected void set(String name, Config value) {
+ ConfigObject co = new ConfigObject(value);
+ set(name, co);
+
+ addSubConfig(value);
+ }
+
+ protected void set(String name, ConfigObject value) {
+ this.properties.put(name, value);
+ }
+
+ private void addSubConfig(Config config) {
+ this.subConfigs.add(config);
+ }
+
+ private void reloadNames() {
+ if (propNames != null) {
+ propNames.clear();
+ }
+ propNames = new HashSet<String>(properties.keySet());
+ for (Config sub : subConfigs) {
+ propNames.addAll(sub.getNames());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigKey.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigKey.java b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigKey.java
new file mode 100644
index 0000000..d89cd9d
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigKey.java
@@ -0,0 +1,6 @@
+package org.apache.haox.config;
+
+public interface ConfigKey {
+ public String getPropertyKey();
+ public Object getDefaultValue();
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigLoader.java
new file mode 100644
index 0000000..b730df5
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigLoader.java
@@ -0,0 +1,31 @@
+package org.apache.haox.config;
+
+public abstract class ConfigLoader {
+ private Resource resource;
+ private ConfigImpl config;
+
+ protected void setResource(Resource resource) {
+ this.resource = resource;
+ }
+
+ protected void setConfig(ConfigImpl config) {
+ this.config = config;
+ }
+
+ public Config load() {
+ if (config == null) {
+ config = new ConfigImpl(resource.getName());
+ }
+ config.reset();
+
+ try {
+ loadConfig(config, resource);
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to load org.haox.config", e);
+ }
+
+ return this.config;
+ }
+
+ protected abstract void loadConfig(ConfigImpl config, Resource resource) throws Exception;
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigObject.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigObject.java b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigObject.java
new file mode 100644
index 0000000..48f3235
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigObject.java
@@ -0,0 +1,61 @@
+package org.apache.haox.config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ConfigObject {
+ protected static enum VALUE_TYPE { PROPERTY, LIST, CONFIG };
+
+ private VALUE_TYPE valueType;
+ private Object value;
+
+ public ConfigObject(String value) {
+ this.value = value;
+ this.valueType = VALUE_TYPE.PROPERTY;
+ }
+
+ public ConfigObject(String[] values) {
+ List<String> valuesList = new ArrayList<String>();
+ for (String v : values) {
+ valuesList.add(v);
+ }
+
+ this.value = valuesList;
+ this.valueType = VALUE_TYPE.LIST;
+ }
+
+ public ConfigObject(List<String> values) {
+ this.value = new ArrayList<String>(values);
+ this.valueType = VALUE_TYPE.LIST;
+ }
+
+ public ConfigObject(Config value) {
+ this.value = value;
+ this.valueType = VALUE_TYPE.CONFIG;
+ }
+
+ public String getPropertyValue() {
+ String result = null;
+ if (valueType == VALUE_TYPE.PROPERTY) {
+ result = (String) value;
+ }
+ return result;
+ }
+
+ public List<String> getListValues() {
+ List<String> results = null;
+ if (valueType == VALUE_TYPE.LIST) {
+ results = (List<String>) value;
+ }
+
+ return results;
+ }
+
+ public Config getConfigValue() {
+ Config result = null;
+ if (valueType == VALUE_TYPE.CONFIG) {
+ result = (Config) value;
+ }
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/IniConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/IniConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/IniConfigLoader.java
new file mode 100644
index 0000000..a3cadde
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/IniConfigLoader.java
@@ -0,0 +1,8 @@
+package org.apache.haox.config;
+
+public class IniConfigLoader extends ConfigLoader {
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) {
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/JsonConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/JsonConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/JsonConfigLoader.java
new file mode 100644
index 0000000..e9c905b
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/JsonConfigLoader.java
@@ -0,0 +1,8 @@
+package org.apache.haox.config;
+
+public class JsonConfigLoader extends ConfigLoader {
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) {
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/MapConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/MapConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/MapConfigLoader.java
new file mode 100644
index 0000000..100aeed
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/MapConfigLoader.java
@@ -0,0 +1,15 @@
+package org.apache.haox.config;
+
+import java.util.Map;
+
+public class MapConfigLoader extends ConfigLoader {
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) {
+ Map<String, String> mapConfig = (Map<String, String>) resource.getResource();
+ String value;
+ for (String key : mapConfig.keySet()) {
+ value = mapConfig.get(key);
+ config.set(key, value);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesConfigLoader.java
new file mode 100644
index 0000000..3f11401
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesConfigLoader.java
@@ -0,0 +1,24 @@
+package org.apache.haox.config;
+
+import java.util.Properties;
+
+public class PropertiesConfigLoader extends ConfigLoader {
+
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
+ Properties propConfig = (Properties) resource.getResource();
+ loadConfig(config, propConfig);
+ }
+
+ protected void loadConfig(ConfigImpl config, Properties propConfig) {
+ Object value;
+ for (Object key : propConfig.keySet()) {
+ if (key instanceof String) {
+ value = propConfig.getProperty((String) key);
+ if (value != null && value instanceof String) {
+ config.set((String) key, (String) value);
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesFileConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesFileConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesFileConfigLoader.java
new file mode 100644
index 0000000..bba9faa
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/PropertiesFileConfigLoader.java
@@ -0,0 +1,14 @@
+package org.apache.haox.config;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+public class PropertiesFileConfigLoader extends PropertiesConfigLoader {
+
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
+ Properties propConfig = new Properties();
+ propConfig.load((InputStream) resource.getResource());
+ loadConfig(config, propConfig);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/Resource.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/Resource.java b/contrib/haox-config/src/main/java/org/apache/haox/config/Resource.java
new file mode 100644
index 0000000..2825ee7
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/Resource.java
@@ -0,0 +1,100 @@
+package org.apache.haox.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+
+public class Resource {
+ public static enum Format {
+ XML_FILE(XmlConfigLoader.class),
+ INI_FILE(IniConfigLoader.class),
+ JSON_FILE(JsonConfigLoader.class),
+ PROPERTIES_FILE(PropertiesFileConfigLoader.class),
+ MAP(MapConfigLoader.class),
+ PROPERTIES(PropertiesConfigLoader.class);
+
+ private Class<? extends ConfigLoader> loaderClass;
+
+ private Format(Class<? extends ConfigLoader> loaderClass) {
+ this.loaderClass = loaderClass;
+ }
+
+ public Class<? extends ConfigLoader> getLoaderClass() {
+ return loaderClass;
+ }
+ }
+
+ private String name;
+ private Object resource;
+ private Format format;
+
+ public static Resource createXmlResource(File xmlFile) throws IOException {
+ return new Resource(xmlFile.getName(), xmlFile, Format.XML_FILE);
+ }
+
+ public static Resource createIniResource(File iniFile) throws IOException {
+ return new Resource(iniFile.getName(), iniFile, Format.INI_FILE);
+ }
+
+ public static Resource createJsonResource(File jsonFile) throws IOException {
+ return new Resource(jsonFile.getName(), jsonFile, Format.JSON_FILE);
+ }
+
+ public static Resource createXmlResource(URL xmlUrl) throws IOException {
+ return new Resource(xmlUrl, Format.XML_FILE);
+ }
+
+ public static Resource createIniResource(URL iniUrl) throws IOException {
+ return new Resource(iniUrl, Format.INI_FILE);
+ }
+
+ public static Resource createJsonResource(URL jsonUrl) throws IOException {
+ return new Resource(jsonUrl, Format.JSON_FILE);
+ }
+
+ public static Resource createMapResource(Map<String,String> mapConfig) {
+ return new Resource("mapConfig", mapConfig, Format.MAP);
+ }
+
+ public static Resource createPropertiesFileResource(File propFile) throws IOException {
+ return new Resource(propFile.getName(), propFile, Format.PROPERTIES_FILE);
+ }
+
+ public static Resource createPropertiesResource(Properties propertiesConfig) {
+ return new Resource("propConfig", propertiesConfig, Format.PROPERTIES);
+ }
+
+ private Resource(String name, File resourceFile, Format format) throws FileNotFoundException {
+ this(name, new FileInputStream(resourceFile), format);
+ }
+
+ private Resource(URL resourceUrl, Format format) throws IOException {
+ this(resourceUrl.toString(), resourceUrl.openStream(), format);
+ }
+
+ private Resource(String name, Object resourceStream, Format format) {
+ this.name = name;
+ this.resource = resourceStream;
+ this.format = format;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Object getResource() {
+ return resource;
+ }
+
+ public Format getFormat() {
+ return format;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/apache/haox/config/XmlConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/XmlConfigLoader.java b/contrib/haox-config/src/main/java/org/apache/haox/config/XmlConfigLoader.java
new file mode 100644
index 0000000..583811b
--- /dev/null
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/XmlConfigLoader.java
@@ -0,0 +1,140 @@
+package org.apache.haox.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.*;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+public class XmlConfigLoader extends ConfigLoader {
+ private static final Logger logger = LoggerFactory.getLogger(Config.class);
+
+ @Override
+ protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
+ Element doc = loadResourceDocument(resource);
+ loadConfig((ConfigImpl) config, doc);
+ }
+
+ private Element loadResourceDocument(Resource resource) throws Exception {
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+
+ docBuilderFactory.setIgnoringComments(true);
+ docBuilderFactory.setNamespaceAware(true);
+ try {
+ docBuilderFactory.setXIncludeAware(true);
+ } catch (UnsupportedOperationException e) {
+ logger.error("Failed to set setXIncludeAware(true) for parser", e);
+ }
+ DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
+ InputStream is = (InputStream) resource.getResource();
+ Document doc = null;
+ try {
+ doc = builder.parse(is);
+ } finally {
+ is.close();
+ }
+
+ Element root = doc.getDocumentElement();
+ validateConfig(root);
+
+ return root;
+ }
+
+ private boolean validateConfig(Element root) {
+ boolean valid = false;
+
+ if ("config".equals(root.getTagName())) {
+ valid = true;
+ } else {
+ logger.error("bad conf element: top-level element not <configuration>");
+ }
+
+ return valid;
+ }
+
+ private void loadConfig(ConfigImpl conifg, Element element) {
+ String name;
+ ConfigObject value;
+
+ NodeList props = element.getChildNodes();
+ for (int i = 0; i < props.getLength(); i++) {
+ Node subNode = props.item(i);
+ if (!(subNode instanceof Element)) {
+ continue;
+ }
+
+ Element prop = (Element)subNode;
+ name = getElementName(prop);
+ if (name == null) {
+ continue;
+ }
+
+ value = null;
+ String tagName = prop.getTagName();
+ if ("property".equals(tagName) && prop.hasChildNodes()) {
+ value = loadProperty(prop);
+ } else if ("config".equals(tagName) && prop.hasChildNodes()) {
+ ConfigImpl cfg = new ConfigImpl(name);
+ loadConfig(cfg, prop);
+ value = new ConfigObject(cfg);
+ }
+
+ if (name != null) {
+ conifg.set(name, value);
+ }
+ }
+ }
+
+ private static ConfigObject loadProperty(Element ele) {
+ String value = null;
+ if (ele.getFirstChild() instanceof Text) {
+ value = ((Text)ele.getFirstChild()).getData();
+ return new ConfigObject(value);
+ }
+
+ ConfigObject result = null;
+ NodeList nodes = ele.getChildNodes();
+ List<String> values = new ArrayList<String>(nodes.getLength());
+ for (int i = 0; i < nodes.getLength(); i++) {
+ value = null;
+ Node valueNode = nodes.item(i);
+ if (!(valueNode instanceof Element))
+ continue;
+
+ Element valueEle = (Element)valueNode;
+ if ("value".equals(valueEle.getTagName()) && valueEle.hasChildNodes()) {
+ value = ((Text)valueEle.getFirstChild()).getData();
+ }
+
+ if (value != null) {
+ values.add(value);
+ }
+ }
+ return new ConfigObject(values);
+ }
+
+ private static String getElementName(Element ele) {
+ String name, value;
+ Node node;
+ Attr attr;
+
+ NamedNodeMap nnm = ele.getAttributes();
+ for (int i = 0; i < nnm.getLength(); ++i) {
+ node = nnm.item(i);
+ if (!(node instanceof Attr))
+ continue;
+ attr = (Attr) node;
+ name = attr.getName();
+ value = attr.getValue();
+
+ if ("name".equals(name)) {
+ return value;
+ }
+ }
+ return null;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/Conf.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/Conf.java b/contrib/haox-config/src/main/java/org/haox/config/Conf.java
deleted file mode 100644
index 8fac4ef..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/Conf.java
+++ /dev/null
@@ -1,266 +0,0 @@
-package org.haox.config;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.*;
-
-public class Conf implements Config {
- private static final Logger logger = LoggerFactory.getLogger(Conf.class);
-
- private List<ConfigLoader> resourceConfigs;
- private final ConfigImpl config;
- private boolean needReload;
-
- public Conf() {
- this.resourceConfigs = new ArrayList<ConfigLoader>(1);
- this.config = new ConfigImpl("Conf");
- this.needReload = true;
- }
-
- public void addXmlConfig(File xmlFile) throws IOException {
- addResource(Resource.createXmlResource(xmlFile));
- }
-
- public void addIniConfig(File iniFile) throws IOException {
- addResource(Resource.createIniResource(iniFile));
- }
-
- public void addJsonConfig(File jsonFile) throws IOException {
- addResource(Resource.createJsonResource(jsonFile));
- }
-
- public void addPropertiesConfig(File propertiesFile) throws IOException {
- addResource(Resource.createPropertiesFileResource(propertiesFile));
- }
-
- public void addPropertiesConfig(Properties propertiesConfig) {
- addResource(Resource.createPropertiesResource(propertiesConfig));
- }
-
- public void addMapConfig(Map<String, String> mapConfig) {
- addResource(Resource.createMapResource(mapConfig));
- }
-
- public void addResource(Resource resource) {
- ConfigLoader loader = getLoader(resource);
- resourceConfigs.add(loader);
- needReload = true;
- }
-
- private static ConfigLoader getLoader(Resource resource) {
- ConfigLoader loader = null;
-
- Class<? extends ConfigLoader> loaderClass = resource.getFormat().getLoaderClass();
- try {
- loader = loaderClass.newInstance();
- } catch (Exception e) {
- throw new RuntimeException("Failed to create org.haox.config loader for " + loaderClass.getName(), e);
- }
- loader.setResource(resource);
- return loader;
- }
-
- private void checkAndLoad() {
- if (needReload) {
- reload();
- needReload = false;
- }
- }
-
- public void reload() {
- config.reset();
- if (resourceConfigs.size() == 1) {
- ConfigLoader loader = resourceConfigs.get(0);
- loader.setConfig(config);
- loader.load();
- } else {
- for (ConfigLoader loader : resourceConfigs) {
- Config loaded = loader.load();
- config.set(loaded.getResource(), loaded);
- }
- }
- }
-
- @Override
- public String getResource() {
- checkAndLoad();
- return config.getResource();
- }
-
- @Override
- public Set<String> getNames() {
- checkAndLoad();
- return config.getNames();
- }
-
- @Override
- public String getString(String name) {
- checkAndLoad();
- return config.getString(name);
- }
-
- @Override
- public String getString(ConfigKey name) {
- checkAndLoad();
- return config.getString(name);
- }
-
- @Override
- public String getString(String name, String defaultValue) {
- checkAndLoad();
- return config.getString(name, defaultValue);
- }
-
- @Override
- public String getTrimmed(String name) {
- checkAndLoad();
- return config.getTrimmed(name);
- }
-
- @Override
- public String getTrimmed(ConfigKey name) {
- checkAndLoad();
- return config.getTrimmed(name);
- }
-
- @Override
- public Boolean getBoolean(String name) {
- checkAndLoad();
- return config.getBoolean(name);
- }
-
- @Override
- public Boolean getBoolean(ConfigKey name) {
- checkAndLoad();
- return config.getBoolean(name);
- }
-
- @Override
- public Boolean getBoolean(String name, boolean defaultValue) {
- checkAndLoad();
- return config.getBoolean(name, defaultValue);
- }
-
- @Override
- public Integer getInt(String name) {
- checkAndLoad();
- return config.getInt(name);
- }
-
- @Override
- public Integer getInt(ConfigKey name) {
- checkAndLoad();
- return config.getInt(name);
- }
-
- @Override
- public Integer getInt(String name, int defaultValue) {
- checkAndLoad();
- return config.getInt(name, defaultValue);
- }
-
- @Override
- public Long getLong(String name) {
- checkAndLoad();
- return config.getLong(name);
- }
-
- @Override
- public Long getLong(ConfigKey name) {
- checkAndLoad();
- return config.getLong(name);
- }
-
- @Override
- public Long getLong(String name, long defaultValue) {
- checkAndLoad();
- return config.getLong(name, defaultValue);
- }
-
- @Override
- public Float getFloat(String name) {
- checkAndLoad();
- return config.getFloat(name);
- }
-
- @Override
- public Float getFloat(ConfigKey name) {
- checkAndLoad();
- return config.getFloat(name);
- }
-
- @Override
- public Float getFloat(String name, float defaultValue) {
- checkAndLoad();
- return config.getFloat(name, defaultValue);
- }
-
- @Override
- public List<String> getList(String name) {
- checkAndLoad();
- return config.getList(name);
- }
-
- @Override
- public List<String> getList(String name, String[] defaultValue) {
- checkAndLoad();
- return config.getList(name, defaultValue);
- }
-
- @Override
- public List<String> getList(ConfigKey name) {
- checkAndLoad();
- return config.getList(name);
- }
-
- @Override
- public Config getConfig(String name) {
- checkAndLoad();
- return config.getConfig(name);
- }
-
- @Override
- public Config getConfig(ConfigKey name) {
- checkAndLoad();
- return config.getConfig(name);
- }
-
- @Override
- public Class<?> getClass(String name) throws ClassNotFoundException {
- checkAndLoad();
- return config.getClass(name);
- }
-
- @Override
- public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException {
- checkAndLoad();
- return config.getClass(name, defaultValue);
- }
-
- @Override
- public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
- checkAndLoad();
- return config.getClass(name);
- }
-
- @Override
- public <T> T getInstance(String name) throws ClassNotFoundException {
- checkAndLoad();
- return config.getInstance(name);
- }
-
- @Override
- public <T> T getInstance(ConfigKey name) throws ClassNotFoundException {
- checkAndLoad();
- return config.getInstance(name);
- }
-
- @Override
- public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException {
- checkAndLoad();
- return config.getInstance(name, xface);
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/Config.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/Config.java b/contrib/haox-config/src/main/java/org/haox/config/Config.java
deleted file mode 100644
index 4838527..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/Config.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package org.haox.config;
-
-import java.util.List;
-import java.util.Set;
-
-public interface Config {
- public String getResource();
- public Set<String> getNames();
-
- public String getString(String name);
- public String getString(ConfigKey name);
- public String getString(String name, String defaultValue);
- public String getTrimmed(String name);
- public String getTrimmed(ConfigKey name);
- public Boolean getBoolean(String name);
- public Boolean getBoolean(ConfigKey name);
- public Boolean getBoolean(String name, boolean defaultValue);
- public Integer getInt(String name);
- public Integer getInt(ConfigKey name);
- public Integer getInt(String name, int defaultValue);
- public Long getLong(String name);
- public Long getLong(ConfigKey name);
- public Long getLong(String name, long defaultValue);
- public Float getFloat(String name);
- public Float getFloat(ConfigKey name);
- public Float getFloat(String name, float defaultValue);
- public List<String> getList(String name);
- public List<String> getList(String name, String[] defaultValue);
- public List<String> getList(ConfigKey name);
- public Config getConfig(String name);
- public Config getConfig(ConfigKey name);
-
- public Class<?> getClass(String name) throws ClassNotFoundException;
- public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException;
- public Class<?> getClass(ConfigKey name) throws ClassNotFoundException;
- public <T> T getInstance(String name) throws ClassNotFoundException;
- public <T> T getInstance(ConfigKey name) throws ClassNotFoundException;
- public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException;
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/ConfigImpl.java b/contrib/haox-config/src/main/java/org/haox/config/ConfigImpl.java
deleted file mode 100644
index 818a063..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/ConfigImpl.java
+++ /dev/null
@@ -1,325 +0,0 @@
-package org.haox.config;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.*;
-
-public class ConfigImpl implements Config {
- private static final Logger logger = LoggerFactory.getLogger(Config.class);
-
- private String resource;
- private Map<String, ConfigObject> properties;
- private List<Config> subConfigs;
-
- private Set<String> propNames;
-
- protected ConfigImpl(String resource) {
- this.resource = resource;
- this.properties = new HashMap<String, ConfigObject>();
- this.subConfigs = new ArrayList<Config>(0);
- }
-
- protected void reset() {
- this.properties.clear();
- this.subConfigs.clear();
- }
-
- @Override
- public String getResource() {
- return resource;
- }
-
- @Override
- public Set<String> getNames() {
- reloadNames();
- return propNames;
- }
-
- @Override
- public String getString(String name) {
- String result = null;
-
- ConfigObject co = properties.get(name);
- if (co != null) {
- result = co.getPropertyValue();
- }
-
- if (result == null) {
- for (Config sub : subConfigs) {
- result = sub.getString(name);
- if (result != null) break;
- }
- }
-
- return result;
- }
-
- @Override
- public String getString(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getString(name.getPropertyKey(), (String) name.getDefaultValue());
- }
- return getString(name.getPropertyKey());
- }
-
- @Override
- public String getString(String name, String defaultValue) {
- String result = getString(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public String getTrimmed(String name) {
- String result = getString(name);
- if (null != result) {
- result = result.trim();
- }
- return result;
- }
-
- @Override
- public String getTrimmed(ConfigKey name) {
- return getTrimmed(name.getPropertyKey());
- }
-
- @Override
- public Integer getInt(String name) {
- Integer result = null;
- String value = getTrimmed(name);
- if (value != null) {
- result = Integer.valueOf(value);
- }
- return result;
- }
-
- @Override
- public Integer getInt(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getInt(name.getPropertyKey(), (Integer) name.getDefaultValue());
- }
- return getInt(name.getPropertyKey());
- }
-
- @Override
- public Integer getInt(String name, int defaultValue) {
- Integer result = getInt(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public Long getLong(String name) {
- Long result = null;
- String value = getTrimmed(name);
- if (value != null) {
- result = Long.valueOf(value);
- }
- return result;
- }
-
- @Override
- public Long getLong(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getLong(name.getPropertyKey(), (Long) name.getDefaultValue());
- }
- return getLong(name.getPropertyKey());
- }
-
- @Override
- public Long getLong(String name, long defaultValue) {
- Long result = getLong(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public Float getFloat(String name) {
- Float result = null;
- String value = getTrimmed(name);
- if (value != null) {
- result = Float.valueOf(value);
- }
- return result;
- }
-
- @Override
- public Float getFloat(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getFloat(name.getPropertyKey(), (Float) name.getDefaultValue());
- }
- return getFloat(name.getPropertyKey());
- }
-
- @Override
- public Float getFloat(String name, float defaultValue) {
- Float result = getFloat(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public Boolean getBoolean(String name) {
- Boolean result = null;
- String value = getTrimmed(name);
- if (value != null) {
- result = Boolean.valueOf(value);
- }
- return result;
- }
-
- @Override
- public Boolean getBoolean(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getBoolean(name.getPropertyKey(), (Boolean) name.getDefaultValue());
- }
- return getBoolean(name.getPropertyKey());
- }
-
- @Override
- public Boolean getBoolean(String name, boolean defaultValue) {
- Boolean result = getBoolean(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public List<String> getList(String name) {
- List<String> results = null;
- ConfigObject co = properties.get(name);
- if (co != null) {
- results = co.getListValues();
- }
- return results;
- }
-
- @Override
- public List<String> getList(String name, String[] defaultValue) {
- List<String> results = getList(name);
- if (results == null) {
- results = Arrays.asList(defaultValue);
- }
- return results;
- }
-
- @Override
- public List<String> getList(ConfigKey name) {
- if (name.getDefaultValue() != null) {
- return getList(name.getPropertyKey(), (String[]) name.getDefaultValue());
- }
- return getList(name.getPropertyKey());
- }
-
- @Override
- public Config getConfig(String name) {
- Config result = null;
- ConfigObject co = properties.get(name);
- if (co != null) {
- result = co.getConfigValue();
- }
- return result;
- }
-
- @Override
- public Config getConfig(ConfigKey name) {
- return getConfig(name.getPropertyKey());
- }
-
- @Override
- public Class<?> getClass(String name) throws ClassNotFoundException {
- Class<?> result = null;
-
- String valueString = getString(name);
- if (valueString != null) {
- Class<?> cls = Class.forName(name);
- result = cls;
- }
-
- return result;
- }
-
- @Override
- public Class<?> getClass(String name, Class<?> defaultValue) throws ClassNotFoundException {
- Class<?> result = getClass(name);
- if (result == null) {
- result = defaultValue;
- }
- return result;
- }
-
- @Override
- public Class<?> getClass(ConfigKey name) throws ClassNotFoundException {
- if (name.getDefaultValue() != null) {
- return getClass(name.getPropertyKey(), (Class<?>) name.getDefaultValue());
- }
- return getClass(name.getPropertyKey());
- }
-
- @Override
- public <T> T getInstance(String name) throws ClassNotFoundException {
- return getInstance(name, null);
- }
-
- @Override
- public <T> T getInstance(ConfigKey name) throws ClassNotFoundException {
- return getInstance(name.getPropertyKey());
- }
-
- @Override
- public <T> T getInstance(String name, Class<T> xface) throws ClassNotFoundException {
- T result = null;
-
- Class<?> cls = getClass(name, null);
- if (xface != null && !xface.isAssignableFrom(cls)) {
- throw new RuntimeException(cls + " does not implement " + xface);
- }
- try {
- result = (T) cls.newInstance();
- } catch (Exception e) {
- throw new RuntimeException("Failed to create instance with class " + cls.getName());
- }
-
- return result;
- }
-
- protected void set(String name, String value) {
- ConfigObject co = new ConfigObject(value);
- set(name, co);
- }
-
- protected void set(String name, Config value) {
- ConfigObject co = new ConfigObject(value);
- set(name, co);
-
- addSubConfig(value);
- }
-
- protected void set(String name, ConfigObject value) {
- this.properties.put(name, value);
- }
-
- private void addSubConfig(Config config) {
- this.subConfigs.add(config);
- }
-
- private void reloadNames() {
- if (propNames != null) {
- propNames.clear();
- }
- propNames = new HashSet<String>(properties.keySet());
- for (Config sub : subConfigs) {
- propNames.addAll(sub.getNames());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/ConfigKey.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/ConfigKey.java b/contrib/haox-config/src/main/java/org/haox/config/ConfigKey.java
deleted file mode 100644
index 6a237f8..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/ConfigKey.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package org.haox.config;
-
-public interface ConfigKey {
- public String getPropertyKey();
- public Object getDefaultValue();
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/ConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/ConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/ConfigLoader.java
deleted file mode 100644
index a5ac18a..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/ConfigLoader.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package org.haox.config;
-
-public abstract class ConfigLoader {
- private Resource resource;
- private ConfigImpl config;
-
- protected void setResource(Resource resource) {
- this.resource = resource;
- }
-
- protected void setConfig(ConfigImpl config) {
- this.config = config;
- }
-
- public Config load() {
- if (config == null) {
- config = new ConfigImpl(resource.getName());
- }
- config.reset();
-
- try {
- loadConfig(config, resource);
- } catch (Exception e) {
- throw new RuntimeException("Failed to load org.haox.config", e);
- }
-
- return this.config;
- }
-
- protected abstract void loadConfig(ConfigImpl config, Resource resource) throws Exception;
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/ConfigObject.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/ConfigObject.java b/contrib/haox-config/src/main/java/org/haox/config/ConfigObject.java
deleted file mode 100644
index 1a7201e..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/ConfigObject.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package org.haox.config;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class ConfigObject {
- protected static enum VALUE_TYPE { PROPERTY, LIST, CONFIG };
-
- private VALUE_TYPE valueType;
- private Object value;
-
- public ConfigObject(String value) {
- this.value = value;
- this.valueType = VALUE_TYPE.PROPERTY;
- }
-
- public ConfigObject(String[] values) {
- List<String> valuesList = new ArrayList<String>();
- for (String v : values) {
- valuesList.add(v);
- }
-
- this.value = valuesList;
- this.valueType = VALUE_TYPE.LIST;
- }
-
- public ConfigObject(List<String> values) {
- this.value = new ArrayList<String>(values);
- this.valueType = VALUE_TYPE.LIST;
- }
-
- public ConfigObject(Config value) {
- this.value = value;
- this.valueType = VALUE_TYPE.CONFIG;
- }
-
- public String getPropertyValue() {
- String result = null;
- if (valueType == VALUE_TYPE.PROPERTY) {
- result = (String) value;
- }
- return result;
- }
-
- public List<String> getListValues() {
- List<String> results = null;
- if (valueType == VALUE_TYPE.LIST) {
- results = (List<String>) value;
- }
-
- return results;
- }
-
- public Config getConfigValue() {
- Config result = null;
- if (valueType == VALUE_TYPE.CONFIG) {
- result = (Config) value;
- }
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/IniConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/IniConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/IniConfigLoader.java
deleted file mode 100644
index 8f2e689..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/IniConfigLoader.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.haox.config;
-
-public class IniConfigLoader extends ConfigLoader {
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) {
-
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/JsonConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/JsonConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/JsonConfigLoader.java
deleted file mode 100644
index 79e93c2..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/JsonConfigLoader.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.haox.config;
-
-public class JsonConfigLoader extends ConfigLoader {
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) {
-
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/MapConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/MapConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/MapConfigLoader.java
deleted file mode 100644
index 65cbf6e..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/MapConfigLoader.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package org.haox.config;
-
-import java.util.Map;
-
-public class MapConfigLoader extends ConfigLoader {
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) {
- Map<String, String> mapConfig = (Map<String, String>) resource.getResource();
- String value;
- for (String key : mapConfig.keySet()) {
- value = mapConfig.get(key);
- config.set(key, value);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/PropertiesConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/PropertiesConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/PropertiesConfigLoader.java
deleted file mode 100644
index a93764f..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/PropertiesConfigLoader.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package org.haox.config;
-
-import java.util.Properties;
-
-public class PropertiesConfigLoader extends ConfigLoader {
-
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
- Properties propConfig = (Properties) resource.getResource();
- loadConfig(config, propConfig);
- }
-
- protected void loadConfig(ConfigImpl config, Properties propConfig) {
- Object value;
- for (Object key : propConfig.keySet()) {
- if (key instanceof String) {
- value = propConfig.getProperty((String) key);
- if (value != null && value instanceof String) {
- config.set((String) key, (String) value);
- }
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/PropertiesFileConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/PropertiesFileConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/PropertiesFileConfigLoader.java
deleted file mode 100644
index 9a49090..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/PropertiesFileConfigLoader.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.haox.config;
-
-import java.io.InputStream;
-import java.util.Properties;
-
-public class PropertiesFileConfigLoader extends PropertiesConfigLoader {
-
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
- Properties propConfig = new Properties();
- propConfig.load((InputStream) resource.getResource());
- loadConfig(config, propConfig);
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/Resource.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/Resource.java b/contrib/haox-config/src/main/java/org/haox/config/Resource.java
deleted file mode 100644
index a9d158a..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/Resource.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package org.haox.config;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.net.URL;
-import java.util.Map;
-import java.util.Properties;
-
-public class Resource {
- public static enum Format {
- XML_FILE(XmlConfigLoader.class),
- INI_FILE(IniConfigLoader.class),
- JSON_FILE(JsonConfigLoader.class),
- PROPERTIES_FILE(PropertiesFileConfigLoader.class),
- MAP(MapConfigLoader.class),
- PROPERTIES(PropertiesConfigLoader.class);
-
- private Class<? extends ConfigLoader> loaderClass;
-
- private Format(Class<? extends ConfigLoader> loaderClass) {
- this.loaderClass = loaderClass;
- }
-
- public Class<? extends ConfigLoader> getLoaderClass() {
- return loaderClass;
- }
- }
-
- private String name;
- private Object resource;
- private Format format;
-
- public static Resource createXmlResource(File xmlFile) throws IOException {
- return new Resource(xmlFile.getName(), xmlFile, Format.XML_FILE);
- }
-
- public static Resource createIniResource(File iniFile) throws IOException {
- return new Resource(iniFile.getName(), iniFile, Format.INI_FILE);
- }
-
- public static Resource createJsonResource(File jsonFile) throws IOException {
- return new Resource(jsonFile.getName(), jsonFile, Format.JSON_FILE);
- }
-
- public static Resource createXmlResource(URL xmlUrl) throws IOException {
- return new Resource(xmlUrl, Format.XML_FILE);
- }
-
- public static Resource createIniResource(URL iniUrl) throws IOException {
- return new Resource(iniUrl, Format.INI_FILE);
- }
-
- public static Resource createJsonResource(URL jsonUrl) throws IOException {
- return new Resource(jsonUrl, Format.JSON_FILE);
- }
-
- public static Resource createMapResource(Map<String,String> mapConfig) {
- return new Resource("mapConfig", mapConfig, Format.MAP);
- }
-
- public static Resource createPropertiesFileResource(File propFile) throws IOException {
- return new Resource(propFile.getName(), propFile, Format.PROPERTIES_FILE);
- }
-
- public static Resource createPropertiesResource(Properties propertiesConfig) {
- return new Resource("propConfig", propertiesConfig, Format.PROPERTIES);
- }
-
- private Resource(String name, File resourceFile, Format format) throws FileNotFoundException {
- this(name, new FileInputStream(resourceFile), format);
- }
-
- private Resource(URL resourceUrl, Format format) throws IOException {
- this(resourceUrl.toString(), resourceUrl.openStream(), format);
- }
-
- private Resource(String name, Object resourceStream, Format format) {
- this.name = name;
- this.resource = resourceStream;
- this.format = format;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public Object getResource() {
- return resource;
- }
-
- public Format getFormat() {
- return format;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/main/java/org/haox/config/XmlConfigLoader.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/haox/config/XmlConfigLoader.java b/contrib/haox-config/src/main/java/org/haox/config/XmlConfigLoader.java
deleted file mode 100644
index 10f3a36..0000000
--- a/contrib/haox-config/src/main/java/org/haox/config/XmlConfigLoader.java
+++ /dev/null
@@ -1,140 +0,0 @@
-package org.haox.config;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.w3c.dom.*;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-public class XmlConfigLoader extends ConfigLoader {
- private static final Logger logger = LoggerFactory.getLogger(Config.class);
-
- @Override
- protected void loadConfig(ConfigImpl config, Resource resource) throws Exception {
- Element doc = loadResourceDocument(resource);
- loadConfig((ConfigImpl) config, doc);
- }
-
- private Element loadResourceDocument(Resource resource) throws Exception {
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
-
- docBuilderFactory.setIgnoringComments(true);
- docBuilderFactory.setNamespaceAware(true);
- try {
- docBuilderFactory.setXIncludeAware(true);
- } catch (UnsupportedOperationException e) {
- logger.error("Failed to set setXIncludeAware(true) for parser", e);
- }
- DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
- InputStream is = (InputStream) resource.getResource();
- Document doc = null;
- try {
- doc = builder.parse(is);
- } finally {
- is.close();
- }
-
- Element root = doc.getDocumentElement();
- validateConfig(root);
-
- return root;
- }
-
- private boolean validateConfig(Element root) {
- boolean valid = false;
-
- if ("config".equals(root.getTagName())) {
- valid = true;
- } else {
- logger.error("bad conf element: top-level element not <configuration>");
- }
-
- return valid;
- }
-
- private void loadConfig(ConfigImpl conifg, Element element) {
- String name;
- ConfigObject value;
-
- NodeList props = element.getChildNodes();
- for (int i = 0; i < props.getLength(); i++) {
- Node subNode = props.item(i);
- if (!(subNode instanceof Element)) {
- continue;
- }
-
- Element prop = (Element)subNode;
- name = getElementName(prop);
- if (name == null) {
- continue;
- }
-
- value = null;
- String tagName = prop.getTagName();
- if ("property".equals(tagName) && prop.hasChildNodes()) {
- value = loadProperty(prop);
- } else if ("config".equals(tagName) && prop.hasChildNodes()) {
- ConfigImpl cfg = new ConfigImpl(name);
- loadConfig(cfg, prop);
- value = new ConfigObject(cfg);
- }
-
- if (name != null) {
- conifg.set(name, value);
- }
- }
- }
-
- private static ConfigObject loadProperty(Element ele) {
- String value = null;
- if (ele.getFirstChild() instanceof Text) {
- value = ((Text)ele.getFirstChild()).getData();
- return new ConfigObject(value);
- }
-
- ConfigObject result = null;
- NodeList nodes = ele.getChildNodes();
- List<String> values = new ArrayList<String>(nodes.getLength());
- for (int i = 0; i < nodes.getLength(); i++) {
- value = null;
- Node valueNode = nodes.item(i);
- if (!(valueNode instanceof Element))
- continue;
-
- Element valueEle = (Element)valueNode;
- if ("value".equals(valueEle.getTagName()) && valueEle.hasChildNodes()) {
- value = ((Text)valueEle.getFirstChild()).getData();
- }
-
- if (value != null) {
- values.add(value);
- }
- }
- return new ConfigObject(values);
- }
-
- private static String getElementName(Element ele) {
- String name, value;
- Node node;
- Attr attr;
-
- NamedNodeMap nnm = ele.getAttributes();
- for (int i = 0; i < nnm.getLength(); ++i) {
- node = nnm.item(i);
- if (!(node instanceof Attr))
- continue;
- attr = (Attr) node;
- name = attr.getName();
- value = attr.getValue();
-
- if ("name".equals(name)) {
- return value;
- }
- }
- return null;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
new file mode 100644
index 0000000..677f1b1
--- /dev/null
+++ b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
@@ -0,0 +1,84 @@
+package org.apache.haox.config;
+
+import junit.framework.Assert;
+import org.apache.haox.config.Conf;
+import org.apache.haox.config.ConfigKey;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+public class ConfTest {
+
+ @Test
+ public void testMapConfig() {
+ String strProp = "hello";
+ Integer intProp = 123456;
+ Boolean boolProp = true;
+ Map<String, String> mapConfig = new HashMap<String, String>();
+ mapConfig.put("strProp", strProp);
+ mapConfig.put("intProp", String.valueOf(intProp));
+ mapConfig.put("boolProp", String.valueOf(boolProp));
+
+ Conf conf = new Conf();
+ conf.addMapConfig(mapConfig);
+ Assert.assertEquals(conf.getString("strProp"), strProp);
+ Assert.assertEquals(conf.getInt("intProp"), intProp);
+ Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
+ }
+
+ @Test
+ public void testPropertiesConfig() {
+ String strProp = "hello";
+ Integer intProp = 123456;
+ Boolean boolProp = true;
+ Properties properties = new Properties();
+ properties.setProperty("strProp", strProp);
+ properties.setProperty("intProp", String.valueOf(intProp));
+ properties.setProperty("boolProp", String.valueOf(boolProp));
+
+ Conf conf = new Conf();
+ conf.addPropertiesConfig(properties);
+ Assert.assertEquals(conf.getString("strProp"), strProp);
+ Assert.assertEquals(conf.getInt("intProp"), intProp);
+ Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
+ }
+
+ static enum TestConfKey implements ConfigKey {
+ ADDRESS("127.0.0.1"),
+ PORT(8015),
+ ENABLE(false);
+
+ private Object defaultValue;
+ private TestConfKey(Object defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
+ @Override
+ public String getPropertyKey() {
+ return name().toLowerCase();
+ }
+
+ @Override
+ public Object getDefaultValue() {
+ return this.defaultValue;
+ }
+ }
+
+ @Test
+ public void testConfKey() {
+ Conf conf = new Conf();
+ Assert.assertEquals(conf.getString(TestConfKey.ADDRESS),
+ TestConfKey.ADDRESS.getDefaultValue());
+ Map<String, String> mapConfig = new HashMap<String, String>();
+ String myAddress = "www.google.com";
+ mapConfig.put(TestConfKey.ADDRESS.getPropertyKey(), myAddress);
+ conf.addMapConfig(mapConfig);
+ Assert.assertEquals(conf.getString(TestConfKey.ADDRESS), myAddress);
+ Assert.assertEquals(conf.getInt(TestConfKey.PORT),
+ TestConfKey.PORT.getDefaultValue());
+ Assert.assertEquals(conf.getBoolean(TestConfKey.ENABLE),
+ TestConfKey.ENABLE.getDefaultValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-config/src/test/java/org/haox/config/ConfTest.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/test/java/org/haox/config/ConfTest.java b/contrib/haox-config/src/test/java/org/haox/config/ConfTest.java
deleted file mode 100644
index 6deedb1..0000000
--- a/contrib/haox-config/src/test/java/org/haox/config/ConfTest.java
+++ /dev/null
@@ -1,82 +0,0 @@
-package org.haox.config;
-
-import junit.framework.Assert;
-import org.junit.Test;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-public class ConfTest {
-
- @Test
- public void testMapConfig() {
- String strProp = "hello";
- Integer intProp = 123456;
- Boolean boolProp = true;
- Map<String, String> mapConfig = new HashMap<String, String>();
- mapConfig.put("strProp", strProp);
- mapConfig.put("intProp", String.valueOf(intProp));
- mapConfig.put("boolProp", String.valueOf(boolProp));
-
- Conf conf = new Conf();
- conf.addMapConfig(mapConfig);
- Assert.assertEquals(conf.getString("strProp"), strProp);
- Assert.assertEquals(conf.getInt("intProp"), intProp);
- Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
- }
-
- @Test
- public void testPropertiesConfig() {
- String strProp = "hello";
- Integer intProp = 123456;
- Boolean boolProp = true;
- Properties properties = new Properties();
- properties.setProperty("strProp", strProp);
- properties.setProperty("intProp", String.valueOf(intProp));
- properties.setProperty("boolProp", String.valueOf(boolProp));
-
- Conf conf = new Conf();
- conf.addPropertiesConfig(properties);
- Assert.assertEquals(conf.getString("strProp"), strProp);
- Assert.assertEquals(conf.getInt("intProp"), intProp);
- Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
- }
-
- static enum TestConfKey implements ConfigKey {
- ADDRESS("127.0.0.1"),
- PORT(8015),
- ENABLE(false);
-
- private Object defaultValue;
- private TestConfKey(Object defaultValue) {
- this.defaultValue = defaultValue;
- }
-
- @Override
- public String getPropertyKey() {
- return name().toLowerCase();
- }
-
- @Override
- public Object getDefaultValue() {
- return this.defaultValue;
- }
- }
-
- @Test
- public void testConfKey() {
- Conf conf = new Conf();
- Assert.assertEquals(conf.getString(TestConfKey.ADDRESS),
- TestConfKey.ADDRESS.getDefaultValue());
- Map<String, String> mapConfig = new HashMap<String, String>();
- String myAddress = "www.google.com";
- mapConfig.put(TestConfKey.ADDRESS.getPropertyKey(), myAddress);
- conf.addMapConfig(mapConfig);
- Assert.assertEquals(conf.getString(TestConfKey.ADDRESS), myAddress);
- Assert.assertEquals(conf.getInt(TestConfKey.PORT),
- TestConfKey.PORT.getDefaultValue());
- Assert.assertEquals(conf.getBoolean(TestConfKey.ENABLE),
- TestConfKey.ENABLE.getDefaultValue());
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/README
----------------------------------------------------------------------
diff --git a/contrib/haox-event/README b/contrib/haox-event/README
index 48c3752..cb3b88a 100644
--- a/contrib/haox-event/README
+++ b/contrib/haox-event/README
@@ -1 +1 @@
-An event driven application framework with network (TCP, UDP) supported.
\ No newline at end of file
+An event driven application framework with mixed (TCP, UDP) x (connector, acceptor) supported.
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractEventHandler.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractEventHandler.java b/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractEventHandler.java
new file mode 100644
index 0000000..ad84421
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractEventHandler.java
@@ -0,0 +1,36 @@
+package org.apache.haox.event;
+
+public abstract class AbstractEventHandler implements EventHandler {
+
+ private Dispatcher dispatcher;
+
+ public AbstractEventHandler() {
+
+ }
+
+ protected void dispatch(Event event) {
+ dispatcher.dispatch(event);
+ }
+
+ @Override
+ public Dispatcher getDispatcher() {
+ return dispatcher;
+ }
+
+ @Override
+ public void setDispatcher(Dispatcher dispatcher) {
+ this.dispatcher = dispatcher;
+ }
+
+ @Override
+ public void handle(Event event) {
+ try {
+ doHandle(event);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected abstract void doHandle(Event event) throws Exception;
+}
+
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractInternalEventHandler.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractInternalEventHandler.java b/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractInternalEventHandler.java
new file mode 100644
index 0000000..8ecfaaf
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/AbstractInternalEventHandler.java
@@ -0,0 +1,47 @@
+package org.apache.haox.event;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public abstract class AbstractInternalEventHandler extends AbstractEventHandler
+ implements InternalEventHandler {
+
+ private int id = -1;
+ protected EventHandler handler;
+
+ private static AtomicInteger idGen = new AtomicInteger(1);
+
+ public AbstractInternalEventHandler() {
+ super();
+
+ this.id = idGen.getAndIncrement();
+
+ init();
+ }
+
+ public AbstractInternalEventHandler(EventHandler handler) {
+ this();
+
+ this.handler = handler;
+ }
+
+ protected void setEventHandler(EventHandler handler) {
+ this.handler = handler;
+ }
+
+ @Override
+ public int id() {
+ return id;
+ }
+
+ public abstract void init();
+
+ protected void process(Event event) {
+ handler.handle(event);
+ }
+
+ @Override
+ public EventType[] getInterestedEvents() {
+ return handler.getInterestedEvents();
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/BufferedEventHandler.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/BufferedEventHandler.java b/contrib/haox-event/src/main/java/org/apache/haox/event/BufferedEventHandler.java
new file mode 100644
index 0000000..3b31e8c
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/BufferedEventHandler.java
@@ -0,0 +1,34 @@
+package org.apache.haox.event;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+
+/**
+ * An EventHandler wrapper buffering events and processing them later
+ */
+public abstract class BufferedEventHandler extends AbstractInternalEventHandler {
+
+ protected BlockingQueue<Event> eventQueue;
+
+ public BufferedEventHandler(EventHandler handler) {
+ super(handler);
+ }
+
+ public BufferedEventHandler() {
+ super();
+ }
+
+ @Override
+ public void init() {
+ this.eventQueue = new ArrayBlockingQueue<Event>(2);
+ }
+
+ @Override
+ protected void doHandle(Event event) throws Exception {
+ try {
+ eventQueue.put(event);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/Dispatcher.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/Dispatcher.java b/contrib/haox-event/src/main/java/org/apache/haox/event/Dispatcher.java
new file mode 100644
index 0000000..14ccdfd
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/Dispatcher.java
@@ -0,0 +1,10 @@
+package org.apache.haox.event;
+
+public interface Dispatcher {
+
+ public void dispatch(Event event);
+
+ public void register(EventHandler handler);
+
+ public void register(InternalEventHandler internalHandler);
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/Event.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/Event.java b/contrib/haox-event/src/main/java/org/apache/haox/event/Event.java
new file mode 100644
index 0000000..83c6d7a
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/Event.java
@@ -0,0 +1,24 @@
+package org.apache.haox.event;
+
+public class Event {
+
+ private EventType eventType;
+ private Object eventData;
+
+ public Event(EventType eventType) {
+ this.eventType = eventType;
+ }
+
+ public Event(EventType eventType, Object eventData) {
+ this.eventType = eventType;
+ this.eventData = eventData;
+ }
+
+ public EventType getEventType() {
+ return eventType;
+ }
+
+ public Object getEventData() {
+ return eventData;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/5a980a4d/contrib/haox-event/src/main/java/org/apache/haox/event/EventHandler.java
----------------------------------------------------------------------
diff --git a/contrib/haox-event/src/main/java/org/apache/haox/event/EventHandler.java b/contrib/haox-event/src/main/java/org/apache/haox/event/EventHandler.java
new file mode 100644
index 0000000..6c31dd0
--- /dev/null
+++ b/contrib/haox-event/src/main/java/org/apache/haox/event/EventHandler.java
@@ -0,0 +1,12 @@
+package org.apache.haox.event;
+
+public interface EventHandler {
+
+ public void handle(Event event);
+
+ public EventType[] getInterestedEvents();
+
+ public Dispatcher getDispatcher();
+
+ public void setDispatcher(Dispatcher dispatcher);
+}