Java Code Examples for io.protostuff.Message#cachedSchema()

The following examples show how to use io.protostuff.Message#cachedSchema() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema() {
	Schema<T> schema = this.schema;
	if (schema == null) {
		synchronized (this) {
			if ((schema = this.schema) == null) {
				if (Message.class.isAssignableFrom(typeClass)) {
					// use the message's schema.
					Message<T> m = (Message<T>) createMessageInstance(typeClass);
					this.schema = schema = m.cachedSchema();
				} else {
					// create new
					this.schema = schema = strategy.newSchema(typeClass);
				}
			}
		}
	}

	return schema;
}
 
Example 2
Source File: ProtostuffEncoder.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Message<T> msg, List<Object> out) throws Exception {
  Schema<T> schema = msg.cachedSchema();

  LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput();
  schema.writeTo(lcpo, (T) msg);

  List<ByteBuffer> buffers = lcpo.buffer.finish();

  long size = lcpo.buffer.size();
  if (size > Integer.MAX_VALUE) {
    throw new EncoderException("Serialized form was too large, actual size: " + size);
  }

  out.add(Unpooled.wrappedBuffer(buffers.toArray(new ByteBuffer[]{})));
}
 
Example 3
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    int id;
    BaseHS<T> wrapper = (BaseHS<T>) getSchemaWrapper(message.getClass(), true);

    // wait till everything is completely set
    while (0 == (id = wrapper.id))
        LockSupport.parkNanos(1);

    output.writeUInt32(fieldNumber, id, false);

    // TODO allow the wrapper to return an override schema?
    return message.cachedSchema();
}
 
Example 4
Source File: ProtostuffMessageBodyWriter.java    From datawave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String,Object> httpHeaders,
                OutputStream out) throws IOException, WebApplicationException {
    
    // TODO: Figure out a method to add the proto file location in the response headers.
    // This map must be mofified before any data is written to out,
    // since at that time the response headers will be flushed.
    
    Schema<Object> schema = null;
    if (message instanceof Message) {
        Message<Object> msg = (Message<Object>) message;
        schema = msg.cachedSchema();
    } else {
        schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
    }
    
    try {
        if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
            XmlIOUtil.writeTo(out, message, schema);
        } else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
            YamlIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protobuf".equals(media.toString())) {
            ProtobufIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protostuff".equals(media.toString())) {
            ProtostuffIOUtil.writeTo(out, message, schema, buffer);
        } else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
            IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
            UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
                            JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
            try {
                JsonIOUtil.writeTo(generator, message, schema, false);
            } finally {
                generator.close();
            }
        }
    } finally {
        buffer.clear();
    }
}
 
Example 5
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    output.writeString(fieldNumber, message.getClass().getName(), false);

    return message.cachedSchema();
}
 
Example 6
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy
                            .newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example 7
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    final Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy.newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example 8
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    final BaseHS<T> wrapper = (BaseHS<T>) pojoMapping.get(message.getClass());

    if (wrapper == null)
        throw new UnknownTypeException("pojo: " + message.getClass());

    output.writeUInt32(fieldNumber, wrapper.id, false);

    // TODO allow the wrapper to return an override schema?
    return message.cachedSchema();
}
 
Example 9
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    final Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy.newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example 10
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber, Message<T> message) throws IOException {
	output.writeString(fieldNumber, message.getClass().getName(), false);

	return message.cachedSchema();
}