Search in sources :

Example 1 with DeserializationPostInitialisable

use of org.mule.runtime.core.privileged.store.DeserializationPostInitialisable in project mule by mulesoft.

the class SerializationUtils method deserialize.

/**
 * <p>
 * Deserializes an <code>Object</code> from the specified stream.
 * </p>
 * <p/>
 * <p>
 * The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.
 * </p>
 * <p/>
 * <p>
 * The stream passed in is not buffered internally within this method. This is the responsibility of your application if
 * desired.
 * </p>
 *
 * @param inputStream the serialized object input stream, must not be null
 * @param cl classloader which can load custom classes from the stream
 * @return the deserialized object
 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
 * @throws org.apache.commons.lang3.SerializationException (runtime) if the serialization fails
 */
public static Object deserialize(InputStream inputStream, ClassLoader cl, MuleContext muleContext) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }
    if (cl == null) {
        throw new IllegalArgumentException("The ClassLoader must not be null");
    }
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ClassLoaderObjectInputStream(cl, inputStream);
        Object obj = in.readObject();
        if (obj instanceof DeserializationPostInitialisable) {
            DeserializationPostInitialisable.Implementation.init(obj, muleContext);
        }
        return obj;
    } catch (ClassNotFoundException ex) {
        throw new SerializationException(ex);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } catch (Exception ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        // ignore close exception
        }
    }
}
Also used : SerializationException(org.apache.commons.lang3.SerializationException) DeserializationPostInitialisable(org.mule.runtime.core.privileged.store.DeserializationPostInitialisable) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) IOException(java.io.IOException) SerializationException(org.apache.commons.lang3.SerializationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream)

Aggregations

IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ClassLoaderObjectInputStream (org.apache.commons.io.input.ClassLoaderObjectInputStream)1 SerializationException (org.apache.commons.lang3.SerializationException)1 DeserializationPostInitialisable (org.mule.runtime.core.privileged.store.DeserializationPostInitialisable)1