/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015  
 */
package com.ibm.streamsx.topology.internal.spljava;

import java.io.IOException;
import java.io.ObjectInputStream;

import com.ibm.streams.operator.StreamSchema;
import com.ibm.streams.operator.Tuple;
import com.ibm.streams.operator.types.Blob;

/**
 * Mapping for a Java object as an SPL schema.
 * Like Java at runtime (the schema) we have no knowledge of
 * the actual type of the Java object, the info for
 *  generic parameter of TStream<T> is not needed. 
 */
class SPLJavaObject extends SPLMapping<Object> {

    public static final String SPL_JAVA_PREFIX = "__spl_j";
    
    public static final String SPL_JAVA_OBJECT = SPL_JAVA_PREFIX + "object";

    SPLJavaObject(StreamSchema schema) {
        super(schema);
    }

    @Override
    public Object convertFrom(Tuple tuple) {
        Blob blob = tuple.getBlob(0);

        if (blob instanceof JavaObjectBlob) {
            JavaObjectBlob jblob = (JavaObjectBlob) blob;
            return jblob.getObject();
        }

        try (ObjectInputStream ois = new ObjectInputStream(blob.getInputStream())) {
            return ois.readObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Tuple convertTo(Object tuple) {

        JavaObjectBlob jblob = new JavaObjectBlob(tuple);
        return getSchema().getTuple(new Blob[] { jblob });
    }
}

@@ -592,68 +592,84 @@
     
-public static final String SPL_JAVA_PREFIX = %22__spl_j%22;
+/**%0A     * Attribute name for a schema with a serialized java object
 %0A    
+ */
 %0A   
@@ -718,33 +718,17 @@
 T = 
-SPL_JAVA_PREFIX + %22object
+%22__spl_jo
 %22;%0A%0A

Fix Java object schema