CAMEL-6380: Polished
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/72d7a7d6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/72d7a7d6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/72d7a7d6 Branch: refs/heads/master Commit: 72d7a7d6da89be85076675751a6b253cd9ad8283 Parents: 33dcf40 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Feb 14 15:29:39 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Feb 14 15:29:39 2015 +0100 ---------------------------------------------------------------------- .../org/apache/camel/model/ModelHelper.java | 34 ++++++++++++++++---- .../BlueprintModelJAXBContextFactory.java | 19 +++++++++-- .../swagger/DefaultCamelSwaggerServlet.scala | 2 +- 3 files changed, 45 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/72d7a7d6/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java b/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java index c24684e..801d2a0 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java +++ b/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java @@ -40,13 +40,18 @@ public final class ModelHelper { /** * Dumps the definition as XML * - * @param context the CamelContext + * @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use * @param definition the definition, such as a {@link org.apache.camel.NamedNode} * @return the output in XML (is formatted) * @throws JAXBException is throw if error marshalling to XML */ public static String dumpModelAsXml(CamelContext context, NamedNode definition) throws JAXBException { - JAXBContext jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + JAXBContext jaxbContext; + if (context == null) { + jaxbContext = createJAXBContext(); + } else { + jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + } Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); @@ -59,14 +64,20 @@ public final class ModelHelper { /** * Marshal the xml to the model definition * - * @param context the CamelContext + * @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use * @param xml the xml * @param type the definition type to return, will throw a {@link ClassCastException} if not the expected type * @return the model definition * @throws javax.xml.bind.JAXBException is thrown if error unmarshalling from xml to model */ public static <T extends NamedNode> T createModelFromXml(CamelContext context, String xml, Class<T> type) throws JAXBException { - JAXBContext jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + JAXBContext jaxbContext; + if (context == null) { + jaxbContext = createJAXBContext(); + } else { + jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + } + StringReader reader = new StringReader(xml); Object result; try { @@ -85,16 +96,27 @@ public final class ModelHelper { /** * Marshal the xml to the model definition * - * @param context the CamelContext + * @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use * @param stream the xml stream * @param type the definition type to return, will throw a {@link ClassCastException} if not the expected type * @return the model definition * @throws javax.xml.bind.JAXBException is thrown if error unmarshalling from xml to model */ public static <T extends NamedNode> T createModelFromXml(CamelContext context, InputStream stream, Class<T> type) throws JAXBException { - JAXBContext jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + JAXBContext jaxbContext; + if (context == null) { + jaxbContext = createJAXBContext(); + } else { + jaxbContext = context.getModelJAXBContextFactory().newJAXBContext(); + } + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Object result = unmarshaller.unmarshal(stream); return type.cast(result); } + + private static JAXBContext createJAXBContext() throws JAXBException { + // must use classloader from CamelContext to have JAXB working + return JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES, CamelContext.class.getClassLoader()); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/72d7a7d6/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java index f3b6c88..90a74d8 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java @@ -1,10 +1,23 @@ +/** + * 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.blueprint; import org.apache.camel.impl.DefaultModelJAXBContextFactory; -/** - * @author - */ public class BlueprintModelJAXBContextFactory extends DefaultModelJAXBContextFactory{ public static final String ADDITIONAL_JAXB_CONTEXT_PACKAGES = ":" http://git-wip-us.apache.org/repos/asf/camel/blob/72d7a7d6/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/DefaultCamelSwaggerServlet.scala ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/DefaultCamelSwaggerServlet.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/DefaultCamelSwaggerServlet.scala index 4ed2aa9..128cac2 100644 --- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/DefaultCamelSwaggerServlet.scala +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/DefaultCamelSwaggerServlet.scala @@ -49,7 +49,7 @@ class DefaultCamelSwaggerServlet extends RestSwaggerApiDeclarationServlet { val result = server.invoke(found, "dumpRestsAsXml", null, null) if (result != null) { val xml = result.asInstanceOf[String] - val rests: RestsDefinition = ModelHelper.createModelFromXml(xml, classOf[RestsDefinition]) + val rests: RestsDefinition = ModelHelper.createModelFromXml(null, xml, classOf[RestsDefinition]) val answer = new scala.collection.mutable.ListBuffer[RestDefinition] for (rest <- rests.getRests.asScala) { answer += rest