You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/02/26 23:05:18 UTC
[05/11] incubator-tamaya-extensions git commit: Added JNDI module.
Added JNDI module.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/a315ab4c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/a315ab4c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/a315ab4c
Branch: refs/heads/master
Commit: a315ab4ca127c4fdc8c642d4a1b85a75c44bdac3
Parents: e71203c
Author: anatole <an...@apache.org>
Authored: Tue Feb 14 23:54:48 2017 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100
----------------------------------------------------------------------
modules/jndi/bnd.bnd | 2 +
modules/jndi/pom.xml | 73 ++++++++
.../apache/tamaya/jndi/JNDIPropertySource.java | 176 +++++++++++++++++++
.../tamaya/jndi/JNDIPropertySourceTest.java | 74 ++++++++
modules/jndi/src/test/jndi-dir/a/test1 | 19 ++
modules/jndi/src/test/jndi-dir/b/test2 | 19 ++
modules/jndi/src/test/jndi-dir/c/c1/test5 | 19 ++
modules/jndi/src/test/jndi-dir/c/test3 | 19 ++
modules/jndi/src/test/jndi-dir/c/test4 | 19 ++
9 files changed, 420 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/bnd.bnd
----------------------------------------------------------------------
diff --git a/modules/jndi/bnd.bnd b/modules/jndi/bnd.bnd
new file mode 100644
index 0000000..f426059
--- /dev/null
+++ b/modules/jndi/bnd.bnd
@@ -0,0 +1,2 @@
+Export-Package: \
+ org.apache.tamaya.jndi
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/pom.xml
----------------------------------------------------------------------
diff --git a/modules/jndi/pom.xml b/modules/jndi/pom.xml
new file mode 100644
index 0000000..dc79e34
--- /dev/null
+++ b/modules/jndi/pom.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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 current 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.
+-->
+<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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.tamaya.ext</groupId>
+ <artifactId>tamaya-extensions</artifactId>
+ <version>0.3-incubating-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>tamaya-jndi</artifactId>
+ <name>Apache Tamaya JNDI Support</name>
+ <packaging>jar</packaging>
+
+ <properties>
+ <jdkVersion>1.7</jdkVersion>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-api</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-core</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>java-hamcrest</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tamaya.ext</groupId>
+ <artifactId>tamaya-spisupport</artifactId>
+ <version>0.3-incubating-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.messaging.mq</groupId>
+ <artifactId>fscontext</artifactId>
+ <version>4.6-b01</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/main/java/org/apache/tamaya/jndi/JNDIPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/jndi/src/main/java/org/apache/tamaya/jndi/JNDIPropertySource.java b/modules/jndi/src/main/java/org/apache/tamaya/jndi/JNDIPropertySource.java
new file mode 100644
index 0000000..307f096
--- /dev/null
+++ b/modules/jndi/src/main/java/org/apache/tamaya/jndi/JNDIPropertySource.java
@@ -0,0 +1,176 @@
+/*
+ * 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.tamaya.jndi;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValue;
+import org.apache.tamaya.spisupport.BasePropertySource;
+
+import javax.naming.*;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Propertysource that accesses JNDI as source for configuration entries.
+ */
+public class JNDIPropertySource extends BasePropertySource {
+ /** The logger used. */
+ private static final Logger LOG = Logger.getLogger(JNDIPropertySource.class.getName());
+ /** The root context, not null. */
+ private Context context;
+ /** The scannable property, default is {@code false}. */
+ private boolean scannable = false;
+
+ /**
+ * Creates a new instance.
+ * @param name the name of the property source, see {@link PropertySource#getName()}.
+ * @param context the root context to be used, not null.
+ */
+ public JNDIPropertySource(String name, Context context){
+ super(name);
+ this.context = Objects.requireNonNull(context);
+ }
+
+ /**
+ * Creates a new instance.
+ * @param name the name of the property source, see {@link PropertySource#getName()}.
+ * @throws NamingException if {@code new InitialContext()} throws an exception.
+ */
+ public JNDIPropertySource(String name) throws NamingException {
+ super(name);
+ this.context = new InitialContext();
+ }
+
+ /**
+ * Creates a new instance, using {@code "jndi"} as property source name.
+ * @throws NamingException if {@code new InitialContext()} throws an exception.
+ */
+ public JNDIPropertySource() throws NamingException {
+ this("jndi");
+ }
+
+ /**
+ * If the property source is not scannable, an empty map is returned, otherwise
+ * the current JNDI context is mapped to configuration map:
+ * <ul>
+ * <li>For each leave entry one entry is created.</li>
+ * <li>The key is the fully path of parent contexts, separated by a '.'.</li>
+ * <li>The value is the value returned from {@code String.valueOf(leaveObject)}.</li>
+ * </ul>
+ * @return a map representation of the JNDI tree.
+ */
+ @Override
+ public Map<String, String> getProperties() {
+ if(scannable){
+ try {
+ return toMap(this.context);
+ } catch (NamingException e) {
+ LOG.log(Level.WARNING, "Error scanning JNDI tree.", e);
+ }
+ }
+ return Collections.emptyMap();
+ }
+
+ @Override
+ public boolean isScannable() {
+ return scannable;
+ }
+
+ /**
+ * If set to true, the property source will return a String representation of the JNDI
+ * tree when calling {@code getProperties()}.
+ * @see #getProperties()
+ * @param val true, to activate scannable (default is false).
+ */
+ public void setScannable(boolean val){
+ this.scannable = val;
+ }
+
+ @Override
+ public PropertyValue get(String key) {
+ try {
+ key = key.replace('.', '/');
+ Object o = context.lookup(key);
+ return PropertyValue.of(key, o.toString(), getName());
+ } catch (NamingException e) {
+ LOG.log(Level.FINER, "Failed to lookup key in JNDI: " + key, e);
+ return null;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "JNDIPropertySource{" +
+ "name=" + getName() +
+ ", context=" + context +
+ '}';
+ }
+
+ /**
+ * Maps the given JNDI Context to a {@code Map<String,String>}:
+ * mapped to configuration map:
+ * <ul>
+ * <li>For each leave entry one entry is created.</li>
+ * <li>The key is the fully path of parent contexts, separated by a '.'.</li>
+ * <li>The value is the value returned from {@code String.valueOf(leaveObject)}.</li>
+ * </ul>
+ * @param ctx the JNDI context, not null.
+ * @return the corresponding map, never null.
+ * @throws NamingException If some JNDI issues occur.
+ */
+ public static Map<String,String> toMap(Context ctx) throws NamingException {
+ String namespace = ctx instanceof InitialContext ? ctx.getNameInNamespace() : "";
+ Map<String, String> map = new HashMap<>();
+ NamingEnumeration<NameClassPair> list = ctx.list(namespace);
+ while (list.hasMoreElements()) {
+ NameClassPair next = list.next();
+ String name = next.getName();
+ String jndiPath = namespace + name;
+ try {
+ Object lookup = ctx.lookup(jndiPath);
+ if (namespace.isEmpty()) {
+ if (lookup instanceof Context) {
+ Map<String, String> childMap = toMap((Context) lookup);
+ for (Map.Entry<String, String> en : childMap.entrySet()) {
+ map.put(name + "." + en.getKey(), en.getValue());
+ }
+ } else {
+ map.put(name, String.valueOf(lookup));
+ }
+ }else{
+ if (lookup instanceof Context) {
+ Map<String, String> childMap = toMap((Context) lookup);
+ for (Map.Entry<String, String> en : childMap.entrySet()) {
+ map.put(namespace + "." + name + "." + en.getKey(), en.getValue());
+ }
+ } else {
+ map.put(namespace + "." + name, String.valueOf(lookup));
+ }
+ }
+ } catch (Exception t) {
+ map.put(namespace + "." + name, "ERROR: " + t.getMessage());
+ }
+ }
+ return map;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/java/org/apache/tamaya/jndi/JNDIPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/java/org/apache/tamaya/jndi/JNDIPropertySourceTest.java b/modules/jndi/src/test/java/org/apache/tamaya/jndi/JNDIPropertySourceTest.java
new file mode 100644
index 0000000..00b3514
--- /dev/null
+++ b/modules/jndi/src/test/java/org/apache/tamaya/jndi/JNDIPropertySourceTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.tamaya.jndi;
+
+import org.junit.Test;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.Hashtable;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertNotNull;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class JNDIPropertySourceTest{
+
+ private InitialContext createFSContext() throws NamingException, MalformedURLException {
+ Hashtable env = new Hashtable();
+ env.put (Context.INITIAL_CONTEXT_FACTORY,
+ "com.sun.jndi.fscontext.RefFSContextFactory");
+ return new InitialContext(env);
+ }
+
+ private Context getTestDirContext(InitialContext ctx) throws NamingException {
+ return (Context)ctx.lookup(new File("./src/test/jndi-dir").getAbsolutePath());
+ }
+
+ @Test
+ public void testCreateWithContext() throws NamingException, MalformedURLException {
+ new JNDIPropertySource("jndi-test", createFSContext());
+ }
+
+ @Test
+ public void testScanContext() throws NamingException, MalformedURLException {
+ JNDIPropertySource ps = new JNDIPropertySource("jndi-test", getTestDirContext(createFSContext()));
+ assertFalse(ps.isScannable());
+ Map<String,String> props = ps.getProperties();
+ assertNotNull(props);
+ assertTrue(props.isEmpty());
+ ps.setScannable(true);
+ assertTrue(ps.isScannable());
+ props = ps.getProperties();
+ assertNotNull(props);
+ assertFalse(props.isEmpty());
+ assertEquals(props.size(), 5);
+ assertNotNull(props.get("c.c1.test5"));
+ assertNotNull(props.get("c.test3"));
+ assertNotNull(props.get("c.test4"));
+ assertNotNull(props.get("b.test2"));
+ assertNotNull(props.get("a.test1"));
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/jndi-dir/a/test1
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/jndi-dir/a/test1 b/modules/jndi/src/test/jndi-dir/a/test1
new file mode 100644
index 0000000..0355740
--- /dev/null
+++ b/modules/jndi/src/test/jndi-dir/a/test1
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+test1Value
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/jndi-dir/b/test2
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/jndi-dir/b/test2 b/modules/jndi/src/test/jndi-dir/b/test2
new file mode 100644
index 0000000..8aadc3a
--- /dev/null
+++ b/modules/jndi/src/test/jndi-dir/b/test2
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+test2Value
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/jndi-dir/c/c1/test5
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/jndi-dir/c/c1/test5 b/modules/jndi/src/test/jndi-dir/c/c1/test5
new file mode 100644
index 0000000..bfb7c6b
--- /dev/null
+++ b/modules/jndi/src/test/jndi-dir/c/c1/test5
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+test5Value
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/jndi-dir/c/test3
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/jndi-dir/c/test3 b/modules/jndi/src/test/jndi-dir/c/test3
new file mode 100644
index 0000000..c3f4519
--- /dev/null
+++ b/modules/jndi/src/test/jndi-dir/c/test3
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+test3Value
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a315ab4c/modules/jndi/src/test/jndi-dir/c/test4
----------------------------------------------------------------------
diff --git a/modules/jndi/src/test/jndi-dir/c/test4 b/modules/jndi/src/test/jndi-dir/c/test4
new file mode 100644
index 0000000..7ece03d
--- /dev/null
+++ b/modules/jndi/src/test/jndi-dir/c/test4
@@ -0,0 +1,19 @@
+#
+# 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 current 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.
+#
+test4Value
\ No newline at end of file