* @param object Object to attach to
* @return Persistable object
*/
private IPersistable doLoad(String name, IPersistable object) {
log.debug("doLoad - name: {} object: {}", name, object);
IPersistable result = object;
// if (log.isTraceEnabled()) {
// try {
// log.trace("Relative #1: {}", (resources.getResource(name) != null ? resources.getResource(name).getFile().getAbsolutePath() : "Not found"));
// log.trace("Absolute #2: {}", (resources.getResource("file://" + rootDir + '/' + name) != null ? resources.getResource("file://" + rootDir + '/' + name).getFile()
// .getAbsolutePath() : "Not found"));
// } catch (IOException e) {
// log.warn("", e);
// }
// }
Resource data = resources.getResource(name);
if (data == null || !data.exists()) {
// no such file
log.debug("Resource / data was not found");
// try again with full path
data = resources.getResource("file://" + rootDir + '/' + name);
if (data == null || !data.exists()) {
log.debug("Resource / data was not found (full path)");
return null;
}
}
FileInputStream input;
String filename;
try {
File fp = data.getFile();
if (fp.length() == 0) {
// File is empty
log.error("The file at {} is empty", data.getFilename());
return null;
}
filename = fp.getAbsolutePath();
input = new FileInputStream(filename);
} catch (FileNotFoundException e) {
log.error("The file at {} does not exist", data.getFilename());
return null;
} catch (IOException e) {
log.error("Could not load file from {}", data.getFilename(), e);
return null;
}
try {
IoBuffer buf = IoBuffer.allocate(input.available());
try {
ServletUtils.copy(input, buf.asOutputStream());
buf.flip();
Input in = new Input(buf);
String className = Deserializer.deserialize(in, String.class);
if (result == null) {
// we need to create the object first
try {
Class<?> theClass = Class.forName(className);
Constructor<?> constructor = null;
try {
// try to create object by calling constructor with Input stream as parameter
for (Class<?> interfaceClass : in.getClass().getInterfaces()) {
constructor = theClass.getConstructor(new Class[] { interfaceClass });
if (constructor != null) {
break;
}
}
if (constructor == null) {
throw new NoSuchMethodException();
}
result = (IPersistable) constructor.newInstance(in);
} catch (NoSuchMethodException err) {
// no valid constructor found, use empty constructor
result = (IPersistable) theClass.newInstance();
result.deserialize(in);
} catch (InvocationTargetException err) {
// error while invoking found constructor, use empty constructor
result = (IPersistable) theClass.newInstance();
result.deserialize(in);
}
} catch (ClassNotFoundException cnfe) {
log.error("Unknown class {}", className);
return null;
} catch (IllegalAccessException iae) {
log.error("Illegal access", iae);
return null;
} catch (InstantiationException ie) {
log.error("Could not instantiate class {}", className);
return null;
}
// set object's properties
log.debug("Name (after load): {}", result.getName());
result.setPath(getObjectPath(name, result.getName()));
} else {
// Initialize existing object
String resultClass = result.getClass().getName();
if (!resultClass.equals(className)) {
log.error("The classes differ: {} != {}", resultClass, className);
return null;
}
result.deserialize(in);
}
} finally {
buf.free();
buf = null;
}
if (result.getStore() != this) {
result.setStore(this);
}
super.save(result);
log.debug("Loaded persistent object {} from {}", result, filename);
} catch (IOException e) {
log.error("Could not load file at {}", filename);