You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/07/09 17:59:13 UTC

svn commit: r962581 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/properties/ camel-core/src/test/java/org/apache/camel/component/properties/ components/camel-spring/src/test/java/org/apache/camel/component/properties/ componen...

Author: davsclaus
Date: Fri Jul  9 15:59:12 2010
New Revision: 962581

URL: http://svn.apache.org/viewvc?rev=962581&view=rev
Log:
CAMEL-2926: Camel properties component can now lookup Properties instances from the Registry. For example in OSGi registry.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java
      - copied, changed from r962552, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.java
      - copied, changed from r962552, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml
      - copied, changed from r962552, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java?rev=962581&r1=962580&r2=962581&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java Fri Jul  9 15:59:12 2010
@@ -40,7 +40,10 @@ public class DefaultPropertiesResolver i
         Properties answer = new Properties();
 
         for (String path : uri) {
-            if (path.startsWith("file:")) {
+            if (path.startsWith("ref:")) {
+                Properties prop = loadPropertiesFromRegistry(context, path);
+                answer.putAll(prop);
+            } else if (path.startsWith("file:")) {
                 Properties prop = loadPropertiesFromFilePath(context, path);
                 answer.putAll(prop);
             } else {
@@ -53,6 +56,16 @@ public class DefaultPropertiesResolver i
         return answer;
     }
 
+    protected Properties loadPropertiesFromFilePath(CamelContext context, String path) throws IOException {
+        if (path.startsWith("file:")) {
+            path = ObjectHelper.after(path, "file:");
+        }
+        InputStream is = new FileInputStream(path);
+        Properties answer = new Properties();
+        answer.load(is);
+        return answer;
+    }
+
     protected Properties loadPropertiesFromClasspath(CamelContext context, String path) throws IOException {
         if (path.startsWith("classpath:")) {
             path = ObjectHelper.after(path, "classpath:");
@@ -66,13 +79,14 @@ public class DefaultPropertiesResolver i
         return answer;
     }
 
-    protected Properties loadPropertiesFromFilePath(CamelContext context, String path) throws IOException {
-        if (path.startsWith("file:")) {
-            path = ObjectHelper.after(path, "file:");
+    protected Properties loadPropertiesFromRegistry(CamelContext context, String path) throws IOException {
+        if (path.startsWith("ref:")) {
+            path = ObjectHelper.after(path, "ref:");
+        }
+        Properties answer = context.getRegistry().lookup(path, Properties.class);
+        if (answer == null) {
+            throw new FileNotFoundException("Properties " + path + " not found in registry");
         }
-        InputStream is = new FileInputStream(path);
-        Properties answer = new Properties();
-        answer.load(is);
         return answer;
     }
 

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java (from r962552, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java&r1=962552&r2=962581&rev=962581&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java Fri Jul  9 15:59:12 2010
@@ -16,16 +16,17 @@
  */
 package org.apache.camel.component.properties;
 
+import java.util.Properties;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
 
 /**
  * @version $Revision$
  */
-public class PropertiesComponentTest extends ContextTestSupport {
+public class PropertiesComponentRefTest extends ContextTestSupport {
 
     @Override
     public boolean isUseRouteBuilder() {
@@ -36,23 +37,7 @@ public class PropertiesComponentTest ext
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("properties:{{cool.end}}");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testPropertiesComponentResult() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:mock:{{cool.result}}");
+                from("{{start}}").to("properties:{{cool.result}}");
             }
         });
         context.start();
@@ -64,146 +49,38 @@ public class PropertiesComponentTest ext
         assertMockEndpointsSatisfied();
     }
 
-    public void testPropertiesComponentMockMock() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:{{cool.mock}}:{{cool.mock}}");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:mock").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testPropertiesComponentConcat() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:cool.concat");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testPropertiesComponentLocationOverride() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:{{bar.end}}?locations=org/apache/camel/component/properties/bar.properties");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:bar").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
     public void testPropertiesComponentLocationsOverride() throws Exception {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("properties:bar.end?locations=org/apache/camel/component/properties/bar.properties");
-                from("direct:cheese").to("properties:cheese.end?locations=org/apache/camel/component/properties/bar.properties,"
-                        + "classpath:org/apache/camel/component/properties/cheese.properties");
+                from("direct:start").to("properties:{{bar.end}}?locations=ref:coolBar");
             }
         });
         context.start();
 
         getMockEndpoint("mock:bar").expectedMessageCount(1);
-        getMockEndpoint("mock:cheese").expectedMessageCount(1);
 
         template.sendBody("direct:start", "Hello World");
-        template.sendBody("direct:cheese", "Hello Cheese");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testPropertiesComponentInvalidKey() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:{{foo.unknown}}");
-            }
-        });
-        try {
-            context.start();
-            fail("Should throw exception");
-        } catch (FailedToCreateRouteException e) {
-            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
-            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
-            assertEquals("Property with key [foo.unknown] not found in properties for uri: {{foo.unknown}}", iae.getMessage());
-        }
-    }
-
-    public void testPropertiesComponentCircularReference() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:cool.a");
-            }
-        });
-        try {
-            context.start();
-            fail("Should throw exception");
-        } catch (FailedToCreateRouteException e) {
-            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
-            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
-            assertEquals("Circular reference detected with key [cool.a] in uri {{cool.a}}", iae.getMessage());
-        }
-    }
-
-    public void testPropertiesComponentCacheDefault() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // properties component can also have {{ }} around but its not needed
-                from("direct:start").to("properties:{{cool.end}}");
-                from("direct:foo").to("properties:mock:{{cool.result}}");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:result").expectedMessageCount(2);
-
-        template.sendBody("direct:start", "Hello World");
-        template.sendBody("direct:foo", "Hello Foo");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testPropertiesComponentCacheDisabled() throws Exception {
-        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
-        pc.setCache(false);
-
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("direct:start").to("properties:cool.end");
-                from("direct:foo").to("properties:mock:{{cool.result}}");
-            }
-        });
-        context.start();
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
 
-        getMockEndpoint("mock:result").expectedMessageCount(2);
+        Properties cool = new Properties();
+        cool.put("cool.end", "mock:end");
+        cool.put("cool.result", "mock:result");
+        cool.put("start", "direct:start");
+        jndi.bind("myCoolProperties", cool);
+
+        Properties bar = new Properties();
+        bar.put("bar.end", "mock:bar");
+        bar.put("dude", "direct:bar");
+        jndi.bind("coolBar", bar);
 
-        template.sendBody("direct:start", "Hello World");
-        template.sendBody("direct:foo", "Hello Foo");
-
-        assertMockEndpointsSatisfied();
+        return jndi;
     }
 
     @Override
@@ -212,10 +89,10 @@ public class PropertiesComponentTest ext
 
         PropertiesComponent pc = new PropertiesComponent();
         pc.setCamelContext(context);
-        pc.setLocations(new String[]{"classpath:org/apache/camel/component/properties/myproperties.properties"});
+        pc.setLocations(new String[]{"ref:myCoolProperties"});
         context.addComponent("properties", pc);
 
         return context;
     }
 
-}
+}
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.java (from r962552, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentTest.java&r1=962552&r2=962581&rev=962581&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.java Fri Jul  9 15:59:12 2010
@@ -23,11 +23,11 @@ import org.springframework.context.suppo
 /**
  * @version $Revision$
  */
-public class SpringPropertiesComponentTest extends SpringTestSupport {
+public class SpringPropertiesComponentRefTest extends SpringTestSupport {
 
     @Override
     protected AbstractXmlApplicationContext createApplicationContext() {
-        return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesComponentTest.xml");
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml");
     }
 
     public void testSpringPropertiesComponentStart() throws Exception {
@@ -38,27 +38,4 @@ public class SpringPropertiesComponentTe
         assertMockEndpointsSatisfied();
     }
 
-    public void testSpringPropertiesComponentBar() throws Exception {
-        getMockEndpoint("mock:bar").expectedMessageCount(1);
-
-        template.sendBody("direct:bar", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testSpringPropertiesComponentStart2() throws Exception {
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        template.sendBody("direct:start2", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testSpringPropertiesComponentBar2() throws Exception {
-        getMockEndpoint("mock:bar").expectedMessageCount(1);
-
-        template.sendBody("direct:bar2", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-}
+}
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml (from r962552, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentTest.xml&r1=962552&r2=962581&rev=962581&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentRefTest.xml Fri Jul  9 15:59:12 2010
@@ -22,31 +22,17 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
+    <bean id="coolProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
         <property name="location" value="classpath:org/apache/camel/component/properties/cheese.properties"/>
     </bean>
 
     <camelContext xmlns="http://camel.apache.org/schema/spring">
-
+        <propertyPlaceholder id="properties" location="ref:coolProperties"/>
         <route>
             <from uri="direct:start"/>
-            <to uri="properties:{{cool.end}}"/>
-        </route>
-
-        <route>
-            <from uri="direct:bar"/>
-            <to uri="properties:mock:{{cool.bar}}"/>
-        </route>
-
-        <route>
-            <from uri="direct:start2"/>
             <to uri="{{cool.end}}"/>
         </route>
 
-        <route>
-            <from uri="direct:bar2"/>
-            <to uri="mock:{{cool.bar}}"/>
-        </route>
     </camelContext>
 
 </beans>