(String name, T x, Supplier<T> sx)
| 125 | } |
| 126 | |
| 127 | private static <T extends Serializable> T hook(String name, T x, Supplier<T> sx) { |
| 128 | hooks.put(name, () -> { |
| 129 | |
| 130 | Log.log("shutdown.autopersist", ()->"saving "+name); |
| 131 | |
| 132 | boolean success = true; |
| 133 | try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File(dir + name+"_tmp"))))) { |
| 134 | Log.log("shutdown.autopersist", ()->" writing "+sx.get()); |
| 135 | oos.writeObject(sx.get()); |
| 136 | } catch (Throwable e2) { |
| 137 | e2.printStackTrace(); |
| 138 | success = false; |
| 139 | } |
| 140 | if (success) |
| 141 | { |
| 142 | try { |
| 143 | Log.log("shutdown.autopersist", ()->" moving into place "); |
| 144 | Files.move(Paths.get(dir+name+"_tmp"), Paths.get(dir+name), StandardCopyOption.ATOMIC_MOVE); |
| 145 | } catch (IOException e) { |
| 146 | Log.log("shutdown.autopersist", ()->e); |
| 147 | } |
| 148 | Log.log("shutdown.autopersist", ()->" finished "); |
| 149 | } |
| 150 | }); |
| 151 | return x; |
| 152 | } |
| 153 | |
| 154 | } |
no test coverage detected