You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by si...@apache.org on 2012/02/16 10:27:45 UTC
svn commit: r1244900 - in
/incubator/directmemory/trunk/serializers/protobuf: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/directmemory/
src/main/java/org/apache/directmemory/serialization/ src/...
Author: simonetripodi
Date: Thu Feb 16 09:27:45 2012
New Revision: 1244900
URL: http://svn.apache.org/viewvc?rev=1244900&view=rev
Log:
first checkin of Google Protobuf serializer adapter
Added:
incubator/directmemory/trunk/serializers/protobuf/ (with props)
incubator/directmemory/trunk/serializers/protobuf/src/
incubator/directmemory/trunk/serializers/protobuf/src/main/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java (with props)
incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java (with props)
Propchange: incubator/directmemory/trunk/serializers/protobuf/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 16 09:27:45 2012
@@ -0,0 +1,2 @@
+bin
+target
Added: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java?rev=1244900&view=auto
==============================================================================
--- incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java (added)
+++ incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java Thu Feb 16 09:27:45 2012
@@ -0,0 +1,109 @@
+package org.apache.directmemory.serialization.protobuf;
+
+/*
+ * 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.
+ */
+
+import static java.lang.String.format;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Method;
+
+import org.apache.directmemory.serialization.Serializer;
+import org.kohsuke.MetaInfServices;
+
+import com.google.protobuf.GeneratedMessage;
+import com.google.protobuf.Message;
+
+@MetaInfServices
+public final class ProtobufSerializer
+ implements Serializer
+{
+
+ private static final String NEW_BUILDER_METHOD = "newBuilder";
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> byte[] serialize( T obj )
+ throws IOException
+ {
+ checkProtobufMessage( obj.getClass() );
+
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ try
+ {
+ ( (Message) obj ).writeTo( baos );
+ }
+ finally
+ {
+ try
+ {
+ baos.close();
+ }
+ catch ( Exception e )
+ {
+ // close quietly
+ }
+ }
+
+ return baos.toByteArray();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> T deserialize( byte[] source, Class<T> clazz )
+ throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
+ {
+ clazz = checkProtobufMessage( clazz );
+
+ try
+ {
+ Method newBuilder = clazz.getMethod( NEW_BUILDER_METHOD );
+
+ // fixme no idea ATM how to fix type inference here
+ GeneratedMessage.Builder builder = (GeneratedMessage.Builder) newBuilder.invoke( clazz );
+
+ @SuppressWarnings( "unchecked" ) // cast should be safe since it is driven by the type
+ T deserialized = (T) builder.mergeFrom( source ).build();
+
+ return deserialized;
+ }
+ catch ( Throwable t )
+ {
+ throw new IOException( t );
+ }
+ }
+
+ private static <T> Class<T> checkProtobufMessage( Class<T> clazz )
+ {
+ if ( !Message.class.isAssignableFrom( clazz ) )
+ {
+ throw new IllegalArgumentException( format( "Class %s cannot be serialized via Google Protobuf, it is not a %s",
+ clazz.getName(),
+ Message.class.getName() ) );
+ }
+ return clazz;
+ }
+
+}
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/ProtobufSerializer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java?rev=1244900&view=auto
==============================================================================
--- incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java (added)
+++ incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java Thu Feb 16 09:27:45 2012
@@ -0,0 +1,23 @@
+/**
+ * Google Protobuf Serializer adapter.
+ */
+package org.apache.directmemory.serialization.protobuf;
+
+/*
+ * 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.
+ */
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/directmemory/trunk/serializers/protobuf/src/main/java/org/apache/directmemory/serialization/protobuf/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain