activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r691644 - in /activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf: BaseMessage.java Message.java compiler/JavaGenerator.java
Date Wed, 03 Sep 2008 16:01:31 GMT
Author: chirino
Date: Wed Sep  3 09:01:30 2008
New Revision: 691644

URL: http://svn.apache.org/viewvc?rev=691644&view=rev
Log:
Made the Message class an interfae and added a few more message options that the java generator
understands.

Added:
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
Modified:
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
    activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java

Added: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java?rev=691644&view=auto
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
(added)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
Wed Sep  3 09:01:30 2008
@@ -0,0 +1,185 @@
+/**
+ * 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.activemq.protobuf;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.CodedOutputStream;
+import com.google.protobuf.ExtensionRegistry;
+import com.google.protobuf.InvalidProtocolBufferException;
+
+import static org.apache.activemq.protobuf.WireInfo.*;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+abstract public class BaseMessage<T> implements Message<T> {
+
+    protected int memoizedSerializedSize = -1;
+
+    static protected <T> void addAll(Iterable<T> values, Collection<? super
T> list) {
+        if (values instanceof Collection) {
+            @SuppressWarnings("unsafe")
+            Collection<T> collection = (Collection<T>)values;
+            list.addAll(collection);
+        } else {
+            for (T value : values) {
+                list.add(value);
+            }
+        }
+    }
+    
+    abstract public T clone() throws CloneNotSupportedException;
+
+    static protected void writeGroup(CodedOutputStream output, int tag, BaseMessage message)
throws IOException {
+        output.writeTag(tag, WIRETYPE_START_GROUP);
+        message.writeTo(output);
+        output.writeTag(tag, WIRETYPE_END_GROUP);
+    }
+
+    static protected void writeMessage(CodedOutputStream output, int tag, BaseMessage message)
throws IOException {
+        output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED);
+        output.writeRawVarint32(message.serializedSize());
+        message.writeTo(output);
+    }
+
+    static protected <T extends BaseMessage> T readGroup(CodedInputStream input, ExtensionRegistry
extensionRegistry, int tag, T group) throws IOException {
+        group.mergeFrom(input, extensionRegistry);
+        input.checkLastTagWas(makeTag(tag, WIRETYPE_END_GROUP));
+        return group;
+    }
+
+    static protected <T extends BaseMessage> T readMessage(CodedInputStream input,
ExtensionRegistry extensionRegistry, T message) throws IOException {
+        int length = input.readRawVarint32();
+        int oldLimit = input.pushLimit(length);
+        message.mergeFrom(input, extensionRegistry);
+        input.checkLastTagWas(0);
+        input.popLimit(oldLimit);
+        return message;
+    }
+
+    static protected int computeGroupSize(int tag, BaseMessage message) {
+        return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSize();
+    }
+
+    static protected int computeMessageSize(int tag, BaseMessage message) {
+        int t = message.serializedSize();
+        return CodedOutputStream.computeTagSize(tag) + CodedOutputStream.computeRawVarint32Size(t)
+ t;
+    }
+
+    public T mergeFrom(CodedInputStream input) throws IOException {
+        return mergeFrom(input, ExtensionRegistry.getEmptyRegistry());
+    }
+
+    public byte[] toByteArray() {
+        try {
+            byte[] result = new byte[serializedSize()];
+            CodedOutputStream output = CodedOutputStream.newInstance(result);
+            writeTo(output);
+            output.checkNoSpaceLeft();
+            return result;
+        } catch (IOException e) {
+            throw new RuntimeException("Serializing to a byte array threw an IOException
" + "(should never happen).", e);
+        }
+    }
+
+    protected List<String> prefix(List<String> missingFields, String prefix)
{
+        ArrayList<String> rc = new ArrayList<String>(missingFields.size());
+        for (String v : missingFields) {
+            rc.add(prefix+v);
+        }
+        return rc;
+    }
+
+    public void writeTo(OutputStream output) throws IOException {
+        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
+        writeTo(codedOutput);
+        codedOutput.flush();
+    }
+
+    public T mergeFrom(ByteString data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = data.newCodedInput();
+            mergeFrom(input);
+            input.checkLastTagWas(0);
+            return (T)this;
+        } catch (InvalidProtocolBufferException e) {
+            throw e;
+        } catch (IOException e) {
+            throw new RuntimeException("Reading from a ByteString threw an IOException (should
" + "never happen).", e);
+        }
+    }
+
+    public T mergeFrom(ByteString data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException
{
+        try {
+            CodedInputStream input = data.newCodedInput();
+            mergeFrom(input, extensionRegistry);
+            input.checkLastTagWas(0);
+            return (T)this;
+        } catch (InvalidProtocolBufferException e) {
+            throw e;
+        } catch (IOException e) {
+            throw new RuntimeException("Reading from a ByteString threw an IOException (should
" + "never happen).", e);
+        }
+    }
+
+    public T mergeFrom(byte[] data) throws InvalidProtocolBufferException {
+        try {
+            CodedInputStream input = CodedInputStream.newInstance(data);
+            mergeFrom(input);
+            input.checkLastTagWas(0);
+            return (T)this;
+        } catch (InvalidProtocolBufferException e) {
+            throw e;
+        } catch (IOException e) {
+            throw new RuntimeException("Reading from a byte array threw an IOException (should
" + "never happen).", e);
+        }
+    }
+
+    public T mergeFrom(byte[] data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException
{
+        try {
+            CodedInputStream input = CodedInputStream.newInstance(data);
+            mergeFrom(input, extensionRegistry);
+            input.checkLastTagWas(0);
+            return (T)this;
+        } catch (InvalidProtocolBufferException e) {
+            throw e;
+        } catch (IOException e) {
+            throw new RuntimeException("Reading from a byte array threw an IOException (should
" + "never happen).", e);
+        }
+    }
+
+    public T mergeFrom(InputStream input) throws IOException {
+        CodedInputStream codedInput = CodedInputStream.newInstance(input);
+        mergeFrom(codedInput);
+        codedInput.checkLastTagWas(0);
+        return (T)this;
+    }
+
+    public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException
{
+        CodedInputStream codedInput = CodedInputStream.newInstance(input);
+        mergeFrom(codedInput, extensionRegistry);
+        codedInput.checkLastTagWas(0);
+        return (T)this;
+    }
+
+}

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java?rev=691644&r1=691643&r2=691644&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
(original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/Message.java
Wed Sep  3 09:01:30 2008
@@ -32,166 +32,38 @@
 import java.util.Iterator;
 import java.util.List;
 
-abstract public class Message<T> {
+public interface Message<T> {
 
-    protected int memoizedSerializedSize = -1;
+    public T mergeFrom(T other);
 
-    static protected <T> void addAll(Iterable<T> values, Collection<? super
T> list) {
-        if (values instanceof Collection) {
-            @SuppressWarnings("unsafe")
-            Collection<T> collection = (Collection<T>)values;
-            list.addAll(collection);
-        } else {
-            for (T value : values) {
-                list.add(value);
-            }
-        }
-    }
-
-    static protected void writeGroup(CodedOutputStream output, int tag, Message message)
throws IOException {
-        output.writeTag(tag, WIRETYPE_START_GROUP);
-        message.writeTo(output);
-        output.writeTag(tag, WIRETYPE_END_GROUP);
-    }
-
-    static protected void writeMessage(CodedOutputStream output, int tag, Message message)
throws IOException {
-        output.writeTag(tag, WIRETYPE_LENGTH_DELIMITED);
-        output.writeRawVarint32(message.serializedSize());
-        message.writeTo(output);
-    }
-
-    static protected <T extends Message> T readGroup(CodedInputStream input, ExtensionRegistry
extensionRegistry, int tag, T group) throws IOException {
-        group.mergeFrom(input, extensionRegistry);
-        input.checkLastTagWas(makeTag(tag, WIRETYPE_END_GROUP));
-        return group;
-    }
-
-    static protected <T extends Message> T readMessage(CodedInputStream input, ExtensionRegistry
extensionRegistry, T message) throws IOException {
-        int length = input.readRawVarint32();
-        int oldLimit = input.pushLimit(length);
-        message.mergeFrom(input, extensionRegistry);
-        input.checkLastTagWas(0);
-        input.popLimit(oldLimit);
-        return message;
-    }
-
-    static protected int computeGroupSize(int tag, Message message) {
-        return CodedOutputStream.computeTagSize(tag) * 2 + message.serializedSize();
-    }
-
-    static protected int computeMessageSize(int tag, Message message) {
-        int t = message.serializedSize();
-        return CodedOutputStream.computeTagSize(tag) + CodedOutputStream.computeRawVarint32Size(t)
+ t;
-    }
-
-    abstract public T mergeFrom(T other);
-
-    public T mergeFrom(CodedInputStream input) throws IOException {
-        return mergeFrom(input, ExtensionRegistry.getEmptyRegistry());
-    }
-
-    abstract public T mergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry)
throws IOException;
-
-    abstract public void writeTo(CodedOutputStream output) throws java.io.IOException;
-
-    abstract public T clone();
-
-    abstract public int serializedSize();
-
-    abstract public void clear();
-
-    abstract public T assertInitialized() throws com.google.protobuf.UninitializedMessageException;
-    
-    public byte[] toByteArray() {
-        try {
-            byte[] result = new byte[serializedSize()];
-            CodedOutputStream output = CodedOutputStream.newInstance(result);
-            writeTo(output);
-            output.checkNoSpaceLeft();
-            return result;
-        } catch (IOException e) {
-            throw new RuntimeException("Serializing to a byte array threw an IOException
" + "(should never happen).", e);
-        }
-    }
-
-    protected List<String> prefix(List<String> missingFields, String prefix)
{
-        ArrayList<String> rc = new ArrayList<String>(missingFields.size());
-        for (String v : missingFields) {
-            rc.add(prefix+v);
-        }
-        return rc;
-    }
-
-    public void writeTo(OutputStream output) throws IOException {
-        CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
-        writeTo(codedOutput);
-        codedOutput.flush();
-    }
-
-    public T mergeFrom(ByteString data) throws InvalidProtocolBufferException {
-        try {
-            CodedInputStream input = data.newCodedInput();
-            mergeFrom(input);
-            input.checkLastTagWas(0);
-            return (T)this;
-        } catch (InvalidProtocolBufferException e) {
-            throw e;
-        } catch (IOException e) {
-            throw new RuntimeException("Reading from a ByteString threw an IOException (should
" + "never happen).", e);
-        }
-    }
-
-    public T mergeFrom(ByteString data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException
{
-        try {
-            CodedInputStream input = data.newCodedInput();
-            mergeFrom(input, extensionRegistry);
-            input.checkLastTagWas(0);
-            return (T)this;
-        } catch (InvalidProtocolBufferException e) {
-            throw e;
-        } catch (IOException e) {
-            throw new RuntimeException("Reading from a ByteString threw an IOException (should
" + "never happen).", e);
-        }
-    }
-
-    public T mergeFrom(byte[] data) throws InvalidProtocolBufferException {
-        try {
-            CodedInputStream input = CodedInputStream.newInstance(data);
-            mergeFrom(input);
-            input.checkLastTagWas(0);
-            return (T)this;
-        } catch (InvalidProtocolBufferException e) {
-            throw e;
-        } catch (IOException e) {
-            throw new RuntimeException("Reading from a byte array threw an IOException (should
" + "never happen).", e);
-        }
-    }
-
-    public T mergeFrom(byte[] data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException
{
-        try {
-            CodedInputStream input = CodedInputStream.newInstance(data);
-            mergeFrom(input, extensionRegistry);
-            input.checkLastTagWas(0);
-            return (T)this;
-        } catch (InvalidProtocolBufferException e) {
-            throw e;
-        } catch (IOException e) {
-            throw new RuntimeException("Reading from a byte array threw an IOException (should
" + "never happen).", e);
-        }
-    }
-
-    public T mergeFrom(InputStream input) throws IOException {
-        CodedInputStream codedInput = CodedInputStream.newInstance(input);
-        mergeFrom(codedInput);
-        codedInput.checkLastTagWas(0);
-        return (T)this;
-    }
-
-    public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException
{
-        CodedInputStream codedInput = CodedInputStream.newInstance(input);
-        mergeFrom(codedInput, extensionRegistry);
-        codedInput.checkLastTagWas(0);
-        return (T)this;
-    }
+    public T mergeFrom(CodedInputStream input) throws IOException;
+
+    public T mergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) throws
IOException;
+
+    public void writeTo(CodedOutputStream output) throws java.io.IOException;
+
+    public T clone() throws CloneNotSupportedException;
+
+    public int serializedSize();
+
+    public void clear();
+
+    public T assertInitialized() throws com.google.protobuf.UninitializedMessageException;
+
+    public byte[] toByteArray();
+
+    public void writeTo(OutputStream output) throws IOException;
+
+    public T mergeFrom(ByteString data) throws InvalidProtocolBufferException;
+
+    public T mergeFrom(ByteString data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException;
+
+    public T mergeFrom(byte[] data) throws InvalidProtocolBufferException;
+
+    public T mergeFrom(byte[] data, ExtensionRegistry extensionRegistry) throws InvalidProtocolBufferException;
+
+    public T mergeFrom(InputStream input) throws IOException;
+
+    public T mergeFrom(InputStream input, ExtensionRegistry extensionRegistry) throws IOException;
 
 }

Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java?rev=691644&r1=691643&r2=691644&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
(original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
Wed Sep  3 09:01:30 2008
@@ -34,6 +34,7 @@
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.StringTokenizer;
 
 import org.apache.activemq.protobuf.compiler.parser.ParseException;
 import org.apache.activemq.protobuf.compiler.parser.ProtoParser;
@@ -258,8 +259,14 @@
             staticOption="";
         }
         
+        String javaImplements = getOption(m, "java_implments", null);
         
-        p("public "+staticOption+"final class " + className + " extends org.apache.activemq.protobuf.Message<"
+ className + "> {");
+        String implementsExpression = "";
+        if( javaImplements!=null ) {
+            implementsExpression = "implements "+javaImplements+" ";
+        }
+        
+        p("public "+staticOption+"final class " + className + " extends org.apache.activemq.protobuf.BaseMessage<"
+ className + "> "+implementsExpression+"{");
         p();
 
         indent();
@@ -305,11 +312,98 @@
         generateMethodParseFrom(m, className);
 
         generateMethodToString(m);
+        
+        generateMethodVisitor(m);
                 
+        generateMethodType(m, className);
+        
         unindent();
         p("}");
         p();
     }
+
+    /**
+     * If the java_visitor message option is set, then this method generates a visitor method.
 The option 
+     * speifiies the class name of the visitor and optionally the return value and exceptions
thrown by the visitor.
+     * 
+     * Examples:
+     * 
+     *   option java_visitor = "org.apache.kahadb.store.Visitor";
+     *   generates:
+     *     public void visit(org.apache.kahadb.store.Visitor visitor) {
+     *       visitor.visit(this);
+     *     }
+     *   
+     *   option java_visitor = "org.apache.kahadb.store.Visitor:int:java.io.IOException";
+     *   generates:
+     *     public int visit(org.apache.kahadb.store.Visitor visitor) throws java.io.IOException
{
+     *       return visitor.visit(this);
+     *     }
+     * 
+     * @param m
+     */
+    private void generateMethodVisitor(MessageDescriptor m) {
+        String javaVisitor = getOption(m, "java_visitor", null);        
+        if( javaVisitor!=null ) {
+            String returns = "void";
+            String throwsException = null;
+            
+            StringTokenizer st = new StringTokenizer(javaVisitor, ":");
+            String vistorClass = st.nextToken();
+            if( st.hasMoreTokens() ) {
+                returns = st.nextToken();
+            }
+            if( st.hasMoreTokens() ) {
+                throwsException = st.nextToken();
+            }
+            
+            String throwsClause = "";
+            if( throwsException!=null ) {
+                throwsClause = "throws "+throwsException+" ";
+            }
+            
+            p("public "+returns+" visit("+vistorClass+" visitor) "+throwsClause+ "{");
+            indent();
+            if( "void".equals(returns) ) {
+                p("visitor.visit(this);");
+            } else {
+                p("return visitor.visit(this);");
+            }
+            unindent();
+            p("}");
+            p();
+        }
+    }
+    
+    private void generateMethodType(MessageDescriptor m, String className) {
+        String typeEnum = getOption(m, "java_type_method", null);        
+        if( typeEnum!=null ) {
+            
+            TypeDescriptor typeDescriptor = m.getType(typeEnum);
+            if( typeDescriptor == null ) {
+                typeDescriptor = m.getProtoDescriptor().getType(typeEnum);
+            }
+            if( typeDescriptor == null || !typeDescriptor.isEnum() ) {
+                errors.add("The java_type_method option on the "+m.getName()+" message does
not point to valid enum type");
+                return;
+            }
+            
+            EnumDescriptor enumDescriptor = (EnumDescriptor)typeDescriptor;
+            if( enumDescriptor.getFields().get(className) == null ) {
+                errors.add("The java_type_method option on the "+m.getName()+" message does
not points to the "+typeEnum+" enum but it does not have an entry for "+className);
+            }
+            
+            String type = javaType(typeDescriptor);
+            
+            p("public "+type+" type() {");
+            indent();
+                p("return "+type+"."+className+";");
+            unindent();
+            p("}");
+            p();
+        }
+    }
+    
     
     private void generateMethodParseFrom(MessageDescriptor m, String className) {
         p("public static "+className+" parseFrom(com.google.protobuf.ByteString data) throws
com.google.protobuf.InvalidProtocolBufferException {");
@@ -1325,7 +1419,15 @@
         }
         return optionDescriptor.getValue();
     }
-
+    
+    private String getOption(MessageDescriptor md, String optionName, String defaultValue)
{
+        OptionDescriptor optionDescriptor = md.getOptions().get(optionName);
+        if (optionDescriptor == null) {
+            return defaultValue;
+        }
+        return optionDescriptor.getValue();
+    }
+    
     static private String removeFileExtension(String name) {
         return name.replaceAll("\\..*", "");
     }



Mime
View raw message