CAMEL-6870: Fixed leak in camel-script when using language component with content cache = false.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fabcedef Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fabcedef Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fabcedef Branch: refs/heads/camel-2.11.x Commit: fabcedef3f3d5853d6826f6f25c230dbc2e1e1c3 Parents: b244526 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Oct 17 12:39:03 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Oct 17 12:39:41 2013 +0200 ---------------------------------------------------------------------- .../component/language/LanguageProducer.java | 14 ++++- .../script/LanguageJavaScriptNoCacheTest.java | 54 ++++++++++++++++++++ .../builder/script/LanguageJavaScriptTest.java | 54 ++++++++++++++++++++ 3 files changed, 120 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fabcedef/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java index 22ac2b1..76a4aec 100644 --- a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java +++ b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java @@ -24,6 +24,7 @@ import org.apache.camel.Expression; import org.apache.camel.impl.DefaultProducer; import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ServiceHelper; /** * Language producer. @@ -79,8 +80,17 @@ public class LanguageProducer extends DefaultProducer { ObjectHelper.notNull(exp, "expression"); - Object result = exp.evaluate(exchange, Object.class); - log.debug("Evaluated expression as: {} with: {}", result, exchange); + Object result; + try { + result = exp.evaluate(exchange, Object.class); + log.debug("Evaluated expression as: {} with: {}", result, exchange); + } finally { + if (!getEndpoint().isContentCache()) { + // some languages add themselves as a service which we then need to remove if we are not cached + ServiceHelper.stopService(exp); + getEndpoint().getCamelContext().removeService(exp); + } + } // set message body if transform is enabled if (getEndpoint().isTransform()) { http://git-wip-us.apache.org/repos/asf/camel/blob/fabcedef/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptNoCacheTest.java ---------------------------------------------------------------------- diff --git a/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptNoCacheTest.java b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptNoCacheTest.java new file mode 100644 index 0000000..1f3b903 --- /dev/null +++ b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptNoCacheTest.java @@ -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.script; + +import org.apache.camel.ScriptTestHelper; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * Tests a routing expression using JavaScript + */ +public class LanguageJavaScriptNoCacheTest extends CamelTestSupport { + + @Test + public void testSendMatchingMessage() throws Exception { + if (!ScriptTestHelper.canRunTestOnThisPlatform()) { + return; + } + + 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:javascript:classpath:myJavascript.js?contentCache=false") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/fabcedef/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptTest.java ---------------------------------------------------------------------- diff --git a/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptTest.java b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptTest.java new file mode 100644 index 0000000..26c5fd9 --- /dev/null +++ b/components/camel-script/src/test/java/org/apache/camel/builder/script/LanguageJavaScriptTest.java @@ -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.script; + +import org.apache.camel.ScriptTestHelper; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * Tests a routing expression using JavaScript + */ +public class LanguageJavaScriptTest extends CamelTestSupport { + + @Test + public void testSendMatchingMessage() throws Exception { + if (!ScriptTestHelper.canRunTestOnThisPlatform()) { + return; + } + + 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:javascript:classpath:myJavascript.js") + .to("mock:result"); + } + }; + } +}