Search in sources :

Example 11 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class OStreamSerializerAnyRecord method toStream.

public byte[] toStream(Object iObject) throws IOException {
    if (iObject == null)
        return null;
    if (((ORecord) iObject).getIdentity() == null)
        throw new OSerializationException("Cannot serialize record without identity. Store it before serialization.");
    final StringBuilder buffer = OStreamSerializerHelper.writeRecordType(iObject.getClass(), new StringBuilder(1024));
    buffer.append(((ORecord) iObject).getIdentity().toString());
    return buffer.toString().getBytes("UTF-8");
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 12 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class OStreamSerializerAnyStreamable method toStream.

/**
   * Serialize the class name size + class name + object content
   */
public byte[] toStream(final Object iObject) throws IOException {
    if (iObject == null)
        return null;
    if (!(iObject instanceof OSerializableStream))
        throw new OSerializationException("Cannot serialize the object [" + iObject.getClass() + ":" + iObject + "] since it does not implement the OSerializableStream interface");
    OSerializableStream stream = (OSerializableStream) iObject;
    // SERIALIZE THE CLASS NAME
    final byte[] className;
    if (iObject instanceof OLiveQuery<?>)
        className = iObject.getClass().getName().getBytes("UTF-8");
    else if (iObject instanceof OSQLSynchQuery<?>)
        className = QUERY_COMMAND_CLASS_ASBYTES;
    else if (iObject instanceof OCommandSQL)
        className = SQL_COMMAND_CLASS_ASBYTES;
    else if (iObject instanceof OCommandScript)
        className = SCRIPT_COMMAND_CLASS_ASBYTES;
    else {
        if (iObject == null)
            className = null;
        else
            className = iObject.getClass().getName().getBytes("UTF-8");
    }
    // SERIALIZE THE OBJECT CONTENT
    byte[] objectContent = stream.toStream();
    byte[] result = new byte[4 + className.length + objectContent.length];
    // COPY THE CLASS NAME SIZE + CLASS NAME + OBJECT CONTENT
    System.arraycopy(OBinaryProtocol.int2bytes(className.length), 0, result, 0, 4);
    System.arraycopy(className, 0, result, 4, className.length);
    System.arraycopy(objectContent, 0, result, 4 + className.length, objectContent.length);
    return result;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OLiveQuery(com.orientechnologies.orient.core.sql.query.OLiveQuery) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream)

Example 13 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class OStreamSerializerAnyStreamable method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(final byte[] iStream) throws IOException {
    if (iStream == null || iStream.length == 0)
        // NULL VALUE
        return null;
    final int classNameSize = OBinaryProtocol.bytes2int(iStream);
    if (classNameSize <= 0) {
        final String message = "Class signature not found in ANY element: " + Arrays.toString(iStream);
        OLogManager.instance().error(this, message);
        throw new OSerializationException(message);
    }
    final String className = new String(iStream, 4, classNameSize, "UTF-8");
    try {
        final OSerializableStream stream;
        // CHECK FOR ALIASES
        if (className.equalsIgnoreCase("q"))
            // QUERY
            stream = new OSQLSynchQuery<Object>();
        else if (className.equalsIgnoreCase("c"))
            // SQL COMMAND
            stream = new OCommandSQL();
        else if (className.equalsIgnoreCase("s"))
            // SCRIPT COMMAND
            stream = new OCommandScript();
        else
            // CREATE THE OBJECT BY INVOKING THE EMPTY CONSTRUCTOR
            stream = (OSerializableStream) Class.forName(className).newInstance();
        return stream.fromStream(OArrays.copyOfRange(iStream, 4 + classNameSize, iStream.length));
    } catch (Exception e) {
        final String message = "Error on unmarshalling content. Class: " + className;
        OLogManager.instance().error(this, message, e);
        throw OException.wrapException(new OSerializationException(message), e);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 14 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class OObjectProxyMethodHandler method updateLoadedFieldMap.

public void updateLoadedFieldMap(final Object proxiedObject, final boolean iReload) {
    final Set<String> fields = new HashSet<String>(loadedFields.keySet());
    for (String fieldName : fields) {
        try {
            if (iReload) {
                // FORCE POJO FIELD VALUE TO DEFAULT VALUE, WHICH CAN BE null, 0 or false
                final Field fieldToReset = OObjectEntitySerializer.getField(fieldName, proxiedObject.getClass());
                OObjectEntitySerializer.setFieldValue(fieldToReset, proxiedObject, getDefaultValueForField(fieldToReset));
            } else {
                final Object value = getValue(proxiedObject, fieldName, false, null);
                if (value instanceof OObjectLazyMultivalueElement) {
                    if (((OObjectLazyMultivalueElement<?>) value).getUnderlying() != doc.field(fieldName))
                        loadedFields.remove(fieldName);
                } else {
                    loadedFields.put(fieldName, doc.getVersion());
                }
            }
        } catch (IllegalArgumentException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (IllegalAccessException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (NoSuchMethodException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (InvocationTargetException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        }
    }
    if (iReload) {
        // RESET LOADED FIELDS, SO THE MUST BE RELOADED FROM DATABASE
        loadedFields.clear();
    }
}
Also used : Field(java.lang.reflect.Field) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 15 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class OObjectEntitySerializer method serializeObject.

/**
   * Method that given an object serialize it an creates a proxy entity, in case the object isn't generated using the
   * ODatabaseObject.newInstance()
   *
   * @param o
   *          - the object to serialize
   * @return the proxied object
   */
public static <T> T serializeObject(T o, ODatabaseObject db) {
    if (o instanceof Proxy) {
        final ODocument iRecord = getDocument((Proxy) o);
        Class<?> pojoClass = o.getClass().getSuperclass();
        invokeCallback(pojoClass, o, iRecord, OBeforeSerialization.class);
        invokeCallback(pojoClass, o, iRecord, OAfterSerialization.class);
        return o;
    }
    Proxy proxiedObject = (Proxy) db.newInstance(o.getClass());
    try {
        return toStream(o, proxiedObject, db);
    } catch (IllegalArgumentException e) {
        throw OException.wrapException(new OSerializationException("Error serializing object of class " + o.getClass()), e);
    } catch (IllegalAccessException e) {
        throw OException.wrapException(new OSerializationException("Error serializing object of class " + o.getClass()), e);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)34 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)11 OException (com.orientechnologies.common.exception.OException)10 IOException (java.io.IOException)9 OType (com.orientechnologies.orient.core.metadata.schema.OType)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 ORecord (com.orientechnologies.orient.core.record.ORecord)6 ORID (com.orientechnologies.orient.core.id.ORID)5 ORecordId (com.orientechnologies.orient.core.id.ORecordId)5 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ParseException (java.text.ParseException)3