Author: davsclaus
Date: Thu Jan 19 09:18:22 2012
New Revision: 1233259
URL: http://svn.apache.org/viewvc?rev=1233259&view=rev
Log:
CAMEL-4915: Serialization data format should use ClassResolver API from Camel
to work with other runtimes.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MySerialBean.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SerializationDataFormatTest.java
- copied, changed from r1233154,
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=1233259&r1=1233258&r2=1233259&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Thu Jan 19 09:18:22 2012
@@ -31,6 +31,7 @@ import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
@@ -326,11 +327,28 @@ public final class IOConverter {
}
@Converter
- public static ObjectInput toObjectInput(InputStream stream) throws
IOException {
+ public static ObjectInput toObjectInput(final InputStream stream, final
Exchange exchange) throws IOException {
if (stream instanceof ObjectInput) {
return (ObjectInput) stream;
} else {
- return new ObjectInputStream(IOHelper.buffered(stream));
+ return new ObjectInputStream(IOHelper.buffered(stream)) {
+ @Override
+ protected Class<?> resolveClass(ObjectStreamClass
objectStreamClass) throws IOException, ClassNotFoundException {
+ // need to let Camel be able to resolve class using
ClassResolver SPI, to let class loading
+ // work in OSGi and other runtimes
+ Class<?> answer = null;
+ String name = objectStreamClass.getName();
+ if (exchange != null) {
+ LOG.trace("Loading class {} using Camel
ClassResolver", name);
+ answer =
exchange.getContext().getClassResolver().resolveClass(name);
+ }
+ if (answer == null) {
+ LOG.trace("Loading class {} using JDK default
implementation", name);
+ answer = super.resolveClass(objectStreamClass);
+ }
+ return answer;
+ }
+ };
}
}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MySerialBean.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MySerialBean.java?rev=1233259&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MySerialBean.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MySerialBean.java
Thu Jan 19 09:18:22 2012
@@ -0,0 +1,44 @@
+/**
+ * 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 java.io.Serializable;
+
+/**
+ *
+ */
+public class MySerialBean implements Serializable {
+
+ private int id;
+ private String name;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Copied:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SerializationDataFormatTest.java
(from r1233154,
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SerializationDataFormatTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SerializationDataFormatTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java&r1=1233154&r2=1233259&rev=1233259&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SerializationDataFormatTest.java
Thu Jan 19 09:18:22 2012
@@ -16,158 +16,42 @@
*/
package org.apache.camel.impl;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
-import org.apache.camel.ProducerTemplate;
-import org.apache.camel.TestSupport;
+import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
/**
- * Unit test of the string data format.
+ *
*/
-public class StringDataFormatTest extends TestSupport {
-
- private CamelContext context;
- private ProducerTemplate template;
-
- protected void setUp() throws Exception {
- context = new DefaultCamelContext();
- context.setTracing(true);
- template = context.createProducerTemplate();
- template.start();
- }
-
- protected void tearDown() throws Exception {
- template.stop();
- context.stop();
- }
-
- public void testMarshalUTF8() throws Exception {
- // NOTE: We are using a processor to do the assertions as the mock
endpoint (Camel) does not yet support
- // type conversion using byte and strings where you can set a charset
encoding
-
- // include a UTF-8 char in the text \u0E08 is a Thai elephant
- final String title = "Hello Thai Elephant \u0E08";
-
- context.addRoutes(new RouteBuilder() {
- public void configure() {
- from("direct:start").marshal().string("UTF-8").process(new
MyBookProcessor("UTF-8", title));
- }
- });
- context.start();
-
- MyBook book = new MyBook();
- book.setTitle(title);
-
- template.sendBody("direct:start", book);
- }
-
- public void testMarshalNoEncoding() throws Exception {
- // NOTE: We are using a processor to do the assertions as the mock
endpoint (Camel) does not yet support
- // type conversion using byte and strings where you can set a charset
encoding
-
- final String title = "Hello World";
-
- context.addRoutes(new RouteBuilder() {
- public void configure() {
- from("direct:start").marshal().string().process(new
MyBookProcessor(null, title));
- }
- });
- context.start();
-
- MyBook book = new MyBook();
- book.setTitle(title);
-
- template.sendBody("direct:start", book);
- }
-
-
- public void testUnmarshalUTF8() throws Exception {
- // NOTE: Here we can use a MockEndpoint as we unmarshal the
inputstream to String
-
- // include a UTF-8 char in the text \u0E08 is a Thai elephant
- final String title = "Hello Thai Elephant \u0E08";
-
- context.addRoutes(new RouteBuilder() {
- public void configure() {
-
from("direct:start").unmarshal().string("UTF-8").to("mock:unmarshal");
- }
- });
- context.start();
-
- byte[] bytes = title.getBytes("UTF-8");
- InputStream in = new ByteArrayInputStream(bytes);
-
- template.sendBody("direct:start", in);
-
- MockEndpoint mock = context.getEndpoint("mock:unmarshal",
MockEndpoint.class);
- mock.setExpectedMessageCount(1);
- mock.expectedBodiesReceived(title);
- }
-
- public void testUnmarshalNoEncoding() throws Exception {
- // NOTE: Here we can use a MockEndpoint as we unmarshal the
inputstream to String
-
- final String title = "Hello World";
-
- context.addRoutes(new RouteBuilder() {
- public void configure() {
- from("direct:start").unmarshal().string().to("mock:unmarshal");
- }
- });
- context.start();
-
- byte[] bytes = title.getBytes();
- InputStream in = new ByteArrayInputStream(bytes);
+public class SerializationDataFormatTest extends ContextTestSupport {
+
+ public void testSerialization() throws Exception {
+ MySerialBean bean = new MySerialBean();
+ bean.setId(123);
+ bean.setName("Donald");
+
+ Object data = template.requestBody("direct:marshal", bean);
+ assertNotNull(data);
+
+ Object out = template.requestBody("direct:unmarshal", data);
+ assertNotNull(out);
+
+ MySerialBean outBean =
context.getTypeConverter().convertTo(MySerialBean.class, out);
+ assertNotNull(outBean);
+ assertEquals(123, outBean.getId());
+ assertEquals("Donald", outBean.getName());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:unmarshal")
+ .unmarshal().serialization();
- template.sendBody("direct:start", in);
-
- MockEndpoint mock = context.getEndpoint("mock:unmarshal",
MockEndpoint.class);
- mock.setExpectedMessageCount(1);
- mock.expectedBodiesReceived(title);
- }
-
- private static class MyBookProcessor implements Processor {
-
- private String encoding;
- private String title;
-
- public MyBookProcessor(String encoding, String title) {
- this.encoding = encoding;
- this.title = title;
- }
-
- public void process(Exchange exchange) throws Exception {
- byte[] body = exchange.getIn().getBody(byte[].class);
-
- String text;
- if (encoding != null) {
- text = new String(body, encoding);
- } else {
- text = new String(body);
+ from("direct:marshal")
+ .marshal().serialization();
}
-
- // does the testing
- assertEquals(text, title);
- }
+ };
}
-
- private static class MyBook {
- private String title;
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String toString() {
- // Camel will fallback to object toString converter and thus we
get this text
- return title;
- }
- }
-
}