polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [34/52] [abbrv] polygene-java git commit: Serialization implementation details cleanup
Date Sun, 02 Apr 2017 17:48:16 GMT
Serialization implementation details cleanup

remove unused imports, prefer final fields, introduce constants where
appropriate, uniform Java serialization and Base64 encoding etc…

POLYGENE-231


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/cf842f45
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/cf842f45
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/cf842f45

Branch: refs/heads/develop
Commit: cf842f451efaa601ea4eb2600a15e5eb1535e221
Parents: b094d05
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon Mar 13 10:04:25 2017 +0100
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Sun Apr 2 19:16:23 2017 +0200

----------------------------------------------------------------------
 .../serialization/javaxjson/JavaxJson.java        |  2 ++
 .../javaxjson/JavaxJsonAdapters.java              |  2 +-
 .../javaxjson/JavaxJsonDeserializer.java          |  9 ++++-----
 .../javaxjson/JavaxJsonSerializer.java            | 18 ++++++------------
 .../javaxjson/JavaxJsonSettings.java              |  4 ++--
 .../spi/serialization/JsonDeserializer.java       |  5 ++---
 .../spi/serialization/XmlDeserializer.java        |  5 -----
 .../javaxxml/JavaxXmlDeserializer.java            |  9 ++++-----
 .../javaxxml/JavaxXmlSerializer.java              | 13 ++++++-------
 .../msgpack/MessagePackDeserializer.java          | 11 ++++++-----
 .../msgpack/MessagePackSerializer.java            | 14 ++++++++------
 11 files changed, 41 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJson.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJson.java
b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJson.java
index 3c44864..3e7ad6f 100644
--- a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJson.java
+++ b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJson.java
@@ -33,6 +33,8 @@ import javax.json.JsonValue;
  */
 public class JavaxJson
 {
+    public static JsonValue EMPTY_STRING = toJsonString( "" );
+
     /**
      * Create a {@link JsonObjectBuilder} populated with the state of a {@link JsonObject}.
      *

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonAdapters.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonAdapters.java
b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonAdapters.java
index fdc9d27..a46accd 100644
--- a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonAdapters.java
+++ b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonAdapters.java
@@ -38,7 +38,7 @@ public interface JavaxJsonAdapters
 
     class Mixin implements JavaxJsonAdapters
     {
-        private Map<ValueType, JavaxJsonAdapter<?>> adapters = new LinkedHashMap<>();
+        private final Map<ValueType, JavaxJsonAdapter<?>> adapters = new LinkedHashMap<>();
 
         @Override
         public void registerAdapter( ValueType valueType, JavaxJsonAdapter<?> adapter
)

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDeserializer.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDeserializer.java
b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDeserializer.java
index a1e5d75..2d8cc53 100644
--- a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDeserializer.java
+++ b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonDeserializer.java
@@ -158,7 +158,8 @@ public class JavaxJsonDeserializer extends AbstractTextDeserializer implements
J
                     return (T) deserializeValueComposite( module, valueDescriptor.valueType(),
object );
                 }
             case STRING:
-                return (T) deserializeBase64( asString( json ) );
+                byte[] bytes = Base64.getDecoder().decode( asString( json ).getBytes( UTF_8
) );
+                return (T) deserializeJava( bytes );
             default:
                 throw new SerializationException( "Don't know how to deserialize " + valueType
+ " from " + json );
         }
@@ -285,17 +286,15 @@ public class JavaxJsonDeserializer extends AbstractTextDeserializer
implements J
         };
     }
 
-    private Object deserializeBase64( String inputString )
+    private Object deserializeJava( byte[] bytes )
     {
-        byte[] bytes = inputString.getBytes( UTF_8 );
-        bytes = Base64.getDecoder().decode( bytes );
         try( ObjectInputStream oin = new ObjectInputStream( new ByteArrayInputStream( bytes
) ) )
         {
             return oin.readObject();
         }
         catch( IOException | ClassNotFoundException ex )
         {
-            throw new SerializationException( "Unable to deserialize Base64 serialized "
+ inputString, ex );
+            throw new SerializationException( "Unable to deserialize using Java serialization",
ex );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializer.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializer.java
b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializer.java
index 07294a9..93a9077 100644
--- a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializer.java
+++ b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSerializer.java
@@ -20,7 +20,6 @@ package org.apache.polygene.serialization.javaxjson;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
-import java.io.UncheckedIOException;
 import java.util.Base64;
 import java.util.Map;
 import java.util.function.Function;
@@ -32,7 +31,6 @@ import javax.json.JsonArray;
 import javax.json.JsonArrayBuilder;
 import javax.json.JsonObject;
 import javax.json.JsonObjectBuilder;
-import javax.json.JsonString;
 import javax.json.JsonValue;
 import org.apache.polygene.api.PolygeneAPI;
 import org.apache.polygene.api.association.AssociationStateHolder;
@@ -41,6 +39,7 @@ import org.apache.polygene.api.injection.scope.This;
 import org.apache.polygene.api.injection.scope.Uses;
 import org.apache.polygene.api.serialization.Converter;
 import org.apache.polygene.api.serialization.Converters;
+import org.apache.polygene.api.serialization.SerializationException;
 import org.apache.polygene.api.service.ServiceDescriptor;
 import org.apache.polygene.api.type.ArrayType;
 import org.apache.polygene.api.type.MapType;
@@ -111,8 +110,8 @@ public class JavaxJsonSerializer extends AbstractTextSerializer implements
JsonS
             return serializeStream( options, (Stream<?>) object );
         }
         // Fallback to Java Serialization in Base 64
-        // Include all arrays!
-        return serializeBase64( object );
+        byte[] bytes = Base64.getEncoder().encode( serializeJava( object ) );
+        return JavaxJson.toJsonString( new String( bytes, UTF_8 ) );
     }
 
     private JsonObject serializeValueComposite( Options options, Object composite, boolean
root )
@@ -219,23 +218,18 @@ public class JavaxJsonSerializer extends AbstractTextSerializer implements
JsonS
         return builder.build();
     }
 
-    private JsonString serializeBase64( Object object )
-    {
-        byte[] bytes = Base64.getEncoder().encode( javaSerialization( object ) );
-        return JavaxJson.toJsonString( new String( bytes, UTF_8 ) );
-    }
-
-    private byte[] javaSerialization( Object object )
+    private byte[] serializeJava( Object object )
     {
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         try( ObjectOutputStream out = new ObjectOutputStream( bout ) )
         {
             out.writeUnshared( object );
+            out.flush();
             return bout.toByteArray();
         }
         catch( IOException ex )
         {
-            throw new UncheckedIOException( ex );
+            throw new SerializationException( "Unable to serialize using Java serialization",
ex );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSettings.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSettings.java
b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSettings.java
index 266bd99..e0f993d 100644
--- a/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSettings.java
+++ b/core/spi/src/main/java/org/apache/polygene/serialization/javaxjson/JavaxJsonSettings.java
@@ -56,13 +56,13 @@ public class JavaxJsonSettings
 
     public JavaxJsonSettings withTypeInfoPropertyName( String typeInfoPropertyName )
     {
-        setTypeInfoPropertyName( typeInfoPropertyName );
+        this.typeInfoPropertyName = typeInfoPropertyName;
         return this;
     }
 
     public JavaxJsonSettings withJsonAdapter( ValueType valueType, JavaxJsonAdapter<?>
adapter )
     {
-        getAdapters().put( valueType, adapter );
+        adapters.put( valueType, adapter );
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/spi/serialization/JsonDeserializer.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/spi/serialization/JsonDeserializer.java
b/core/spi/src/main/java/org/apache/polygene/spi/serialization/JsonDeserializer.java
index 84c8993..a0dac71 100644
--- a/core/spi/src/main/java/org/apache/polygene/spi/serialization/JsonDeserializer.java
+++ b/core/spi/src/main/java/org/apache/polygene/spi/serialization/JsonDeserializer.java
@@ -34,6 +34,7 @@ import javax.json.stream.JsonParsingException;
 import org.apache.polygene.api.serialization.Deserializer;
 import org.apache.polygene.api.structure.ModuleDescriptor;
 import org.apache.polygene.api.type.ValueType;
+import org.apache.polygene.serialization.javaxjson.JavaxJson;
 import org.apache.polygene.spi.module.ModuleSpi;
 
 import static java.util.stream.Collectors.joining;
@@ -157,8 +158,6 @@ public interface JsonDeserializer extends Deserializer
             return outOfStructureFunction.apply( stateString );
         }
         // Empty state string?
-        JsonValue emptyJsonString = Json.createReader( new StringReader( "{\"empty\":\"\"}"
) )
-                                        .readObject().get( "empty" );
-        return fromJson( module, valueType, emptyJsonString );
+        return fromJson( module, valueType, JavaxJson.EMPTY_STRING );
     }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/core/spi/src/main/java/org/apache/polygene/spi/serialization/XmlDeserializer.java
----------------------------------------------------------------------
diff --git a/core/spi/src/main/java/org/apache/polygene/spi/serialization/XmlDeserializer.java
b/core/spi/src/main/java/org/apache/polygene/spi/serialization/XmlDeserializer.java
index c7ac42b..9e559c8 100644
--- a/core/spi/src/main/java/org/apache/polygene/spi/serialization/XmlDeserializer.java
+++ b/core/spi/src/main/java/org/apache/polygene/spi/serialization/XmlDeserializer.java
@@ -28,12 +28,7 @@ import javax.xml.parsers.ParserConfigurationException;
 import org.apache.polygene.api.serialization.Deserializer;
 import org.apache.polygene.api.serialization.SerializationException;
 import org.apache.polygene.api.structure.ModuleDescriptor;
-import org.apache.polygene.api.type.CollectionType;
-import org.apache.polygene.api.type.EnumType;
-import org.apache.polygene.api.type.MapType;
-import org.apache.polygene.api.type.ValueCompositeType;
 import org.apache.polygene.api.type.ValueType;
-import org.apache.polygene.api.value.ValueDescriptor;
 import org.apache.polygene.spi.module.ModuleSpi;
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlDeserializer.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlDeserializer.java
b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlDeserializer.java
index f477a8c..146d763 100644
--- a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlDeserializer.java
+++ b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlDeserializer.java
@@ -315,22 +315,21 @@ public class JavaxXmlDeserializer extends AbstractTextDeserializer implements
Xm
         }
         if( xml.getNodeType() == Node.CDATA_SECTION_NODE )
         {
-            return deserializeBase64( xml.getNodeValue() );
+            byte[] bytes = Base64.getDecoder().decode( xml.getNodeValue().getBytes( UTF_8
) );
+            return deserializeJava( bytes );
         }
         throw new SerializationException( "Don't know how to deserialize " + valueType +
" from " + xml );
     }
 
-    private Object deserializeBase64( String inputString )
+    private Object deserializeJava( byte[] bytes )
     {
-        byte[] bytes = inputString.getBytes( UTF_8 );
-        bytes = Base64.getDecoder().decode( bytes );
         try( ObjectInputStream oin = new ObjectInputStream( new ByteArrayInputStream( bytes
) ) )
         {
             return oin.readObject();
         }
         catch( IOException | ClassNotFoundException ex )
         {
-            throw new SerializationException( "Unable to deserialize Base64 serialized "
+ inputString, ex );
+            throw new SerializationException( "Unable to deserialize using Java serialization",
ex );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlSerializer.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlSerializer.java
b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlSerializer.java
index 49f1fa0..576647b 100644
--- a/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlSerializer.java
+++ b/extensions/serialization-javaxxml/src/main/java/org/apache/polygene/serialization/javaxxml/JavaxXmlSerializer.java
@@ -20,7 +20,6 @@ package org.apache.polygene.serialization.javaxxml;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
-import java.io.UncheckedIOException;
 import java.util.Base64;
 import java.util.Map;
 import java.util.function.Function;
@@ -139,8 +138,8 @@ public class JavaxXmlSerializer extends AbstractTextSerializer implements
XmlSer
             return serializeStream( document, options, (Stream<?>) object );
         }
         // Fallback to Java Serialization in Base 64
-        // Include all arrays!
-        return serializeBase64( document, object );
+        byte[] bytes = Base64.getEncoder().encode( serializeJava( object ) );
+        return document.createCDATASection( new String( bytes, UTF_8 ) );
     }
 
     private <T> Node serializeValueComposite( Document document, Options options, T
composite, boolean root )
@@ -280,18 +279,18 @@ public class JavaxXmlSerializer extends AbstractTextSerializer implements
XmlSer
         return collectionElement;
     }
 
-    private <T> Node serializeBase64( Document document, T object )
+    private byte[] serializeJava( Object object )
     {
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         try( ObjectOutputStream out = new ObjectOutputStream( bout ) )
         {
             out.writeUnshared( object );
-            byte[] bytes = Base64.getEncoder().encode( bout.toByteArray() );
-            return document.createCDATASection( new String( bytes, UTF_8 ) );
+            out.flush();
+            return bout.toByteArray();
         }
         catch( IOException ex )
         {
-            throw new UncheckedIOException( ex );
+            throw new SerializationException( "Unable to serialize using Java serialization",
ex );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
index 7a82751..9d5c9df 100644
--- a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
+++ b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackDeserializer.java
@@ -56,7 +56,6 @@ import org.apache.polygene.spi.serialization.AbstractBinaryDeserializer;
 import org.msgpack.core.MessagePack;
 import org.msgpack.core.MessageUnpacker;
 import org.msgpack.value.ArrayValue;
-import org.msgpack.value.BinaryValue;
 import org.msgpack.value.ImmutableValue;
 import org.msgpack.value.MapValue;
 import org.msgpack.value.Value;
@@ -285,7 +284,7 @@ public interface MessagePackDeserializer extends Deserializer
             switch( value.getValueType() )
             {
                 case BINARY:
-                    return deserializeJava( value.asBinaryValue() );
+                    return deserializeJava( value.asBinaryValue().asByteArray() );
                 case MAP:
                     MapValue mapValue = value.asMapValue();
                     Optional<String> typeInfo = mapValue
@@ -313,14 +312,16 @@ public interface MessagePackDeserializer extends Deserializer
             }
         }
 
-        private Object deserializeJava( BinaryValue value )
-            throws IOException, ClassNotFoundException
+        private Object deserializeJava( byte[] bytes )
         {
-            byte[] bytes = value.asByteArray();
             try( ObjectInputStream oin = new ObjectInputStream( new ByteArrayInputStream(
bytes ) ) )
             {
                 return oin.readObject();
             }
+            catch( IOException | ClassNotFoundException ex )
+            {
+                throw new SerializationException( "Unable to deserialize using Java serialization",
ex );
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/cf842f45/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackSerializer.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackSerializer.java
b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackSerializer.java
index c01ef29..ea66ad0 100644
--- a/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackSerializer.java
+++ b/extensions/serialization-msgpack/src/main/java/org/apache/polygene/serialization/msgpack/MessagePackSerializer.java
@@ -45,7 +45,6 @@ import org.apache.polygene.spi.serialization.AbstractBinarySerializer;
 import org.msgpack.core.MessagePack;
 import org.msgpack.core.MessagePacker;
 import org.msgpack.value.ArrayValue;
-import org.msgpack.value.BinaryValue;
 import org.msgpack.value.MapValue;
 import org.msgpack.value.Value;
 import org.msgpack.value.ValueFactory;
@@ -124,8 +123,7 @@ public interface MessagePackSerializer extends Serializer
                     return serializeStream( options, (Stream<?>) object );
                 }
                 // Fallback to Java Serialization
-                // Include all arrays!
-                return serializeJava( object );
+                return ValueFactory.newBinary( serializeJava( object ) );
             }
             catch( IOException ex )
             {
@@ -203,14 +201,18 @@ public interface MessagePackSerializer extends Serializer
                                                 .collect( toList() ) );
         }
 
-        private BinaryValue serializeJava( Object object ) throws IOException
+        private byte[] serializeJava( Object object )
         {
             ByteArrayOutputStream bout = new ByteArrayOutputStream();
             try( ObjectOutputStream out = new ObjectOutputStream( bout ) )
             {
                 out.writeUnshared( object );
-                byte[] bytes = bout.toByteArray();
-                return ValueFactory.newBinary( bytes );
+                out.flush();
+                return bout.toByteArray();
+            }
+            catch( IOException ex )
+            {
+                throw new SerializationException( "Unable to serialize using Java serialization",
ex );
             }
         }
     }


Mime
View raw message