Repository: camel Updated Branches: refs/heads/master 3ff5e3b4d -> 43bf93b8e
CAMEL-8176: camel-jackson - Make it easier to register custom modules Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/43bf93b8 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/43bf93b8 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/43bf93b8 Branch: refs/heads/master Commit: 43bf93b8e1bbea88e0df0241868b263ecfe287eb Parents: 3ff5e3b Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Feb 12 11:17:55 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Feb 12 11:17:55 2015 +0100 ---------------------------------------------------------------------- .../camel/model/dataformat/JsonDataFormat.java | 17 ++++++++ .../component/jackson/JacksonDataFormat.java | 26 +++++++++++ .../component/jackson/JacksonModuleRefTest.java | 46 ++++++++++++++++++++ 3 files changed, 89 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/43bf93b8/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java index cd3d8d2..50e61f0 100644 --- a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java @@ -62,6 +62,8 @@ public class JsonDataFormat extends DataFormatDefinition { private Boolean enableJaxbAnnotationModule; @XmlAttribute private String moduleClassNames; + @XmlAttribute + private String moduleRefs; public JsonDataFormat() { super("json"); @@ -203,6 +205,18 @@ public class JsonDataFormat extends DataFormatDefinition { this.moduleClassNames = moduleClassNames; } + public String getModuleRefs() { + return moduleRefs; + } + + /** + * To use custom Jackson modules referred from the Camel registry. + * Multiple modules can be separated by comma. + */ + public void setModuleRefs(String moduleRefs) { + this.moduleRefs = moduleRefs; + } + @Override public String getDataFormatName() { // json data format is special as the name can be from different bundles @@ -266,6 +280,9 @@ public class JsonDataFormat extends DataFormatDefinition { if (moduleClassNames != null) { setProperty(camelContext, dataFormat, "modulesClassNames", moduleClassNames); } + if (moduleRefs != null) { + setProperty(camelContext, dataFormat, "moduleRefs", moduleRefs); + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/43bf93b8/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java index 5b5ec80..015ee52 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java @@ -35,6 +35,7 @@ import org.apache.camel.CamelContextAware; import org.apache.camel.Exchange; import org.apache.camel.spi.DataFormat; import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +53,7 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Cam private Class<? extends Collection> collectionType; private List<Module> modules; private String moduleClassNames; + private String moduleRefs; private Class<?> unmarshalType; private Class<?> jsonView; private String include; @@ -250,6 +252,18 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Cam this.moduleClassNames = moduleClassNames; } + public String getModuleRefs() { + return moduleRefs; + } + + /** + * To use custom Jackson modules referred from the Camel registry. + * Multiple modules can be separated by comma. + */ + public void setModuleRefs(String moduleRefs) { + this.moduleRefs = moduleRefs; + } + /** * Uses {@link java.util.ArrayList} when unmarshalling. */ @@ -311,6 +325,18 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Cam objectMapper.registerModule(module); } } + if (moduleRefs != null) { + Iterable<Object> it = ObjectHelper.createIterable(moduleRefs); + for (Object o : it) { + String name = o.toString(); + if (name.startsWith("#")) { + name = name.substring(1); + } + Module module = CamelContextHelper.mandatoryLookup(camelContext, name, Module.class); + LOG.info("Registering module: {} -> {}", name, module); + objectMapper.registerModule(module); + } + } } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/43bf93b8/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonModuleRefTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonModuleRefTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonModuleRefTest.java new file mode 100644 index 0000000..2b87933 --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonModuleRefTest.java @@ -0,0 +1,46 @@ +/** + * 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.component.jackson; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +public class JacksonModuleRefTest extends JacksonModuleTest { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myJacksonModule", new MyModule()); + return jndi; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + JacksonDataFormat format = new JacksonDataFormat(); + format.setInclude("NON_NULL"); + format.setModuleRefs("myJacksonModule"); + + from("direct:marshal").marshal(format).to("mock:marshal"); + } + }; + } + +}