@Test public void testLoadProxyIdFromFile() throws Exception { UUID proxyId = UUID.randomUUID(); String path = File.createTempFile("proxyTestIdFile", null).getPath(); Files.asCharSink(new File(path), Charsets.UTF_8).write(proxyId.toString()); UUID uuid = ProxyUtil.getOrCreateProxyIdFromFile(path); assertEquals(proxyId, uuid); path = File.createTempFile("proxyTestIdFile", null).getPath() + ".id"; uuid = ProxyUtil.getOrCreateProxyIdFromFile(path); assertEquals(uuid, ProxyUtil.getOrCreateProxyIdFromFile(path)); }
static UUID getOrCreateProxyIdFromFile(@Nullable String idFileName) { File proxyIdFile; UUID proxyId = UUID.randomUUID(); if (idFileName != null) { proxyIdFile = new File(idFileName); } else { File userHome = new File(System.getProperty("user.home")); if (!userHome.exists() || !userHome.isDirectory()) { throw new RuntimeException("Cannot read from user.home, quitting"); } File configDirectory = new File(userHome, ".dshell"); if (configDirectory.exists()) { if (!configDirectory.isDirectory()) { throw new RuntimeException(configDirectory + " must be a directory!"); } } else { if (!configDirectory.mkdir()) { throw new RuntimeException("Cannot create .dshell directory under " + userHome); } } proxyIdFile = new File(configDirectory, "id"); } if (proxyIdFile.exists()) { if (proxyIdFile.isFile()) { try { proxyId = UUID.fromString(Objects.requireNonNull(Files.asCharSource(proxyIdFile, Charsets.UTF_8).readFirstLine())); logger.info("Proxy Id read from file: " + proxyId); } catch (IllegalArgumentException ex) { throw new RuntimeException("Cannot read proxy id from " + proxyIdFile + ", content is malformed"); } catch (IOException e) { throw new RuntimeException("Cannot read from " + proxyIdFile, e); } } else { throw new RuntimeException(proxyIdFile + " is not a file!"); } } else { logger.info("Proxy Id created: " + proxyId); try { Files.asCharSink(proxyIdFile, Charsets.UTF_8).write(proxyId.toString()); } catch (IOException e) { throw new RuntimeException("Cannot write to " + proxyIdFile); } } return proxyId; }
ProxyUtil { static UUID getOrCreateProxyIdFromFile(@Nullable String idFileName) { File proxyIdFile; UUID proxyId = UUID.randomUUID(); if (idFileName != null) { proxyIdFile = new File(idFileName); } else { File userHome = new File(System.getProperty("user.home")); if (!userHome.exists() || !userHome.isDirectory()) { throw new RuntimeException("Cannot read from user.home, quitting"); } File configDirectory = new File(userHome, ".dshell"); if (configDirectory.exists()) { if (!configDirectory.isDirectory()) { throw new RuntimeException(configDirectory + " must be a directory!"); } } else { if (!configDirectory.mkdir()) { throw new RuntimeException("Cannot create .dshell directory under " + userHome); } } proxyIdFile = new File(configDirectory, "id"); } if (proxyIdFile.exists()) { if (proxyIdFile.isFile()) { try { proxyId = UUID.fromString(Objects.requireNonNull(Files.asCharSource(proxyIdFile, Charsets.UTF_8).readFirstLine())); logger.info("Proxy Id read from file: " + proxyId); } catch (IllegalArgumentException ex) { throw new RuntimeException("Cannot read proxy id from " + proxyIdFile + ", content is malformed"); } catch (IOException e) { throw new RuntimeException("Cannot read from " + proxyIdFile, e); } } else { throw new RuntimeException(proxyIdFile + " is not a file!"); } } else { logger.info("Proxy Id created: " + proxyId); try { Files.asCharSink(proxyIdFile, Charsets.UTF_8).write(proxyId.toString()); } catch (IOException e) { throw new RuntimeException("Cannot write to " + proxyIdFile); } } return proxyId; } }
ProxyUtil { static UUID getOrCreateProxyIdFromFile(@Nullable String idFileName) { File proxyIdFile; UUID proxyId = UUID.randomUUID(); if (idFileName != null) { proxyIdFile = new File(idFileName); } else { File userHome = new File(System.getProperty("user.home")); if (!userHome.exists() || !userHome.isDirectory()) { throw new RuntimeException("Cannot read from user.home, quitting"); } File configDirectory = new File(userHome, ".dshell"); if (configDirectory.exists()) { if (!configDirectory.isDirectory()) { throw new RuntimeException(configDirectory + " must be a directory!"); } } else { if (!configDirectory.mkdir()) { throw new RuntimeException("Cannot create .dshell directory under " + userHome); } } proxyIdFile = new File(configDirectory, "id"); } if (proxyIdFile.exists()) { if (proxyIdFile.isFile()) { try { proxyId = UUID.fromString(Objects.requireNonNull(Files.asCharSource(proxyIdFile, Charsets.UTF_8).readFirstLine())); logger.info("Proxy Id read from file: " + proxyId); } catch (IllegalArgumentException ex) { throw new RuntimeException("Cannot read proxy id from " + proxyIdFile + ", content is malformed"); } catch (IOException e) { throw new RuntimeException("Cannot read from " + proxyIdFile, e); } } else { throw new RuntimeException(proxyIdFile + " is not a file!"); } } else { logger.info("Proxy Id created: " + proxyId); try { Files.asCharSink(proxyIdFile, Charsets.UTF_8).write(proxyId.toString()); } catch (IOException e) { throw new RuntimeException("Cannot write to " + proxyIdFile); } } return proxyId; } private  ProxyUtil(); }
ProxyUtil { static UUID getOrCreateProxyIdFromFile(@Nullable String idFileName) { File proxyIdFile; UUID proxyId = UUID.randomUUID(); if (idFileName != null) { proxyIdFile = new File(idFileName); } else { File userHome = new File(System.getProperty("user.home")); if (!userHome.exists() || !userHome.isDirectory()) { throw new RuntimeException("Cannot read from user.home, quitting"); } File configDirectory = new File(userHome, ".dshell"); if (configDirectory.exists()) { if (!configDirectory.isDirectory()) { throw new RuntimeException(configDirectory + " must be a directory!"); } } else { if (!configDirectory.mkdir()) { throw new RuntimeException("Cannot create .dshell directory under " + userHome); } } proxyIdFile = new File(configDirectory, "id"); } if (proxyIdFile.exists()) { if (proxyIdFile.isFile()) { try { proxyId = UUID.fromString(Objects.requireNonNull(Files.asCharSource(proxyIdFile, Charsets.UTF_8).readFirstLine())); logger.info("Proxy Id read from file: " + proxyId); } catch (IllegalArgumentException ex) { throw new RuntimeException("Cannot read proxy id from " + proxyIdFile + ", content is malformed"); } catch (IOException e) { throw new RuntimeException("Cannot read from " + proxyIdFile, e); } } else { throw new RuntimeException(proxyIdFile + " is not a file!"); } } else { logger.info("Proxy Id created: " + proxyId); try { Files.asCharSink(proxyIdFile, Charsets.UTF_8).write(proxyId.toString()); } catch (IOException e) { throw new RuntimeException("Cannot write to " + proxyIdFile); } } return proxyId; } private  ProxyUtil();  }
ProxyUtil { static UUID getOrCreateProxyIdFromFile(@Nullable String idFileName) { File proxyIdFile; UUID proxyId = UUID.randomUUID(); if (idFileName != null) { proxyIdFile = new File(idFileName); } else { File userHome = new File(System.getProperty("user.home")); if (!userHome.exists() || !userHome.isDirectory()) { throw new RuntimeException("Cannot read from user.home, quitting"); } File configDirectory = new File(userHome, ".dshell"); if (configDirectory.exists()) { if (!configDirectory.isDirectory()) { throw new RuntimeException(configDirectory + " must be a directory!"); } } else { if (!configDirectory.mkdir()) { throw new RuntimeException("Cannot create .dshell directory under " + userHome); } } proxyIdFile = new File(configDirectory, "id"); } if (proxyIdFile.exists()) { if (proxyIdFile.isFile()) { try { proxyId = UUID.fromString(Objects.requireNonNull(Files.asCharSource(proxyIdFile, Charsets.UTF_8).readFirstLine())); logger.info("Proxy Id read from file: " + proxyId); } catch (IllegalArgumentException ex) { throw new RuntimeException("Cannot read proxy id from " + proxyIdFile + ", content is malformed"); } catch (IOException e) { throw new RuntimeException("Cannot read from " + proxyIdFile, e); } } else { throw new RuntimeException(proxyIdFile + " is not a file!"); } } else { logger.info("Proxy Id created: " + proxyId); try { Files.asCharSink(proxyIdFile, Charsets.UTF_8).write(proxyId.toString()); } catch (IOException e) { throw new RuntimeException("Cannot write to " + proxyIdFile); } } return proxyId; } private  ProxyUtil();   }