com.alachisoft.ncache.serialization.standard.io.surrogates.ThrowableSerializationSurrogate Maven / Gradle / Ivy
Show all versions of nc-serialization Show documentation
/*
* @(#)ThrowableSerializationSurrogate.java 1.0
*
* Created on September 18, 2008, 12:36 PM
*
* Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
* "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
* to license terms.
*/
package com.alachisoft.ncache.serialization.standard.io.surrogates;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.surrogates.BuiltinSerializationSurrogate;
import com.alachisoft.ncache.serialization.core.io.surrogates.NCacheIOException;
import com.alachisoft.ncache.serialization.core.io.surrogates.NCacheInstantiationException;
import com.alachisoft.ncache.serialization.core.io.surrogates.SerializationSurrogateImpl;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* ThrowableSerializationSurrogate is responsible for writing and reading
* instances of class that extend from Throwable.
*
* @version 1.0, September 18, 2008
*/
public class ThrowableSerializationSurrogate
extends SerializationSurrogateImpl
implements BuiltinSerializationSurrogate {
private Constructor mConstructor;
/**
* Creates a new instance of ThrowableSerializationSurrogate
*/
public ThrowableSerializationSurrogate(Class cls) {
super(cls);
try {
this.mConstructor = cls.getDeclaredConstructor(String.class, Throwable.class);
this.mConstructor.setAccessible(true);
} catch (Exception ex) {
}
}
/**
* Creates instance of type returned by getRealClass(). This is different from NxSerializationSurrogateBase.createTypeInstance()
* in the sense that an NCacheObjectInput object is passed as parameter that can be used to read creation specific
* information from the stream. For example it can be used to read the length of the array before actually
* reading the values.
*
* The default implementation simply delegates to super.createTypeInstance().
*
* @param input stream reader
* @return Object that this surrogate must deserialize
* @throws NCacheInstantiationException Object creation related exceptions.
*/
@Override
public Object instantiate(NCacheObjectInput input) throws NCacheInstantiationException {
try {
String message = input.readUTF();
Throwable cause = (Throwable) input.readObject();
if (this.mConstructor != null) {
return this.mConstructor.newInstance(message, cause);
}
throw new NCacheInstantiationException("No constructor found");
} catch (ClassNotFoundException ex) {
throw new NCacheInstantiationException(ex);
} catch (InstantiationException ex) {
throw new NCacheInstantiationException(ex);
} catch (IllegalAccessException ex) {
throw new NCacheInstantiationException(ex);
} catch (InvocationTargetException ex) {
throw new NCacheInstantiationException(ex);
} catch (IOException ex) {
throw new NCacheInstantiationException(ex);
}
}
/**
* Read an object of type returned by getRealClass() from the stream reader.
* A fresh instance of the object is passed as parameter.
* The surrogate should populate fields in the object from data on the stream
*
* @param input stream reader
* @param graph a fresh instance of the object that the surrogate must deserialize.
* @return object read from the stream reader
* @throws NCacheInstantiationException Object creation related exceptions.
* @throws NCacheIOException Any of the usual Input/Output related exceptions.
*/
public Object readDirect(NCacheObjectInput input, Object graph)
throws NCacheInstantiationException, NCacheIOException {
Throwable object = (Throwable) graph;
try {
StackTraceElement[] trace = (StackTraceElement[]) input.readObject();
if (trace != null) {
object.setStackTrace(trace);
}
} catch (IOException ex) {
throw new NCacheIOException(ex);
} catch (ClassNotFoundException ex) {
throw new NCacheIOException(ex);
}
return graph;
}
/**
* Write an object of type returned by getRealClass() to the stream writer
*
* @param output stream writer
* @param graph object to be written to the stream reader
* @throws NCacheIOException Any of the usual Input/Output related exceptions.
*/
public void writeDirect(NCacheObjectOutput output, Object graph)
throws NCacheIOException {
Throwable object = (Throwable) graph;
try {
output.writeUTF(object.getMessage());
output.writeObject(object.getCause());
output.writeObject(object.getStackTrace());
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
@Override
public void skipDirect(NCacheObjectInput input, Object graph) throws NCacheInstantiationException, NCacheIOException {
try {
input.skipObject();
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
}