Example #1
0
 private void grabPromptOrJustExecuteCommand(ShellClient client, Args args) throws Exception {
   String command = args.get(ARG_COMMAND, null);
   if (command != null) {
     client.evaluate(command);
     client.shutdown();
     return;
   }
   String fileName = args.get(ARG_FILE, null);
   if (fileName != null) {
     BufferedReader reader = null;
     try {
       if (fileName.equals(ARG_FILE_STDIN)) {
         reader = new BufferedReader(new InputStreamReader(System.in, UTF_8));
       } else {
         File file = new File(fileName);
         if (!file.exists()) {
           throw new ShellException("File to execute " + "does not exist: " + fileName);
         }
         reader = newBufferedFileReader(file, UTF_8);
       }
       executeCommandStream(client, reader);
     } finally {
       if (reader != null) {
         reader.close();
       }
     }
     return;
   }
   client.grabPrompt();
 }
Example #2
0
  private void start(String[] arguments, CtrlCHandler signalHandler) {
    Args args = Args.withFlags(ARG_READONLY).parse(arguments);
    if (args.has("?") || args.has("h") || args.has("help") || args.has("usage")) {
      printUsage();
      return;
    }

    String path = args.get(ARG_PATH, null);
    String host = args.get(ARG_HOST, null);
    String port = args.get(ARG_PORT, null);
    String name = args.get(ARG_NAME, null);
    String pid = args.get(ARG_PID, null);

    if ((path != null && (port != null || name != null || host != null || pid != null))
        || (pid != null && host != null)) {
      System.err.println(
          "You have supplied both "
              + ARG_PATH
              + " as well as "
              + ARG_HOST
              + "/"
              + ARG_PORT
              + "/"
              + ARG_NAME
              + ". "
              + "You should either supply only "
              + ARG_PATH
              + " or "
              + ARG_HOST
              + "/"
              + ARG_PORT
              + "/"
              + ARG_NAME
              + " so that either a local or "
              + "remote shell client can be started");
    }
    // Local
    else if (path != null) {
      try {
        checkNeo4jDependency();
      } catch (ShellException e) {
        handleException(e, args);
      }
      startLocal(args, signalHandler);
    }
    // Remote
    else {
      String readonly = args.get(ARG_READONLY, null);
      if (readonly != null) {
        System.err.println(
            "Warning: -" + ARG_READONLY + " is ignored unless you connect with -" + ARG_PATH + "!");
      }

      // Start server on the supplied process
      if (pid != null) {
        startServer(pid, args);
      }
      startRemote(args, signalHandler);
    }
  }
Example #3
0
 public static void main(String args[]) throws IOException {
   Args arguments = new Args(args);
   TimeZone timeZone = parseTimeZoneConfig(arguments);
   try (Printer printer = getPrinter(arguments)) {
     for (String fileAsString : arguments.orphans()) {
       new DumpLogicalLog(new DefaultFileSystemAbstraction())
           .dump(fileAsString, printer.getFor(fileAsString), timeZone);
     }
   }
 }
Example #4
0
 private void startServer(String pid, Args args) {
   String port = args.get("port", Integer.toString(SimpleAppServer.DEFAULT_PORT));
   String name = args.get("name", SimpleAppServer.DEFAULT_NAME);
   try {
     String jarfile =
         new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI())
             .getAbsolutePath();
     Object vm = attachMethod.invoke(null, pid);
     loadMethod.invoke(vm, jarfile, new ShellBootstrap(Integer.parseInt(port), name).serialize());
   } catch (Exception e) {
     handleException(e, args);
   }
 }
Example #5
0
 private String usage() {
   return lines(
       Args.jarUsage(
           getClass(), "[-propowner] [-recovery] [-config <neo4j.properties>] <storedir>"),
       "WHERE:   <storedir>         is the path to the store to check",
       "         -recovery          to perform recovery on the store before checking",
       "         <neo4j.properties> is the location of an optional properties file",
       "                            containing tuning parameters for the consistency check");
 }
Example #6
0
 private void startRemote(Args args, CtrlCHandler signalHandler) {
   try {
     String host = args.get(ARG_HOST, "localhost");
     int port = args.getNumber(ARG_PORT, SimpleAppServer.DEFAULT_PORT).intValue();
     String name = args.get(ARG_NAME, SimpleAppServer.DEFAULT_NAME);
     ShellClient client =
         ShellLobby.newClient(
             RmiLocation.location(host, port, name),
             getSessionVariablesFromArgs(args),
             signalHandler);
     if (!isCommandLine(args)) {
       System.out.println(
           "NOTE: Remote Neo4j graph database service '" + name + "' at port " + port);
     }
     grabPromptOrJustExecuteCommand(client, args);
   } catch (Exception e) {
     handleException(e, args);
   }
 }
Example #7
0
 private static void handleException(Exception e, Args args) {
   String message =
       e.getCause() instanceof ConnectException ? "Connection refused" : e.getMessage();
   System.err.println("ERROR (-v for expanded information):\n\t" + message);
   if (args.has("v")) {
     e.printStackTrace(System.err);
   }
   System.err.println();
   printUsage();
   System.exit(1);
 }
Example #8
0
  static Map<String, Serializable> getSessionVariablesFromArgs(Args args)
      throws RemoteException, ShellException {
    String profile = args.get("profile", null);
    Map<String, Serializable> session = new HashMap<String, Serializable>();
    if (profile != null) {
      applyProfileFile(new File(profile), session);
    }

    for (Map.Entry<String, String> entry : args.asMap().entrySet()) {
      String key = entry.getKey();
      if (key.startsWith("D")) {
        key = key.substring(1);
        session.put(key, entry.getValue());
      }
    }
    if (isCommandLine(args)) {
      session.put("quiet", true);
    }
    return session;
  }
Example #9
0
  private void startLocal(Args args, CtrlCHandler signalHandler) {
    String dbPath = args.get(ARG_PATH, null);
    if (dbPath == null) {
      System.err.println(
          "ERROR: To start a local Neo4j service and a "
              + "shell client on top of that you need to supply a path to a "
              + "Neo4j store or just a new path where a new store will "
              + "be created if it doesn't exist. -"
              + ARG_PATH
              + " /my/path/here");
      return;
    }

    try {
      boolean readOnly = args.getBoolean(ARG_READONLY, false, true);
      tryStartLocalServerAndClient(dbPath, readOnly, args, signalHandler);
    } catch (Exception e) {
      handleException(e, args);
    }
    System.exit(0);
  }
Example #10
0
 private String determineStoreDirectory(Args arguments) throws ToolFailureException {
   List<String> unprefixedArguments = arguments.orphans();
   if (unprefixedArguments.size() != 1) {
     throw new ToolFailureException(usage());
   }
   String storeDir = unprefixedArguments.get(0);
   if (!new File(storeDir).isDirectory()) {
     throw new ToolFailureException(
         lines(String.format("'%s' is not a directory", storeDir)) + usage());
   }
   return storeDir;
 }
Example #11
0
  void run(String... args) throws ToolFailureException {
    Args arguments = Args.withFlags(RECOVERY, PROP_OWNER).parse(args);
    String storeDir = determineStoreDirectory(arguments);
    Config tuningConfiguration = readTuningConfiguration(storeDir, arguments);

    attemptRecoveryOrCheckStateOfLogicalLogs(arguments, storeDir);

    StringLogger logger = StringLogger.SYSTEM;
    try {
      consistencyCheckService.runFullConsistencyCheck(
          storeDir, tuningConfiguration, ProgressMonitorFactory.textual(System.err), logger);
    } catch (ConsistencyCheckIncompleteException e) {
      throw new ToolFailureException("Check aborted due to exception", e);
    } finally {
      logger.flush();
    }
  }
Example #12
0
  private Config readTuningConfiguration(String storeDir, Args arguments)
      throws ToolFailureException {
    Map<String, String> specifiedProperties = stringMap();

    String propertyFilePath = arguments.get(CONFIG, null);
    if (propertyFilePath != null) {
      File propertyFile = new File(propertyFilePath);
      try {
        specifiedProperties = MapUtil.load(propertyFile);
      } catch (IOException e) {
        throw new ToolFailureException(
            String.format("Could not read configuration properties file [%s]", propertyFilePath),
            e);
      }
    }
    specifiedProperties.put(GraphDatabaseSettings.store_dir.name(), storeDir);
    return new Config(
        specifiedProperties, GraphDatabaseSettings.class, ConsistencyCheckSettings.class);
  }
Example #13
0
  private void attemptRecoveryOrCheckStateOfLogicalLogs(Args arguments, String storeDir) {
    if (arguments.getBoolean(RECOVERY, false, true)) {
      dbFactory.newEmbeddedDatabase(storeDir).shutdown();
    } else {
      try {
        if (recoveryChecker.recoveryNeededAt(new File(storeDir))) {
          systemError.print(
              lines(
                  "Active logical log detected, this might be a source of inconsistencies.",
                  "Consider allowing the database to recover before running the consistency check.",
                  "Consistency checking will continue, abort if you wish to perform recovery first.",
                  "To perform recovery before checking consistency, use the '--recovery' flag."));

          exitHandle.pull();
        }
      } catch (IOException e) {
        systemError.printf(
            "Failure when checking for recovery state: '%s', continuing as normal.%n", e);
      }
    }
  }
  public static Map<String, Collection<String>> loadAggregations(Args arguments)
      throws IOException {
    String file = arguments.get(KEY_AGGREGATIONS_FILE, null);
    if (file == null) {
      return null;
    }

    Map<String, Collection<String>> result = new HashMap<String, Collection<String>>();
    Properties properties = new Properties();
    properties.load(new FileInputStream(new File(file)));
    for (Object pattern : properties.keySet()) {
      String name = properties.getProperty((String) pattern);
      Collection<String> patterns = result.get(name);
      if (patterns == null) {
        patterns = new ArrayList<String>();
        result.put((String) name, patterns);
      }
      patterns.add((String) pattern);
    }
    return result;
  }
Example #15
0
  private void tryStartLocalServerAndClient(
      String dbPath, boolean readOnly, Args args, CtrlCHandler signalHandler) throws Exception {
    String configFile = args.get(ARG_CONFIG, null);
    final GraphDatabaseShellServer server =
        new GraphDatabaseShellServer(dbPath, readOnly, configFile);
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                shutdownIfNecessary(server);
              }
            });

    if (!isCommandLine(args)) {
      System.out.println("NOTE: Local Neo4j graph database service at '" + dbPath + "'");
    }
    ShellClient client =
        ShellLobby.newClient(server, getSessionVariablesFromArgs(args), signalHandler);
    grabPromptOrJustExecuteCommand(client, args);

    shutdownIfNecessary(server);
  }
  public static WeightedPattern[] loadFilters(Args arguments) throws IOException {
    String filterFile = arguments.get(KEY_BENCH_FILTER_FILE, "default-bench-filter");
    if (filterFile == null || !new File(filterFile).exists()) {
      return null;
    }

    Collection<WeightedPattern> result = new ArrayList<WeightedPattern>();
    BufferedReader reader = new BufferedReader(new FileReader(filterFile));
    String line = null;
    while ((line = reader.readLine()) != null) {
      line = line.trim();
      if (line.length() == 0 || line.startsWith("#")) {
        continue;
      }

      char firstChar = line.charAt(0);
      boolean type = firstChar != '-';
      if (line.startsWith("+") || line.startsWith("-")) {
        line = line.substring(1);
      }
      result.add(new WeightedPattern(line, type));
    }
    return result.toArray(new WeightedPattern[result.size()]);
  }
Example #17
0
 public static Printer getPrinter(Args args) {
   boolean toFile = args.getBoolean("tofile", false, true).booleanValue();
   return toFile ? new FilePrinter() : SYSTEM_OUT_PRINTER;
 }
Example #18
0
 private static boolean isCommandLine(Args args) {
   return args.get(ARG_COMMAND, null) != null || args.get(ARG_FILE, null) != null;
 }
Example #19
0
 public static TimeZone parseTimeZoneConfig(Args arguments) {
   return getTimeZone(arguments.get("timezone", DEFAULT_TIME_ZONE.getID()));
 }
 public static File getResultsFile(Args arguments) {
   return new File(arguments.get(KEY_RESULTS_FILE, "results"));
 }