Skip to content
Permalink
Browse files
Updated build to the latest released protobuf jar. Also made some enh…
…ancements 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.



git-svn-id: https://svn.apache.org/repos/asf/activemq/sandbox/activemq-protobuf@692498 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chirino committed Sep 5, 2008
1 parent 3797da2 commit 68f8bbd722124876fb3460a8b05a68ed8eda67d1
Showing 7 changed files with 119 additions and 31 deletions.
@@ -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>
@@ -52,14 +52,14 @@ static protected <T> void addAll(Iterable<T> values, Collection<? super T> list)

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 @@ public void writeTo(OutputStream output) throws IOException {
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 {
@@ -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;
@@ -26,6 +26,7 @@ public class EnumDescriptor implements TypeDescriptor {
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 @@ public boolean isEnum() {
return true;
}

public Map<String, OptionDescriptor> getOptions() {
return options;
}

public void setOptions(Map<String, OptionDescriptor> options) {
this.options = options;
}


}
@@ -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 @@ public void compile(File file) throws CompilerException {
// 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 @@ private void generateMessageBean(MessageDescriptor m) {
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 @@ private void generateMessageBean(MessageDescriptor m) {
* @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 generateMethodVisitor(MessageDescriptor m) {
}

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 @@ private void generateMethodType(MessageDescriptor m, String className) {
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 @@ private void generateMethodSerializedSize(MessageDescriptor m) {
* @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 @@ private void generateEnum(EnumDescriptor ed) {
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 boolean equals(String o1, String o2) {
}

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 @@ private String javaPackage(ProtoDescriptor proto) {
name = name.replace('-', '.');
name = name.replace('/', '.');
}
return getOption(proto, "java_package", name);
return getOption(proto.getOptions(), "java_package", name);
}


@@ -1412,22 +1436,14 @@ private void p() {
w.println();
}

private String getOption(ProtoDescriptor proto, String optionName, String defaultValue) {
OptionDescriptor optionDescriptor = proto.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();
}

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("\\..*", "");
}
@@ -1456,6 +1472,42 @@ static private String lCamel(String name) {
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;
@@ -70,7 +70,7 @@ SPECIAL_TOKEN :

<COMMENT> SPECIAL_TOKEN :
{
<("\n" | "\r" | "\r\n" )> : DEFAULT
<("\n" | "\r" | "\r\n" | "|")> : DEFAULT
}

<COMMENT> MORE :
@@ -431,20 +431,26 @@ EnumDescriptor EnumDescriptor(ProtoDescriptor proto, MessageDescriptor parent) :
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;
}
}
@@ -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>

0 comments on commit 68f8bbd

Please sign in to comment.