private static void handleFilepath() { final BufferedReader br; try { br = new BufferedReader(new FileReader(filePath)); } catch (FileNotFoundException e) { throw new RuntimeException("File not found: " + filePath, e); } String line; try { while ((line = br.readLine()) != null) { try { cloudname.createCoordinate(Coordinate.parse(line)); System.out.println("Created " + line); } catch (Exception e) { System.err.println("Could not create: " + line + "Got error: " + e.getMessage()); } } } catch (IOException e) { throw new RuntimeException("Failed to read coordinate from file. " + e.getMessage(), e); } finally { cloudname.close(); try { br.close(); } catch (IOException e) { System.err.println("Failed while trying to close file reader. " + e.getMessage()); } } }
public static void main(final String[] args) { // Disable log system, we want full control over what is sent to console. final ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.activateOptions(); consoleAppender.setLayout(new PatternLayout("%p %t %C:%M %m%n")); consoleAppender.setThreshold(Level.OFF); BasicConfigurator.configure(consoleAppender); // Parse the flags. Flags flags = new Flags().loadOpts(ZkTool.class).parse(args); // Check if we wish to print out help text if (flags.helpFlagged()) { flags.printHelp(System.out); System.out.println("Must specify one of the following options:"); System.out.println(actionSelectingFlagNames); return; } checkArgumentCombinationValid(flags); ZkCloudname.Builder builder = new ZkCloudname.Builder(); if (zooKeeperFlag == null) { builder.setDefaultConnectString(); } else { builder.setConnectString(zooKeeperFlag); } try { cloudname = builder.build().connect(); } catch (CloudnameException e) { System.err.println("Could not connect to zookeeper " + e.getMessage()); return; } try { if (filePath != null) { handleFilepath(); } else if (coordinateFlag != null) { handleCoordinateOperation(); } else if (resolverExpression != null) { handleResolverExpression(); } else if (listFlag) { listCoordinates(); } else { System.err.println("No action specified"); } } catch (Exception e) { System.err.println("An error occurred: " + e.getMessage()); e.printStackTrace(); } finally { cloudname.close(); } }