Hi Evan,

Thanks for your reply.  I am trying to do more along the lines of a).

I am trying to write a "DataFormat" for use with Apache Camel.  In
Camel there is the notion of data transformations performed by the
framework before and after they are sent over transports. So you say
for example take this object, turn it into JSON, then send it over

Below is the implementation of the Protobuf DataFormat. The steps that
take place are:

1) Use the generated protobuf builder to create an instance of object
to be transferred.
2) Give the object to Camel
3) Camel invokes the DataFormat using the descriptor that matches.  A
new DataFormat is instantiated for each type of message, so the
Descriptor is unique.
4) The DataFormat marshals the object to the OutputStream.  This is
easy as the Message interface contains a writeTo method.
5) Camel then sends the message over the transport
6) On the other side Camel receives the message then attempts to
unmarhsal it.
7) Camel invokes unmarshal on my ProtobufDataFormat.  This is the part
I'm stuck on.  I have the descriptor but I don't want to "hard code"
in the builder as then it is no longer generic.
8) The framework then casts the result and returns it to the

The result of all this means that the client should be able to use the
Camel DSL to do things such as:


public class ProtobufDataFormat implements DataFormat {

  private Descriptor descriptor;

  public ProtobufDataFormat(Descriptor descriptor) {
    this.descriptor = descriptor;

  public void marshal(Exchange exchange, Object graph, OutputStream
      throws Exception {

  public Object unmarshal(Exchange exchange, InputStream inputStream)
throws Exception {
    //TODO what happens here?
    return null;

On Oct 31, 11:37 pm, Evan Jones <ev...@mit.edu> wrote:
> On Oct 31, 2009, at 11:57 , Martin Gilday wrote:
> > I would like have a generic method which takes a Descriptor and a
> > stream which gives back an instance of message which I can later cast
> > to my desired type.  I have tried DynamicMessage from =
> > DynamicMessage.parseFrom(descriptor, inputStream); but I get an
> > exception when I attempt to cast this as my generate proto class.
> > Is there a solution to this?
> Here are a few alternatives, depending on what you want to do:
> a) You want to write Java code which takes multiple types of protocol  
> buffer messages, examines them, and does something with them.
> In this case, you need to create your specific kind of message  
> (MyFooMessage.Builder.build()), then pass it in to the "generic"  
> method as a Message. Then you can use getDescriptor() and getField()  
> to manipulate it however you want. Later downcasts will work.
> b) You want to pass around a message that contains another message of  
> some type. 
> See:http://code.google.com/apis/protocolbuffers/docs/techniques.html#union
> For my RPC implementation, I just use something like:
> message RpcRequest {
>      required string message_name = 2;
>      required bytes request = 3;
> }
> Then I have a mapping from message names to Message "prototypes", so  
> you can create a Message of an appropriate type.
> Hope this helps,
> Evan
> --
> Evan Joneshttp://evanjones.ca/
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to