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");
}
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;
}
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);
}
}
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();
}
}
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);
}
}
Aggregations