This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new e6ec9ebaf7c CAMEL-19090: Remove deprecated apis in core e6ec9ebaf7c is described below commit e6ec9ebaf7ce000e80c1b98980f1d6014f4b0a96 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Feb 28 09:44:25 2023 +0100 CAMEL-19090: Remove deprecated apis in core --- .../org/apache/camel/builder/SimpleBuilder.java | 127 ------------------ .../apache/camel/model/ExpressionNodeHelper.java | 20 +-- .../apache/camel/builder/SimpleBuilderTest.java | 149 --------------------- .../ROOT/pages/camel-4-migration-guide.adoc | 1 + 4 files changed, 4 insertions(+), 293 deletions(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/SimpleBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/SimpleBuilder.java deleted file mode 100644 index 2d631f2dca5..00000000000 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/SimpleBuilder.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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; - -import org.apache.camel.CamelContext; -import org.apache.camel.Exchange; -import org.apache.camel.Expression; -import org.apache.camel.Predicate; -import org.apache.camel.spi.ExpressionResultTypeAware; -import org.apache.camel.spi.Language; - -/** - * Creates a Simple language builder. - * <p/> - * This builder is available in the Java DSL from the {@link RouteBuilder} which means that using simple language for - * {@link Expression}s or {@link Predicate}s is very easy with the help of this builder. - */ -@Deprecated -public class SimpleBuilder implements Predicate, Expression, ExpressionResultTypeAware { - - private String text; - private Class<?> resultType; - // cache the expression/predicate - private Language simple; - private volatile Expression expression; - private volatile Predicate predicate; - - public SimpleBuilder(String text) { - this.text = text; - } - - public static SimpleBuilder simple(String text) { - return new SimpleBuilder(text); - } - - public static SimpleBuilder simple(String text, Class<?> resultType) { - SimpleBuilder answer = simple(text); - answer.setResultType(resultType); - return answer; - } - - public static SimpleBuilder simpleF(String formatText, Object... values) { - return simple(String.format(formatText, values)); - } - - public static SimpleBuilder simpleF(String formatText, Class<?> resultType, Object... values) { - return simple(String.format(formatText, values), resultType); - } - - public String getText() { - return text; - } - - @Override - public String getExpressionText() { - return getText(); - } - - @Override - public Class<?> getResultType() { - return resultType; - } - - public void setResultType(Class<?> resultType) { - this.resultType = resultType; - } - - public SimpleBuilder resultType(Class<?> resultType) { - setResultType(resultType); - return this; - } - - @Override - public String toString() { - return text; - } - - @Override - public <T> T evaluate(Exchange exchange, Class<T> type) { - if (expression == null) { - if (simple == null) { - init(exchange.getContext()); - } - if (resultType != null) { - Object[] properties = new Object[2]; - properties[0] = resultType; - expression = simple.createExpression(text, properties); - } else { - expression = simple.createExpression(text); - } - expression.init(exchange.getContext()); - } - return expression.evaluate(exchange, type); - } - - @Override - public boolean matches(Exchange exchange) { - if (predicate == null) { - if (simple == null) { - init(exchange.getContext()); - } - predicate = simple.createPredicate(text); - predicate.init(exchange.getContext()); - } - return predicate.matches(exchange); - } - - @Override - public void init(CamelContext context) { - simple = context.resolveLanguage("simple"); - text = context.resolvePropertyPlaceholders(text); - } -} diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionNodeHelper.java b/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionNodeHelper.java index 63810a17044..5d8c0e189e1 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionNodeHelper.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionNodeHelper.java @@ -18,7 +18,6 @@ package org.apache.camel.model; import org.apache.camel.Expression; import org.apache.camel.Predicate; -import org.apache.camel.builder.SimpleBuilder; import org.apache.camel.builder.ValueBuilder; import org.apache.camel.model.language.ExpressionDefinition; import org.apache.camel.model.language.SimpleExpression; @@ -43,13 +42,7 @@ public final class ExpressionNodeHelper { * @return a definition which describes the expression */ public static ExpressionDefinition toExpressionDefinition(Expression expression) { - if (expression instanceof SimpleBuilder) { - SimpleBuilder builder = (SimpleBuilder) expression; - // we want to use the definition objects in the route graph - SimpleExpression answer = new SimpleExpression(builder.getText()); - answer.setResultType(builder.getResultType()); - return answer; - } else if (expression instanceof ExpressionResultTypeAware + if (expression instanceof ExpressionResultTypeAware && expression.getClass().getName().equals("org.apache.camel.language.xpath.XPathBuilder")) { ExpressionResultTypeAware aware = (ExpressionResultTypeAware) expression; // we keep the original expression by using the constructor that @@ -74,20 +67,13 @@ public final class ExpressionNodeHelper { * Determines which {@link ExpressionDefinition} describes the given predicate in the best possible way. * <p/> * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc. if the given - * predicate is detect as such a type. + * predicate is detected as such a type. * * @param predicate the predicate * @return a definition which describes the predicate */ public static ExpressionDefinition toExpressionDefinition(Predicate predicate) { - if (predicate instanceof SimpleBuilder) { - SimpleBuilder builder = (SimpleBuilder) predicate; - // we want to use the definition objects in the route graph - SimpleExpression answer = new SimpleExpression(builder.getText()); - answer.setExpression(builder.getText()); - answer.setResultType(builder.getResultType()); - return answer; - } else if (predicate instanceof ExpressionResultTypeAware + if (predicate instanceof ExpressionResultTypeAware && predicate.getClass().getName().equals("org.apache.camel.language.xpath.XPathBuilder")) { ExpressionResultTypeAware aware = (ExpressionResultTypeAware) predicate; Expression expression = (Expression) predicate; diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java deleted file mode 100644 index 70fc69a47f0..00000000000 --- a/core/camel-core/src/test/java/org/apache/camel/builder/SimpleBuilderTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * 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; - -import java.util.Properties; - -import org.apache.camel.Exchange; -import org.apache.camel.TestSupport; -import org.apache.camel.TypeConversionException; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.spi.PropertiesComponent; -import org.apache.camel.support.DefaultExchange; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -public class SimpleBuilderTest extends TestSupport { - - protected Exchange exchange = new DefaultExchange(new DefaultCamelContext()); - - @Test - public void testPredicate() throws Exception { - exchange.getIn().setBody("foo"); - - assertTrue(SimpleBuilder.simple("${body} == 'foo'").matches(exchange)); - assertFalse(SimpleBuilder.simple("${body} == 'bar'").matches(exchange)); - } - - @Test - public void testExpression() throws Exception { - exchange.getIn().setBody("foo"); - - assertEquals("foo", SimpleBuilder.simple("${body}").evaluate(exchange, String.class)); - assertNull(SimpleBuilder.simple("${header.cheese}").evaluate(exchange, String.class)); - } - - @Test - public void testFormatExpression() throws Exception { - exchange.getIn().setHeader("head", "foo"); - - assertEquals("foo", SimpleBuilder.simpleF("${header.%s}", "head").evaluate(exchange, String.class)); - assertNull(SimpleBuilder.simple("${header.cheese}").evaluate(exchange, String.class)); - } - - @Test - public void testFormatExpressionWithResultType() throws Exception { - exchange.getIn().setHeader("head", "200"); - - assertEquals(200, SimpleBuilder.simpleF("${header.%s}", Integer.class, "head").evaluate(exchange, Object.class)); - } - - @Test - public void testResultType() throws Exception { - exchange.getIn().setBody("foo"); - exchange.getIn().setHeader("cool", true); - - assertEquals("foo", SimpleBuilder.simple("${body}", String.class).evaluate(exchange, Object.class)); - try { - // error during conversion - SimpleBuilder.simple("${body}", int.class).evaluate(exchange, Object.class); - fail("Should have thrown exception"); - } catch (TypeConversionException e) { - assertIsInstanceOf(NumberFormatException.class, e.getCause()); - } - - assertEquals(true, SimpleBuilder.simple("${header.cool}", boolean.class).evaluate(exchange, Object.class)); - assertEquals("true", SimpleBuilder.simple("${header.cool}", String.class).evaluate(exchange, Object.class)); - // not possible - assertNull(SimpleBuilder.simple("${header.cool}", int.class).evaluate(exchange, Object.class)); - - assertEquals(true, SimpleBuilder.simple("${header.cool}").resultType(Boolean.class).evaluate(exchange, Object.class)); - assertEquals("true", SimpleBuilder.simple("${header.cool}").resultType(String.class).evaluate(exchange, Object.class)); - // not possible - assertNull(SimpleBuilder.simple("${header.cool}").resultType(int.class).evaluate(exchange, Object.class)); - - // should be convertable to integers - assertEquals(11, SimpleBuilder.simple("11", int.class).evaluate(exchange, Object.class)); - } - - @Test - public void testRegexAllWithPlaceHolders() { - exchange.getIn().setHeader("activateUrl", "http://some/rest/api/(id)/activate"); - assertEquals("http://some/rest/api/12/activate", - SimpleBuilder.simple("${header.activateUrl.replaceAll(\"\\(id\\)\",\"12\")}").evaluate(exchange, String.class)); - - // passes when contains { only - exchange.getIn().setHeader("activateUrl", "http://some/rest/api/{id/activate"); - assertEquals("http://some/rest/api/12/activate", - SimpleBuilder.simple("${header.activateUrl.replaceAll(\"\\{id\",\"12\")}").evaluate(exchange, String.class)); - - String replaced = "http://some/rest/api/{id}/activate".replaceAll("\\{id\\}", "12"); - assertEquals("http://some/rest/api/12/activate", replaced); - - // passes when contains { } - exchange.getIn().setHeader("activateUrl", "http://some/rest/api/{id}/activate"); - assertEquals("http://some/rest/api/12/activate", - SimpleBuilder.simple("${header.activateUrl.replaceAll(\"\\{id\\}\",\"12\")}").evaluate(exchange, String.class)); - - // passes when contains { } and another ${body} function - exchange.getIn().setBody("12"); - assertEquals("http://some/rest/api/12/activate", SimpleBuilder - .simple("${header.activateUrl.replaceAll(\"\\{id\\}\",\"${body}\")}").evaluate(exchange, String.class)); - - // passes when } is escaped with \} - assertEquals("http://some/rest/api/{}/activate", SimpleBuilder - .simple("${header.activateUrl.replaceAll(\"\\{id\\}\",\"{\\}\")}").evaluate(exchange, String.class)); - } - - @Test - public void testPropertyPlaceholder() throws Exception { - exchange.getIn().setBody("Hello"); - - Properties prop = new Properties(); - prop.put("foo", "bar"); - PropertiesComponent pc = exchange.getContext().getPropertiesComponent(); - pc.setOverrideProperties(prop); - - assertEquals("bar", SimpleBuilder.simple("{{foo}}").evaluate(exchange, String.class)); - assertEquals("bar", SimpleBuilder.simple("${properties:foo}").evaluate(exchange, String.class)); - - try { - SimpleBuilder.simple("{{bar}}").evaluate(exchange, String.class); - fail("Should fail"); - } catch (Exception e) { - // expected - } - try { - SimpleBuilder.simple("${properties:bar}").evaluate(exchange, String.class); - fail("Should fail"); - } catch (Exception e) { - // expected - } - } - -} diff --git a/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc b/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc index 278c79d286c..3dde1b074f4 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc @@ -78,6 +78,7 @@ We have removed deprecated APIs such as the following: - Replaced `adapt()` from `org.apache.camel.ExtendedExchange` with `getExchangeExtension` - Exchange failure handling status has moved from being a property defined as `ExchangePropertyKey.FAILURE_HANDLED` to a member of the ExtendedExchange, accessible via `isFailureHandled()`method. - Removed `Discard` and `DiscardOldest` from `org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy`. +- Removed `org.apache.camel.builder.SimpleBuilder`. Was mostly used internally in Camel with the Java DSL in some situations. == EIP Changes