directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerberos git commit: DIRKRB-112 MIT Kerberos configuration format support. Contributed by Lin Chen
Date Wed, 21 Jan 2015 05:08:41 GMT
Repository: directory-kerberos
Updated Branches:
  refs/heads/master 0286d930a -> c8e3dfde2


DIRKRB-112 MIT Kerberos configuration format support. Contributed by Lin Chen


Project: http://git-wip-us.apache.org/repos/asf/directory-kerberos/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerberos/commit/c8e3dfde
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerberos/tree/c8e3dfde
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerberos/diff/c8e3dfde

Branch: refs/heads/master
Commit: c8e3dfde25e21452768d31393f89939f9d26301d
Parents: 0286d93
Author: drankye <drankye@gmail.com>
Authored: Wed Jan 21 21:05:40 2015 +0800
Committer: drankye <drankye@gmail.com>
Committed: Wed Jan 21 21:05:40 2015 +0800

----------------------------------------------------------------------
 .../org/apache/haox/config/IniConfigLoader.java | 50 +++++++++++--
 .../org/apache/haox/config/IniConfigTest.java   | 76 ++++++++++++++++++++
 2 files changed, 122 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/c8e3dfde/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
index b328262..05c163b 100644
--- 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
@@ -6,22 +6,64 @@
  *  to you under the Apache License, Version 2.0 (the
  *  "License"); you may not use this file except in compliance
  *  with the License.  You may obtain a copy of the License at
- *  
+ *
  *    http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
  *  under the License. 
- *  
+ *
  */
 package org.apache.haox.config;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
 public class IniConfigLoader extends ConfigLoader {
+    private static final String COMMENT_SYMBOL = "#";
+
+    private ConfigImpl rootConfig;
+    private ConfigImpl currentConfig;
+
+    /**
+     *  Load configs form the INI configuration format file.
+     */
     @Override
-    protected void loadConfig(ConfigImpl config, Resource resource) {
+    protected void loadConfig(ConfigImpl config, Resource resource) throws IOException {
+        rootConfig = config;
+        currentConfig = config;
+
+        InputStream is = (InputStream) resource.getResource();
+        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+
+        String line;
+        while ((line = reader.readLine()) != null) {
+            parseLine(line);
+        }
+    }
+
+    private void parseLine(String line) {
+        line = line.trim();
+
+        if (line.startsWith(COMMENT_SYMBOL)) {
+            return;
+        }
 
+        if (line.matches("\\[.*\\]")) {
+            String subConfigName = line.replaceFirst("\\[(.*)\\]", "$1");
+            ConfigImpl subConfig = new ConfigImpl(subConfigName);
+            rootConfig.set(subConfigName, subConfig);
+            currentConfig = subConfig;
+        } else if (line.matches(".*=.*")) {
+            int i = line.indexOf('=');
+            String name = line.substring(0, i).trim();
+            String value = line.substring(i + 1).trim();
+            currentConfig.set(name, value);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/c8e3dfde/contrib/haox-config/src/test/java/org/apache/haox/config/IniConfigTest.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/test/java/org/apache/haox/config/IniConfigTest.java b/contrib/haox-config/src/test/java/org/apache/haox/config/IniConfigTest.java
new file mode 100644
index 0000000..bd2f93b
--- /dev/null
+++ b/contrib/haox-config/src/test/java/org/apache/haox/config/IniConfigTest.java
@@ -0,0 +1,76 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.haox.config;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class IniConfigTest {
+
+    private final static String TEST_DIR = new File(System.getProperty(
+            "test.build.data", "/tmp")).getAbsolutePath();
+    private final static File TEST_FILE = new File(TEST_DIR, "test-ini-config");
+
+    /**
+     * Build a INI format configuration file.
+     */
+    private void buildFile() throws IOException {
+        PrintWriter out = new PrintWriter(new FileWriter(TEST_FILE));
+        out.println("#note = notenote");
+        out.println("default = FILE:/var/log/krb5libs.log");
+        out.println("kdc = FILE:/var/log/krb5kdc.log");
+        out.println("admin_server = FILE:/var/log/kadmind.log");
+        out.println("[libdefaults]");
+        out.println("default_realm = EXAMPLE.COM");
+        out.println("dns_lookup_realm = false");
+        out.println("dns_lookup_kdc = false");
+        out.println("ticket_lifetime = 24h");
+        out.println("renew_lifetime = 7d");
+        out.println("forwardable = true");
+        out.println("[lib1]");
+        out.println("default_realm = EXAMPLE.COM1");
+        out.println("dns_lookup_realm = true");
+        out.close();
+    }
+
+    @Test
+    public void testIniConfig() throws IOException {
+        buildFile();
+
+        Conf conf = new Conf();
+        conf.addIniConfig(TEST_FILE);
+
+        Assert.assertEquals(conf.getString("default"), "FILE:/var/log/krb5libs.log");
+        Assert.assertEquals(conf.getString("#note"), null);//Comments should be ignored when
loading.
+
+        Config config = conf.getConfig("libdefaults");
+        Assert.assertFalse(config.getBoolean("dns_lookup_realm"));
+        Assert.assertTrue(config.getBoolean("forwardable"));
+
+        Config config1 = conf.getConfig("lib1");
+        Assert.assertTrue(config1.getBoolean("dns_lookup_realm"));
+    }
+
+}


Mime
View raw message