activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r521675 - in /activemq/camel/trunk: ./ camel-script/ camel-script/src/ camel-script/src/main/ camel-script/src/main/java/ camel-script/src/main/java/org/ camel-script/src/main/java/org/apache/ camel-script/src/main/java/org/apache/camel/ ca...
Date Fri, 23 Mar 2007 11:20:03 GMT
Author: jstrachan
Date: Fri Mar 23 04:20:01 2007
New Revision: 521675

URL: http://svn.apache.org/viewvc?view=rev&rev=521675
Log:
added a script module for JSR 223 support; also tidied up the spring dependency pom stuff

Added:
    activemq/camel/trunk/camel-script/
    activemq/camel/trunk/camel-script/pom.xml   (with props)
    activemq/camel/trunk/camel-script/src/
    activemq/camel/trunk/camel-script/src/main/
    activemq/camel/trunk/camel-script/src/main/java/
    activemq/camel/trunk/camel-script/src/main/java/org/
    activemq/camel/trunk/camel-script/src/main/java/org/apache/
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
  (with props)
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
  (with props)
    activemq/camel/trunk/camel-script/src/main/resources/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/services/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/services/org/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/services/org/apache/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/services/org/apache/camel/
    activemq/camel/trunk/camel-script/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/
    activemq/camel/trunk/camel-script/src/test/
    activemq/camel/trunk/camel-script/src/test/java/
    activemq/camel/trunk/camel-script/src/test/java/org/
    activemq/camel/trunk/camel-script/src/test/java/org/apache/
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
  (with props)
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
  (with props)
    activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
  (with props)
    activemq/camel/trunk/camel-script/src/test/resources/
Modified:
    activemq/camel/trunk/camel-spring/pom.xml
    activemq/camel/trunk/pom.xml

Added: activemq/camel/trunk/camel-script/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/pom.xml?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/pom.xml (added)
+++ activemq/camel/trunk/camel-script/pom.xml Fri Mar 23 04:20:01 2007
@@ -0,0 +1,79 @@
+<?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-script</artifactId>
+  <name>Camel :: Script</name>
+  <description>Camel Script support</description>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+    </dependency>
+
+    <!-- TODO is there a jsr223 jar anywhere else? -->
+    <!-- TODO add this to a profile for Java 5 only  -->
+    <dependency>
+      <groupId>org.livetribe</groupId>
+      <artifactId>livetribe-jsr223</artifactId>
+      <version>2.0.0</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>groovy</groupId>
+      <artifactId>groovy-all</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.script</groupId>
+      <artifactId>groovy-engine</artifactId>
+      <version>20070112</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>

Propchange: activemq/camel/trunk/camel-script/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
(added)
+++ activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
Fri Mar 23 04:20:01 2007
@@ -0,0 +1,459 @@
+/**
+ *
+ * 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.script;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.Processor;
+import org.apache.camel.util.ObjectHelper;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.UrlResource;
+
+import javax.script.Compilable;
+import javax.script.CompiledScript;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A builder class for creating {@link Processor}, {@link Expression} and {@link Predicate}
objects using
+ * the JSR 223 scripting engine.
+ *
+ * @version $Revision$
+ */
+public class ScriptBuilder<E extends Exchange> implements Expression<E>, Predicate<E>,
Processor<E> {
+    private String scriptEngineName;
+    private Resource scriptResource;
+    private String scriptText;
+    private ScriptEngine engine;
+    private CompiledScript compiledScript;
+
+    public ScriptBuilder(String scriptEngineName, String scriptText) {
+        this.scriptEngineName = scriptEngineName;
+        this.scriptText = scriptText;
+    }
+
+    public ScriptBuilder(String scriptEngineName, Resource scriptResource) {
+        this.scriptEngineName = scriptEngineName;
+        this.scriptResource = scriptResource;
+    }
+
+    @Override
+    public String toString() {
+        return getScriptDescription();
+    }
+
+    public Object evaluate(E exchange) {
+        return evaluateScript(exchange);
+    }
+
+    public boolean matches(E exchange) {
+        return ObjectHelper.toBoolean(evaluateScript(exchange));
+    }
+
+    public void onExchange(E exchange) {
+        evaluateScript(exchange);
+    }
+
+    
+    // Builder API
+    //-------------------------------------------------------------------------
+
+    /**
+     * Sets the attribute on the context so that it is available to the script as a variable
+     * in the {@link ScriptContext#ENGINE_SCOPE}
+     *
+     * @param name the name of the attribute
+     * @param value the attribute value
+     * @return this builder
+     */
+    public ScriptBuilder attribute(String name, Object value) {
+        getScriptContext().setAttribute(name, value, ScriptContext.ENGINE_SCOPE);
+        return this;
+    }
+
+
+    // Create any scripting language builder recognised by JSR 223
+    //-------------------------------------------------------------------------
+
+    /**
+     * Creates a script builder for the named language and script contents
+     *
+     * @param language the language to use for the script
+     * @param scriptText the script text to be evaluted
+     * @return the builder
+     */
+    public static ScriptBuilder script(String language, String scriptText) {
+        return new ScriptBuilder(language, scriptText);
+    }
+
+    /**
+     * Creates a script builder for the named language and script @{link Resource}
+     *
+     * @param language the language to use for the script
+     * @param scriptResource the resource used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder script(String language, Resource scriptResource) {
+        return new ScriptBuilder(language, scriptResource);
+    }
+
+    /**
+     * Creates a script builder for the named language and script @{link File}
+     *
+     * @param language the language to use for the script
+     * @param scriptFile the file used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder script(String language, File scriptFile) {
+        return new ScriptBuilder(language, new FileSystemResource(scriptFile));
+    }
+
+    /**
+     * Creates a script builder for the named language and script @{link URL}
+     *
+     * @param language the language to use for the script
+     * @param scriptURL the URL used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder script(String language, URL scriptURL) {
+        return new ScriptBuilder(language, new UrlResource(scriptURL));
+    }
+
+
+    // Groovy
+    //-------------------------------------------------------------------------
+
+    /**
+     * Creates a script builder for the groovy script contents
+     *
+     * @param scriptText the script text to be evaluted
+     * @return the builder
+     */
+    public static ScriptBuilder groovy(String scriptText) {
+        return new ScriptBuilder("groovy", scriptText);
+    }
+
+    /**
+     * Creates a script builder for the groovy script @{link Resource}
+     *
+     * @param scriptResource the resource used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder groovy(Resource scriptResource) {
+        return new ScriptBuilder("groovy", scriptResource);
+    }
+
+    /**
+     * Creates a script builder for the groovy script @{link File}
+     *
+     * @param scriptFile the file used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder groovy(File scriptFile) {
+        return new ScriptBuilder("groovy", new FileSystemResource(scriptFile));
+    }
+
+    /**
+     * Creates a script builder for the groovy script @{link URL}
+     *
+     * @param scriptURL the URL used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder groovy(URL scriptURL) {
+        return new ScriptBuilder("groovy", new UrlResource(scriptURL));
+    }
+
+
+    // JavaScript
+    //-------------------------------------------------------------------------
+
+    /**
+     * Creates a script builder for the JavaScript/ECMAScript script contents
+     *
+     * @param scriptText the script text to be evaluted
+     * @return the builder
+     */
+    public static ScriptBuilder javaScript(String scriptText) {
+        return new ScriptBuilder("js", scriptText);
+    }
+
+    /**
+     * Creates a script builder for the JavaScript/ECMAScript script @{link Resource}
+     *
+     * @param scriptResource the resource used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder javaScript(Resource scriptResource) {
+        return new ScriptBuilder("js", scriptResource);
+    }
+
+    /**
+     * Creates a script builder for the JavaScript/ECMAScript script @{link File}
+     *
+     * @param scriptFile the file used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder javaScript(File scriptFile) {
+        return new ScriptBuilder("js", new FileSystemResource(scriptFile));
+    }
+
+    /**
+     * Creates a script builder for the JavaScript/ECMAScript script @{link URL}
+     *
+     * @param scriptURL the URL used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder javaScript(URL scriptURL) {
+        return new ScriptBuilder("js", new UrlResource(scriptURL));
+    }
+
+
+
+    // PHP
+    //-------------------------------------------------------------------------
+
+    /**
+     * Creates a script builder for the PHP script contents
+     *
+     * @param scriptText the script text to be evaluted
+     * @return the builder
+     */
+    public static ScriptBuilder php(String scriptText) {
+        return new ScriptBuilder("php", scriptText);
+    }
+
+    /**
+     * Creates a script builder for the PHP script @{link Resource}
+     *
+     * @param scriptResource the resource used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder php(Resource scriptResource) {
+        return new ScriptBuilder("php", scriptResource);
+    }
+
+    /**
+     * Creates a script builder for the PHP script @{link File}
+     *
+     * @param scriptFile the file used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder php(File scriptFile) {
+        return new ScriptBuilder("php", new FileSystemResource(scriptFile));
+    }
+
+    /**
+     * Creates a script builder for the PHP script @{link URL}
+     *
+     * @param scriptURL the URL used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder php(URL scriptURL) {
+        return new ScriptBuilder("php", new UrlResource(scriptURL));
+    }
+
+
+    // JRuby
+    //-------------------------------------------------------------------------
+
+    /**
+     * Creates a script builder for the Ruby/JRuby script contents
+     *
+     * @param scriptText the script text to be evaluted
+     * @return the builder
+     */
+    public static ScriptBuilder ruby(String scriptText) {
+        return new ScriptBuilder("jruby", scriptText);
+    }
+
+    /**
+     * Creates a script builder for the Ruby/JRuby script @{link Resource}
+     *
+     * @param scriptResource the resource used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder ruby(Resource scriptResource) {
+        return new ScriptBuilder("jruby", scriptResource);
+    }
+
+    /**
+     * Creates a script builder for the Ruby/JRuby script @{link File}
+     *
+     * @param scriptFile the file used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder ruby(File scriptFile) {
+        return new ScriptBuilder("jruby", new FileSystemResource(scriptFile));
+    }
+
+    /**
+     * Creates a script builder for the Ruby/JRuby script @{link URL}
+     *
+     * @param scriptURL the URL used to load the script
+     * @return the builder
+     */
+    public static ScriptBuilder ruby(URL scriptURL) {
+        return new ScriptBuilder("jruby", new UrlResource(scriptURL));
+    }
+
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public ScriptEngine getEngine() {
+        checkInitialised();
+        return engine;
+    }
+
+    public CompiledScript getCompiledScript() {
+        return compiledScript;
+    }
+
+    public String getScriptText() {
+        return scriptText;
+    }
+
+    public String getScriptEngineName() {
+        return scriptEngineName;
+    }
+
+    /**
+     * Returns a description of the script
+     *
+     * @return the script description
+     */
+    public String getScriptDescription() {
+        if (scriptText != null) {
+            return scriptEngineName + ": " + scriptText;
+        }
+        else {
+            return scriptEngineName + ": " + scriptResource.getDescription();
+        }
+    }
+
+    /**
+     * Access the script context so that it can be configured such as adding attributes
+     */
+    public ScriptContext getScriptContext() {
+        return getEngine().getContext();
+    }
+
+    /**
+     * Sets the context to use by the script
+     */
+    public void setScriptContext(ScriptContext scriptContext) {
+        getEngine().setContext(scriptContext);
+    }
+
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected void checkInitialised() {
+        if (engine == null) {
+            engine = createScriptEngine();
+        }
+        if (compiledScript == null) {
+            if (engine instanceof Compilable) {
+                compileScript((Compilable) engine);
+            }
+        }
+    }
+    protected ScriptEngine createScriptEngine() {
+        ScriptEngineManager manager = new ScriptEngineManager();
+        return manager.getEngineByName(scriptEngineName);
+    }
+
+    protected void compileScript(Compilable compilable) {
+        try {
+            if (scriptText != null) {
+                compiledScript = compilable.compile(scriptText);
+            }
+            else if (scriptResource != null) {
+                compiledScript = compilable.compile(createScriptReader());
+            }
+        }
+        catch (ScriptException e) {
+            throw createScriptCompileException(e);
+        }
+        catch (IOException e) {
+            throw createScriptCompileException(e);
+        }
+    }
+
+    protected synchronized Object evaluateScript(Exchange exchange) {
+        try {
+            getScriptContext();
+            populateBindings(getEngine(), exchange);
+            return runScript();
+        }
+        catch (ScriptException e) {
+            throw createScriptEvaluationException(e.getCause());
+        }
+        catch (IOException e) {
+            throw createScriptEvaluationException(e);
+        }
+    }
+
+
+    protected Object runScript() throws ScriptException, IOException {
+        checkInitialised();
+        if (compiledScript != null) {
+            return compiledScript.eval();
+        }
+        else {
+            if (scriptText != null) {
+                return getEngine().eval(scriptText);
+            }
+            else {
+                return getEngine().eval(createScriptReader());
+            }
+        }
+    }
+
+    protected void populateBindings(ScriptEngine engine, Exchange exchange) {
+        ScriptContext context = engine.getContext();
+        int scope = ScriptContext.ENGINE_SCOPE;
+        context.setAttribute("context", exchange.getContext(), scope);
+        context.setAttribute("exchange", exchange, scope);
+        context.setAttribute("request", exchange.getIn(), scope);
+        context.setAttribute("response", exchange.getOut(), scope);
+    }
+
+    protected InputStreamReader createScriptReader() throws IOException {
+        // TODO consider character sets?
+        return new InputStreamReader(scriptResource.getInputStream());
+    }
+
+    protected ScriptEvaluationException createScriptCompileException(Exception e) {
+        return new ScriptEvaluationException("Failed to compile: " + getScriptDescription()
+ ". Cause: " + e, e);
+    }
+
+    protected ScriptEvaluationException createScriptEvaluationException(Throwable e) {
+        return new ScriptEvaluationException("Failed to evaluate: " + getScriptDescription()
+ ". Cause: " + e, e);
+    }
+
+}

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
(added)
+++ activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
Fri Mar 23 04:20:01 2007
@@ -0,0 +1,40 @@
+/**
+ *
+ * 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.script;
+
+import org.apache.camel.RuntimeCamelException;
+
+/**
+ * An exception thrown if the script evaluation fails
+ * 
+ * @version $Revision$
+ */
+public class ScriptEvaluationException extends RuntimeCamelException {
+
+    public ScriptEvaluationException(String message) {
+        super(message);
+    }
+
+    public ScriptEvaluationException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public ScriptEvaluationException(Throwable cause) {
+        super(cause);
+    }
+}

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptEvaluationException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
(added)
+++ activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
Fri Mar 23 04:20:01 2007
@@ -0,0 +1,95 @@
+/**
+ *
+ * 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.script;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import static org.apache.camel.builder.script.ScriptBuilder.*;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @version $Revision$
+ */
+public class GroovyTest extends ScriptTestSupport {
+    private static final transient Log log = LogFactory.getLog(GroovyTest.class);
+
+    protected Exchange exchange;
+
+    public void testExpressionReturnsTheCorrectValue() throws Exception {
+        assertExpression(groovy("exchange.in.headers['foo.bar']"), exchange, "cheese");
+        assertExpression(groovy("exchange.in.headers.name"), exchange, "James");
+        assertExpression(groovy("exchange.in.headers['doesNotExist']"), exchange, null);
+    }
+    
+    public void testPredicateUsingScriptAttribute() throws Exception {
+        assertPredicate(groovy("request.headers.name == hacker").attribute("hacker", "James"),
exchange, true);
+    }
+
+    public void testPredicateEvaluation() throws Exception {
+        assertPredicate(groovy("exchange.in.headers.name == 'James'"), exchange, true);
+        assertPredicate(groovy("exchange.in.headers.name == 'Hiram'"), exchange, false);
+
+        assertPredicate(groovy("request.headers.name == 'James'"), exchange, true);
+    }
+
+    public void testInvalidExpressionFailsWithMeaningfulException() throws Exception {
+        try {
+            groovy("exchange.doesNotExist").evaluate(exchange);
+            fail("This test case should have thrown an exception!");
+        }
+        catch (ScriptEvaluationException e) {
+            log.debug("Caught expected exception: " + e, e);
+            String message = e.getMessage();
+            assertTrue("The message should include 'doesNotExist' but was: " + message, message.contains("doesNotExist"));
+        }
+    }
+
+    /**
+     * Asserts that the given expression when evaluated returns the given answer
+     */
+    protected void assertExpression(Expression expression, Exchange exchange, Object expected)
{
+        Object value = expression.evaluate(exchange);
+        
+        log.debug("Evaluated expression: " + expression + " on exchange: " + exchange + "
result: " + value);
+
+        assertEquals("Expression: " + expression + " on Exchange: " + exchange, expected,
value);
+    }
+
+    /**
+     * Asserts that the predicate returns the expected value on the exchange
+     */
+    protected void assertPredicate(Predicate expression, Exchange exchange, boolean expected)
{
+        boolean value = expression.matches(exchange);
+
+        log.debug("Evaluated predicate: " + expression + " on exchange: " + exchange + "
result: " + value);
+
+        assertEquals("Predicate: " + expression + " on Exchange: " + exchange, expected,
value);
+    }
+
+
+    @Override
+    protected void setUp() throws Exception {
+        exchange = new DefaultExchange(new DefaultCamelContext());
+        exchange.getIn().setHeader("foo.bar", "cheese");
+        exchange.getIn().setHeader("name", "James");
+    }
+}

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/GroovyTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
(added)
+++ activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
Fri Mar 23 04:20:01 2007
@@ -0,0 +1,36 @@
+/**
+ *
+ * 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.script;
+
+import junit.framework.TestCase;
+
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptEngineFactory;
+
+/**
+ * @version $Revision$
+ */
+public class Jsr223Test extends TestCase {
+    public void testLanguageNames() throws Exception {
+        ScriptEngineManager manager = new ScriptEngineManager();
+        ScriptEngineFactory[] factories = manager.getEngineFactories();
+        for (ScriptEngineFactory factory : factories) {
+            System.out.println("Factory: " + factory.getNames() + " " + factory.getEngineName());
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/Jsr223Test.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java?view=auto&rev=521675
==============================================================================
--- activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
(added)
+++ activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
Fri Mar 23 04:20:01 2007
@@ -0,0 +1,26 @@
+/**
+ *
+ * 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.script;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public abstract class ScriptTestSupport extends TestCase {
+}

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-script/src/test/java/org/apache/camel/builder/script/ScriptTestSupport.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-spring/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/pom.xml?view=diff&rev=521675&r1=521674&r2=521675
==============================================================================
--- activemq/camel/trunk/camel-spring/pom.xml (original)
+++ activemq/camel/trunk/camel-spring/pom.xml Fri Mar 23 04:20:01 2007
@@ -58,41 +58,6 @@
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring</artifactId>
-      <version>2.0</version>
-      <exclusions>
-        <exclusion>
-          <groupId>javax.mail</groupId>
-          <artifactId>mail</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>javax.resource</groupId>
-          <artifactId>connector</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>javax.transaction</groupId>
-          <artifactId>jta</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>org.springframework</groupId>
-          <artifactId>spring-support</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>org.springframework</groupId>
-          <artifactId>spring-orm</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>org.springframework</groupId>
-          <artifactId>spring-hibernate</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>org.springframework</groupId>
-          <artifactId>spring-remoting</artifactId>
-        </exclusion>
-        <exclusion>
-          <groupId>org.springframework</groupId>
-          <artifactId>spring-core</artifactId>
-        </exclusion>
-      </exclusions>
     </dependency>
 
     <dependency>

Modified: activemq/camel/trunk/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/pom.xml?view=diff&rev=521675&r1=521674&r2=521675
==============================================================================
--- activemq/camel/trunk/pom.xml (original)
+++ activemq/camel/trunk/pom.xml Fri Mar 23 04:20:01 2007
@@ -117,6 +117,7 @@
     <module>camel-jms</module>
     <module>camel-jbi</module>
     <module>camel-mina</module>
+    <module>camel-script</module>
     <module>camel-spring</module>
   </modules>
 
@@ -317,6 +318,27 @@
         <artifactId>jetty</artifactId>
         <version>6.1.2rc1</version>
       </dependency>
+
+
+      <!-- optional scripting support -->
+      <dependency>
+        <groupId>org.livetribe</groupId>
+        <artifactId>livetribe-jsr223</artifactId>
+        <version>2.0.0</version>
+      </dependency>
+      <dependency>
+        <groupId>groovy</groupId>
+        <artifactId>groovy-all</artifactId>
+        <version>1.0</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>com.sun.script</groupId>
+        <artifactId>groovy-engine</artifactId>
+        <version>20070112</version>
+        <scope>test</scope>
+      </dependency>
+
 
       <dependency>
         <groupId>junit</groupId>



Mime
View raw message