You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2008/09/05 19:39:14 UTC
svn commit: r692498 - in /activemq/sandbox/activemq-protobuf: ./
activemq-protobuf/
activemq-protobuf/src/main/java/org/apache/activemq/protobuf/
activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/
activemq-protobuf/src/main/javacc/
Author: chirino
Date: Fri Sep 5 10:39:14 2008
New Revision: 692498
URL: http://svn.apache.org/viewvc?rev=692498&view=rev
Log:
Updated build to the latest released protobuf jar. Also made some enhancements to Message so that you can write it to
a stream as a partial or as message. Doing a partial will allow you to append more messages to the stream and when demarshalled
they will be merged as one message.
Modified:
activemq/sandbox/activemq-protobuf/activemq-protobuf/pom.xml
activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java
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/EnumDescriptor.java
activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/JavaGenerator.java
activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/javacc/proto-parser.jj
activemq/sandbox/activemq-protobuf/pom.xml
Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/pom.xml
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/pom.xml?rev=692498&r1=692497&r2=692498&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/pom.xml (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/pom.xml Fri Sep 5 10:39:14 2008
@@ -39,7 +39,7 @@
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
- <version>2.0.1rc1</version>
+ <version>2.0.1</version>
</dependency>
<dependency>
@@ -62,6 +62,14 @@
<scope>test</scope>
</dependency>
</dependencies>
+
+ <repositories>
+ <repository>
+ <id>google</id>
+ <name>Google Maven Repo</name>
+ <url>http://google-maven-repository.googlecode.com/svn/repository</url>
+ </repository>
+ </repositories>
<build>
<plugins>
Modified: 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=692498&r1=692497&r2=692498&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/BaseMessage.java Fri Sep 5 10:39:14 2008
@@ -52,14 +52,14 @@
static protected void writeGroup(CodedOutputStream output, int tag, BaseMessage message) throws IOException {
output.writeTag(tag, WIRETYPE_START_GROUP);
- message.writeTo(output);
+ message.writePartialTo(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);
+ message.writePartialTo(output);
}
static protected <T extends BaseMessage> T readGroup(CodedInputStream input, ExtensionRegistry extensionRegistry, int tag, T group) throws IOException {
@@ -115,6 +115,11 @@
writeTo(codedOutput);
codedOutput.flush();
}
+
+ public void writeTo(CodedOutputStream output) throws java.io.IOException {
+ writePartialTo(output);
+ output.writeTag(0, WIRETYPE_END_GROUP);
+ }
public T mergeFrom(ByteString data) throws InvalidProtocolBufferException {
try {
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=692498&r1=692497&r2=692498&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 Fri Sep 5 10:39:14 2008
@@ -52,6 +52,8 @@
public byte[] toByteArray();
+ public void writePartialTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException;
+
public void writeTo(OutputStream output) throws IOException;
public T mergeFrom(ByteString data) throws InvalidProtocolBufferException;
Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumDescriptor.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumDescriptor.java?rev=692498&r1=692497&r2=692498&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumDescriptor.java (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/java/org/apache/activemq/protobuf/compiler/EnumDescriptor.java Fri Sep 5 10:39:14 2008
@@ -26,6 +26,7 @@
private Map<String,EnumFieldDescriptor> fields= new LinkedHashMap<String, EnumFieldDescriptor>();
private final ProtoDescriptor protoDescriptor;
private final MessageDescriptor parent;
+ private Map<String, OptionDescriptor> options = new LinkedHashMap<String, OptionDescriptor>();
public EnumDescriptor(ProtoDescriptor protoDescriptor, MessageDescriptor parent) {
this.protoDescriptor = protoDescriptor;
@@ -72,5 +73,13 @@
return true;
}
+ public Map<String, OptionDescriptor> getOptions() {
+ return options;
+ }
+
+ public void setOptions(Map<String, OptionDescriptor> options) {
+ this.options = options;
+ }
+
}
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=692498&r1=692497&r2=692498&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 Fri Sep 5 10:39:14 2008
@@ -34,6 +34,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import java.util.StringTokenizer;
import org.apache.activemq.protobuf.compiler.parser.ParseException;
@@ -121,7 +122,7 @@
// Load the options..
javaPackage = javaPackage(proto);
outerClassName = javaClassName(proto);
- optimizeFor = getOption(proto, "optimize_for", "SPEED");
+ optimizeFor = getOption(proto.getOptions(), "optimize_for", "SPEED");
multipleFiles = isMultipleFilesEnabled(proto);
if( multipleFiles ) {
@@ -259,7 +260,7 @@
staticOption="";
}
- String javaImplements = getOption(m, "java_implments", null);
+ String javaImplements = getOption(m.getOptions(), "java_implments", null);
String implementsExpression = "";
if( javaImplements!=null ) {
@@ -343,7 +344,7 @@
* @param m
*/
private void generateMethodVisitor(MessageDescriptor m) {
- String javaVisitor = getOption(m, "java_visitor", null);
+ String javaVisitor = getOption(m.getOptions(), "java_visitor", null);
if( javaVisitor!=null ) {
String returns = "void";
String throwsException = null;
@@ -376,7 +377,7 @@
}
private void generateMethodType(MessageDescriptor m, String className) {
- String typeEnum = getOption(m, "java_type_method", null);
+ String typeEnum = getOption(m.getOptions(), "java_type_method", null);
if( typeEnum!=null ) {
TypeDescriptor typeDescriptor = m.getType(typeEnum);
@@ -388,23 +389,24 @@
return;
}
+
+ String constant = constantCase(className);
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);
+ if( enumDescriptor.getFields().get(constant) == 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 "+constant);
}
String type = javaType(typeDescriptor);
p("public "+type+" type() {");
indent();
- p("return "+type+"."+className+";");
+ p("return "+type+"."+constant+";");
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 {");
indent();
@@ -532,7 +534,7 @@
* @param m
*/
private void generateMethodWriteTo(MessageDescriptor m) {
- p("public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {");
+ p("public void writePartialTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {");
indent();
for (FieldDescriptor field : m.getFields().values()) {
String uname = uCamel(field.getName());
@@ -1284,13 +1286,35 @@
p(" return null;");
p(" }");
p("}");
+ p();
+ String createMessage = getOption(ed.getOptions(), "java_create_message", null);
+ if( "true".equals(createMessage) ) {
+ p("public final org.apache.activemq.protobuf.Message createMessage() {");
+ indent();
+ p("switch (this) {");
+ indent();
+ for (EnumFieldDescriptor field : ed.getFields().values()) {
+ p("case "+field.getName()+":");
+ String type = constantToUCamelCase(field.getName());
+ p(" return new "+type+"();");
+ }
+ p("default:");
+ p(" return null;");
+ unindent();
+ p("}");
+ unindent();
+ p("}");
+ p();
+ }
+
unindent();
p("}");
p();
}
-
+
+
private String javaCollectionType(FieldDescriptor field) {
if( field.isInteger32Type() ) {
@@ -1369,11 +1393,11 @@
}
private String javaClassName(ProtoDescriptor proto) {
- return getOption(proto, "java_outer_classname", uCamel(removeFileExtension(proto.getName())));
+ return getOption(proto.getOptions(), "java_outer_classname", uCamel(removeFileExtension(proto.getName())));
}
private boolean isMultipleFilesEnabled(ProtoDescriptor proto) {
- return "true".equals(getOption(proto, "java_multiple_files", "false"));
+ return "true".equals(getOption(proto.getOptions(), "java_multiple_files", "false"));
}
@@ -1383,7 +1407,7 @@
name = name.replace('-', '.');
name = name.replace('/', '.');
}
- return getOption(proto, "java_package", name);
+ return getOption(proto.getOptions(), "java_package", name);
}
@@ -1412,22 +1436,14 @@
w.println();
}
- private String getOption(ProtoDescriptor proto, String optionName, String defaultValue) {
- OptionDescriptor optionDescriptor = proto.getOptions().get(optionName);
- if (optionDescriptor == null) {
- return defaultValue;
- }
- return optionDescriptor.getValue();
- }
-
- private String getOption(MessageDescriptor md, String optionName, String defaultValue) {
- OptionDescriptor optionDescriptor = md.getOptions().get(optionName);
+ private String getOption(Map<String, OptionDescriptor> options, String optionName, String defaultValue) {
+ OptionDescriptor optionDescriptor = options.get(optionName);
if (optionDescriptor == null) {
return defaultValue;
}
return optionDescriptor.getValue();
}
-
+
static private String removeFileExtension(String name) {
return name.replaceAll("\\..*", "");
}
@@ -1456,6 +1472,42 @@
String uCamel = uCamel(name);
return uCamel.substring(0,1).toLowerCase()+uCamel.substring(1);
}
+
+
+ private String constantToUCamelCase(String name) {
+ boolean upNext=true;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < name.length(); i++) {
+ char c = name.charAt(i);
+ if( Character.isJavaIdentifierPart(c) && Character.isLetterOrDigit(c)) {
+ if( upNext ) {
+ c = Character.toUpperCase(c);
+ upNext=false;
+ } else {
+ c = Character.toLowerCase(c);
+ }
+ sb.append(c);
+ } else {
+ upNext=true;
+ }
+ }
+ return sb.toString();
+ }
+
+
+ private String constantCase(String name) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < name.length(); i++) {
+ char c = name.charAt(i);
+ if( i!=0 ) {
+ if( Character.isUpperCase(c) ) {
+ sb.append("_");
+ }
+ }
+ sb.append(Character.toUpperCase(c));
+ }
+ return sb.toString();
+ }
public File getOut() {
return out;
Modified: activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/javacc/proto-parser.jj
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/javacc/proto-parser.jj?rev=692498&r1=692497&r2=692498&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/javacc/proto-parser.jj (original)
+++ activemq/sandbox/activemq-protobuf/activemq-protobuf/src/main/javacc/proto-parser.jj Fri Sep 5 10:39:14 2008
@@ -70,7 +70,7 @@
<COMMENT> SPECIAL_TOKEN :
{
- <("\n" | "\r" | "\r\n" )> : DEFAULT
+ <("\n" | "\r" | "\r\n" | "|")> : DEFAULT
}
<COMMENT> MORE :
@@ -431,20 +431,26 @@
Token name;
LinkedHashMap<String,EnumFieldDescriptor> fields = new LinkedHashMap<String,EnumFieldDescriptor>();
EnumDescriptor rc = new EnumDescriptor(proto, parent);
+ LinkedHashMap<String,OptionDescriptor> opts = new LinkedHashMap<String,OptionDescriptor>();
+
EnumFieldDescriptor enumD;
+ OptionDescriptor optionD;
}
{
<ENUM> name = <ID> <LBRACE>
(
+ LOOKAHEAD(2)
+ <OPTION> optionD = OptionDescriptor() <SEMICOLON>
+ { opts.put(optionD.getName(),optionD); }
+ |
enumD = EnumFieldDescriptor(rc) <SEMICOLON>
- {
- fields.put(enumD.getName(),enumD);
- }
+ { fields.put(enumD.getName(),enumD); }
)*
<RBRACE>
{
rc.setName(name.image);
rc.setFields(fields);
+ rc.setOptions(opts);
return rc;
}
}
Modified: activemq/sandbox/activemq-protobuf/pom.xml
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-protobuf/pom.xml?rev=692498&r1=692497&r2=692498&view=diff
==============================================================================
--- activemq/sandbox/activemq-protobuf/pom.xml (original)
+++ activemq/sandbox/activemq-protobuf/pom.xml Fri Sep 5 10:39:14 2008
@@ -19,6 +19,12 @@
<modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-parent</artifactId>
+ <version>5.1.0</version>
+ </parent>
+
<groupId>org.apache.activemq.protobuf</groupId>
<artifactId>activemq-protobuf-pom</artifactId>
<version>1.0-SNAPSHOT</version>