camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r563503 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/util/jndi/ test/java/org/apache/camel/util/jndi/ test/resources/
Date Tue, 07 Aug 2007 14:03:04 GMT
Author: jstrachan
Date: Tue Aug  7 07:03:03 2007
New Revision: 563503

URL: http://svn.apache.org/viewvc?view=rev&rev=563503
Log:
minor patch to the JNDI provider to allow it to easily populate itself with JavaBeans using
simple string injection

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java
  (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/JndiContext.java
    activemq/camel/trunk/camel-core/src/test/resources/jndi.properties

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java?view=diff&rev=563503&r1=563502&r2=563503
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
Tue Aug  7 07:03:03 2007
@@ -31,6 +31,6 @@
 public class CamelInitialContextFactory implements InitialContextFactory {
 
     public Context getInitialContext(Hashtable environment) throws NamingException {
-        return new JndiContext(environment, environment);
+        return new JndiContext(environment);
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/JndiContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/JndiContext.java?view=diff&rev=563503&r1=563502&r2=563503
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/JndiContext.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/JndiContext.java
Tue Aug  7 07:03:03 2007
@@ -16,26 +16,18 @@
  */
 package org.apache.camel.util.jndi;
 
-import javax.naming.Binding;
-import javax.naming.CompositeName;
-import javax.naming.Context;
-import javax.naming.LinkRef;
-import javax.naming.Name;
-import javax.naming.NameClassPair;
-import javax.naming.NameNotFoundException;
-import javax.naming.NameParser;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.naming.NotContextException;
-import javax.naming.OperationNotSupportedException;
-import javax.naming.Reference;
+import org.apache.camel.impl.ReflectionInjector;
+import org.apache.camel.spi.Injector;
+import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
+
+import javax.naming.*;
 import javax.naming.spi.NamingManager;
 import java.io.Serializable;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * A default JNDI context
@@ -44,28 +36,56 @@
  */
 public class JndiContext implements Context, Serializable {
     public static final String SEPARATOR = "/";
-
     private static final long serialVersionUID = -5754338187296859149L;
-
     protected static final NameParser nameParser = new NameParser() {
         public Name parse(String name) throws NamingException {
             return new CompositeName(name);
         }
     };
-
+    protected static Injector injector = new ReflectionInjector();
     private final Hashtable environment;        // environment for this context
     private final Map bindings;         // bindings at my level
     private final Map treeBindings;     // all bindings under me
-
     private boolean frozen = false;
     private String nameInNamespace = "";
 
+    /**
+     * A helper method to create the JNDI bindings from the input environment properties
+     * using $foo.class to point to a class name with $foo.* being properties set on the
injected bean
+     */
+    public static Map createBindingsMapFromEnvironment(Hashtable env) {
+        Map answer = new HashMap(env);
+
+        for (Object object : env.entrySet()) {
+            Map.Entry entry = (Map.Entry) object;
+            Object key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (key instanceof String && value instanceof String) {
+                String keyText = (String) key;
+                String valueText = (String) value;
+                if (keyText.endsWith(".class")) {
+                    Class<?> type = ObjectHelper.loadClass(valueText);
+                    if (type != null) {
+                        String newEntry = keyText.substring(0, keyText.length() - ".class".length());
+                        Object bean = createBean(type, answer, newEntry + ".");
+                        if (bean != null) {
+                            answer.put(newEntry, bean);
+                        }
+                    }
+                }
+            }
+        }
+
+        return answer;
+    }
+
     public JndiContext() {
         this(new Hashtable());
     }
 
     public JndiContext(Hashtable env) {
-        this(env, new ConcurrentHashMap());
+        this(env, createBindingsMapFromEnvironment(env));
     }
 
     public JndiContext(Hashtable environment, Map bindings) {
@@ -410,5 +430,11 @@
             Map.Entry entry = getNext();
             return new Binding((String) entry.getKey(), entry.getValue());
         }
+    }
+
+    protected static Object createBean(Class<?> type, Map properties, String prefix)
{
+        Object value = injector.newInstance(type);
+        IntrospectionSupport.setProperties(value, properties, prefix);
+        return value;
     }
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java?view=auto&rev=563503
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java
Tue Aug  7 07:03:03 2007
@@ -0,0 +1,46 @@
+/**
+ *
+ * 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.camel.util.jndi;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class ExampleBean {
+    private String name;
+    private double price;
+
+    public String toString() {
+        return "ExampleBean[name: " + name + " price: " + price + "]";
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/ExampleBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java?view=auto&rev=563503
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java
Tue Aug  7 07:03:03 2007
@@ -0,0 +1,57 @@
+/**
+ *
+ * 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.camel.util.jndi;
+
+import org.apache.camel.TestSupport;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class JndiTest extends TestSupport {
+    protected Context context;
+
+    public void testLookupOfSimpleName() throws Exception {
+        Object value = assertLookup("foo");
+        assertEquals("foo", "bar", value);
+    }
+
+    public void testLookupOfTypedObject() throws Exception {
+        Object value = assertLookup("example");
+        ExampleBean bean = assertIsInstanceOf(ExampleBean.class, value);
+        assertEquals("Bean.name", "James", bean.getName());
+        assertEquals("Bean.price", 2.34, bean.getPrice());
+
+        log.info("Found bean: " + bean);
+    }
+
+    protected Object assertLookup(String name) throws NamingException {
+        Object value = context.lookup(name);
+        assertNotNull("Should have found JNDI entry: " + name + " in context: " + context,
value);
+        return value;
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        context = new InitialContext();
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/test/resources/jndi.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/resources/jndi.properties?view=diff&rev=563503&r1=563502&r2=563503
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/resources/jndi.properties (original)
+++ activemq/camel/trunk/camel-core/src/test/resources/jndi.properties Tue Aug  7 07:03:03
2007
@@ -22,4 +22,8 @@
 # the following properties will be copied into the context
 foo = bar
 
+example.class = org.apache.camel.util.jndi.ExampleBean
+example.name = James
+example.price = 2.34
+
 # END SNIPPET: jndi



Mime
View raw message