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/0beba6c7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0beba6c7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0beba6c7

Branch: refs/heads/master
Commit: 0beba6c7fd622193bc59e6563f052a08c737aac7
Parents: fc7e2df
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sat Feb 14 15:20:12 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sat Feb 14 15:20:12 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/camel/CamelContext.java     | 15 ++++++
 .../impl/DefaultModelJAXBContextFactory.java    | 24 ++++++++--
 .../apache/camel/model/ModelCamelContext.java   |  8 ----
 .../org/apache/camel/model/ModelHelper.java     | 49 ++++++++++++--------
 .../camel/spi/ModelJAXBContextFactory.java      | 29 ++++++++++--
 5 files changed, 89 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0beba6c7/camel-core/src/main/java/org/apache/camel/CamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java 
b/camel-core/src/main/java/org/apache/camel/CamelContext.java
index 28e83e9..bfe116c 100644
--- a/camel-core/src/main/java/org/apache/camel/CamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java
@@ -49,6 +49,7 @@ import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.ManagementMBeanAssembler;
 import org.apache.camel.spi.ManagementNameStrategy;
 import org.apache.camel.spi.ManagementStrategy;
+import org.apache.camel.spi.ModelJAXBContextFactory;
 import org.apache.camel.spi.NodeIdFactory;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.ProcessorFactory;
@@ -1538,4 +1539,18 @@ public interface CamelContext extends 
SuspendableService, RuntimeConfiguration {
      */
     List<RoutePolicyFactory> getRoutePolicyFactories();
 
+    /**
+     * Returns the JAXB Context factory used to create Models.
+     *
+     * @return the JAXB Context factory used to create Models.
+     */
+    ModelJAXBContextFactory getModelJAXBContextFactory();
+
+    /**
+     * Sets a custom JAXB Context factory to be used
+     *
+     * @param modelJAXBContextFactory a JAXB Context factory
+     */
+    void setModelJAXBContextFactory(ModelJAXBContextFactory 
modelJAXBContextFactory);
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/0beba6c7/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
index d5d93fc..c830241 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
@@ -1,13 +1,29 @@
+/**
+ * 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.impl;
 
-import org.apache.camel.model.Constants;
-import org.apache.camel.spi.ModelJAXBContextFactory;
-
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 
+import org.apache.camel.model.Constants;
+import org.apache.camel.spi.ModelJAXBContextFactory;
+
 /**
- * @author
+ * Default implementation of {@link 
org.apache.camel.spi.ModelJAXBContextFactory}.
  */
 public class DefaultModelJAXBContextFactory implements ModelJAXBContextFactory 
{
 

http://git-wip-us.apache.org/repos/asf/camel/blob/0beba6c7/camel-core/src/main/java/org/apache/camel/model/ModelCamelContext.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/ModelCamelContext.java 
b/camel-core/src/main/java/org/apache/camel/model/ModelCamelContext.java
index 977edaa..912aa62 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ModelCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ModelCamelContext.java
@@ -23,7 +23,6 @@ import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.model.rest.RestDefinition;
-import org.apache.camel.spi.ModelJAXBContextFactory;
 
 /**
  * Model level interface for the {@link CamelContext}
@@ -31,13 +30,6 @@ import org.apache.camel.spi.ModelJAXBContextFactory;
 public interface ModelCamelContext extends CamelContext {
 
     /**
-     * @return the JAXB Context factory used to create Models.
-     */
-    ModelJAXBContextFactory getModelJAXBContextFactory();
-
-    void setModelJAXBContextFactory(ModelJAXBContextFactory 
modelJAXBContextFactory);
-
-    /**
      * Returns a list of the current route definitions
      *
      * @return list of the current route definitions

http://git-wip-us.apache.org/repos/asf/camel/blob/0beba6c7/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 91aed54..c24684e 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
@@ -16,15 +16,17 @@
  */
 package org.apache.camel.model;
 
-import org.apache.camel.NamedNode;
-
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
-import java.io.InputStream;
-import java.io.StringReader;
-import java.io.StringWriter;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.NamedNode;
+import org.apache.camel.util.IOHelper;
 
 /**
  * Helper for the Camel {@link org.apache.camel.model model} classes.
@@ -38,12 +40,13 @@ public final class ModelHelper {
     /**
      * Dumps the definition as XML
      *
-     * @param definition  the definition, such as a {@link 
org.apache.camel.NamedNode}
-     * @return            the output in XML (is formatted)
+     * @param context    the CamelContext
+     * @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(ModelCamelContext modelCamelContext, 
NamedNode definition) throws JAXBException {
-        JAXBContext jaxbContext = 
modelCamelContext.getModelJAXBContextFactory().newJAXBContext();
+    public static String dumpModelAsXml(CamelContext context, NamedNode 
definition) throws JAXBException {
+        JAXBContext jaxbContext = 
context.getModelJAXBContextFactory().newJAXBContext();
 
         Marshaller marshaller = jaxbContext.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
@@ -56,17 +59,22 @@ public final class ModelHelper {
     /**
      * Marshal the xml to the model definition
      *
-     * @param xml the xml
-     * @param type the definition type to return, will throw a {@link 
ClassCastException} if not the expected type
+     * @param context the CamelContext
+     * @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(ModelCamelContext 
modelCamelContext, String xml, Class<T> type) throws JAXBException {
-        JAXBContext jaxbContext = 
modelCamelContext.getModelJAXBContextFactory().newJAXBContext();
+    public static <T extends NamedNode> T createModelFromXml(CamelContext 
context, String xml, Class<T> type) throws JAXBException {
+        JAXBContext jaxbContext = 
context.getModelJAXBContextFactory().newJAXBContext();
         StringReader reader = new StringReader(xml);
-        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
-        Object result = unmarshaller.unmarshal(reader);
-        reader.close();
+        Object result;
+        try {
+            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+            result = unmarshaller.unmarshal(reader);
+        } finally {
+            IOHelper.close(reader);
+        }
 
         if (result == null) {
             throw new JAXBException("Cannot unmarshal to " + type + " using 
JAXB from XML: " + xml);
@@ -77,13 +85,14 @@ public final class ModelHelper {
     /**
      * Marshal the xml to the model definition
      *
-     * @param stream the xml stream
-     * @param type the definition type to return, will throw a {@link 
ClassCastException} if not the expected type
+     * @param context the CamelContext
+     * @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(ModelCamelContext 
modelCamelContext, InputStream stream, Class<T> type) throws JAXBException {
-        JAXBContext jaxbContext = 
modelCamelContext.getModelJAXBContextFactory().newJAXBContext();
+    public static <T extends NamedNode> T createModelFromXml(CamelContext 
context, InputStream stream, Class<T> type) throws JAXBException {
+        JAXBContext jaxbContext = 
context.getModelJAXBContextFactory().newJAXBContext();
         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
         Object result = unmarshaller.unmarshal(stream);
         return type.cast(result);

http://git-wip-us.apache.org/repos/asf/camel/blob/0beba6c7/camel-core/src/main/java/org/apache/camel/spi/ModelJAXBContextFactory.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/spi/ModelJAXBContextFactory.java 
b/camel-core/src/main/java/org/apache/camel/spi/ModelJAXBContextFactory.java
index b2ae3ad..97ea3bf 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/ModelJAXBContextFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/ModelJAXBContextFactory.java
@@ -1,3 +1,19 @@
+/**
+ * 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.spi;
 
 import javax.xml.bind.JAXBContext;
@@ -5,10 +21,15 @@ import javax.xml.bind.JAXBException;
 
 /**
  * Factory to abstract the creation of the Model's JAXBContext.
- *
- * @author
  */
-public interface ModelJAXBContextFactory
-{
+public interface ModelJAXBContextFactory {
+
+    /**
+     * Creates a new {@link javax.xml.bind.JAXBContext} used for loading the 
Camel model
+     *
+     * @return a new JAXBContext
+     * @throws JAXBException is thrown if error creating the JAXBContext
+     */
     JAXBContext newJAXBContext() throws JAXBException;
+
 }
\ No newline at end of file

Reply via email to