Author: davsclaus
Date: Fri Apr 29 12:57:34 2011
New Revision: 1097805
URL: http://svn.apache.org/viewvc?rev=1097805&view=rev
Log:
CAMEL-3914: camel-jaxb does not try to convert to BeanInvocation/Processor as
it would lead to unnessasary convertion attempts.
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNotificationService.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyPersonService.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/NotificationType.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanNotificationTypeTest.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanPersonTypeTest.java
Modified:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties
Modified:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=1097805&r1=1097804&r2=1097805&view=diff
==============================================================================
---
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
(original)
+++
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
Fri Apr 29 12:57:34 2011
@@ -40,8 +40,10 @@ import javax.xml.transform.Source;
import org.apache.camel.Exchange;
import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.Processor;
import org.apache.camel.StreamCache;
import org.apache.camel.TypeConverter;
+import org.apache.camel.component.bean.BeanInvocation;
import org.apache.camel.converter.IOConverter;
import org.apache.camel.spi.TypeConverterAware;
import org.apache.camel.util.IOHelper;
@@ -50,7 +52,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * @version
+ * @version
*/
public class FallbackTypeConverter implements TypeConverter,
TypeConverterAware {
private static final transient Logger LOG =
LoggerFactory.getLogger(FallbackTypeConverter.class);
@@ -79,6 +81,13 @@ public class FallbackTypeConverter imple
}
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
+ // do not convert to stream cache
+ if (BeanInvocation.class.isAssignableFrom(type) ||
Processor.class.isAssignableFrom(type)) {
+ // JAXB cannot convert to a BeanInvocation / Processor, so we need
to indicate this
+ // to avoid Camel trying to do this when using beans with JAXB
payloads
+ return null;
+ }
+
try {
if (isJaxbType(type)) {
return unmarshall(type, exchange, value);
@@ -92,7 +101,7 @@ public class FallbackTypeConverter imple
} catch (Exception e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
-
+
}
public <T> T mandatoryConvertTo(Class<T> type, Object value) throws
NoTypeConversionAvailableException {
@@ -116,6 +125,8 @@ public class FallbackTypeConverter imple
* Lets try parse via JAXB
*/
protected <T> T unmarshall(Class<T> type, Exchange exchange, Object value)
throws Exception {
+ LOG.trace("Unmarshal to {} with value {}", type, value);
+
if (value == null) {
throw new IllegalArgumentException("Cannot convert from null value
to JAXBSource");
}
@@ -154,6 +165,8 @@ public class FallbackTypeConverter imple
}
protected <T> T marshall(Class<T> type, Exchange exchange, Object value)
throws JAXBException, XMLStreamException, FactoryConfigurationError {
+ LOG.trace("Marshal from value {} to type {}", value, type);
+
T answer = null;
if (parentTypeConverter != null) {
// lets convert the object to a JAXB source and try convert that to
@@ -179,18 +192,7 @@ public class FallbackTypeConverter imple
return answer;
}
- /**
- * Unmarshals the given value with the unmarshaller
- *
- * @param unmarshaller the unmarshaller
- * @param exchange the exchange
- * @param value the stream to unmarshal (will close it after use, also if
exception is thrown)
- * @return the value
- * @throws JAXBException is thrown if an exception occur while
unmarshalling
- * @throws UnsupportedEncodingException
- */
- protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange,
Object value)
- throws JAXBException, UnsupportedEncodingException {
+ protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange,
Object value) throws JAXBException, UnsupportedEncodingException {
try {
if (value instanceof InputStream) {
if (needFiltering(exchange)) {
@@ -215,7 +217,7 @@ public class FallbackTypeConverter imple
}
return null;
}
-
+
protected boolean needFiltering(Exchange exchange) {
// exchange property takes precedence over data format property
return exchange != null &&
exchange.getProperty(Exchange.FILTER_NON_XML_CHARS, Boolean.FALSE,
Boolean.class);
Modified:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java?rev=1097805&r1=1097804&r2=1097805&view=diff
==============================================================================
---
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
(original)
+++
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
Fri Apr 29 12:57:34 2011
@@ -30,13 +30,14 @@ import org.w3c.dom.Document;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
-import org.apache.camel.converter.HasAnnotation;
import org.apache.camel.converter.jaxp.XmlConverter;
/**
* As we have the JAXB FallbackTypeConverter, so we don't need to register
this converter
- * @version
+ *
+ * @deprecated will be removed in the near future
*/
+@Deprecated
public final class JaxbConverter {
private XmlConverter xmlConverter = new XmlConverter();
private Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>,
JAXBContext>();
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNotificationService.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNotificationService.java?rev=1097805&view=auto
==============================================================================
---
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNotificationService.java
(added)
+++
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNotificationService.java
Fri Apr 29 12:57:34 2011
@@ -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.jaxb;
+
+import org.apache.camel.Exchange;
+
+/**
+ * NotificationType does not have any JAXB ObjectFactory so we should test you
can still route using that
+ */
+public class MyNotificationService {
+
+ public void createNotification(Exchange exchange) {
+ NotificationType notification = new NotificationType();
+ notification.setEvent("Hello");
+
+ exchange.getOut().setBody(notification);
+ }
+
+ public void sendNotification(Exchange exchange) {
+ NotificationType notification = (NotificationType)
exchange.getIn().getBody();
+
+ exchange.getContext().createProducerTemplate().sendBody("mock:notify",
notification);
+ }
+
+}
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyPersonService.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyPersonService.java?rev=1097805&view=auto
==============================================================================
---
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyPersonService.java
(added)
+++
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyPersonService.java
Fri Apr 29 12:57:34 2011
@@ -0,0 +1,41 @@
+/**
+ * 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.jaxb;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.foo.bar.PersonType;
+
+/**
+ *
+ */
+public class MyPersonService {
+
+ public void createPerson(Exchange exchange) {
+ PersonType person = new PersonType();
+ person.setFirstName("Homer");
+ person.setLastName("Simpson");
+
+ exchange.getOut().setBody(person);
+ }
+
+ public void sendPerson(Exchange exchange) {
+ PersonType person = (PersonType) exchange.getIn().getBody();
+
+ exchange.getContext().createProducerTemplate().sendBody("mock:person",
person);
+ }
+
+}
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/NotificationType.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/NotificationType.java?rev=1097805&view=auto
==============================================================================
---
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/NotificationType.java
(added)
+++
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/NotificationType.java
Fri Apr 29 12:57:34 2011
@@ -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.jaxb;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlRootElement(name = "Notification")
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "NotificationType", propOrder = {"event"})
+public class NotificationType {
+ @XmlElement(required = true)
+ protected String event;
+
+ public String getEvent() {
+ return event;
+ }
+
+ public void setEvent(String event) {
+ this.event = event;
+ }
+}
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanNotificationTypeTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanNotificationTypeTest.java?rev=1097805&view=auto
==============================================================================
---
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanNotificationTypeTest.java
(added)
+++
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanNotificationTypeTest.java
Fri Apr 29 12:57:34 2011
@@ -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.jaxb;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class TimerBeanToBeanNotificationTypeTest extends CamelTestSupport {
+
+ @Test
+ public void testBeanToBean() throws Exception {
+ getMockEndpoint("mock:notify").expectedMessageCount(1);
+
getMockEndpoint("mock:notify").message(0).body().isInstanceOf(NotificationType.class);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("timer:foo?repeatCount=1")
+ .log("Timer triggered")
+ .bean(MyNotificationService.class, "createNotification")
+ .bean(MyNotificationService.class, "sendNotification");
+ }
+ };
+ }
+}
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanPersonTypeTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanPersonTypeTest.java?rev=1097805&view=auto
==============================================================================
---
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanPersonTypeTest.java
(added)
+++
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/TimerBeanToBeanPersonTypeTest.java
Fri Apr 29 12:57:34 2011
@@ -0,0 +1,49 @@
+/**
+ * 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.jaxb;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.foo.bar.PersonType;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * PersonType has a ObjectFactory so JAXB can convert to it, but we should
still route it as is
+ */
+public class TimerBeanToBeanPersonTypeTest extends CamelTestSupport {
+
+ @Test
+ public void testBeanToBean() throws Exception {
+ getMockEndpoint("mock:person").expectedMessageCount(1);
+
getMockEndpoint("mock:person").message(0).body().isInstanceOf(PersonType.class);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("timer:foo?repeatCount=1")
+ .log("Timer triggered")
+ .bean(MyPersonService.class, "createPerson")
+ .bean(MyPersonService.class, "sendPerson");
+ }
+ };
+ }
+}
Modified: camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties?rev=1097805&r1=1097804&r2=1097805&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties
(original)
+++ camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties Fri
Apr 29 12:57:34 2011
@@ -22,6 +22,7 @@ log4j.rootLogger=INFO, file
# uncomment this to turn on debug of camel
#log4j.logger.org.apache.camel=DEBUG
+#log4j.logger.org.apache.camel.converter.jaxb=TRACE
# CONSOLE appender not used by default
log4j.appender.stdout=org.apache.log4j.ConsoleAppender