/** * Creates a new instance. * * @param data the data to be serialized * @return a new instance * @throws BatchRuntimeException if a failure to serialize the data occurs */ public static SerializableData of(final Serializable data) { if (data instanceof SerializableData) { return (SerializableData) data; } if (data instanceof byte[]) { return new SerializableData((byte[]) data, null); } if (data == null) { return new SerializableData(null, null); } Class<?> c = data.getClass(); if (c.isArray()) { c = c.getComponentType(); } if (requiresSerialization(c)) { try { return new SerializableData(BatchUtil.objectToBytes(data), null); } catch (IOException e) { throw BatchMessages.MESSAGES.failedToSerialize(e, data); } } return new SerializableData(null, data); }
/** * If the data was previously serialized it's deserialized using the TCCL. If the TCCL is not set * the class loader for this class will be used. * * @return the serialized data or {@code null} if the data was {@code null} when created * @throws BatchRuntimeException if there was a failure to desrialize the value */ public Serializable deserialize() throws BatchRuntimeException { if (raw != null) { return raw; } if (serialized != null) { // In an EE container the TCCL should be set to the class loader required for the deployment, // if it's not // set we'll default to the class loader for this class to deserialize the data ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); if (cl == null) { cl = WildFlySecurityManager.getClassLoaderPrivileged(SerializableData.class); } try { return BatchUtil.bytesToSerializableObject(serialized, cl); } catch (IOException e) { throw BatchMessages.MESSAGES.failedToDeserialize(e, Arrays.toString(serialized)); } catch (ClassNotFoundException e) { throw BatchMessages.MESSAGES.failedToDeserialize(e, Arrays.toString(serialized)); } } return null; }