in common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java [165:252]
private void doUpdate(Runnable updater) {
if (!Files.isReadable(registryFile)) {
throw new DaemonException("Registry became unaccessible");
}
synchronized (lck) {
try (FileLock l = tryLock()) {
channel.position(0);
DataInputStream is = new DataInputStream(new BufferedInputStream(Channels.newInputStream(channel)));
infosMap.clear();
int nb = is.available() < 4 ? 0 : is.readInt();
for (int i = 0; i < nb; i++) {
String daemonId = is.readUTF();
String javaHome = is.readUTF();
String mavenHome = is.readUTF();
int pid = is.readInt();
String address = is.readUTF();
byte[] token = new byte[DaemonInfo.TOKEN_SIZE];
is.read(token);
String locale = is.readUTF();
List<String> opts = new ArrayList<>();
int nbOpts = is.readInt();
for (int j = 0; j < nbOpts; j++) {
opts.add(is.readUTF());
}
DaemonState state = DaemonState.values()[is.readByte()];
long lastIdle = is.readLong();
long lastBusy = is.readLong();
DaemonInfo di = new DaemonInfo(
daemonId, javaHome, mavenHome, pid, address, token, locale, opts, state, lastIdle,
lastBusy);
infosMap.putIfAbsent(di.getId(), di);
}
stopEvents.clear();
nb = is.available() < 4 ? 0 : is.readInt();
for (int i = 0; i < nb; i++) {
String daemonId = is.readUTF();
long date = is.readLong();
int ord = is.readByte();
DaemonExpirationStatus des = ord >= 0 ? DaemonExpirationStatus.values()[ord] : null;
String reason = is.readUTF();
DaemonStopEvent se = new DaemonStopEvent(daemonId, date, des, reason);
stopEvents.add(se);
}
if (updater != null) {
updater.run();
channel.truncate(0);
DataOutputStream os =
new DataOutputStream(new BufferedOutputStream(Channels.newOutputStream(channel)));
os.writeInt(infosMap.size());
for (DaemonInfo di : infosMap.values()) {
String id = di.getId();
os.writeUTF(id);
os.writeUTF(di.getJavaHome());
os.writeUTF(di.getMvndHome());
os.writeInt(di.getPid());
os.writeUTF(di.getAddress());
os.write(di.getToken());
os.writeUTF(di.getLocale());
os.writeInt(di.getOptions().size());
for (String opt : di.getOptions()) {
os.writeUTF(opt);
}
os.writeByte((byte) di.getState().ordinal());
os.writeLong(di.getLastIdle());
os.writeLong(di.getLastBusy());
}
os.writeInt(stopEvents.size());
for (DaemonStopEvent dse : stopEvents) {
os.writeUTF(dse.getDaemonId());
os.writeLong(dse.getTimestamp());
os.writeByte((byte)
(dse.getStatus() == null ? -1 : dse.getStatus().ordinal()));
os.writeUTF(dse.getReason());
}
os.flush();
}
} catch (DaemonException e) {
throw e;
} catch (Exception e) {
LOGGER.warn("Invalid daemon registry info at [{}], trying to recover.", registryFile, e);
this.reset();
}
}
}