This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new e2e8367 CAMEL-14828: camel-core - Remove multiple camel context support for restContextRef which removes JAXB API dependency e2e8367 is described below commit e2e8367ceddb19c1b5a8bc6c7812fa5610ff33ea Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Apr 2 09:31:38 2020 +0200 CAMEL-14828: camel-core - Remove multiple camel context support for restContextRef which removes JAXB API dependency --- .../model/RestContextRefDefinitionHelper.java | 117 +-------------------- .../model/RouteContextRefDefinitionHelper.java | 99 +---------------- 2 files changed, 6 insertions(+), 210 deletions(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/RestContextRefDefinitionHelper.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/RestContextRefDefinitionHelper.java index b9ec262..06cb0b6 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/RestContextRefDefinitionHelper.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/RestContextRefDefinitionHelper.java @@ -16,25 +16,10 @@ */ package org.apache.camel.model; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; import org.apache.camel.CamelContext; -import org.apache.camel.ExtendedCamelContext; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.model.language.NamespaceAwareExpression; import org.apache.camel.model.rest.RestDefinition; -import org.apache.camel.model.rest.VerbDefinition; import org.apache.camel.support.CamelContextHelper; import org.apache.camel.util.ObjectHelper; @@ -43,33 +28,18 @@ import org.apache.camel.util.ObjectHelper; */ public final class RestContextRefDefinitionHelper { - private static JAXBContext jaxbContext; - private RestContextRefDefinitionHelper() { } /** * Lookup the rests from the * {@link org.apache.camel.model.RestContextRefDefinition}. - * <p/> - * This implementation must be used to lookup the rests as it performs a - * deep clone of the rests as a - * {@link org.apache.camel.model.RestContextRefDefinition} can be re-used - * with multiple {@link org.apache.camel.model.ModelCamelContext} and each - * context should have their own instances of the routes. This is to ensure - * no side-effects and sharing of instances between the contexts. For - * example such as property placeholders may be context specific so the - * routes should not use placeholders from another - * {@link org.apache.camel.CamelContext}. * * @param camelContext the CamelContext - * @param ref the id of the - * {@link org.apache.camel.model.RestContextRefDefinition} to - * lookup and get the routes. + * @param ref the id of the {@link org.apache.camel.model.RestContextRefDefinition} to lookup and get the routes. * @return the rests. */ - @SuppressWarnings("unchecked") - public static synchronized List<RestDefinition> lookupRests(CamelContext camelContext, String ref) { + public static List<RestDefinition> lookupRests(CamelContext camelContext, String ref) { ObjectHelper.notNull(camelContext, "camelContext"); ObjectHelper.notNull(ref, "ref"); @@ -77,88 +47,7 @@ public final class RestContextRefDefinitionHelper { if (answer == null) { throw new IllegalArgumentException("Cannot find RestContext with id " + ref); } - - // must clone the rest definitions as they can be reused with multiple - // CamelContexts - // and they would need their own instances of the definitions to not - // have side effects among - // the CamelContext - for example property placeholder resolutions etc. - List<RestDefinition> clones = new ArrayList<>(answer.size()); - try { - JAXBContext jaxb = getOrCreateJAXBContext(camelContext); - for (RestDefinition def : answer) { - RestDefinition clone = cloneRestDefinition(jaxb, def); - if (clone != null) { - clones.add(clone); - } - } - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - - return clones; - } - - private static synchronized JAXBContext getOrCreateJAXBContext(final CamelContext camelContext) throws JAXBException { - if (jaxbContext == null) { - // must use classloader from CamelContext to have JAXB working - jaxbContext = camelContext.adapt(ExtendedCamelContext.class).getModelJAXBContextFactory().newJAXBContext(); - } - return jaxbContext; - } - - private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException { - Marshaller marshal = jaxbContext.createMarshaller(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - marshal.marshal(def, bos); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - Object clone = unmarshaller.unmarshal(bis); - - if (clone instanceof RestDefinition) { - RestDefinition def2 = (RestDefinition)clone; - - Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator(); - Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator(); - - while (verbit1.hasNext() && verbit2.hasNext()) { - VerbDefinition verb1 = verbit1.next(); - VerbDefinition verb2 = verbit2.next(); - - if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) { - RouteDefinition route1 = (RouteDefinition)verb1.getToOrRoute(); - RouteDefinition route2 = (RouteDefinition)verb2.getToOrRoute(); - - // need to clone the namespaces also as they are not JAXB - // marshalled (as they are transient) - Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class); - Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class); - while (it.hasNext() && it2.hasNext()) { - ExpressionNode node = it.next(); - ExpressionNode node2 = it2.next(); - - NamespaceAwareExpression name = null; - NamespaceAwareExpression name2 = null; - if (node.getExpression() instanceof NamespaceAwareExpression) { - name = (NamespaceAwareExpression)node.getExpression(); - } - if (node2.getExpression() instanceof NamespaceAwareExpression) { - name2 = (NamespaceAwareExpression)node2.getExpression(); - } - - if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) { - Map<String, String> map = new HashMap<>(); - map.putAll(name.getNamespaces()); - name2.setNamespaces(map); - } - } - } - } - return def2; - } - - return null; + return answer; } } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/RouteContextRefDefinitionHelper.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/RouteContextRefDefinitionHelper.java index 66cea9b..5cf4e46 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/RouteContextRefDefinitionHelper.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/RouteContextRefDefinitionHelper.java @@ -16,23 +16,9 @@ */ package org.apache.camel.model; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; import org.apache.camel.CamelContext; -import org.apache.camel.ExtendedCamelContext; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.model.language.NamespaceAwareExpression; import org.apache.camel.support.CamelContextHelper; import org.apache.camel.util.ObjectHelper; @@ -41,29 +27,18 @@ import org.apache.camel.util.ObjectHelper; */ public final class RouteContextRefDefinitionHelper { - private static JAXBContext jaxbContext; - private RouteContextRefDefinitionHelper() { } /** * Lookup the routes from the {@link RouteContextRefDefinition}. - * <p/> - * This implementation must be used to lookup the routes as it performs a - * deep clone of the routes as a {@link RouteContextRefDefinition} can be - * re-used with multiple {@link ModelCamelContext} and each context should - * have their own instances of the routes. This is to ensure no side-effects - * and sharing of instances between the contexts. For example such as - * property placeholders may be context specific so the routes should not - * use placeholders from another {@link ModelCamelContext}. * * @param camelContext the CamelContext - * @param ref the id of the {@link RouteContextRefDefinition} to lookup and - * get the routes. + * @param ref the id of the {@link RouteContextRefDefinition} to lookup and get the routes. * @return the routes. */ @SuppressWarnings("unchecked") - public static synchronized List<RouteDefinition> lookupRoutes(CamelContext camelContext, String ref) { + public static List<RouteDefinition> lookupRoutes(CamelContext camelContext, String ref) { ObjectHelper.notNull(camelContext, "camelContext"); ObjectHelper.notNull(ref, "ref"); @@ -71,75 +46,7 @@ public final class RouteContextRefDefinitionHelper { if (answer == null) { throw new IllegalArgumentException("Cannot find RouteContext with id " + ref); } - - // must clone the route definitions as they can be reused with multiple - // CamelContexts - // and they would need their own instances of the definitions to not - // have side effects among - // the CamelContext - for example property placeholder resolutions etc. - List<RouteDefinition> clones = new ArrayList<>(answer.size()); - try { - JAXBContext jaxb = getOrCreateJAXBContext(camelContext); - for (RouteDefinition def : answer) { - RouteDefinition clone = cloneRouteDefinition(jaxb, def); - if (clone != null) { - clones.add(clone); - } - } - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - - return clones; - } - - private static synchronized JAXBContext getOrCreateJAXBContext(final CamelContext camelContext) throws JAXBException { - if (jaxbContext == null) { - jaxbContext = camelContext.adapt(ExtendedCamelContext.class).getModelJAXBContextFactory().newJAXBContext(); - } - return jaxbContext; - } - - private static RouteDefinition cloneRouteDefinition(JAXBContext jaxbContext, RouteDefinition def) throws JAXBException { - Marshaller marshal = jaxbContext.createMarshaller(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - marshal.marshal(def, bos); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - Object clone = unmarshaller.unmarshal(bis); - - if (clone instanceof RouteDefinition) { - RouteDefinition def2 = (RouteDefinition)clone; - - // need to clone the namespaces also as they are not JAXB marshalled - // (as they are transient) - Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(def.getOutputs(), ExpressionNode.class); - Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(def2.getOutputs(), ExpressionNode.class); - while (it.hasNext() && it2.hasNext()) { - ExpressionNode node = it.next(); - ExpressionNode node2 = it2.next(); - - NamespaceAwareExpression name = null; - NamespaceAwareExpression name2 = null; - if (node.getExpression() instanceof NamespaceAwareExpression) { - name = (NamespaceAwareExpression)node.getExpression(); - } - if (node2.getExpression() instanceof NamespaceAwareExpression) { - name2 = (NamespaceAwareExpression)node2.getExpression(); - } - - if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) { - Map<String, String> map = new HashMap<>(); - map.putAll(name.getNamespaces()); - name2.setNamespaces(map); - } - } - - return def2; - } - - return null; + return answer; } }