You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/04/16 10:37:29 UTC

svn commit: r529167 - in /activemq/camel/trunk/camel-josql: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/camel/ src/main/java/org/apache/camel/builder/ src/main/java/org/apache/camel/builder/sql...

Author: jstrachan
Date: Mon Apr 16 01:37:25 2007
New Revision: 529167

URL: http://svn.apache.org/viewvc?view=rev&rev=529167
Log:
added support for SQL expressions and predicates on POJOs using JoSQL

Added:
    activemq/camel/trunk/camel-josql/
    activemq/camel/trunk/camel-josql/pom.xml
    activemq/camel/trunk/camel-josql/src/
    activemq/camel/trunk/camel-josql/src/main/
    activemq/camel/trunk/camel-josql/src/main/java/
    activemq/camel/trunk/camel-josql/src/main/java/org/
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
    activemq/camel/trunk/camel-josql/src/test/
    activemq/camel/trunk/camel-josql/src/test/java/
    activemq/camel/trunk/camel-josql/src/test/java/org/
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/Person.java
    activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/SqlTest.java

Added: activemq/camel/trunk/camel-josql/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-josql/pom.xml?view=auto&rev=529167
==============================================================================
--- activemq/camel/trunk/camel-josql/pom.xml (added)
+++ activemq/camel/trunk/camel-josql/pom.xml Mon Apr 16 01:37:25 2007
@@ -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 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.
+-->
+
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.camel</groupId>
+    <artifactId>camel-parent</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>camel-josql</artifactId>
+  <name>Camel :: JoSQL</name>
+  <description>Camel JoSQL support</description>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+      <type>test-jar</type>
+      <optional>true</optional>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-jms_1.1_spec</artifactId>
+    </dependency>
+
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>

Added: activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java?view=auto&rev=529167
==============================================================================
--- activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java (added)
+++ activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java Mon Apr 16 01:37:25 2007
@@ -0,0 +1,138 @@
+/*
+ * 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.builder.sql;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Message;
+import org.apache.camel.Predicate;
+import org.apache.camel.RuntimeExpressionException;
+import org.josql.Query;
+import org.josql.QueryExecutionException;
+import org.josql.QueryParseException;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.HashMap;
+
+/**
+ * A builder of SQL {@link org.apache.camel.Expression} and {@link org.apache.camel.Predicate} implementations
+ *
+ * @version $Revision: $
+ */
+public class SqlBuilder<E extends Exchange> implements Expression<E>, Predicate<E> {
+
+    private Query query;
+    private Map<String,Object> variables = new HashMap<String, Object>();
+
+    public SqlBuilder(Query query) {
+        this.query = query;
+    }
+
+    public Object evaluate(E exchange) {
+        return evaluateQuery(exchange);
+    }
+
+    public boolean matches(E exchange) {
+        List list = evaluateQuery(exchange);
+        if (!list.isEmpty()) {
+            Object value = list.get(0);
+            if (value instanceof Boolean) {
+                Boolean flag = (Boolean) value;
+                return flag.booleanValue();
+            }
+            else {
+                // lets assume non-empty results are true
+                return true;
+            }
+        }
+        return false;
+    }
+
+    // Builder API
+    //-----------------------------------------------------------------------
+
+    /**
+     * Creates a new builder for the given SQL query string
+     *
+     * @param sql the SQL query to perform
+     * @return a new builder
+     * @throws QueryParseException if there is an issue with the SQL
+     */
+    public static <E extends Exchange> SqlBuilder<E> sql(String sql) throws QueryParseException {
+        Query q = new Query();
+        q.parse(sql);
+        return new SqlBuilder(q);
+    }
+
+    /**
+     * Adds the variable value to be used by the SQL query
+     */
+    public SqlBuilder<E> variable(String name, Object value) {
+        getVariables().put(name, value);
+        return this;
+    }
+
+
+    // Properties
+    //-----------------------------------------------------------------------
+    public Map<String, Object> getVariables() {
+        return variables;
+    }
+
+    public void setVariables(Map<String, Object> properties) {
+        this.variables = properties;
+    }
+
+
+    // Implementation methods
+    //-----------------------------------------------------------------------
+    protected List evaluateQuery(E exchange) {
+        configureQuery(exchange);
+        Message in = exchange.getIn();
+        List list = in.getBody(List.class);
+        if (list == null) {
+            list = Collections.singletonList(in.getBody());
+        }
+        try {
+            return query.execute(list).getResults();
+        }
+        catch (QueryExecutionException e) {
+            throw new RuntimeExpressionException(e);
+        }
+    }
+
+    protected void configureQuery(E exchange) {
+        // lets pass in the headers as variables that the SQL can use
+        addVariables(exchange.getProperties());
+        addVariables(exchange.getIn().getHeaders());
+        addVariables(getVariables());
+
+        query.setVariable("exchange", exchange);
+        query.setVariable("in", exchange.getIn());
+        query.setVariable("out", exchange.getOut());
+    }
+
+    protected void addVariables(Map <String, Object> map) {
+        Set<Map.Entry<String, Object>> propertyEntries = map.entrySet();
+        for (Map.Entry<String, Object> entry : propertyEntries) {
+            query.setVariable(entry.getKey(), entry.getValue());
+        }
+    }
+}

Added: activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/Person.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/Person.java?view=auto&rev=529167
==============================================================================
--- activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/Person.java (added)
+++ activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/Person.java Mon Apr 16 01:37:25 2007
@@ -0,0 +1,54 @@
+/*
+ * 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.builder.sql;
+
+/**
+ * @version $Revision: $
+ */
+public class Person {
+    private String name;
+    private String city;
+
+
+    public Person() {
+    }
+
+    public Person(String name, String city) {
+        this.name = name;
+        this.city = city;
+    }
+
+    public String toString() {
+        return "Person[" + name + "]";
+    }
+
+    public String getCity() {
+        return city;
+    }
+
+    public void setCity(String city) {
+        this.city = city;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

Added: activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/SqlTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/SqlTest.java?view=auto&rev=529167
==============================================================================
--- activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/SqlTest.java (added)
+++ activemq/camel/trunk/camel-josql/src/test/java/org/apache/camel/builder/sql/SqlTest.java Mon Apr 16 01:37:25 2007
@@ -0,0 +1,92 @@
+/*
+ * 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.builder.sql;
+
+import static org.apache.camel.builder.sql.SqlBuilder.sql;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Message;
+import org.apache.camel.TestSupport;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+
+import java.util.List;
+
+/**
+ * @version $Revision: $
+ */
+public class SqlTest extends TestSupport {
+
+    protected CamelContext context = new DefaultCamelContext();
+    protected Exchange exchange;
+
+    public void testExpression() throws Exception {
+        Expression expression = sql("SELECT * FROM org.apache.camel.builder.sql.Person where city = 'London'");
+        Object value = expression.evaluate(exchange);
+        assertIsInstanceOf(List.class, value);
+
+        List list = (List) value;
+        assertEquals("List size", 2, list.size());
+
+        for (Object person : list) {
+            log.info("Found: " + person);
+        }
+    }
+
+    public void testExpressionWithHeaderVariable() throws Exception {
+        Expression expression = sql("SELECT * FROM org.apache.camel.builder.sql.Person where name = :fooHeader");
+        Object value = expression.evaluate(exchange);
+        assertIsInstanceOf(List.class, value);
+
+        List<Person> list = (List<Person>) value;
+        assertEquals("List size", 1, list.size());
+
+        for (Person person : list) {
+            log.info("Found: " + person);
+
+            assertEquals("name", "James", person.getName());
+        }
+    }
+
+    public void testPredicates() throws Exception {
+        assertPredicate(sql("SELECT * FROM org.apache.camel.builder.sql.Person where city = 'London'"), exchange, true);
+        assertPredicate(sql("SELECT * FROM org.apache.camel.builder.sql.Person where city = 'Manchester'"), exchange, false);
+    }
+
+    public void testPredicateWithHeaderVariable() throws Exception {
+        assertPredicate(sql("SELECT * FROM org.apache.camel.builder.sql.Person where name = :fooHeader"), exchange, true);
+    }
+
+    protected void setUp() throws Exception {
+        exchange = createExchange();
+    }
+
+    protected Exchange createExchange() {
+        Exchange exchange = new DefaultExchange(context);
+        Message message = exchange.getIn();
+        message.setHeader("fooHeader", "James");
+
+        Person[] people = {new Person("James", "London"),
+                new Person("Guillaume", "Normandy"),
+                new Person("Rob", "London"),
+                new Person("Hiram", "Tampa")};
+
+        message.setBody(people);
+        return exchange;
+    }
+}