protected void store() throws Exception { ObjectOutputStream oos = null; File tmpFile = null; try { tmpFile = File.createTempFile("RNS", ".tmp", storageDirBase); tmpFile.deleteOnExit(); oos = new ObjectOutputStream(new FileOutputStream(tmpFile)); /* 1 */ oos.writeObject(rnsProps.getCreateTime()); /* 2 */ oos.writeObject(rnsProps.getAccessTime()); /* 3 */ oos.writeObject(rnsProps.getModificationTime()); if (list == null || list.size() == 0) { /* 4 */ oos.writeInt(0); // list size } else { /* 4 */ oos.writeInt(list.size()); Iterator<String> i = list.keySet().iterator(); while (i.hasNext()) { String name = i.next(); /* 5 */ oos.writeUTF(name); RNSEntryData ent = list.get(name); /* 6 */ oos.writeBoolean(ent.isDirectory()); /* 7 */ if (ent.getLocalID() == null) { oos.writeUTF(""); } else { oos.writeUTF(ent.getLocalID()); } /* 8 */ String eprStr; if (ent.getEpr() == null) { eprStr = ""; } else { eprStr = RNSUtil.toXMLString(ent.getEpr()); } oos.writeUTF(eprStr); if (ent.getRNSMetadata() != null && ent.getRNSMetadata().get_any() != null) { MessageElement[] me = ent.getRNSMetadata().get_any(); /* 9 */ oos.writeInt(me.length); for (int n = 0; n < me.length; n++) { String xml = me[n].getAsString(); /* 10 */ oos.writeUTF(xml); // System.out.println(getID() + "/" + name + // ": store XML: " + xml); } } else { oos.writeInt(0); /* 9 */ /* 10: nothing */ } } } /* ACL */ if (aclents == null || aclents.length == 0) { oos.writeInt(0); /* 11 */ /* 12: nothing */ } else { oos.writeInt(aclents.length); for (ACLEntryType aet : aclents) { /* 12 */ oos.writeShort(aet.getType()); String aclname = aet.getName(); if (aclname == null) { aclname = DUMMY; } oos.writeUTF(aclname); oos.writeShort(aet.getPerm()); } } logger.debug("store id=" + id); } catch (Exception e) { if (tmpFile != null) { if (tmpFile.delete() == false) { logger.error("cannot delete: " + tmpFile.getAbsolutePath()); } } logger.error("cannot store resource: id=" + id); throw e; } finally { if (oos != null) { try { oos.close(); } catch (Exception e2) { logger.warn("cannot write-close: " + tmpFile.getAbsolutePath() + ": " + e2.getMessage()); } } } File file = getIdAsFile(id, data_suffix); if (file == null) { logger.error("directory is not exist: id=" + id); throw new Exception("Failed to store resource"); } if (file.exists()) { if (file.delete() == false) { logger.error("cannot delete: " + file.getAbsolutePath()); } } if (!tmpFile.renameTo(file)) { if (tmpFile.delete() == false) { logger.error("cannot rename: " + tmpFile.getAbsolutePath()); } throw new Exception("Failed to store resource"); } }
protected void load() throws Exception { if (id == null) { return; } File file = getIdAsFile(id, data_suffix); if (file == null || !file.exists()) { throw new Exception("unexpected condition:not exist:id=" + id); } if (file.length() == 0) { return; // need setRNSDirProps(not null) } ObjectInputStream ois = null; RNSDirectoryProperties newRnsProps = new RNSDirectoryProperties(); try { ois = new ObjectInputStream(new FileInputStream(file)); /* 1 */ newRnsProps.setCreateTime((Calendar) ois.readObject()); /* 2 */ newRnsProps.setAccessTime((Calendar) ois.readObject()); /* 3 */ newRnsProps.setModificationTime((Calendar) ois.readObject()); /* 4 */ int size = ois.readInt(); /* create new list */ Map<String, RNSEntryData> newlist = Collections.synchronizedMap(new HashMap<String, RNSEntryData>()); for (int i = 0; i < size; i++) { /* 5 */ String keyname = ois.readUTF(); RNSEntryData ent = new RNSEntryData(); /* 6 */ ent.setDirectory(ois.readBoolean()); /* 7 */ ent.setLocalID(ois.readUTF()); if ("".equals(ent.getLocalID())) { ent.setLocalID(null); } /* 8 */ String eprStr = ois.readUTF(); if (eprStr != null && eprStr.length() != 0) { ent.setEpr(RNSUtil.toEPR(eprStr)); } /* 9 */ int metalen = ois.readInt(); if (metalen > 0) { MessageElement[] me = new MessageElement[metalen]; for (int n = 0; n < metalen; n++) { /* 10 */ String xml = ois.readUTF(); me[n] = RNSUtil.toMessageElement(xml); } ent.setAny(me); } newlist.put(keyname, ent); } ACLEntryType[] newaclents; /* ACL */ int acllen; try { acllen = ois.readInt(); /* 11 */ } catch (EOFException eof) { acllen = 0; } if (acllen > 0) { newaclents = new ACLEntryType[acllen]; for (int n = 0; n < acllen; n++) { /* 12 */ newaclents[n] = new ACLEntryType(); newaclents[n].setType(ois.readShort()); String aclname = ois.readUTF(); if (aclname.equals(DUMMY)) { aclname = null; } newaclents[n].setName(aclname); newaclents[n].setPerm(ois.readShort()); } } else { newaclents = null; } rnsProps = newRnsProps; list = newlist; aclents = newaclents; logger.debug("load id=" + id); } catch (IOException e) { throw new Exception("Failed to load resource: id=" + id, e); } finally { if (ois != null) { try { ois.close(); } catch (Exception e2) { logger.warn("cannot close: resource id=" + id + ": " + e2.getMessage()); } } } }