You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by jo...@apache.org on 2013/03/21 02:42:08 UTC
git commit: DELTASPIKE-273 Add support for property keys.
Updated Branches:
refs/heads/master d2e84a3a3 -> 4b7e36494
DELTASPIKE-273 Add support for property keys.
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/4b7e3649
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/4b7e3649
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/4b7e3649
Branch: refs/heads/master
Commit: 4b7e364941d2f900889020a19efe3fd77812c821
Parents: d2e84a3
Author: John D. Ament <jo...@gmail.com>
Authored: Wed Mar 20 21:41:42 2013 -0400
Committer: John D. Ament <jo...@gmail.com>
Committed: Wed Mar 20 21:41:42 2013 -0400
----------------------------------------------------------------------
.../deltaspike/core/api/config/ConfigResolver.java | 22 ++-
.../deltaspike/core/spi/config/ConfigSource.java | 9 +
.../test/api/config/TestConfigSource.java | 12 ++
.../test/api/config/TestConfigSourceProvider.java | 32 ++-
.../config/EnvironmentPropertyConfigSource.java | 19 +--
.../core/impl/config/LocalJndiConfigSource.java | 35 +++--
.../core/impl/config/MapConfigSource.java | 48 +++++
.../core/impl/config/PropertiesConfigSource.java | 62 ++++++
.../core/impl/config/PropertyFileConfigSource.java | 22 +--
.../impl/config/SystemPropertyConfigSource.java | 18 +--
.../deltaspike/core/impl/util/JndiUtils.java | 151 +++++++++++----
.../test/core/impl/util/JndiUtilsTest.java | 21 ++-
12 files changed, 332 insertions(+), 119 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
index 4d018b6..f939d6a 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
@@ -22,19 +22,20 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
-import org.apache.deltaspike.core.util.ClassUtils;
+import javax.enterprise.inject.Typed;
+
import org.apache.deltaspike.core.spi.config.ConfigSource;
import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
+import org.apache.deltaspike.core.util.ClassUtils;
import org.apache.deltaspike.core.util.ServiceUtils;
-import javax.enterprise.inject.Typed;
-
/**
* <p>Resolve the configuration via their well defined ordinals.</p>
*
@@ -174,6 +175,20 @@ public final class ConfigResolver
return result;
}
+ public static Map<String, String> getAllProperties()
+ {
+ List<ConfigSource> appConfigSources =
+ sortAscending(new ArrayList<ConfigSource>(Arrays.asList(getConfigSources())));
+ Map<String, String> result = new HashMap<String, String>();
+
+ for (ConfigSource configSource : appConfigSources)
+ {
+ result.putAll(configSource.getProperties());
+ }
+
+ return Collections.unmodifiableMap(result);
+ }
+
private static synchronized ConfigSource[] getConfigSources()
{
ClassLoader currentClassLoader = ClassUtils.getClassLoader(null);
@@ -245,4 +260,5 @@ public final class ConfigResolver
});
return configSources;
}
+
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
index 692b07a..52f94bd 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java
@@ -18,6 +18,8 @@
*/
package org.apache.deltaspike.core.spi.config;
+import java.util.Map;
+
/**
* <p>Implement this interfaces to provide a ConfigSource.
* A ConfigSource provides properties from a specific place, like
@@ -76,6 +78,12 @@ public interface ConfigSource
int getOrdinal();
/**
+ * Return properties contained in this config source.
+ * @return Properties available in this config source.
+ */
+ Map<String, String> getProperties();
+
+ /**
* @param key for the property
* @return configured value or <code>null</code> if this ConfigSource doesn't provide any value for the given key.
*/
@@ -85,4 +93,5 @@ public interface ConfigSource
* @return the 'name' of the configuration source, e.g. 'property-file mylocation/myproperty.properties'
*/
String getConfigName();
+
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
index b159acc..360f8ff 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
@@ -18,6 +18,9 @@
*/
package org.apache.deltaspike.test.api.config;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.deltaspike.core.spi.config.ConfigSource;
/**
@@ -47,4 +50,13 @@ public class TestConfigSource implements ConfigSource
{
return "testkey".equals(key) ? "testvalue" : null;
}
+
+ @Override
+ public Map<String, String> getProperties()
+ {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("testkey", "testvalue");
+ return map;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSourceProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSourceProvider.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSourceProvider.java
index 7664c68..15e5d87 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSourceProvider.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSourceProvider.java
@@ -18,12 +18,14 @@
*/
package org.apache.deltaspike.test.api.config;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.apache.deltaspike.core.spi.config.ConfigSource;
import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
-import java.util.ArrayList;
-import java.util.List;
-
/**
* ConfigSourceProvider for basic unit-test
*/
@@ -32,13 +34,7 @@ public class TestConfigSourceProvider implements ConfigSourceProvider
@Override
public List<ConfigSource> getConfigSources()
{
- return new ArrayList<ConfigSource>()
- {
- {
- add(new TestConfigSource1());
- add(new TestConfigSource2());
- }
- };
+ return Arrays.asList(new TestConfigSource1(), new TestConfigSource2());
}
private static class TestConfigSource1 implements ConfigSource
@@ -60,6 +56,14 @@ public class TestConfigSourceProvider implements ConfigSourceProvider
}
@Override
+ public Map<String, String> getProperties()
+ {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("test", "test1");
+ return map;
+ }
+
+ @Override
public String getConfigName()
{
return TestConfigSourceProvider.class.getName();
@@ -85,6 +89,14 @@ public class TestConfigSourceProvider implements ConfigSourceProvider
}
@Override
+ public Map<String, String> getProperties()
+ {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("test", "test2");
+ return map;
+ }
+
+ @Override
public String getConfigName()
{
return TestConfigSourceProvider.class.getName();
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/EnvironmentPropertyConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/EnvironmentPropertyConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/EnvironmentPropertyConfigSource.java
index fb95681..b0c4a26 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/EnvironmentPropertyConfigSource.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/EnvironmentPropertyConfigSource.java
@@ -19,30 +19,21 @@
package org.apache.deltaspike.core.impl.config;
+
/**
* {@link org.apache.deltaspike.core.spi.config.ConfigSource}
- * which uses System#getenv
+ * which uses {@link System#getenv()}
*/
-class EnvironmentPropertyConfigSource extends BaseConfigSource
+class EnvironmentPropertyConfigSource extends MapConfigSource
{
public EnvironmentPropertyConfigSource()
{
+ super(System.getenv());
initOrdinal(300);
}
- /**
- * The given key gets used for a lookup via System#getenv
- *
- * @param key for the property
- * @return value for the given key or null if there is no configured value
- */
- @Override
- public String getPropertyValue(String key)
- {
- return System.getenv(key);
- }
-
+
/**
* {@inheritDoc}
*/
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/LocalJndiConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/LocalJndiConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/LocalJndiConfigSource.java
index 35fd6d5..856876f 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/LocalJndiConfigSource.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/LocalJndiConfigSource.java
@@ -18,10 +18,13 @@
*/
package org.apache.deltaspike.core.impl.config;
-import org.apache.deltaspike.core.impl.util.JndiUtils;
+import java.util.HashMap;
+import java.util.Map;
import javax.enterprise.inject.Typed;
+import org.apache.deltaspike.core.impl.util.JndiUtils;
+
/**
* {@link org.apache.deltaspike.core.spi.config.ConfigSource}
* which uses JNDI for the lookup
@@ -47,17 +50,7 @@ class LocalJndiConfigSource extends BaseConfigSource
{
try
{
- String jndiKey;
- if (key.startsWith("java:comp/env"))
- {
- jndiKey = key;
- }
- else
- {
- jndiKey = BASE_NAME + key;
- }
-
- return JndiUtils.lookup(jndiKey, String.class);
+ return JndiUtils.lookup(getJndiKey(key), String.class);
}
catch (Exception e)
{
@@ -66,6 +59,24 @@ class LocalJndiConfigSource extends BaseConfigSource
return null;
}
+ private String getJndiKey(String key)
+ {
+ if (key.startsWith("java:comp/env"))
+ {
+ return key;
+ }
+ return BASE_NAME + key;
+ }
+
+ @Override
+ public Map<String, String> getProperties()
+ {
+ Map<String, String> result = new HashMap<String, String>();
+ result.putAll(JndiUtils.list(BASE_NAME, String.class));
+ result.putAll(JndiUtils.list("java:comp/env", String.class));
+ return result;
+ }
+
/**
* {@inheritDoc}
*/
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/MapConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/MapConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/MapConfigSource.java
new file mode 100644
index 0000000..9038a53
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/MapConfigSource.java
@@ -0,0 +1,48 @@
+/*
+ * 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.deltaspike.core.impl.config;
+
+import java.util.Map;
+
+/**
+ * Base class for configurations based on regular {@link Map}
+ */
+public abstract class MapConfigSource extends BaseConfigSource
+{
+
+ private final Map<String, String> map;
+
+ public MapConfigSource(Map<String, String> map)
+ {
+ this.map = map;
+ }
+
+ @Override
+ public Map<String, String> getProperties()
+ {
+ return map;
+ }
+
+ @Override
+ public String getPropertyValue(String key)
+ {
+ return map.get(key);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertiesConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertiesConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertiesConfigSource.java
new file mode 100644
index 0000000..acca053
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertiesConfigSource.java
@@ -0,0 +1,62 @@
+/*
+ * 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.deltaspike.core.impl.config;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Base class for configuration sources based on {@link Properties} object.
+ */
+public abstract class PropertiesConfigSource extends BaseConfigSource
+{
+
+ private final Properties properties;
+
+ protected PropertiesConfigSource(Properties properties)
+ {
+ this.properties = properties;
+ }
+
+ /**
+ * The given key gets used for a lookup via a properties object
+ *
+ * @param key for the property
+ * @return value for the given key or null if there is no configured value
+ */
+ @Override
+ public String getPropertyValue(String key)
+ {
+ return properties.getProperty(key);
+ }
+
+ @Override
+ public Map<String, String> getProperties()
+ {
+ Map<String,String> result = new HashMap<String, String>();
+ for (String propertyName : properties.stringPropertyNames())
+ {
+ result.put(propertyName, properties.getProperty(propertyName));
+ }
+
+ return result;
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertyFileConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertyFileConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertyFileConfigSource.java
index a0b4d5b..5f15c6f 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertyFileConfigSource.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/PropertyFileConfigSource.java
@@ -18,40 +18,26 @@
*/
package org.apache.deltaspike.core.impl.config;
-import org.apache.deltaspike.core.util.PropertyFileUtils;
-
import java.net.URL;
-import java.util.Properties;
+
+import org.apache.deltaspike.core.util.PropertyFileUtils;
/**
* {@link org.apache.deltaspike.core.spi.config.ConfigSource} which uses
* <i>META-INF/apache-deltaspike.properties</i> for the lookup
*/
-class PropertyFileConfigSource extends BaseConfigSource
+class PropertyFileConfigSource extends PropertiesConfigSource
{
- private Properties properties;
private String fileName;
PropertyFileConfigSource(URL propertyFileUrl)
{
+ super(PropertyFileUtils.loadProperties(propertyFileUrl));
fileName = propertyFileUrl.toExternalForm();
- properties = PropertyFileUtils.loadProperties(propertyFileUrl);
initOrdinal(100);
}
/**
- * The given key gets used for a lookup via a properties file
- *
- * @param key for the property
- * @return value for the given key or null if there is no configured value
- */
- @Override
- public String getPropertyValue(String key)
- {
- return (String) properties.get(key);
- }
-
- /**
* {@inheritDoc}
*/
@Override
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/SystemPropertyConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/SystemPropertyConfigSource.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/SystemPropertyConfigSource.java
index 01ff095..6f927fd 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/SystemPropertyConfigSource.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/SystemPropertyConfigSource.java
@@ -18,31 +18,19 @@
*/
package org.apache.deltaspike.core.impl.config;
-
/**
* {@link org.apache.deltaspike.core.spi.config.ConfigSource}
- * which uses System#getProperty
+ * which uses {@link System#getProperties()}
*/
-class SystemPropertyConfigSource extends BaseConfigSource
+class SystemPropertyConfigSource extends PropertiesConfigSource
{
SystemPropertyConfigSource()
{
+ super(System.getProperties());
initOrdinal(400);
}
/**
- * The given key gets used for a lookup via System#getProperty
- *
- * @param key for the property
- * @return value for the given key or null if there is no configured value
- */
- @Override
- public String getPropertyValue(String key)
- {
- return System.getProperty(key);
- }
-
- /**
* {@inheritDoc}
*/
@Override
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/JndiUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/JndiUtils.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/JndiUtils.java
index aeb424c..1fcecc1 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/JndiUtils.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/JndiUtils.java
@@ -18,13 +18,20 @@
*/
package org.apache.deltaspike.core.impl.util;
-import org.apache.deltaspike.core.util.ClassUtils;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import javax.enterprise.inject.Typed;
import javax.naming.InitialContext;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+
+import org.apache.deltaspike.core.util.ClassUtils;
/**
* This is the internal helper class for low level access to JNDI
@@ -61,68 +68,128 @@ public abstract class JndiUtils
* @param <T> type
* @return the found instance, null otherwise
*/
- @SuppressWarnings("unchecked")
+ public static <T> T lookup(Name name, Class<? extends T> targetType)
+ {
+ try
+ {
+ return verifyLookupResult(initialContext.lookup(name), name.toString(), targetType);
+ }
+ catch (NamingException e)
+ {
+ throw new IllegalStateException("Could not get " + name + " from JNDI", e);
+ }
+ }
+
+ /**
+ * Resolves an instance for the given name.
+ *
+ * @param name current name
+ * @param targetType target type
+ * @param <T> type
+ * @return the found instance, null otherwise
+ */
public static <T> T lookup(String name, Class<? extends T> targetType)
{
try
{
- Object result = initialContext.lookup(name);
+ return verifyLookupResult(initialContext.lookup(name), name, targetType);
+ }
+ catch (NamingException e)
+ {
+ throw new IllegalStateException("Could not get " + name + " from JNDI", e);
+ }
+ }
- if (result != null)
+ /**
+ * Does a checks on given instance looked up previously from JNDI.
+ *
+ * @param name current name
+ * @param targetType target type
+ * @param <T> type
+ * @return the found instance, null otherwise
+ */
+ @SuppressWarnings("unchecked")
+ private static <T> T verifyLookupResult(Object result, String name, Class<? extends T> targetType)
+ {
+ if (result != null)
+ {
+ if (targetType.isAssignableFrom(result.getClass()))
{
- if (targetType.isAssignableFrom(result.getClass()))
- {
- // we have a value and the type fits
- return (T) result;
- }
- else if (result instanceof String) //but the target type != String
+ // we have a value and the type fits
+ return (T) result;
+ }
+ else if (result instanceof String) //but the target type != String
+ {
+ // lookedUp might be a class name
+ try
{
- // lookedUp might be a class name
- try
+ Class<?> classOfResult = ClassUtils.loadClassForName((String) result);
+ if (targetType.isAssignableFrom(classOfResult))
{
- Class<?> classOfResult = ClassUtils.loadClassForName((String) result);
- if (targetType.isAssignableFrom(classOfResult))
+ try
{
- try
- {
- return (T) classOfResult.newInstance();
- }
- catch (Exception e)
- {
- // could not create instance
- LOG.log(Level.SEVERE, "Class " + classOfResult + " from JNDI lookup for name "
- + name + " could not be instantiated", e);
- }
+ return (T) classOfResult.newInstance();
}
- else
+ catch (Exception e)
{
- // lookedUpClass does not extend/implement expectedClass
- LOG.log(Level.SEVERE, "JNDI lookup for key " + name
- + " returned class " + classOfResult.getName()
- + " which does not implement/extend the expected class"
- + targetType.getName());
+ // could not create instance
+ LOG.log(Level.SEVERE, "Class " + classOfResult + " from JNDI lookup for name "
+ + name + " could not be instantiated", e);
}
}
- catch (ClassNotFoundException cnfe)
+ else
{
- // could not find class
- LOG.log(Level.SEVERE, "Could not find Class " + result
- + " from JNDI lookup for name " + name, cnfe);
+ // lookedUpClass does not extend/implement expectedClass
+ LOG.log(Level.SEVERE, "JNDI lookup for key " + name
+ + " returned class " + classOfResult.getName()
+ + " which does not implement/extend the expected class"
+ + targetType.getName());
}
}
- else
+ catch (ClassNotFoundException cnfe)
{
- // we have a value, but the value does not fit
- LOG.log(Level.SEVERE, "JNDI lookup for key " + name + " should return a value of "
- + targetType + ", but returned " + result);
+ // could not find class
+ LOG.log(Level.SEVERE, "Could not find Class " + result
+ + " from JNDI lookup for name " + name, cnfe);
}
}
+ else
+ {
+ // we have a value, but the value does not fit
+ LOG.log(Level.SEVERE, "JNDI lookup for key " + name + " should return a value of "
+ + targetType + ", but returned " + result);
+ }
+ }
+
+ return null;
+ }
- return null;
+ /**
+ * Resolves an instances for the given naming context.
+ *
+ * @param name context name
+ * @param targetType target type
+ * @param <T> type
+ * @return the found instances, null otherwise
+ */
+ public static <T> Map<String, T> list(String name, Class<T> type)
+ {
+ try
+ {
+ Map<String, T> result = new HashMap<String, T>();
+ NameParser nameParser = initialContext.getNameParser(name);
+ NamingEnumeration<NameClassPair> enumeration = initialContext.list(name);
+ while (enumeration.hasMoreElements())
+ {
+ NameClassPair binding = enumeration.nextElement();
+ Name bindingName = nameParser.parse(name).add(binding.getName());
+ result.put(binding.getName(), lookup(bindingName, type));
+ }
+ return result;
}
catch (NamingException e)
{
- throw new IllegalStateException("Could not get " + name + " from JNDI", e);
+ throw new IllegalStateException("Could not list " + name + " from JNDI", e);
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b7e3649/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/util/JndiUtilsTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/util/JndiUtilsTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/util/JndiUtilsTest.java
index b7205ef..9dba4e1 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/util/JndiUtilsTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/util/JndiUtilsTest.java
@@ -18,8 +18,13 @@
*/
package org.apache.deltaspike.test.core.impl.util;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Map;
+
+import javax.enterprise.inject.spi.BeanManager;
+
import org.apache.deltaspike.core.impl.util.JndiUtils;
-import org.apache.deltaspike.test.category.SeCategory;
import org.apache.deltaspike.test.category.WebProfileCategory;
import org.apache.deltaspike.test.util.ArchiveUtils;
import org.jboss.arquillian.container.test.api.Deployment;
@@ -32,10 +37,6 @@ import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
-import javax.enterprise.inject.spi.BeanManager;
-
-import static org.junit.Assert.assertNotNull;
-
@RunWith(Arquillian.class)
@Category(WebProfileCategory.class)
public class JndiUtilsTest
@@ -61,4 +62,14 @@ public class JndiUtilsTest
BeanManager beanManager = JndiUtils.lookup("java:comp/BeanManager", BeanManager.class);
assertNotNull("JNDI lookup failed", beanManager);
}
+
+ /**
+ * Tests {@link JndiUtils#list(String, Class)} by digging in java:comp namespace
+ */
+ @Test
+ public void testList()
+ {
+ Map<String, BeanManager> beanManager = JndiUtils.list("java:comp", BeanManager.class);
+ assertNotNull("JNDI lookup failed", beanManager);
+ }
}