public T copyForWrite(T entity) { if (entity.isCachedAndShared() == false) return entity; T copy; try { byte[] bytes; try (ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(buf)) { serializer.serialize(entity, dout); bytes = buf.toByteArray(); } try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) { copy = serializer.deserialize(in); } } catch (IOException e) { throw new RuntimeException(e); } copy.setCachedAndShared(false); initEntityAfterReload(copy, entity.resourceName()); return copy; }
public T reloadAt(String path) { try { T entity = store.getResource(path, serializer); if (entity == null) { logger.warn("No " + entityType.getSimpleName() + " found at " + path + ", returning null"); cache.removeLocal(resourceName(path)); return null; } // mark cached object entity.setCachedAndShared(true); entity = initEntityAfterReload(entity, resourceName(path)); if (path.equals(resourcePath(entity.resourceName())) == false) throw new IllegalStateException("The entity " + entity + " read from " + path + " will save to a different path " + resourcePath(entity.resourceName())); cache.putLocal(entity.resourceName(), entity); return entity; } catch (Exception e) { throw new IllegalStateException("Error loading " + entityType.getSimpleName() + " at " + path, e); } }
public T copyForWrite(T entity) { if (entity.isCachedAndShared() == false) return entity; T copy; try { byte[] bytes; try (ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(buf)) { serializer.serialize(entity, dout); bytes = buf.toByteArray(); } try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) { copy = serializer.deserialize(in); } } catch (IOException e) { throw new RuntimeException(e); } copy.setCachedAndShared(false); initEntityAfterReload(copy, entity.resourceName()); return copy; }
public T reloadAt(String path) { try { T entity = store.getResource(path, serializer); if (entity == null) { logger.warn("No " + entityType.getSimpleName() + " found at " + path + ", returning null"); cache.removeLocal(resourceName(path)); return null; } // mark cached object entity.setCachedAndShared(true); entity = initEntityAfterReload(entity, resourceName(path)); if (path.equals(resourcePath(entity.resourceName())) == false) throw new IllegalStateException("The entity " + entity + " read from " + path + " will save to a different path " + resourcePath(entity.resourceName())); cache.putLocal(entity.resourceName(), entity); return entity; } catch (Exception e) { throw new IllegalStateException("Error loading " + entityType.getSimpleName() + " at " + path, e); } }