use of org.hibernate.type.SerializationException in project hibernate-orm by hibernate.
the class CacheableFileXmlSource method doBind.
@Override
@SuppressWarnings("unchecked")
public Binding doBind(Binder binder) {
if (strict) {
try {
return new Binding(readSerFile(), getOrigin());
} catch (SerializationException e) {
throw new MappingException(String.format("Unable to deserialize from cached file [%s]", getOrigin().getName()), e, getOrigin());
} catch (FileNotFoundException e) {
throw new MappingException(String.format("Unable to locate cached file [%s]", getOrigin().getName()), e, getOrigin());
}
} else {
if (!isSerfileObsolete()) {
try {
return readSerFile();
} catch (SerializationException e) {
log.unableToDeserializeCache(serFile.getName(), e);
} catch (FileNotFoundException e) {
log.cachedFileNotFound(serFile.getName(), e);
}
} else {
log.cachedFileObsolete(serFile);
}
log.readingMappingsFromFile(xmlFile.getPath());
final Binding binding = FileXmlSource.doBind(binder, xmlFile, getOrigin());
writeSerFile(binding);
return binding;
}
}
use of org.hibernate.type.SerializationException in project hibernate-orm by hibernate.
the class SerializationHelper method serialize.
// Serialize
//-----------------------------------------------------------------------
/**
* <p>Serializes an <code>Object</code> to 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 obj the object to serialize to bytes, may be null
* @param outputStream the stream to write to, must not be null
*
* @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {
if (outputStream == null) {
throw new IllegalArgumentException("The OutputStream must not be null");
}
if (LOG.isTraceEnabled()) {
if (Hibernate.isInitialized(obj)) {
LOG.tracev("Starting serialization of object [{0}]", obj);
} else {
LOG.trace("Starting serialization of [uninitialized proxy]");
}
}
ObjectOutputStream out = null;
try {
// stream closed in the finally
out = new ObjectOutputStream(outputStream);
out.writeObject(obj);
} catch (IOException ex) {
throw new SerializationException("could not serialize", ex);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ignored) {
}
}
}
use of org.hibernate.type.SerializationException in project hibernate-orm by hibernate.
the class SessionFactorySerializationTest method testUnNamedSessionFactorySerialization.
@Test
public void testUnNamedSessionFactorySerialization() throws Exception {
// IMPL NOTE : this test is a control to testNamedSessionFactorySerialization
// here, the test should fail based just on attempted uuid resolution
Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
"false");
SessionFactory factory = cfg.buildSessionFactory();
// we need to do some tricking here so that Hibernate thinks the deserialization happens in a
// different VM
String uuid = ((SessionFactoryImplementor) factory).getUuid();
// deregister under this uuid...
SessionFactoryRegistry.INSTANCE.removeSessionFactory(uuid, null, false, null);
// and then register under a different uuid...
SessionFactoryRegistry.INSTANCE.addSessionFactory("some-other-uuid", null, false, factory, null);
try {
SerializationHelper.clone(factory);
fail("Expecting an error");
} catch (SerializationException expected) {
}
SessionFactoryRegistry.INSTANCE.removeSessionFactory("some-other-uuid", null, false, null);
factory.close();
assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Aggregations