flatenned the hierarchy of serialization packages

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/1e6bb8b0
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/1e6bb8b0
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/1e6bb8b0

Branch: refs/heads/trunk
Commit: 1e6bb8b0272992a5f0abfc81cef61380ad2a2db4
Parents: d36370c
Author: jvermillard <[email protected]>
Authored: Thu May 9 22:27:52 2013 +0200
Committer: jvermillard <[email protected]>
Committed: Thu May 9 22:27:52 2013 +0200

----------------------------------------------------------------------
 NOTE-serialization.txt                             |   36 +
 pom.xml                                            |    3 +-
 protobuf/pom.xml                                   |   72 +
 protobuf/prepare.sh                                |   15 +
 .../mina/codec/delimited/ProtobufDecoder.java      |   46 +
 .../codec/delimited/ProtobufDynamicDecoder.java    |   40 +
 .../mina/codec/delimited/ProtobufEncoder.java      |   42 +
 .../ProtobufDynamicMessageDecoder.java             |   76 +
 .../serialization/ProtobufMessageDecoder.java      |   67 +
 .../serialization/ProtobufMessageEncoder.java      |   47 +
 .../apache/mina/codec/delimited/ProtobufTest.java  |   63 +
 .../delimited/serialization/ProtobufTest.java      |   70 +
 .../mina/generated/protoc/AddressBookProtos.java   | 2563 +++++++++++++++
 .../org/apache/mina/generated/protoc/Calc.java     | 1160 +++++++
 protobuf/src/test/protobuf/addressbook.proto       |   30 +
 protobuf/src/test/protobuf/calc.proto              |   25 +
 serialization/NOTES.txt                            |   36 -
 serialization/pom.xml                              |   46 -
 serialization/protobuf/pom.xml                     |   72 -
 serialization/protobuf/prepare.sh                  |   15 -
 .../mina/codec/delimited/ProtobufDecoder.java      |   46 -
 .../codec/delimited/ProtobufDynamicDecoder.java    |   40 -
 .../mina/codec/delimited/ProtobufEncoder.java      |   42 -
 .../ProtobufDynamicMessageDecoder.java             |   76 -
 .../serialization/ProtobufMessageDecoder.java      |   67 -
 .../serialization/ProtobufMessageEncoder.java      |   47 -
 .../apache/mina/codec/delimited/ProtobufTest.java  |   63 -
 .../delimited/serialization/ProtobufTest.java      |   70 -
 .../mina/generated/protoc/AddressBookProtos.java   | 2563 ---------------
 .../org/apache/mina/generated/protoc/Calc.java     | 1160 -------
 .../protobuf/src/test/protobuf/addressbook.proto   |   30 -
 .../protobuf/src/test/protobuf/calc.proto          |   25 -
 serialization/thrift/pom.xml                       |   70 -
 serialization/thrift/prepare.sh                    |   14 -
 .../apache/mina/codec/delimited/ThriftDecoder.java |   37 -
 .../mina/codec/delimited/ThriftDynamicDecoder.java |   40 -
 .../apache/mina/codec/delimited/ThriftEncoder.java |   40 -
 .../serialization/ThriftDynamicMessageDecoder.java |   69 -
 .../serialization/ThriftMessageDecoder.java        |   57 -
 .../serialization/ThriftMessageEncoder.java        |   67 -
 .../apache/mina/codec/delimited/ThriftTest.java    |   71 -
 .../codec/delimited/serialization/ThriftTest.java  |   69 -
 .../apache/mina/generated/thrift/UserProfile.java  |  602 ----
 .../apache/mina/generated/thrift/UserStorage.java  | 1596 ---------
 serialization/thrift/src/test/thrift/user.thrift   |   29 -
 thrift/pom.xml                                     |   70 +
 thrift/prepare.sh                                  |   14 +
 .../apache/mina/codec/delimited/ThriftDecoder.java |   37 +
 .../mina/codec/delimited/ThriftDynamicDecoder.java |   40 +
 .../apache/mina/codec/delimited/ThriftEncoder.java |   40 +
 .../serialization/ThriftDynamicMessageDecoder.java |   69 +
 .../serialization/ThriftMessageDecoder.java        |   57 +
 .../serialization/ThriftMessageEncoder.java        |   67 +
 .../apache/mina/codec/delimited/ThriftTest.java    |   71 +
 .../codec/delimited/serialization/ThriftTest.java  |   69 +
 .../apache/mina/generated/thrift/UserProfile.java  |  602 ++++
 .../apache/mina/generated/thrift/UserStorage.java  | 1596 +++++++++
 thrift/src/test/thrift/user.thrift                 |   29 +
 58 files changed, 7115 insertions(+), 7160 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/NOTE-serialization.txt
----------------------------------------------------------------------
diff --git a/NOTE-serialization.txt b/NOTE-serialization.txt
new file mode 100644
index 0000000..22be0dd
--- /dev/null
+++ b/NOTE-serialization.txt
@@ -0,0 +1,36 @@
+ *  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.
+ 
+ 
+ 
+ Notes on Apache Thrift and Google Protocol Buffer in MINA 3.0
+ =============================================================
+ 
+ Both Thrift and Protobuf need a specific executable files to generate the 
+ required Java code.
+ 
+ Both modules doesn't have any dependency against code generated by these
+ executable files, but the dedicated unit tests have dependencies against
+ them.
+ 
+ Not having the appropriate version of Apache Thrift or Google Protocol
+ Buffer should not prevent developers/users to build MINA 3.0. For this
+ particular reason, the generated files are also stored on the
+ repository.
+ 
+ The generation scripts are stored in both directories under the name 
+ "prepare.sh". 
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 4a08653..77461a8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,7 +109,8 @@
       <module>http</module>
       <module>examples</module>
       <module>coap</module>
-      <module>serialization</module>
+      <module>thrift</module>
+      <module>protobuf</module>
   </modules>
 
   <dependencyManagement>

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/pom.xml
----------------------------------------------------------------------
diff --git a/protobuf/pom.xml b/protobuf/pom.xml
new file mode 100644
index 0000000..2f400ac
--- /dev/null
+++ b/protobuf/pom.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.mina</groupId>
+        <artifactId>mina-parent</artifactId>
+        <version>3.0.0-M1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>mina-protobuf</artifactId>
+    <name>Apache MINA Serialization::Protobuf ${project.version}</name>
+    <packaging>bundle</packaging>
+
+    <properties>
+        <symbolicName>${project.groupId}.protobuf</symbolicName>
+        <exportedPackage>${project.groupId}</exportedPackage>
+        <protobuf.version>2.5.0</protobuf.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.google.protobuf</groupId>
+            <artifactId>protobuf-java</artifactId>
+            <version>${protobuf.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+
+        <dependency>
+            <groupId>org.apache.logging.log4j.adapters</groupId>
+            <artifactId>slf4j-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>mina-codec</artifactId>
+            <version>${project.version}</version>
+            <type>bundle</type>
+        </dependency>
+        
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>mina-codec</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+            <classifier>tests</classifier>
+        </dependency>
+        
+    </dependencies>
+</project>
+

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/prepare.sh
----------------------------------------------------------------------
diff --git a/protobuf/prepare.sh b/protobuf/prepare.sh
new file mode 100644
index 0000000..84fd1fc
--- /dev/null
+++ b/protobuf/prepare.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+if hash protoc 2>/dev/null; then
+       VERSION=`protoc --version`
+
+       if [[ $VERSION =~ libprotoc\ 2\.5\. ]];
+       then
+               protoc --java_out=src/test/java 
src/test/protobuf/addressbook.proto src/test/protobuf/calc.proto
+       else
+               echo "Need Google Protocol Buffer 2.5.x (found $VERSION)"
+       fi
+else
+       echo "No installation of Google Protocol Buffer found"
+fi
+

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDecoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDecoder.java 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDecoder.java
new file mode 100644
index 0000000..7072d6e
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDecoder.java
@@ -0,0 +1,46 @@
+/*
+ *  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.mina.codec.delimited;
+
+import org.apache.mina.codec.delimited.ints.VarInt;
+import org.apache.mina.codec.delimited.serialization.ProtobufMessageDecoder;
+
+import com.google.protobuf.ExtensionRegistryLite;
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufDecoder<M extends GeneratedMessage> extends 
SizePrefixedDecoder<M> {
+    static public <L extends GeneratedMessage> ProtobufDecoder<L> 
newInstance(Class<L> clazz,
+            ExtensionRegistryLite registry) throws SecurityException, 
NoSuchMethodException {
+        return new ProtobufDecoder<L>(clazz, registry);
+    }
+
+    static public <L extends GeneratedMessage> ProtobufDecoder<L> 
newInstance(Class<L> clazz) throws SecurityException,
+            NoSuchMethodException {
+        return newInstance(clazz, ExtensionRegistryLite.getEmptyRegistry());
+    }
+
+    public ProtobufDecoder(Class<M> clazz, ExtensionRegistryLite registry) 
throws SecurityException,
+            NoSuchMethodException {
+        super(new VarInt.Decoder(), ProtobufMessageDecoder.newInstance(clazz, 
registry));
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDynamicDecoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDynamicDecoder.java
 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDynamicDecoder.java
new file mode 100644
index 0000000..e3b2506
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufDynamicDecoder.java
@@ -0,0 +1,40 @@
+/*
+ *  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.mina.codec.delimited;
+
+import org.apache.mina.codec.delimited.ints.VarInt;
+import 
org.apache.mina.codec.delimited.serialization.ProtobufDynamicMessageDecoder;
+
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufDynamicDecoder extends
+        
SizePrefixedDecoder<ProtobufDynamicMessageDecoder.ProtobufSerializedMessage> {
+    static public <L extends GeneratedMessage> ProtobufDynamicDecoder 
newInstance() throws SecurityException,
+            NoSuchMethodException {
+        return new ProtobufDynamicDecoder();
+    }
+
+    public ProtobufDynamicDecoder() throws SecurityException, 
NoSuchMethodException {
+        super(new VarInt.Decoder(), 
ProtobufDynamicMessageDecoder.newInstance());
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufEncoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufEncoder.java 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufEncoder.java
new file mode 100644
index 0000000..8386e89
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/ProtobufEncoder.java
@@ -0,0 +1,42 @@
+/*
+ *  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.mina.codec.delimited;
+
+import org.apache.mina.codec.delimited.ints.VarInt;
+import org.apache.mina.codec.delimited.serialization.ProtobufMessageEncoder;
+
+import com.google.protobuf.GeneratedMessage;
+
+
+/**
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufEncoder<OUT extends GeneratedMessage> extends
+               SizePrefixedEncoder<OUT> {
+
+       static public <L extends GeneratedMessage> ProtobufEncoder<L> 
newInstance(
+                       Class<L> clazz) {
+               return new ProtobufEncoder<L>(clazz);
+       }
+
+       public ProtobufEncoder(Class<OUT> clazz) {
+               super(new VarInt.Encoder(), 
ProtobufMessageEncoder.newInstance(clazz));
+       }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicMessageDecoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicMessageDecoder.java
 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicMessageDecoder.java
new file mode 100644
index 0000000..b574c08
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicMessageDecoder.java
@@ -0,0 +1,76 @@
+/*
+ *  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.mina.codec.delimited.serialization;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.util.ByteBufferInputStream;
+
+import com.google.protobuf.ExtensionRegistryLite;
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * An alternative decoder for protobuf which allows the use various target
+ * classes with the same decoder.
+ * 
+ * This decoder converts incoming {@link ByteBuffer} into
+ * {@link ProtobufSerializedMessage}.
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufDynamicMessageDecoder extends
+        
ByteBufferDecoder<ProtobufDynamicMessageDecoder.ProtobufSerializedMessage> {
+
+    static public ProtobufDynamicMessageDecoder newInstance() {
+        return new ProtobufDynamicMessageDecoder();
+    }
+
+    @Override
+    public ProtobufSerializedMessage decode(ByteBuffer input) throws 
ProtocolDecoderException {
+        return new ProtobufSerializedMessage(input);
+    }
+
+    final static public class ProtobufSerializedMessage {
+        final private ByteBuffer input;
+
+        public ProtobufSerializedMessage(ByteBuffer input) {
+            this.input = input;
+        }
+
+        @SuppressWarnings("unchecked")
+        public <L extends GeneratedMessage> L get(Class<L> clazz, 
ExtensionRegistryLite registry)
+                throws SecurityException, NoSuchMethodException, 
IllegalArgumentException, IllegalAccessException,
+                InvocationTargetException {
+            Method parseMethod = clazz.getDeclaredMethod("parseFrom", 
InputStream.class, ExtensionRegistryLite.class);
+            return (L) parseMethod.invoke(null, new 
ByteBufferInputStream(input.duplicate()), registry);
+        }
+
+        public <L extends GeneratedMessage> L get(Class<L> clazz) throws 
SecurityException, NoSuchMethodException,
+                IllegalArgumentException, IllegalAccessException, 
InvocationTargetException {
+            return get(clazz, ExtensionRegistryLite.getEmptyRegistry());
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageDecoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageDecoder.java
 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageDecoder.java
new file mode 100644
index 0000000..94b5218
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageDecoder.java
@@ -0,0 +1,67 @@
+/*
+ *  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.mina.codec.delimited.serialization;
+
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.util.ByteBufferInputStream;
+
+import com.google.protobuf.ExtensionRegistryLite;
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufMessageDecoder<IN extends GeneratedMessage> extends 
ByteBufferDecoder<IN> {
+    final private Method parseMethod;
+
+    final private ExtensionRegistryLite registry;
+
+    public static <TYPE extends GeneratedMessage> ProtobufMessageDecoder<TYPE> 
newInstance(Class<TYPE> c)
+            throws SecurityException, NoSuchMethodException {
+        return newInstance(c, ExtensionRegistryLite.getEmptyRegistry());
+    }
+
+    public static <TYPE extends GeneratedMessage> ProtobufMessageDecoder<TYPE> 
newInstance(Class<TYPE> c,
+            ExtensionRegistryLite registry) throws SecurityException, 
NoSuchMethodException {
+        return new ProtobufMessageDecoder<TYPE>(c, registry);
+    }
+
+    private ProtobufMessageDecoder(Class<IN> clazz, ExtensionRegistryLite 
registry) throws SecurityException,
+            NoSuchMethodException {
+        super();
+        parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, 
ExtensionRegistryLite.class);
+        this.registry = registry;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public IN decode(ByteBuffer input) throws ProtocolDecoderException {
+        try {
+            return (IN) parseMethod.invoke(null, new 
ByteBufferInputStream(input), registry);
+        } catch (Exception e) {
+            throw new ProtocolDecoderException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageEncoder.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageEncoder.java
 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageEncoder.java
new file mode 100644
index 0000000..178d9b5
--- /dev/null
+++ 
b/protobuf/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufMessageEncoder.java
@@ -0,0 +1,47 @@
+/*
+ *  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.mina.codec.delimited.serialization;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufMessageEncoder<OUT extends GeneratedMessage> extends 
ByteBufferEncoder<OUT> {
+
+    static public <T extends GeneratedMessage> ProtobufMessageEncoder<T> 
newInstance(Class<T> clazz) {
+        return new ProtobufMessageEncoder<T>();
+    }
+
+    @Override
+    public int getEncodedSize(OUT message) {
+        return message.getSerializedSize();
+    }
+
+    @Override
+    public void writeTo(OUT message, ByteBuffer buffer) {
+        buffer.put(message.toByteArray());
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/test/java/org/apache/mina/codec/delimited/ProtobufTest.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/test/java/org/apache/mina/codec/delimited/ProtobufTest.java 
b/protobuf/src/test/java/org/apache/mina/codec/delimited/ProtobufTest.java
new file mode 100644
index 0000000..3f90370
--- /dev/null
+++ b/protobuf/src/test/java/org/apache/mina/codec/delimited/ProtobufTest.java
@@ -0,0 +1,63 @@
+/*
+ *  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.mina.codec.delimited;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.mina.generated.protoc.AddressBookProtos.Person;
+import org.apache.mina.util.ByteBufferOutputStream;
+
+/**
+ * A {@link ProtobufEncoder} and {@link ProtobufDecoder} test.
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufTest extends DelimitTest<Person> {
+  
+
+    @Override
+    public List<Person> getObjects() {
+
+        List<Person> list = new LinkedList<Person>();
+
+        list.add(Person.newBuilder().setId(1).setName("Jean 
Dupond").setEmail("[email protected]").build());
+        list.add(Person.newBuilder().setId(2).setName("Marie 
Blanc").setEmail("[email protected]").build());
+
+        return list;
+    }
+
+    @Override
+    protected ByteBuffer delimitWithOriginal() throws IOException {
+        ByteBufferOutputStream bbos = new ByteBufferOutputStream();
+        bbos.setElastic(true);
+        for (Person p : getObjects()) {
+            p.writeDelimitedTo(bbos);
+        }
+        return bbos.getByteBuffer();
+    }
+
+    @Override
+    public SizePrefixedEncoder<Person> getSerializer() {
+        return ProtobufEncoder.newInstance(Person.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/1e6bb8b0/protobuf/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
----------------------------------------------------------------------
diff --git 
a/protobuf/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
 
b/protobuf/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
new file mode 100644
index 0000000..abc96ea
--- /dev/null
+++ 
b/protobuf/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
@@ -0,0 +1,70 @@
+/*
+ *  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.mina.codec.delimited.serialization;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+import 
org.apache.mina.codec.delimited.serialization.ProtobufDynamicMessageDecoder.ProtobufSerializedMessage;
+import org.apache.mina.generated.protoc.AddressBookProtos.Person;
+import org.junit.Test;
+
+/**
+ * A {@link ProtobufMessageEncoder} and {@link ProtobufMessageDecoder} test.
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ProtobufTest extends GenericSerializerTest<Person> {
+
+    @Override
+    public List<Person> getObjects() {
+        List<Person> list = new LinkedList<Person>();
+
+        list.add(Person.newBuilder().setId(1).setName("Jean 
Dupond").setEmail("[email protected]").build());
+        list.add(Person.newBuilder().setId(2).setName("Marie 
Blanc").setEmail("[email protected]").build());
+
+        return list;
+    }
+ 
+    @Override
+    public ByteBufferDecoder<Person> getDecoder() throws Exception {
+        return ProtobufMessageDecoder.newInstance(Person.class);
+    }
+
+    @Override
+    public ByteBufferEncoder<Person> getEncoder() throws Exception {
+        return new ProtobufMessageEncoder<Person>();
+    }
+
+    @Test
+    public void testDynamic() throws Exception {
+        ByteBufferEncoder<Person> encoder = getEncoder();
+        ProtobufDynamicMessageDecoder decoder = new 
ProtobufDynamicMessageDecoder();
+
+        for (Person object : getObjects()) {
+            ProtobufSerializedMessage message = 
decoder.decode(encoder.encode(object));
+            assertEquals(object, message.get(Person.class));
+        }
+    }
+}

Reply via email to