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/10 14:30:59 UTC
[15/42] directory-kerberos git commit: Initially import Haox codebase
(https://github.com/drankye/haox)
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Type.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Type.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Type.java
new file mode 100644
index 0000000..a4e89c8
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Type.java
@@ -0,0 +1,22 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.EncodingOption;
+import org.apache.haox.asn1.TaggingOption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public interface Asn1Type {
+ public int tagFlags();
+ public int tagNo();
+ public void setEncodingOption(EncodingOption encodingOption);
+ public int encodingLength();
+ public byte[] encode();
+ public void encode(ByteBuffer buffer);
+ public void decode(byte[] content) throws IOException;
+ public void decode(ByteBuffer content) throws IOException;
+ public byte[] taggedEncode(TaggingOption taggingOption);
+ public void taggedEncode(ByteBuffer buffer, TaggingOption taggingOption);
+ public void taggedDecode(ByteBuffer content, TaggingOption taggingOption) throws IOException;
+ public void taggedDecode(byte[] content, TaggingOption taggingOption) throws IOException;
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UniversalString.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UniversalString.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UniversalString.java
new file mode 100644
index 0000000..2297cfc
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UniversalString.java
@@ -0,0 +1,14 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.UniversalTag;
+
+public class Asn1UniversalString extends Asn1String
+{
+ public Asn1UniversalString() {
+ this(null);
+ }
+
+ public Asn1UniversalString(String value) {
+ super(UniversalTag.UNIVERSAL_STRING, value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UtcTime.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UtcTime.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UtcTime.java
new file mode 100644
index 0000000..44cbc26
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1UtcTime.java
@@ -0,0 +1,70 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.UniversalTag;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.SimpleTimeZone;
+
+public class Asn1UtcTime extends Asn1Simple<Date>
+{
+ public Asn1UtcTime() {
+ this(null);
+ }
+
+ public Asn1UtcTime(long time) {
+ super(UniversalTag.UTC_TIME, new Date(time * 1000L));
+ }
+
+ public Asn1UtcTime(Date date) {
+ super(UniversalTag.UTC_TIME, date);
+ }
+
+ protected void toValue() throws IOException {
+ String dateStr = new String(getBytes(), StandardCharsets.US_ASCII);
+ String fixedDateStr = dateStr;
+
+ /*
+ * Make sure fixed date str be of the complete pattern 'YYMMDDhhmmss+/-hhmm'
+ */
+ int strLen = fixedDateStr.length();
+ if (strLen == 6) { // YYMMDD
+ fixedDateStr += "000000+0000";
+ } else if (strLen == 7) { // YYMMDDZ
+ fixedDateStr = fixedDateStr.replace("Z", "000000+0000");
+ } else if (strLen == 10) { // YYMMDDhhmm
+ fixedDateStr += "00+0000";
+ } else if (strLen == 11) { // YYMMDDhhmmZ
+ fixedDateStr = fixedDateStr.replace("Z", "00+0000");
+ } else if (strLen == 12) { // YYMMDDhhmmss
+ fixedDateStr += "+0000";
+ } else if (strLen == 13) { // YYMMDDhhmmZ
+ fixedDateStr = fixedDateStr.replace("Z", "+0000");
+ } else if (strLen != 17) {
+ throw new IllegalArgumentException("Bad utc time string " + dateStr);
+ }
+
+ SimpleDateFormat sdf;
+ sdf = new SimpleDateFormat("yyMMddHHmmssZ");
+ sdf.setTimeZone(new SimpleTimeZone(0, "Z"));
+ try {
+ setValue(sdf.parse(fixedDateStr));
+ } catch (ParseException e) {
+ throw new IOException("Failed to parse " + dateStr + " as utc time", e);
+ }
+ }
+
+ @Override
+ protected void toBytes() {
+ Date date = getValue();
+ SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss'Z'");
+ sdf.setTimeZone(new SimpleTimeZone(0, "Z"));
+
+ String str = sdf.format(date);
+ byte[] bytes = str.getBytes(StandardCharsets.US_ASCII);
+ setBytes(bytes);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Utf8String.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Utf8String.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Utf8String.java
new file mode 100644
index 0000000..06d19e8
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1Utf8String.java
@@ -0,0 +1,28 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.UniversalTag;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+public class Asn1Utf8String extends Asn1String
+{
+ public Asn1Utf8String() {
+ this(null);
+ }
+
+ public Asn1Utf8String(String value) {
+ super(UniversalTag.UTF8_STRING, value);
+ }
+
+ @Override
+ protected void toBytes() {
+ byte[] bytes = getValue().getBytes(StandardCharsets.UTF_8);
+ setBytes(bytes);
+ }
+
+ protected void toValue() throws IOException {
+ byte[] bytes = getBytes();
+ setValue(new String(bytes, StandardCharsets.UTF_8));
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1VisibleString.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1VisibleString.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1VisibleString.java
new file mode 100644
index 0000000..d3e043d
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/Asn1VisibleString.java
@@ -0,0 +1,14 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.UniversalTag;
+
+public class Asn1VisibleString extends Asn1String
+{
+ public Asn1VisibleString() {
+ this(null);
+ }
+
+ public Asn1VisibleString(String value) {
+ super(UniversalTag.VISIBLE_STRING, value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingCollection.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingCollection.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingCollection.java
new file mode 100644
index 0000000..1d5d884
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingCollection.java
@@ -0,0 +1,86 @@
+package org.apache.haox.asn1.type;
+
+import org.apache.haox.asn1.EncodingOption;
+import org.apache.haox.asn1.LimitedByteBuffer;
+import org.apache.haox.asn1.TagClass;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+/**
+ * For tagging a collection type with tagNo, either application specific or context specific class
+ */
+public abstract class TaggingCollection extends AbstractAsn1Type<Asn1CollectionType> {
+ private Asn1Tagging<Asn1CollectionType> tagging;
+ private Asn1CollectionType tagged;
+
+ public TaggingCollection(int taggingTagNo, Asn1FieldInfo[] tags, boolean isAppSpecific) {
+ super(isAppSpecific ? TagClass.APPLICATION : TagClass.CONTEXT_SPECIFIC, taggingTagNo);
+ this.tagged = createTaggedCollection(tags);
+ setValue(tagged);
+ this.tagging = new Asn1Tagging<Asn1CollectionType>(taggingTagNo, tagged, isAppSpecific);
+ setEncodingOption(EncodingOption.EXPLICIT);
+ }
+
+ protected abstract Asn1CollectionType createTaggedCollection(Asn1FieldInfo[] tags);
+
+ public void setEncodingOption(EncodingOption encodingOption) {
+ tagging.setEncodingOption(encodingOption);
+ }
+
+ @Override
+ public boolean isConstructed() {
+ return tagging.isConstructed();
+ }
+
+ @Override
+ protected int encodingBodyLength() {
+ return tagging.encodingBodyLength();
+ }
+
+ @Override
+ protected void encodeBody(ByteBuffer buffer) {
+ tagging.encodeBody(buffer);
+ }
+
+ @Override
+ protected void decodeBody(LimitedByteBuffer content) throws IOException {
+ tagging.decodeBody(content);
+ }
+
+ protected <T extends Asn1Type> T getFieldAs(int index, Class<T> t) {
+ return tagged.getFieldAs(index, t);
+ }
+
+ protected void setFieldAs(int index, Asn1Type value) {
+ tagged.setFieldAs(index, value);
+ }
+
+ protected String getFieldAsString(int index) {
+ return tagged.getFieldAsString(index);
+ }
+
+ protected byte[] getFieldAsOctets(int index) {
+ return tagged.getFieldAsOctets(index);
+ }
+
+ protected void setFieldAsOctets(int index, byte[] bytes) {
+ tagged.setFieldAsOctets(index, bytes);
+ }
+
+ protected Integer getFieldAsInteger(int index) {
+ return tagged.getFieldAsInteger(index);
+ }
+
+ protected void setFieldAsInt(int index, int value) {
+ tagged.setFieldAsInt(index, value);
+ }
+
+ protected byte[] getFieldAsOctetBytes(int index) {
+ return tagged.getFieldAsOctets(index);
+ }
+
+ protected void setFieldAsOctetBytes(int index, byte[] value) {
+ tagged.setFieldAsOctets(index, value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSequence.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSequence.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSequence.java
new file mode 100644
index 0000000..53ccaa1
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSequence.java
@@ -0,0 +1,16 @@
+package org.apache.haox.asn1.type;
+
+/**
+ * For tagging a sequence type with tagNo, either application specific or context specific class
+ */
+public class TaggingSequence extends TaggingCollection {
+
+ public TaggingSequence(int taggingTagNo, Asn1FieldInfo[] tags, boolean isAppSpecific) {
+ super(taggingTagNo, tags, isAppSpecific);
+ }
+
+ @Override
+ protected Asn1CollectionType createTaggedCollection(Asn1FieldInfo[] tags) {
+ return new Asn1SequenceType(tags);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSet.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSet.java b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSet.java
new file mode 100644
index 0000000..81bbd16
--- /dev/null
+++ b/contrib/haox-asn1/src/main/java/org/apache/haox/asn1/type/TaggingSet.java
@@ -0,0 +1,16 @@
+package org.apache.haox.asn1.type;
+
+/**
+ * For tagging a sequence type with tagNo, either application specific or context specific class
+ */
+public class TaggingSet extends TaggingCollection {
+
+ public TaggingSet(int taggingTagNo, Asn1FieldInfo[] tags, boolean isAppSpecific) {
+ super(taggingTagNo, tags, isAppSpecific);
+ }
+
+ @Override
+ protected Asn1CollectionType createTaggedCollection(Asn1FieldInfo[] tags) {
+ return new Asn1SetType(tags);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/PersonnelRecord.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/PersonnelRecord.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/PersonnelRecord.java
new file mode 100644
index 0000000..59797bf
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/PersonnelRecord.java
@@ -0,0 +1,190 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.EncodingOption;
+import org.apache.haox.asn1.type.*;
+
+/**
+ * Ref. X.690-0207(http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf),
+ * Annex A, A.1 ASN.1 description of the record structure
+ */
+public class PersonnelRecord extends TaggingSet {
+ private static int NAME = 0;
+ private static int TITLE = 1;
+ private static int NUMBER = 2;
+ private static int DATEOFHIRE= 3;
+ private static int NAMEOFSPOUSE = 4;
+ private static int CHILDREN = 5;
+
+ static Asn1FieldInfo[] fieldInfos = new Asn1FieldInfo[] {
+ new Asn1FieldInfo(NAME, -1, Name.class),
+ new Asn1FieldInfo(TITLE, 0, Asn1VisibleString.class),
+ new Asn1FieldInfo(NUMBER, -1, EmployeeNumber.class),
+ new Asn1FieldInfo(DATEOFHIRE, 1, Date.class),
+ new Asn1FieldInfo(NAMEOFSPOUSE, 2, Name.class),
+ new Asn1FieldInfo(CHILDREN, 3, Children.class, true)
+ };
+
+ public PersonnelRecord() {
+ super(0, fieldInfos, true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+
+ public void setName(Name name) {
+ setFieldAs(NAME, name);
+ }
+
+ public Name getName() {
+ return getFieldAs(NAME, Name.class);
+ }
+
+ public void setTitle(String title) {
+ setFieldAs(TITLE, new Asn1VisibleString(title));
+ }
+
+ public String getTitle() {
+ return getFieldAsString(TITLE);
+ }
+
+ public void setEmployeeNumber(EmployeeNumber employeeNumber) {
+ setFieldAs(NUMBER, employeeNumber);
+ }
+
+ public EmployeeNumber getEmployeeNumber() {
+ return getFieldAs(NUMBER, EmployeeNumber.class);
+ }
+
+ public void setDateOfHire(Date dateOfHire) {
+ setFieldAs(DATEOFHIRE, dateOfHire);
+ }
+
+ public Date getDateOfHire() {
+ return getFieldAs(DATEOFHIRE, Date.class);
+ }
+
+ public void setNameOfSpouse(Name spouse) {
+ setFieldAs(NAMEOFSPOUSE, spouse);
+ }
+
+ public Name getNameOfSpouse() {
+ return getFieldAs(NAMEOFSPOUSE, Name.class);
+ }
+
+ public void setChildren(Children children) {
+ setFieldAs(CHILDREN, children);
+ }
+
+ public Children getChildren() {
+ return getFieldAs(CHILDREN, Children.class);
+ }
+
+ public static class Children extends Asn1SequenceOf<ChildInformation> {
+ public Children(ChildInformation ... children) {
+ super();
+ for (ChildInformation child : children) {
+ addElement(child);
+ }
+ }
+
+ public Children() {
+ super();
+ }
+ }
+
+ public static class ChildInformation extends Asn1SetType {
+ private static int NAME = 0;
+ private static int DATEOFBIRTH = 1;
+
+ static Asn1FieldInfo[] tags = new Asn1FieldInfo[] {
+ new Asn1FieldInfo(NAME, -1, Name.class),
+ new Asn1FieldInfo(DATEOFBIRTH, 0, Date.class)
+ };
+
+ public ChildInformation() {
+ super(tags);
+ }
+
+ public void setName(Name name) {
+ setFieldAs(NAME, name);
+ }
+
+ public Name getName() {
+ return getFieldAs(NAME, Name.class);
+ }
+
+ public void setDateOfBirth(Date date) {
+ setFieldAs(DATEOFBIRTH, date);
+ }
+
+ public Date getDateOfBirth() {
+ return getFieldAs(DATEOFBIRTH, Date.class);
+ }
+ }
+
+ public static class Name extends TaggingSequence {
+ private static int GIVENNAME = 0;
+ private static int INITIAL = 1;
+ private static int FAMILYNAME = 2;
+
+ static Asn1FieldInfo[] tags = new Asn1FieldInfo[] {
+ new Asn1FieldInfo(GIVENNAME, -1, Asn1VisibleString.class),
+ new Asn1FieldInfo(INITIAL, -1, Asn1VisibleString.class),
+ new Asn1FieldInfo(FAMILYNAME, -1, Asn1VisibleString.class)
+ };
+
+ public Name() {
+ super(1, tags, true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+
+ public Name(String givenName, String initial, String familyName) {
+ this();
+ setGivenName(givenName);
+ setInitial(initial);
+ setFamilyName(familyName);
+ }
+
+ public void setGivenName(String givenName) {
+ setFieldAs(GIVENNAME, new Asn1VisibleString(givenName));
+ }
+
+ public String getGivenName() {
+ return getFieldAsString(GIVENNAME);
+ }
+
+ public void setInitial(String initial) {
+ setFieldAs(INITIAL, new Asn1VisibleString(initial));
+ }
+
+ public String getInitial() {
+ return getFieldAsString(INITIAL);
+ }
+
+ public void setFamilyName(String familyName) {
+ setFieldAs(FAMILYNAME, new Asn1VisibleString(familyName));
+ }
+
+ public String getFamilyName() {
+ return getFieldAsString(FAMILYNAME);
+ }
+ }
+
+ public static class EmployeeNumber extends Asn1Tagging<Asn1Integer> {
+ public EmployeeNumber(Integer value) {
+ super(2, new Asn1Integer(value), true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+ public EmployeeNumber() {
+ this(null);
+ }
+ }
+
+ public static class Date extends Asn1Tagging<Asn1VisibleString> {
+ public Date(String value) {
+ super(3, new Asn1VisibleString(value), true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+ public Date() {
+ this(null);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Boolean.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Boolean.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Boolean.java
new file mode 100644
index 0000000..114a59c
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Boolean.java
@@ -0,0 +1,37 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.type.Asn1Boolean;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAsn1Boolean {
+
+ @Test
+ public void testEncoding() {
+ testEncodingWith(true, "0x01 01 FF");
+ testEncodingWith(false, "0x01 01 00");
+ }
+
+ private void testEncodingWith(Boolean value, String expectedEncoding) {
+ byte[] expected = Util.hex2bytes(expectedEncoding);
+ Asn1Boolean aValue = new Asn1Boolean(value);
+ aValue.setEncodingOption(EncodingOption.DER);
+ byte[] encodingBytes = aValue.encode();
+ Assert.assertArrayEquals(expected, encodingBytes);
+ }
+
+ @Test
+ public void testDecoding() throws IOException {
+ testDecodingWith(true, "0x01 01 FF");
+ testDecodingWith(false, "0x01 01 00");
+ }
+
+ private void testDecodingWith(Boolean expectedValue, String content) throws IOException {
+ Asn1Boolean decoded = new Asn1Boolean();
+ decoded.setEncodingOption(EncodingOption.DER);
+ decoded.decode(Util.hex2bytes(content));
+ Assert.assertEquals(expectedValue, decoded.getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Collection.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Collection.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Collection.java
new file mode 100644
index 0000000..7117a76
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Collection.java
@@ -0,0 +1,36 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.type.*;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAsn1Collection {
+ static String TEST_STR = "Jones";
+ static Boolean TEST_BOOL = true;
+ static byte[] EXPECTED_BYTES = new byte[] {(byte) 0x30, (byte) 0x0A,
+ (byte) 0x16, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73,
+ (byte) 0x01, (byte) 0x01, (byte) 0xFF
+ };
+
+ @Test
+ public void testSequenceEncoding() {
+ Asn1Sequence seq = new Asn1Sequence();
+ seq.addItem(new Asn1IA5String(TEST_STR));
+ seq.addItem(new Asn1Boolean(TEST_BOOL));
+
+ Assert.assertArrayEquals(EXPECTED_BYTES, seq.encode());
+ }
+
+ @Test
+ public void testSequenceDecoding() throws IOException {
+ Asn1Sequence seq = new Asn1Sequence();
+ seq.decode(EXPECTED_BYTES);
+ AbstractAsn1Type field = (AbstractAsn1Type) seq.getValue().get(0).getValue();
+ Assert.assertEquals(TEST_STR, field.getValue());
+
+ field = (AbstractAsn1Type) seq.getValue().get(1).getValue();
+ Assert.assertEquals(TEST_BOOL, field.getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Input.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Input.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Input.java
new file mode 100644
index 0000000..fd803cf
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Input.java
@@ -0,0 +1,16 @@
+package org.apache.haox.asn1;
+
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAsn1Input {
+
+ @Test
+ public void testDecoding() throws IOException {
+ //PersonnelRecord expected = TestData.createSamplePersonnel();
+ byte[] data = TestData.createSammplePersonnelEncodingData();
+ //Asn1InputBuffer ib = new Asn1InputBuffer(data);
+ Asn1Dump.dump(data);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Integer.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Integer.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Integer.java
new file mode 100644
index 0000000..2766ea4
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1Integer.java
@@ -0,0 +1,51 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.type.Asn1Integer;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAsn1Integer {
+
+ @Test
+ public void testEncoding() {
+ testEncodingWith(0, "0x02 01 00");
+ testEncodingWith(1, "0x02 01 01");
+ testEncodingWith(2, "0x02 01 02");
+ testEncodingWith(127, "0x02 01 7F");
+ testEncodingWith(128, "0x02 02 00 80");
+ testEncodingWith(-1, "0x02 01 FF");
+ testEncodingWith(-128, "0x02 01 80");
+ testEncodingWith(-32768, "0x02 02 80 00");
+ testEncodingWith(1234567890, "0x02 04 49 96 02 D2");
+ }
+
+ private void testEncodingWith(int value, String expectedEncoding) {
+ byte[] expected = Util.hex2bytes(expectedEncoding);
+ Asn1Integer aValue = new Asn1Integer(value);
+ aValue.setEncodingOption(EncodingOption.DER);
+ byte[] encodingBytes = aValue.encode();
+ Assert.assertArrayEquals(expected, encodingBytes);
+ }
+
+ @Test
+ public void testDecoding() throws IOException {
+ testDecodingWith(0, "0x02 01 00");
+ testDecodingWith(1, "0x02 01 01");
+ testDecodingWith(2, "0x02 01 02");
+ testDecodingWith(127, "0x02 01 7F");
+ testDecodingWith(128, "0x02 02 00 80");
+ testDecodingWith(-1, "0x02 01 FF");
+ testDecodingWith(-128, "0x02 01 80");
+ testDecodingWith(-32768, "0x02 02 80 00");
+ testDecodingWith(1234567890, "0x02 04 49 96 02 D2");
+ }
+
+ private void testDecodingWith(Integer expectedValue, String content) throws IOException {
+ Asn1Integer decoded = new Asn1Integer();
+ decoded.setEncodingOption(EncodingOption.DER);
+ decoded.decode(Util.hex2bytes(content));
+ Assert.assertEquals(expectedValue, decoded.getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1ObjectIdentifier.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1ObjectIdentifier.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1ObjectIdentifier.java
new file mode 100644
index 0000000..8fa3c3b
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1ObjectIdentifier.java
@@ -0,0 +1,45 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.type.Asn1ObjectIdentifier;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAsn1ObjectIdentifier {
+
+ @Test
+ public void testEncoding() throws Exception {
+ /**
+ * Cryptography for Developers -> ASN.1 UTCTIME Type
+ * Applying this to the MD5 OID, we first transform the dotted decimal form into the
+ * array of words.Thus, 1.2.840.113549.2.5 becomes {42, 840, 113549, 2, 5}, and then further
+ * 404_CRYPTO_02.qxd 10/27/06 3:40 PM Page 36split into seven-bit digits with the proper most significant bits as
+ * {{0x2A}, {0x86, 0x48},{0x86, 0xF7, 0x0D}, {0x02}, {0x05}}.Therefore, the full encoding for MD5 is 0x06 08 2A
+ * 86 48 86 F7 0D 02 05.
+ */
+ testEncodingWith("1.2.840.113549.2.5",
+ "0x06 08 2A 86 48 86 F7 0D 02 05");
+ }
+
+ private void testEncodingWith(String oid, String expectedEncoding) {
+ byte[] expected = Util.hex2bytes(expectedEncoding);
+ Asn1ObjectIdentifier aValue = new Asn1ObjectIdentifier(oid);
+ aValue.setEncodingOption(EncodingOption.DER);
+ byte[] encodingBytes = aValue.encode();
+ Assert.assertArrayEquals(expected, encodingBytes);
+ }
+
+ @Test
+ public void testDecoding() throws Exception {
+ testDecodingWith("1.2.840.113549.2.5",
+ "0x06 08 2A 86 48 86 F7 0D 02 05");
+ }
+
+ private void testDecodingWith(String expectedValue, String content) throws IOException {
+ Asn1ObjectIdentifier decoded = new Asn1ObjectIdentifier();
+ decoded.setEncodingOption(EncodingOption.DER);
+ decoded.decode(Util.hex2bytes(content));
+ Assert.assertEquals(expectedValue, decoded.getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1UtcTime.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1UtcTime.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1UtcTime.java
new file mode 100644
index 0000000..3de3b08
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestAsn1UtcTime.java
@@ -0,0 +1,51 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.type.Asn1UtcTime;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.SimpleTimeZone;
+
+public class TestAsn1UtcTime {
+
+ @Test
+ public void testEncoding() throws Exception {
+ /**
+ * Cryptography for Developers -> ASN.1 UTCTIME Type
+ * the encoding of July 4, 2003 at 11:33 and 28 seconds would be
+ “030704113328Z” and be encoded as 0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A.
+ */
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ sdf.setTimeZone(new SimpleTimeZone(0, "Z"));
+ String dateInString = "2003-07-04 11:33:28";
+ Date date = sdf.parse(dateInString);
+ testEncodingWith(date, "0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A");
+ }
+
+ private void testEncodingWith(Date value, String expectedEncoding) {
+ byte[] expected = Util.hex2bytes(expectedEncoding);
+ Asn1UtcTime aValue = new Asn1UtcTime(value);
+ aValue.setEncodingOption(EncodingOption.DER);
+ byte[] encodingBytes = aValue.encode();
+ Assert.assertArrayEquals(expected, encodingBytes);
+ }
+
+ @Test
+ public void testDecoding() throws Exception {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String dateInString = "2003-07-04 11:33:28";
+ sdf.setTimeZone(new SimpleTimeZone(0, "Z"));
+ Date date = sdf.parse(dateInString);
+ testDecodingWith(date, "0x17 0D 30 33 30 37 30 34 31 31 33 33 32 38 5A");
+ }
+
+ private void testDecodingWith(Date expectedValue, String content) throws IOException {
+ Asn1UtcTime decoded = new Asn1UtcTime();
+ decoded.setEncodingOption(EncodingOption.DER);
+ decoded.decode(Util.hex2bytes(content));
+ Assert.assertEquals(expectedValue, decoded.getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestData.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestData.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestData.java
new file mode 100644
index 0000000..e7855b8
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestData.java
@@ -0,0 +1,110 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.PersonnelRecord.*;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+public class TestData {
+
+ public static PersonnelRecord createSamplePersonnel() {
+ PersonnelRecord pr = new PersonnelRecord();
+
+ pr.setName(new Name("John", "P", "Smith"));
+
+ pr.setTitle("Director");
+
+ pr.setEmployeeNumber(new EmployeeNumber(51));
+
+ pr.setDateOfHire(new Date("19710917"));
+
+ pr.setNameOfSpouse(new Name("Mary", "T", "Smith"));
+
+ ChildInformation child1 = new ChildInformation();
+ child1.setName(new Name("Ralph", "T", "Smith"));
+ child1.setDateOfBirth(new Date("19571111"));
+
+ ChildInformation child2 = new ChildInformation();
+ child2.setName(new Name("Susan", "B", "Jones"));
+ child2.setDateOfBirth(new Date("19590717"));
+
+ pr.setChildren(new Children(child1, child2));
+
+ return pr;
+ }
+
+ public static byte[] createSammplePersonnelEncodingData() {
+ class BufferOutput {
+ ByteBuffer buffer;
+
+ void put(byte ... bytes) {
+ buffer.put(bytes);
+ }
+
+ void put(String s) {
+ byte[] bytes = s.getBytes(StandardCharsets.US_ASCII);
+ buffer.put(bytes);
+ }
+
+ public byte[] output() {
+ int len = (int) 0x85 + 3;
+ buffer = ByteBuffer.allocate(len);
+
+ // personnel record
+ put((byte) 0x60, (byte) 0x81, (byte) 0x85);
+
+ // -name
+ put((byte) 0x61, (byte) 0x10);
+ put((byte) 0x1A, (byte) 0x04); put("John");
+ put((byte) 0x1A, (byte) 0x01); put("P");
+ put((byte) 0x1A, (byte) 0x05); put("Smith");
+
+ //-title
+ put((byte) 0xA0, (byte) 0x0A);
+ put((byte) 0x1A, (byte) 0x08); put("Director");
+
+ //-employee number
+ put((byte) 0x42, (byte) 0x01, (byte) 0x33);
+
+ //-date of hire
+ put((byte) 0xA1, (byte) 0x0A);
+ put((byte) 0x43, (byte) 0x08); put("19710917");
+
+ //-spouse
+ put((byte) 0xA2, (byte) 0x12);
+ put((byte) 0x61, (byte) 0x10);
+ put((byte) 0x1A, (byte) 0x04); put("Mary");
+ put((byte) 0x1A, (byte) 0x01); put("T");
+ put((byte) 0x1A, (byte) 0x05); put("Smith");
+
+ //-children
+ put((byte) 0xA3, (byte) 0x42);
+ //--child 1
+ put((byte) 0x31, (byte) 0x1F);
+ //---name
+ put((byte) 0x61, (byte) 0x11);
+ put((byte) 0x1A, (byte) 0x05); put("Ralph");
+ put((byte) 0x1A, (byte) 0x01); put("T");
+ put((byte) 0x1A, (byte) 0x05); put("Smith");
+ //-date of birth
+ put((byte) 0xA0, (byte) 0x0A);
+ put((byte) 0x43, (byte) 0x08); put("19571111");
+ //--child 2
+ put((byte) 0x31, (byte) 0x1F);
+ //---name
+ put((byte) 0x61, (byte) 0x11);
+ put((byte) 0x1A, (byte) 0x05); put("Susan");
+ put((byte) 0x1A, (byte) 0x01); put("B");
+ put((byte) 0x1A, (byte) 0x05); put("Jones");
+ //-date of birth
+ put((byte) 0xA0, (byte) 0x0A);
+ put((byte) 0x43, (byte) 0x08); put("19590717");
+
+ return buffer.array();
+ }
+ }
+
+ BufferOutput buffer = new BufferOutput();
+ return buffer.output();
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestPersonnelRecord.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestPersonnelRecord.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestPersonnelRecord.java
new file mode 100644
index 0000000..6045c0c
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestPersonnelRecord.java
@@ -0,0 +1,107 @@
+package org.apache.haox.asn1;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+/**
+ * Ref. X.690-0207(http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf),
+ * Annex A, A.1 ASN.1 description of the record structure
+ */
+public class TestPersonnelRecord {
+
+ static boolean verbose = false;
+
+ @Test
+ public void testEncoding() {
+ PersonnelRecord pr = TestData.createSamplePersonnel();
+
+ if (verbose) {
+ System.out.println("Name:");
+ System.out.println(Util.bytesToHex(pr.getName().encode()));
+
+ /*
+ System.out.println("Title:");
+ System.out.println(Util.bytesToHex(pr.getFieldAs(1, Asn1VisibleString.class).encode()));
+
+ System.out.println("EmployeeNumber:");
+ System.out.println(Util.bytesToHex(pr.getFieldAs(2, EmployeeNumber.class).encode()));
+ */
+
+ System.out.println("DateOfHire:");
+ System.out.println(Util.bytesToHex(pr.getDateOfHire().encode()));
+
+ System.out.println("SpouseName:");
+ System.out.println(Util.bytesToHex(pr.getNameOfSpouse().encode()));
+
+ System.out.println("Child1:");
+ System.out.println(Util.bytesToHex(pr.getChildren().getElements().get(0).encode()));
+
+ System.out.println("Child2:");
+ System.out.println(Util.bytesToHex(pr.getChildren().getElements().get(1).encode()));
+
+ System.out.println("Children:");
+ System.out.println(Util.bytesToHex(pr.getChildren().encode()));
+ }
+
+ byte[] data = TestData.createSammplePersonnelEncodingData();
+ byte[] encoded = pr.encode();
+
+ if (verbose) {
+ System.out.println("ExpectedData:");
+ System.out.println(Util.bytesToHex(data));
+
+ System.out.println("Encoded:");
+ System.out.println(Util.bytesToHex(encoded));
+ }
+
+ Assert.assertArrayEquals(data, encoded);
+ }
+
+ @Test
+ public void testDecoding() throws IOException {
+ PersonnelRecord expected = TestData.createSamplePersonnel();
+ byte[] data = TestData.createSammplePersonnelEncodingData();
+ PersonnelRecord decoded = new PersonnelRecord();
+ decoded.decode(data);
+
+ Assert.assertEquals(expected.getName().getGivenName(),
+ decoded.getName().getGivenName());
+ Assert.assertEquals(expected.getName().getInitial(),
+ decoded.getName().getInitial());
+ Assert.assertEquals(expected.getName().getFamilyName(),
+ decoded.getName().getFamilyName());
+
+ Assert.assertEquals(expected.getDateOfHire().getValue().getValue(),
+ decoded.getDateOfHire().getValue().getValue());
+ Assert.assertEquals(expected.getTitle(), decoded.getTitle());
+ Assert.assertEquals(expected.getEmployeeNumber().getValue().getValue(),
+ decoded.getEmployeeNumber().getValue().getValue());
+
+ Assert.assertEquals(expected.getNameOfSpouse().getGivenName(),
+ decoded.getNameOfSpouse().getGivenName());
+ Assert.assertEquals(expected.getNameOfSpouse().getInitial(),
+ decoded.getNameOfSpouse().getInitial());
+ Assert.assertEquals(expected.getNameOfSpouse().getFamilyName(),
+ decoded.getNameOfSpouse().getFamilyName());
+
+ Assert.assertEquals(expected.getChildren().getElements().get(0).getName().getGivenName(),
+ decoded.getChildren().getElements().get(0).getName().getGivenName());
+ Assert.assertEquals(expected.getChildren().getElements().get(0).getName().getInitial(),
+ decoded.getChildren().getElements().get(0).getName().getInitial());
+ Assert.assertEquals(expected.getChildren().getElements().get(0).getName().getFamilyName(),
+ decoded.getChildren().getElements().get(0).getName().getFamilyName());
+ Assert.assertEquals(expected.getChildren().getElements().get(0).getDateOfBirth().getValue().getValue(),
+ decoded.getChildren().getElements().get(0).getDateOfBirth().getValue().getValue());
+
+ Assert.assertEquals(expected.getChildren().getElements().get(1).getName().getGivenName(),
+ decoded.getChildren().getElements().get(1).getName().getGivenName());
+ Assert.assertEquals(expected.getChildren().getElements().get(1).getName().getInitial(),
+ decoded.getChildren().getElements().get(1).getName().getInitial());
+ Assert.assertEquals(expected.getChildren().getElements().get(1).getName().getFamilyName(),
+ decoded.getChildren().getElements().get(1).getName().getFamilyName());
+ Assert.assertEquals(expected.getChildren().getElements().get(1).getDateOfBirth().getValue().getValue(),
+ decoded.getChildren().getElements().get(1).getDateOfBirth().getValue().getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestTaggingEncoding.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestTaggingEncoding.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestTaggingEncoding.java
new file mode 100644
index 0000000..ddd3481
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/TestTaggingEncoding.java
@@ -0,0 +1,186 @@
+package org.apache.haox.asn1;
+
+import org.apache.haox.asn1.EncodingOption;
+import org.apache.haox.asn1.TaggingOption;
+import org.apache.haox.asn1.type.Asn1Tagging;
+import org.apache.haox.asn1.type.Asn1VisibleString;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+/**
+ Ref. X.690-0207 8.14 Encoding of a tagged value
+ EXAMPLE
+ With ASN.1 type definitions (in an explicit tagging environment) of:
+ Type1 ::= VisibleString
+ Type2 ::= [APPLICATION 3] IMPLICIT Type1
+ Type3 ::= [2] Type2
+ Type4 ::= [APPLICATION 7] IMPLICIT Type3
+ Type5 ::= [2] IMPLICIT Type2
+ a value of:
+ "Jones"
+ is encoded as follows:
+ For Type1:
+ VisibleString Length Contents
+ 1A16 0516 4A6F6E657316
+ For Type2:
+ [Application 3] Length Contents
+ 4316 0516 4A6F6E657316
+ For Type3:
+ [2] Length Contents
+ A216 0716
+ [APPLICATION 3] Length Contents
+ 4316 0516 4A6F6E657316
+ For Type4:
+ [Application 7] Length Contents
+ 6716 0716
+ [APPLICATION 3] Length Contents
+ 4316 0516 4A6F6E657316
+ For Type5:
+ [2] Length Contents
+ 8216 0516 4A6F6E657316
+ */
+
+public class TestTaggingEncoding {
+ static final String TEST_STRING = "Jones";
+ static byte[] TYPE1_EXPECTED_BYTES = new byte[] {(byte) 0x1A, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73};
+ static byte[] TYPE2_EXPECTED_BYTES = new byte[] {(byte) 0x43, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73};
+ static byte[] TYPE3_EXPECTED_BYTES = new byte[] {(byte) 0xA2, (byte) 0x07, (byte) 0x43, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73};
+ static byte[] TYPE4_EXPECTED_BYTES = new byte[] {(byte) 0x67, (byte) 0x07, (byte) 0x43, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73};
+ static byte[] TYPE5_EXPECTED_BYTES = new byte[] {(byte) 0x82, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73};
+
+
+ public static class Type1 extends Asn1VisibleString {
+ public Type1(String value) {
+ super(value);
+ }
+ public Type1() {
+ this(null);
+ }
+ }
+
+ public static class Type2 extends Asn1Tagging<Type1> {
+ public Type2(Type1 value) {
+ super(3, value, true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+ public Type2() {
+ this(null);
+ }
+ }
+
+ public static class Type3 extends Asn1Tagging<Type2> {
+ public Type3(Type2 value) {
+ super(2, value, false);
+ setEncodingOption(EncodingOption.EXPLICIT);
+ }
+ public Type3() {
+ this(null);
+ }
+ }
+
+ public static class Type4 extends Asn1Tagging<Type3> {
+ public Type4(Type3 value) {
+ super(7, value, true);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+ public Type4() {
+ this(null);
+ }
+ }
+
+ public static class Type5 extends Asn1Tagging<Type2> {
+ public Type5(Type2 value) {
+ super(2, value, false);
+ setEncodingOption(EncodingOption.IMPLICIT);
+ }
+ public Type5() {
+ this(null);
+ }
+ }
+
+ @Test
+ public void testAsn1TaggingEncoding() {
+ Type1 aType1 = new Type1(TEST_STRING);
+ Type2 aType2 = new Type2(aType1);
+ Type3 aType3 = new Type3(aType2);
+ Type4 aType4 = new Type4(aType3);
+ Type5 aType5 = new Type5(aType2);
+
+ Assert.assertArrayEquals(TYPE1_EXPECTED_BYTES, aType1.encode());
+ Assert.assertArrayEquals(TYPE2_EXPECTED_BYTES, aType2.encode());
+ Assert.assertArrayEquals(TYPE3_EXPECTED_BYTES, aType3.encode());
+ Assert.assertArrayEquals(TYPE4_EXPECTED_BYTES, aType4.encode());
+ Assert.assertArrayEquals(TYPE5_EXPECTED_BYTES, aType5.encode());
+ }
+
+ @Test
+ public void testAsn1TaggingDecoding() throws IOException {
+ Type1 aType1 = new Type1();
+ aType1.decode(TYPE1_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType1.getValue());
+
+ Type2 aType2 = new Type2();
+ aType2.decode(TYPE2_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType2.getValue().getValue());
+
+ Type3 aType3 = new Type3();
+ aType3.decode(TYPE3_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType3.getValue().getValue().getValue());
+
+ Type4 aType4 = new Type4();
+ aType4.decode(TYPE4_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType4.getValue().getValue().getValue().getValue());
+
+ Type5 aType5 = new Type5();
+ aType5.decode(TYPE5_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType5.getValue().getValue().getValue());
+ }
+
+ @Test
+ public void testTaggingEncodingOption() {
+ Type1 aType1 = new Type1(TEST_STRING);
+ Type2 aType2 = new Type2(aType1);
+ Type3 aType3 = new Type3(aType2);
+ Type4 aType4 = new Type4(aType3);
+ Type5 aType5 = new Type5(aType2);
+
+ Assert.assertArrayEquals(TYPE1_EXPECTED_BYTES, aType1.encode());
+ Assert.assertArrayEquals(TYPE2_EXPECTED_BYTES,
+ aType1.taggedEncode(TaggingOption.newImplicitAppSpecific(3))); // for Type2
+ Assert.assertArrayEquals(TYPE3_EXPECTED_BYTES,
+ aType2.taggedEncode(TaggingOption.newExplicitContextSpecific(2))); // for Type3
+ Assert.assertArrayEquals(TYPE4_EXPECTED_BYTES,
+ aType3.taggedEncode(TaggingOption.newImplicitAppSpecific(7))); // for Type4
+ Assert.assertArrayEquals(TYPE5_EXPECTED_BYTES,
+ aType2.taggedEncode(TaggingOption.newImplicitContextSpecific(2))); // for Type5
+ }
+
+ @Test
+ public void testTaggingDecodingOption() throws IOException {
+ Type1 aType1 = new Type1();
+ aType1.decode(TYPE1_EXPECTED_BYTES);
+ Assert.assertEquals(TEST_STRING, aType1.getValue());
+
+ // for Type2
+ aType1 = new Type1();
+ aType1.taggedDecode(TYPE2_EXPECTED_BYTES, TaggingOption.newImplicitAppSpecific(3));
+ Assert.assertEquals(TEST_STRING, aType1.getValue());
+
+ // for Type3
+ Type2 aType2 = new Type2();
+ aType2.taggedDecode(TYPE3_EXPECTED_BYTES, TaggingOption.newExplicitContextSpecific(2));
+ Assert.assertEquals(TEST_STRING, aType2.getValue().getValue());
+
+ // for Type4
+ Type3 aType3 = new Type3();
+ aType3.taggedDecode(TYPE4_EXPECTED_BYTES, TaggingOption.newImplicitAppSpecific(7));
+ Assert.assertEquals(TEST_STRING, aType3.getValue().getValue().getValue());
+
+ // for Type5
+ aType2 = new Type2();
+ aType2.taggedDecode(TYPE5_EXPECTED_BYTES, TaggingOption.newImplicitContextSpecific(2));
+ Assert.assertEquals(TEST_STRING, aType2.getValue().getValue());
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/Util.java
----------------------------------------------------------------------
diff --git a/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/Util.java b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/Util.java
new file mode 100644
index 0000000..a72c800
--- /dev/null
+++ b/contrib/haox-asn1/src/test/java/org/apache/haox/asn1/Util.java
@@ -0,0 +1,53 @@
+package org.apache.haox.asn1;
+
+public class Util {
+
+ final static String HEX_CHARS_STR = "0123456789ABCDEF";
+ final static char[] HEX_CHARS = HEX_CHARS_STR.toCharArray();
+
+ /**
+ * Convert bytes into format as:
+ * 0x02 02 00 80
+ */
+ public static String bytesToHex(byte[] bytes) {
+ int len = bytes.length * 2;
+ len += bytes.length; // for ' ' appended for each char
+ len += 2; // for '0x' prefix
+ char[] hexChars = new char[len];
+ hexChars[0] = '0';
+ hexChars[1] = 'x';
+ for ( int j = 0; j < bytes.length; j++ ) {
+ int v = bytes[j] & 0xFF;
+ hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4];
+ hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F];
+ hexChars[j * 3 + 4] = ' ';
+ }
+
+ return new String(hexChars);
+ }
+
+ /**
+ * Convert hex string like follows into byte array
+ * 0x02 02 00 80
+ */
+ public static byte[] hex2bytes(String hexString) {
+ hexString = hexString.toUpperCase();
+ String hexStr = hexString;
+ if (hexString.startsWith("0X")) {
+ hexStr = hexString.substring(2);
+ }
+ String[] hexParts = hexStr.split(" ");
+
+ byte[] bytes = new byte[hexParts.length];
+ char[] hexPart;
+ for (int i = 0; i < hexParts.length; ++i) {
+ hexPart = hexParts[i].toCharArray();
+ if (hexPart.length != 2) {
+ throw new IllegalArgumentException("Invalid hex string to convert");
+ }
+ bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4) + HEX_CHARS_STR.indexOf(hexPart[1]));
+ }
+
+ return bytes;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-config/README
----------------------------------------------------------------------
diff --git a/contrib/haox-config/README b/contrib/haox-config/README
new file mode 100644
index 0000000..37eb019
--- /dev/null
+++ b/contrib/haox-config/README
@@ -0,0 +1 @@
+An unified configuration API that crosses various popular configuration formats like XML, JSON, INI and etc.
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/contrib/haox-config/pom.xml
----------------------------------------------------------------------
diff --git a/contrib/haox-config/pom.xml b/contrib/haox-config/pom.xml
new file mode 100644
index 0000000..3e519db
--- /dev/null
+++ b/contrib/haox-config/pom.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>contrib</artifactId>
+ <groupId>org.haox</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>haox-config</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.5</version>
+ </dependency>
+ </dependencies>
+
+
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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/23c1fd12/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