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 2014/01/10 11:06:42 UTC
git commit: CAMEL-7122: camel-script resolves javascript using
different alias names so eg ECMAScript/js/javaScript can find the script
engine to use.
Updated Branches:
refs/heads/camel-2.12.x 642a1d695 -> 3ad782cfe
CAMEL-7122: camel-script resolves javascript using different alias names so eg ECMAScript/js/javaScript can find the script engine to use.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3ad782cf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3ad782cf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3ad782cf
Branch: refs/heads/camel-2.12.x
Commit: 3ad782cfe559fcb1db71c8f2f881918fdfde18cd
Parents: 642a1d6
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Jan 10 11:08:34 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Jan 10 11:10:32 2014 +0100
----------------------------------------------------------------------
.../camel/builder/script/ScriptBuilder.java | 33 ++++++++++---
.../org/apache/camel/script/osgi/Activator.java | 22 +++++++--
.../LanguageECMAScriptAsJavaScriptTest.java | 51 ++++++++++++++++++++
3 files changed, 96 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/3ad782cf/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
----------------------------------------------------------------------
diff --git a/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java b/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
index a0228ea..7fb1382 100644
--- a/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
+++ b/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
@@ -293,20 +293,39 @@ public class ScriptBuilder extends ServiceSupport implements Expression, Predica
return ObjectConverter.toBool(scriptValue);
}
+ private static String[] getScriptNames(String name) {
+ if (name.equals("js")) {
+ return new String[]{"js", "javaScript", "ECMAScript"};
+ } else if (name.equals("javaScript")) {
+ return new String[]{"javaScript", "js", "ECMAScript"};
+ } else if (name.equals("ECMAScript")) {
+ return new String[]{"ECMAScript", "javaScript", "js"};
+ }
+ return new String[]{name};
+ }
+
protected ScriptEngine createScriptEngine() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = null;
- try {
- engine = manager.getEngineByName(scriptEngineName);
- } catch (NoClassDefFoundError ex) {
- LOG.error("Cannot load the scriptEngine for " + scriptEngineName + ", the exception is " + ex
- + ", please ensure correct JARs is provided on classpath.");
+
+ // some script names has alias
+ String[] names = getScriptNames(scriptEngineName);
+ for (String name : names) {
+ try {
+ engine = manager.getEngineByName(name);
+ if (engine != null) {
+ break;
+ }
+ } catch (NoClassDefFoundError ex) {
+ LOG.error("Cannot load the scriptEngine for " + name + ", the exception is " + ex
+ + ", please ensure correct JARs is provided on classpath.");
+ }
}
if (engine == null) {
engine = checkForOSGiEngine();
}
if (engine == null) {
- throw new IllegalArgumentException("No script engine could be created for: " + getScriptEngineName());
+ throw new IllegalArgumentException("No script engine could be created for: " + scriptEngineName);
}
if (isPython()) {
ScriptContext context = engine.getContext();
@@ -316,7 +335,7 @@ public class ScriptBuilder extends ServiceSupport implements Expression, Predica
}
private ScriptEngine checkForOSGiEngine() {
- LOG.debug("No script engine found for " + scriptEngineName + " using standard javax.script auto-registration. Checking OSGi registry...");
+ LOG.debug("No script engine found for " + scriptEngineName + " using standard javax.script auto-registration. Checking OSGi registry...");
try {
// Test the OSGi environment with the Activator
Class<?> c = Class.forName("org.apache.camel.script.osgi.Activator");
http://git-wip-us.apache.org/repos/asf/camel/blob/3ad782cf/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java b/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
index e8f5b81..3dd1c73 100644
--- a/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
+++ b/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
@@ -117,7 +117,6 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
return null;
}
-
protected void registerScriptEngines(Bundle bundle, List<BundleScriptEngineResolver> resolvers) {
URL configURL = null;
for (Enumeration<?> e = bundle.findEntries(META_INF_SERVICES_DIR, SCRIPT_ENGINE_SERVICE_FILE, false); e != null && e.hasMoreElements();) {
@@ -128,9 +127,11 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
resolvers.add(new BundleScriptEngineResolver(bundle, configURL));
}
}
+
public interface ScriptEngineResolver {
ScriptEngine resolveScriptEngine(String name);
}
+
protected static class BundleScriptEngineResolver implements ScriptEngineResolver {
protected final Bundle bundle;
private ServiceRegistration reg;
@@ -140,13 +141,15 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
this.bundle = bundle;
this.configFile = configFile;
}
+
public void register() {
- reg = bundle.getBundleContext().registerService(ScriptEngineResolver.class.getName(),
- this, null);
+ reg = bundle.getBundleContext().registerService(ScriptEngineResolver.class.getName(), this, null);
}
+
public void unregister() {
reg.unregister();
}
+
public ScriptEngine resolveScriptEngine(String name) {
try {
BufferedReader in = IOHelper.buffered(new InputStreamReader(configFile.openStream()));
@@ -181,6 +184,19 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
}
}
+ private boolean matchEngine(String name, String engineName) {
+ if (name.equals(engineName)) {
+ return true;
+ }
+
+ // javascript have many aliases
+ if (name.equals("js") || name.equals("javaScript") || name.equals("ECMAScript")) {
+ return engineName.equals("js") || engineName.equals("javaScript") || engineName.equals("ECMAScript");
+ }
+
+ return false;
+ }
+
@Override
public String toString() {
return "OSGi script engine resolver for " + bundle.getSymbolicName();
http://git-wip-us.apache.org/repos/asf/camel/blob/3ad782cf/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageECMAScriptAsJavaScriptTest.java
----------------------------------------------------------------------
diff --git a/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageECMAScriptAsJavaScriptTest.java b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageECMAScriptAsJavaScriptTest.java
new file mode 100644
index 0000000..0c214ab
--- /dev/null
+++ b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageECMAScriptAsJavaScriptTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Tests a routing expression using ECMAScript which will be resolved as java script
+ */
+@Ignore("May fail on CI servers")
+public class LanguageECMAScriptAsJavaScriptTest extends CamelTestSupport {
+
+ @Test
+ public void testSendMatchingMessage() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived(7, 9);
+
+ sendBody("direct:start", 3);
+ sendBody("direct:start", 4);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("language:ECMAScript:classpath:myJavascript.js")
+ .to("mock:result");
+ }
+ };
+ }
+}