You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/04/05 10:14:00 UTC
[1/4] camel git commit: CAMEL-4074: Make it easier to add custom type
converters manually from a class without having to use that META-INF marker
file. For example spring/blueprint users can then just add a .
Repository: camel
Updated Branches:
refs/heads/master 2b9ccadf8 -> ae6599c6e
CAMEL-4074: Make it easier to add custom type converters manually from a class without having to use that META-INF marker file. For example spring/blueprint users can then just add a <bean>.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/14e95417
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/14e95417
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/14e95417
Branch: refs/heads/master
Commit: 14e95417f956add2eb8cd2a1a934ac8d9a364357
Parents: 2b9ccad
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Apr 5 09:06:19 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Apr 5 09:06:19 2015 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/TypeConverters.java | 26 ++++++
.../converter/BaseTypeConverterRegistry.java | 14 +++
.../impl/converter/TypeConvertersLoader.java | 48 ++++++++++
.../TypeConvertersPackageScanClassResolver.java | 93 ++++++++++++++++++++
.../apache/camel/spi/TypeConverterRegistry.java | 8 ++
.../apache/camel/impl/converter/Country.java | 39 ++++++++
.../camel/impl/converter/MyConverters.java | 36 ++++++++
.../impl/converter/TypeConvertersTest.java | 43 +++++++++
8 files changed, 307 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/main/java/org/apache/camel/TypeConverters.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/TypeConverters.java b/camel-core/src/main/java/org/apache/camel/TypeConverters.java
new file mode 100644
index 0000000..1e276ec
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/TypeConverters.java
@@ -0,0 +1,26 @@
+/**
+ * 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;
+
+/**
+ * A tagging interface to mark this class implements type converters using the {@link Converter} annotations.
+ * <p/>
+ * This can be used to provide custom type converters that can be manually added to the {@link CamelContext} using
+ * {@link org.apache.camel.spi.TypeConverterRegistry#addTypeConverters(TypeConverters)}.
+ */
+public interface TypeConverters {
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java b/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
index e61a918..9650b24 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
@@ -35,6 +35,8 @@ import org.apache.camel.NoFactoryAvailableException;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.TypeConversionException;
import org.apache.camel.TypeConverter;
+import org.apache.camel.TypeConverterLoaderException;
+import org.apache.camel.TypeConverters;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.Injector;
import org.apache.camel.spi.PackageScanClassResolver;
@@ -383,6 +385,18 @@ public abstract class BaseTypeConverterRegistry extends ServiceSupport implement
}
@Override
+ public void addTypeConverters(TypeConverters typeConverters) {
+ log.trace("Adding type converters: {}", typeConverters);
+ try {
+ // scan the class for @Converter and load them into this registry
+ TypeConvertersLoader loader = new TypeConvertersLoader(typeConverters);
+ loader.load(this);
+ } catch (TypeConverterLoaderException e) {
+ throw ObjectHelper.wrapRuntimeCamelException(e);
+ }
+ }
+
+ @Override
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
log.trace("Removing type converter from: {} to: {}", fromType, toType);
TypeMapping key = new TypeMapping(toType, fromType);
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java b/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
new file mode 100644
index 0000000..4ae9d78
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
@@ -0,0 +1,48 @@
+/**
+ * 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.converter;
+
+import java.io.IOException;
+
+import org.apache.camel.TypeConverters;
+
+/**
+ * A type converter loader, that <b>only</b> supports scanning a {@link org.apache.camel.TypeConverters} class
+ * for methods that has been annotated with {@link org.apache.camel.Converter}.
+ */
+public class TypeConvertersLoader extends AnnotationTypeConverterLoader {
+
+ private final TypeConverters typeConverters;
+
+ /**
+ * Creates the loader
+ *
+ * @param typeConverters The implementation that has the type converters
+ */
+ public TypeConvertersLoader(TypeConverters typeConverters) {
+ super(new TypeConvertersPackageScanClassResolver(typeConverters.getClass()));
+ this.typeConverters = typeConverters;
+ }
+
+ @Override
+ protected String[] findPackageNames() throws IOException {
+ // this method doesn't change the behavior of the CorePackageScanClassResolver
+ String name = typeConverters.getClass().getPackage().getName();
+ return new String[]{name};
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersPackageScanClassResolver.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersPackageScanClassResolver.java b/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersPackageScanClassResolver.java
new file mode 100644
index 0000000..2854274
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/impl/converter/TypeConvertersPackageScanClassResolver.java
@@ -0,0 +1,93 @@
+/**
+ * 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.converter;
+
+import java.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.camel.spi.PackageScanClassResolver;
+import org.apache.camel.spi.PackageScanFilter;
+
+/**
+ * A {@link org.apache.camel.spi.ClassResolver} which loads type converters
+ * from an instance that implements {@link org.apache.camel.TypeConverters}.
+ * <p/>
+ * This is used when adding converters manually using the
+ * {@link org.apache.camel.impl.converter.BaseTypeConverterRegistry#addTypeConverters(org.apache.camel.TypeConverters)} method.
+ */
+public class TypeConvertersPackageScanClassResolver implements PackageScanClassResolver {
+
+ private final Set<ClassLoader> classLoaders = new LinkedHashSet<ClassLoader>();
+ private final Set<Class<?>> converters = new LinkedHashSet<Class<?>>();
+
+ public TypeConvertersPackageScanClassResolver(Class<?> clazz) {
+ converters.add(clazz);
+ // use the classloader that loaded the class
+ classLoaders.add(clazz.getClassLoader());
+ }
+
+ @Override
+ public void setClassLoaders(Set<ClassLoader> classLoaders) {
+ // add all the class loaders
+ this.classLoaders.addAll(classLoaders);
+ }
+
+ @Override
+ public Set<ClassLoader> getClassLoaders() {
+ // return a new set to avoid any concurrency issues in other runtimes such as OSGi
+ return Collections.unmodifiableSet(new LinkedHashSet<ClassLoader>(classLoaders));
+ }
+
+ @Override
+ public void addClassLoader(ClassLoader classLoader) {
+ classLoaders.add(classLoader);
+ }
+
+ @Override
+ public Set<Class<?>> findAnnotated(Class<? extends Annotation> annotation, String... packageNames) {
+ return converters;
+ }
+
+ @Override
+ public Set<Class<?>> findAnnotated(Set<Class<? extends Annotation>> annotations, String... packageNames) {
+ return converters;
+ }
+
+ @Override
+ public Set<Class<?>> findImplementations(Class<?> parent, String... packageNames) {
+ // noop
+ return null;
+ }
+
+ @Override
+ public Set<Class<?>> findByFilter(PackageScanFilter filter, String... packageNames) {
+ // noop
+ return null;
+ }
+
+ @Override
+ public void addFilter(PackageScanFilter filter) {
+ // noop
+ }
+
+ @Override
+ public void removeFilter(PackageScanFilter filter) {
+ // noop
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java b/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
index ca381d5..eb0df81 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/TypeConverterRegistry.java
@@ -20,6 +20,7 @@ import java.util.List;
import org.apache.camel.StaticService;
import org.apache.camel.TypeConverter;
+import org.apache.camel.TypeConverters;
/**
* Registry for type converters.
@@ -98,6 +99,13 @@ public interface TypeConverterRegistry extends StaticService {
boolean removeTypeConverter(Class<?> toType, Class<?> fromType);
/**
+ * Registers all the type converters from the class, each converter must be implemented as a method and annotated with {@link org.apache.camel.Converter}.
+ *
+ * @param typeConverters class which implements the type converters
+ */
+ void addTypeConverters(TypeConverters typeConverters);
+
+ /**
* Registers a new fallback type converter
*
* @param typeConverter the type converter to use
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/test/java/org/apache/camel/impl/converter/Country.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/converter/Country.java b/camel-core/src/test/java/org/apache/camel/impl/converter/Country.java
new file mode 100644
index 0000000..020a32d
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/converter/Country.java
@@ -0,0 +1,39 @@
+/**
+ * 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.converter;
+
+public class Country {
+
+ private String iso;
+ private String name;
+
+ public String getIso() {
+ return iso;
+ }
+
+ public void setIso(String iso) {
+ this.iso = iso;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java b/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
new file mode 100644
index 0000000..726e7bc
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
@@ -0,0 +1,36 @@
+/**
+ * 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.converter;
+
+import org.apache.camel.Converter;
+import org.apache.camel.TypeConverters;
+
+public class MyConverters implements TypeConverters {
+
+ @Converter
+ public static Country toCountry(String iso) {
+ Country answer = new Country();
+ answer.setIso("en");
+ answer.setName("England");
+ return answer;
+ }
+
+ @Converter
+ public static String toIso(Country country) {
+ return country.getIso();
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/14e95417/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java b/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
new file mode 100644
index 0000000..9138a38
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
@@ -0,0 +1,43 @@
+/**
+ * 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.converter;
+
+import org.apache.camel.ContextTestSupport;
+
+public class TypeConvertersTest extends ContextTestSupport {
+
+ private MyConverters converters = new MyConverters();
+
+ public void testAdd() throws Exception {
+ int before = context.getTypeConverterRegistry().size();
+
+ context.getTypeConverterRegistry().addTypeConverters(converters);
+
+ int after = context.getTypeConverterRegistry().size();
+ int delta = after - before;
+ assertEquals("There should be 2 more type converters", 2, delta);
+
+ Country country = context.getTypeConverter().convertTo(Country.class, "en");
+ assertNotNull(country);
+ assertEquals("England", country.getName());
+
+ String iso = context.getTypeConverter().convertTo(String.class, country);
+ assertNotNull(iso);
+ assertEquals("en", iso);
+ }
+
+}
[4/4] camel git commit: CAMEL-4074: Fixed CS
Posted by da...@apache.org.
CAMEL-4074: Fixed CS
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ae6599c6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ae6599c6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ae6599c6
Branch: refs/heads/master
Commit: ae6599c6ec02c7973209010991a23fde5db8217f
Parents: 65c57ee
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Apr 5 10:16:31 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Apr 5 10:16:31 2015 +0200
----------------------------------------------------------------------
.../test/java/org/apache/camel/impl/converter/MyConverters.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/ae6599c6/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java b/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
index 726e7bc..7c2ff1c 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/converter/MyConverters.java
@@ -22,7 +22,7 @@ import org.apache.camel.TypeConverters;
public class MyConverters implements TypeConverters {
@Converter
- public static Country toCountry(String iso) {
+ public Country toCountry(String iso) {
Country answer = new Country();
answer.setIso("en");
answer.setName("England");
@@ -30,7 +30,7 @@ public class MyConverters implements TypeConverters {
}
@Converter
- public static String toIso(Country country) {
+ public String toIso(Country country) {
return country.getIso();
}
}
[2/4] camel git commit: CAMEL-4074: Make it easier to add custom type
converters manually from a class without having to use that META-INF marker
file. For example spring/blueprint users can then just add a .
Posted by da...@apache.org.
CAMEL-4074: Make it easier to add custom type converters manually from a class without having to use that META-INF marker file. For example spring/blueprint users can then just add a <bean>.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/140ace54
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/140ace54
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/140ace54
Branch: refs/heads/master
Commit: 140ace545ce5cad52af025d82ba067843d9724a9
Parents: 14e9541
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Apr 5 09:14:30 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Apr 5 09:14:30 2015 +0200
----------------------------------------------------------------------
.../xml/AbstractCamelContextFactoryBean.java | 10 +++++
.../converter/SpringTypeConvertersTest.java | 43 ++++++++++++++++++++
.../impl/converter/SpringTypeConvertersTest.xml | 32 +++++++++++++++
3 files changed, 85 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/140ace54/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index d3904e8..cc77527 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -34,6 +34,7 @@ import org.apache.camel.ManagementStatisticsLevel;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.ShutdownRoute;
import org.apache.camel.ShutdownRunningTask;
+import org.apache.camel.TypeConverters;
import org.apache.camel.builder.ErrorHandlerBuilderRef;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
@@ -236,6 +237,15 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
LOG.info("Using custom RuntimeEndpointRegistry: {}", runtimeEndpointRegistry);
getContext().setRuntimeEndpointRegistry(runtimeEndpointRegistry);
}
+ // custom type converters defined as <bean>s
+ Map<String, TypeConverters> typeConverters = getContext().getRegistry().findByTypeWithName(TypeConverters.class);
+ if (typeConverters != null && !typeConverters.isEmpty()) {
+ for (Entry<String, TypeConverters> entry : typeConverters.entrySet()) {
+ TypeConverters converter = entry.getValue();
+ LOG.info("Adding custom TypeConverters with id: {} and implementation: {}", entry.getKey(), converter);
+ getContext().getTypeConverterRegistry().addTypeConverters(converter);
+ }
+ }
// set the event notifier strategies if defined
Map<String, EventNotifier> eventNotifiers = getContext().getRegistry().findByTypeWithName(EventNotifier.class);
if (eventNotifiers != null && !eventNotifiers.isEmpty()) {
http://git-wip-us.apache.org/repos/asf/camel/blob/140ace54/components/camel-spring/src/test/java/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.java
new file mode 100644
index 0000000..afb5229
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.java
@@ -0,0 +1,43 @@
+/**
+ * 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.spring.impl.converter;
+
+import org.apache.camel.impl.converter.Country;
+import org.apache.camel.spring.SpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringTypeConvertersTest extends SpringTestSupport {
+
+ @Override
+ protected AbstractXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.xml");
+ }
+
+ @Test
+ public void testConvertersShouldBeAddedAutomaticBySpring() throws Exception {
+ Country country = context.getTypeConverter().convertTo(Country.class, "en");
+ assertNotNull(country);
+ assertEquals("England", country.getName());
+
+ String iso = context.getTypeConverter().convertTo(String.class, country);
+ assertNotNull(iso);
+ assertEquals("en", iso);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/140ace54/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.xml
new file mode 100644
index 0000000..db0efda
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/converter/SpringTypeConvertersTest.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+ ">
+
+ <!-- this is our custom type converters implementation -->
+ <bean id="myConverters" class="org.apache.camel.impl.converter.MyConverters"/>
+
+ <!-- we just have an empty CamelContext as we test the converters without routes -->
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+ </camelContext>
+
+</beans>
[3/4] camel git commit: CAMEL-4074: Make it easier to add custom type
converters manually from a class without having to use that META-INF marker
file. For example spring/blueprint users can then just add a .
Posted by da...@apache.org.
CAMEL-4074: Make it easier to add custom type converters manually from a class without having to use that META-INF marker file. For example spring/blueprint users can then just add a <bean>.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/65c57ee2
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/65c57ee2
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/65c57ee2
Branch: refs/heads/master
Commit: 65c57ee27a74a4b6f88162f768a9d0ecab43c587
Parents: 140ace5
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Apr 5 09:26:47 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Apr 5 09:26:47 2015 +0200
----------------------------------------------------------------------
.../camel/core/osgi/OsgiTypeConverter.java | 11 ++++--
.../osgi/test/MockTypeConverterRegistry.java | 5 +++
.../test/blueprint/converter2/Country.java | 40 ++++++++++++++++++++
.../test/blueprint/converter2/MyConverters.java | 36 ++++++++++++++++++
.../blueprint/converter2/MyConvertersTest.java | 40 ++++++++++++++++++++
.../blueprint/converter2/MyConvertersTest.xml | 30 +++++++++++++++
6 files changed, 159 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiTypeConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiTypeConverter.java b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiTypeConverter.java
index 00f3349..cd1de17 100644
--- a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiTypeConverter.java
+++ b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiTypeConverter.java
@@ -26,6 +26,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
+import org.apache.camel.TypeConverters;
import org.apache.camel.impl.DefaultPackageScanClassResolver;
import org.apache.camel.impl.converter.DefaultTypeConverter;
import org.apache.camel.spi.FactoryFinder;
@@ -134,6 +135,10 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
getDelegate().addTypeConverter(toType, fromType, typeConverter);
}
+ public void addTypeConverters(TypeConverters typeConverters) {
+ getDelegate().addTypeConverters(typeConverters);
+ }
+
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
return getDelegate().removeTypeConverter(toType, fromType);
}
@@ -178,13 +183,13 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
DefaultTypeConverter answer = new DefaultTypeConverter(new DefaultPackageScanClassResolver() {
@Override
public Set<ClassLoader> getClassLoaders() {
- // we don't need any classloaders as we use osgi service tracker instead
+ // we don't need any classloaders as we use OSGi service tracker instead
return Collections.emptySet();
}
}, injector, factoryFinder);
try {
- // only load the core type converters, as osgi activator will keep track on bundles
+ // only load the core type converters, as OSGi activator will keep track on bundles
// being installed/uninstalled and load type converters as part of that process
answer.loadCoreTypeConverters();
} catch (Exception e) {
@@ -201,7 +206,7 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
Collections.sort(servicesList);
for (ServiceReference<TypeConverterLoader> sr : servicesList) {
try {
- LOG.debug("loading the type converter from bundle{} ", sr.getBundle().getSymbolicName());
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
} catch (Throwable t) {
throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/test/MockTypeConverterRegistry.java
----------------------------------------------------------------------
diff --git a/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/test/MockTypeConverterRegistry.java b/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/test/MockTypeConverterRegistry.java
index 17fa76b..e9c8777 100644
--- a/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/test/MockTypeConverterRegistry.java
+++ b/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/test/MockTypeConverterRegistry.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.camel.TypeConverter;
+import org.apache.camel.TypeConverters;
import org.apache.camel.spi.Injector;
import org.apache.camel.spi.TypeConverterRegistry;
@@ -39,6 +40,10 @@ public class MockTypeConverterRegistry implements TypeConverterRegistry {
typeConverters.add(typeConverter);
}
+ public void addTypeConverters(TypeConverters typeConverters) {
+ // noop
+ }
+
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
// noop
return true;
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/Country.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/Country.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/Country.java
new file mode 100644
index 0000000..a13a12a
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/Country.java
@@ -0,0 +1,40 @@
+/**
+ * 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.test.blueprint.converter2;
+
+public class Country {
+
+ private String iso;
+ private String name;
+
+ public String getIso() {
+ return iso;
+ }
+
+ public void setIso(String iso) {
+ this.iso = iso;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConverters.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConverters.java
new file mode 100644
index 0000000..e39d89b
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConverters.java
@@ -0,0 +1,36 @@
+/**
+ * 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.test.blueprint.converter2;
+
+import org.apache.camel.Converter;
+import org.apache.camel.TypeConverters;
+
+public class MyConverters implements TypeConverters {
+
+ @Converter
+ public static Country toCountry(String iso) {
+ Country answer = new Country();
+ answer.setIso("en");
+ answer.setName("England");
+ return answer;
+ }
+
+ @Converter
+ public static String toIso(Country country) {
+ return country.getIso();
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConvertersTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConvertersTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConvertersTest.java
new file mode 100644
index 0000000..cb0504a
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/converter2/MyConvertersTest.java
@@ -0,0 +1,40 @@
+/**
+ * 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.test.blueprint.converter2;
+
+import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
+import org.junit.Test;
+
+public class MyConvertersTest extends CamelBlueprintTestSupport {
+
+ @Override
+ protected String getBlueprintDescriptor() {
+ return "org/apache/camel/test/blueprint/converter2/MyConvertersTest.xml";
+ }
+
+ @Test
+ public void testConvertersShouldBeAddedAutomaticByBlueprint() throws Exception {
+ Country country = context.getTypeConverter().convertTo(Country.class, "en");
+ assertNotNull(country);
+ assertEquals("England", country.getName());
+
+ String iso = context.getTypeConverter().convertTo(String.class, country);
+ assertNotNull(iso);
+ assertEquals("en", iso);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/65c57ee2/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/converter2/MyConvertersTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/converter2/MyConvertersTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/converter2/MyConvertersTest.xml
new file mode 100644
index 0000000..0a740b6
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/converter2/MyConvertersTest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+ <!-- this is our custom type converters implementation -->
+ <bean id="myConverters" class="org.apache.camel.test.blueprint.converter2.MyConverters"/>
+
+ <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+ </camelContext>
+
+</blueprint>
+