You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2009/02/05 15:23:09 UTC

svn commit: r741105 - in /servicemix/smx4/nmr/trunk/nmr/core: ./ src/main/java/org/apache/servicemix/nmr/core/ src/main/java/org/apache/servicemix/nmr/core/converter/ src/main/java/org/apache/servicemix/nmr/core/util/ src/main/resources/ src/main/resou...

Author: gnodet
Date: Thu Feb  5 14:23:08 2009
New Revision: 741105

URL: http://svn.apache.org/viewvc?rev=741105&view=rev
Log:
SMX4NMR-62, SMX4NMR-63, SMX4NMR-64

Added:
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/StringSourceConverter.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/StringSource.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/
    servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
    servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/StringSourceTest.java
Modified:
    servicemix/smx4/nmr/trunk/nmr/core/pom.xml
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/MessageImpl.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/ExchangeUtils.java
    servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/MessageImplTest.java
    servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/ExchangeUtilsTest.java

Modified: servicemix/smx4/nmr/trunk/nmr/core/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/pom.xml?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/pom.xml Thu Feb  5 14:23:08 2009
@@ -66,7 +66,6 @@
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-core</artifactId>
-            <version>${camel.version}</version>
             <optional>true</optional>
         </dependency>
     </dependencies>

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java Thu Feb  5 14:23:08 2009
@@ -63,7 +63,8 @@
     private InternalEndpoint destination;
     private Semaphore consumerLock;
     private Semaphore providerLock;
-    private transient Converter converter;
+
+    private static transient Converter converter;
 
     /**
      * Creates and exchange of the given pattern
@@ -456,14 +457,22 @@
         if (type.isInstance(body)) {
             return type.cast(body);
         }
+        return getConverter().convert(body, type);
+    }
+
+    public static Converter getConverter() {
         if (converter == null) {
-            try {
-                converter = new CamelConverter();
-            } catch (Throwable t) {
-                converter = new DummyConverter();
+            synchronized (ExchangeImpl.class) {
+                if (converter == null) {
+                    try {
+                        converter = new CamelConverter();
+                    } catch (Throwable t) {
+                        converter = new DummyConverter();
+                    }
+                }
             }
         }
-        return converter.convert(body, type);
+        return converter;
     }
 
 }

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/MessageImpl.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/MessageImpl.java?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/MessageImpl.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/MessageImpl.java Thu Feb  5 14:23:08 2009
@@ -44,7 +44,7 @@
     private Subject securitySubject;
     private Map<String, Object> headers;
     private Map<String, Object> attachments;
-    private transient Converter converter;
+    private static transient Converter converter;
 
     public MessageImpl() {
     }
@@ -367,14 +367,7 @@
         if (type.isInstance(body)) {
             return type.cast(body);
         }
-        if (converter == null) {
-            try {
-                converter = new CamelConverter();
-            } catch (Throwable t) {
-                converter = new DummyConverter();
-            }
-        }
-        return converter.convert(body, type);
+        return ExchangeImpl.getConverter().convert(body, type);
     }
 
    public String display(boolean displayContent) {

Added: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/StringSourceConverter.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/StringSourceConverter.java?rev=741105&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/StringSourceConverter.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/converter/StringSourceConverter.java Thu Feb  5 14:23:08 2009
@@ -0,0 +1,13 @@
+package org.apache.servicemix.nmr.core.converter;
+
+import org.apache.camel.Converter;
+import org.apache.servicemix.nmr.core.util.StringSource;
+
+@Converter
+public class StringSourceConverter {
+
+    @Converter
+    public static String toString(StringSource source) {
+        return source.getText();
+    }
+}

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/ExchangeUtils.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/ExchangeUtils.java?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/ExchangeUtils.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/ExchangeUtils.java Thu Feb  5 14:23:08 2009
@@ -150,8 +150,8 @@
                 result = result.substring(0, MAX_MSG_DISPLAY_SIZE) + "...";
             }
             return result;
-        } catch (Exception e) {
-            return "Error display value (" + e.toString() + ")";
+        } catch (Throwable t) {
+            return "Error display value (" + t.toString() + ")";
         }
     }
 
@@ -231,12 +231,21 @@
         return is;
     }
 
+    private static TransformerFactory transformerFactory;
+
+    private static TransformerFactory getTransformerFactory() {
+        if (transformerFactory == null) {
+            transformerFactory = TransformerFactory.newInstance();
+        }
+        return transformerFactory;
+    }
+
     private static Source convertSource(Source src) throws TransformerException {
-        if (!(src instanceof DOMSource)) {
-            DOMResult result = new DOMResult();
-            Transformer transformer = TransformerFactory.newInstance().newTransformer();
-            transformer.transform(src, result);
-            return new DOMSource(result.getNode());
+        if (!(src instanceof StringSource)) {
+            StringWriter writer = new StringWriter();
+            StreamResult result = new StreamResult(writer);
+            getTransformerFactory().newTransformer().transform(src, result);
+            return new StringSource(writer.toString());
         }
         return src;
     }

Added: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/StringSource.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/StringSource.java?rev=741105&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/StringSource.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/StringSource.java Thu Feb  5 14:23:08 2009
@@ -0,0 +1,127 @@
+/**
+ * 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.servicemix.nmr.core.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * A helper class which provides a JAXP {@link javax.xml.transform.Source Source} from a String which can
+ * be read as many times as required.
+ *
+ * @version $Revision: 668062 $
+ */
+public class StringSource extends StreamSource implements Externalizable {
+    private String text;
+    private String encoding = "UTF-8";
+
+    public StringSource() {
+    }
+
+    public StringSource(String text) {
+        if (text == null) {
+            throw new NullPointerException("text can not be null");
+        }
+        this.text = text;
+    }
+
+    public StringSource(String text, String systemId) {
+        this(text);
+        setSystemId(systemId);
+    }
+
+    public StringSource(String text, String systemId, String encoding) {
+        this.text = text;
+        this.encoding = encoding;
+        setSystemId(systemId);
+    }
+
+    public InputStream getInputStream() {
+        try {
+            return new ByteArrayInputStream(text.getBytes(encoding));
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Reader getReader() {
+        return new StringReader(text);
+    }
+
+    public String toString() {
+        return "StringSource[" + text + "]";
+    }
+
+    public String getText() {
+        return text;
+    }
+
+    public String getEncoding() {
+        return encoding;
+    }
+
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    public void setText(String text) {
+        this.text = text;
+    }
+
+    public void writeExternal(ObjectOutput out) throws IOException {
+        int b = ((text != null ? 0x01 : 0x00) + (encoding != null ? 0x02 : 0x00)
+                            + (getPublicId() != null ? 0x04 : 0x00) + (getSystemId() != null ? 0x08 : 0x00));
+        out.writeByte(b);
+        if ((b & 0x01) != 0) {
+            out.writeUTF(text);
+        }
+        if ((b & 0x02) != 0) {
+            out.writeUTF(encoding);
+        }
+        if ((b & 0x04) != 0) {
+            out.writeUTF(getPublicId());
+        }
+        if ((b & 0x08) != 0) {
+            out.writeUTF(getSystemId());
+        }
+    }
+
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        int b = in.readByte();
+        if ((b & 0x01) != 0) {
+            text = in.readUTF();
+        }
+        if ((b & 0x02) != 0) {
+            encoding = in.readUTF();
+        }
+        if ((b & 0x04) != 0) {
+            setPublicId(in.readUTF());
+        }
+        if ((b & 0x08) != 0) {
+            setSystemId(in.readUTF());
+        }
+    }
+}

Added: servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/TypeConverter?rev=741105&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/TypeConverter (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/resources/META-INF/services/org/apache/camel/TypeConverter Thu Feb  5 14:23:08 2009
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicemix.nmr.core.converter

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/MessageImplTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/MessageImplTest.java?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/MessageImplTest.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/MessageImplTest.java Thu Feb  5 14:23:08 2009
@@ -46,8 +46,6 @@
         assertNotNull(msg.getBody(byte[].class));
         msg.setBody("<hello>world</hello>", byte[].class);
         assertTrue(msg.getBody() instanceof byte[]);
-        msg.setBody("hello", Message.class);
-        assertEquals("hello", msg.getBody());
     }
 
     @Test

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/ExchangeUtilsTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/ExchangeUtilsTest.java?rev=741105&r1=741104&r2=741105&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/ExchangeUtilsTest.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/ExchangeUtilsTest.java Thu Feb  5 14:23:08 2009
@@ -21,6 +21,9 @@
 
 import javax.xml.namespace.QName;
 import javax.xml.transform.dom.DOMSource;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.Document;
 
 import junit.framework.TestCase;
 import org.apache.servicemix.nmr.api.Exchange;
@@ -28,7 +31,6 @@
 import org.apache.servicemix.nmr.api.Status;
 import org.apache.servicemix.nmr.api.Message;
 import org.apache.servicemix.nmr.core.ExchangeImpl;
-import org.apache.camel.converter.jaxp.StringSource;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -40,12 +42,12 @@
         Exchange e = new ExchangeImpl(Pattern.InOnly);
         Message msg = e.getIn();
         msg.addAttachment("id", new BufferedInputStream(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })));
-        msg.setBody(new StringSource("<hello/>"));
+        msg.setBody(new DOMSource(parse("<hello/>")));
 
         e.ensureReReadable();
 
         assertNotNull(msg.getBody());
-        assertTrue(msg.getBody() instanceof DOMSource);
+        assertTrue(msg.getBody() instanceof StringSource);
         assertNotNull(msg.getAttachment("id"));
         assertTrue(msg.getAttachment("id") instanceof ByteArrayInputStream);
     }
@@ -70,9 +72,13 @@
         str = e.display(true);
         LOG.info(str);
         assertNotNull(msg.getBody());
-        assertTrue(msg.getBody() instanceof DOMSource);
+        assertTrue(msg.getBody() instanceof StringSource);
         assertNotNull(msg.getAttachment("id"));
         assertTrue(msg.getAttachment("id") instanceof ByteArrayInputStream);
     }
 
+    private Document parse(String str) throws Exception {
+        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(str.getBytes()));
+    }
+
 }

Added: servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/StringSourceTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/StringSourceTest.java?rev=741105&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/StringSourceTest.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/StringSourceTest.java Thu Feb  5 14:23:08 2009
@@ -0,0 +1,51 @@
+/*
+ * 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.servicemix.nmr.core.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ObjectInputStream;
+
+import javax.xml.transform.Source;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class StringSourceTest {
+
+    @Test
+    public void testSerialize() throws Exception {
+        Source src = new StringSource("<hello/>");
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(src);
+        oos.close();
+
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        Object obj = ois.readObject();
+
+        assertNotNull(obj);
+        assertTrue(obj instanceof StringSource);
+        assertEquals(((StringSource) src).getText(), ((StringSource) obj).getText());
+    }
+
+}