예제 #1
0
  /**
   * Perform the 'alias' command.
   *
   * @param session JSwat session on which to operate.
   * @param args Tokenized string of command arguments.
   * @param out Output to write messages to.
   */
  public void perform(Session session, CommandArguments args, Log out) {
    CommandManager cmdman = (CommandManager) session.getManager(CommandManager.class);
    if (args.hasMoreTokens()) {
      String aliasName = args.nextToken();
      if (args.hasMoreTokens()) {
        // Grab rest of string as alias value.
        // Be sure to preserve the quotes and escapes.
        args.returnAsIs(true);
        String alias = args.rest().trim();
        if (alias.charAt(0) == '"' && alias.charAt(alias.length() - 1) == '"') {
          // Must remove the enclosing quotes because the
          // command parser doesn't handle that.
          alias = alias.substring(1, alias.length() - 1);
        }

        // Add the command alias to the list.
        cmdman.createAlias(aliasName, alias);
        out.writeln(Bundle.getString("alias.defined") + ' ' + aliasName);
      } else {
        // One argument, show the alias definition.
        String alias = cmdman.getAlias(aliasName);
        if (alias == null) {
          throw new CommandException(Bundle.getString("alias.undefined") + ' ' + aliasName);
        } else {
          out.writeln("alias " + aliasName + ' ' + alias);
        }
      }
    } else {
      // No arguments, show the defined aliases.
      cmdman.listAliases();
    }
  } // perform
예제 #2
0
  /**
   * Perform the 'history' command.
   *
   * @param session JSwat session on which to operate.
   * @param args Tokenized string of command arguments.
   * @param out Output to write messages to.
   */
  public void perform(Session session, CommandArguments args, Log out) {
    CommandManager cmdman = (CommandManager) session.getManager(CommandManager.class);

    if (args.hasMoreTokens()) {
      String arg = args.nextToken();
      try {
        int size = Integer.parseInt(arg);
        cmdman.setHistorySize(size);
        out.writeln(Bundle.getString("history.sizeSet"));
      } catch (NumberFormatException nfe) {
        throw new CommandException(Bundle.getString("history.invalidSize"));
      } catch (IllegalArgumentException iae) {
        throw new CommandException(Bundle.getString("history.invalidRange"));
      }
    } else {
      cmdman.displayHistory();
    }
  } // perform
예제 #3
0
  /**
   * Perform the 'capture' command.
   *
   * @param session debugging session on which to operate.
   * @param args Tokenized string of command arguments.
   * @param out Output to write messages to.
   */
  public void perform(Session session, CommandArguments args, Log out) {
    // Get the current capture settings.
    boolean captureStdout = Capture.isOutputEnabled(session);
    boolean captureFile = Capture.isFileEnabled(session);
    String filename = Capture.getFilename(session);

    if (args.hasMoreTokens()) {
      // Modify the capture settings.

      while (args.hasMoreTokens()) {
        String token = args.nextToken();
        boolean enable = false;
        boolean disable = false;
        if (token.startsWith("+")) {
          enable = true;
          token = token.substring(1);
        } else if (token.startsWith("-")) {
          disable = true;
          token = token.substring(1);
        }
        if (token.equals("stdout")) {
          captureStdout = enable || !disable && !captureStdout;
        } else if (token.equals("file")) {
          captureFile = enable || !disable && !captureFile;
          if (captureFile) {
            if (args.hasMoreTokens()) {
              filename = args.nextToken();
            } else {
              throw new MissingArgumentsException(Bundle.getString("capture.missingFilename"));
            }
          }
        } else {
          throw new CommandException(Bundle.getString("capture.unknownType") + ' ' + token);
        }
      }

      Capture.setOutputEnabled(captureStdout, session);

      File file = null;
      if (captureFile) {
        // Create the file and check that it exists.
        file = new File(filename);
        if (file.exists() && !file.canWrite()) {
          throw new CommandException(Bundle.getString("capture.readOnlyFile"));
        }
      }

      // Enable or disable the file capture.
      try {
        Capture.setFileEnabled(captureFile, file, session);
      } catch (FileNotFoundException fnfe) {
        throw new CommandException(Bundle.getString("capture.fileNotFound"));
      } catch (IOException ioe) {
        throw new CommandException(ioe);
      }
    } else {
      // Display the capture settings.
      StringBuffer buf = new StringBuffer();
      if (captureStdout) {
        buf.append(Bundle.getString("capture.stdout"));
        buf.append('\n');
      }
      if (captureFile) {
        buf.append(Bundle.getString("capture.file") + ' ' + filename);
        buf.append('\n');
      }
      if (buf.length() > 0) {
        out.write(buf.toString());
      } else {
        out.writeln(Bundle.getString("capture.none"));
      }
    }
  } // perform
예제 #4
0
  /**
   * Perform the 'hotswap' command.
   *
   * @param session JSwat session on which to operate.
   * @param args Tokenized string of command arguments.
   * @param out Output to write messages to.
   */
  public void perform(Session session, CommandArguments args, Log out) {
    if (!session.isActive()) {
      throw new CommandException(Bundle.getString("noActiveSession"));
    }

    if (!args.hasMoreTokens()) {
      throw new MissingArgumentsException();
    }

    // Name of class is required; name of .class file is optional.
    String cname = args.nextToken();
    String cfile = null;
    if (args.hasMoreTokens()) {
      cfile = args.nextToken();
    }

    // Find the ReferenceType for this class.
    VirtualMachine vm = session.getConnection().getVM();
    List classes = vm.classesByName(cname);
    if (classes.size() == 0) {
      throw new CommandException(Bundle.getString("hotswap.noSuchClass"));
    }
    ReferenceType clazz = (ReferenceType) classes.get(0);

    // Did the user give us a .class file?
    InputStream is = null;
    if (cfile == null) {
      // Try to find the .class file.
      PathManager pathman = (PathManager) session.getManager(PathManager.class);
      SourceSource src = pathman.mapClass(clazz);
      if (src == null) {
        throw new CommandException(Bundle.getString("hotswap.fileNotFound"));
      }
      is = src.getInputStream();
    } else {
      // A filename was given, just open it.
      try {
        is = new FileInputStream(cfile);
      } catch (FileNotFoundException fnfe) {
        throw new CommandException(Bundle.getString("hotswap.fileNotFound"));
      }
    }

    // Do the actual hotswap operation.
    try {
      Classes.hotswap(clazz, is, vm);
      out.writeln(Bundle.getString("hotswap.success"));
    } catch (UnsupportedOperationException uoe) {
      if (!vm.canRedefineClasses()) {
        throw new CommandException(Bundle.getString("hotswap.noHotSwap"));
      } else if (!vm.canAddMethod()) {
        throw new CommandException(Bundle.getString("hotswap.noAddMethod"));
      } else if (!vm.canUnrestrictedlyRedefineClasses()) {
        throw new CommandException(Bundle.getString("hotswap.noUnrestricted"));
      } else {
        throw new CommandException(Bundle.getString("hotswap.unsupported"));
      }
    } catch (IOException ioe) {
      throw new CommandException(Bundle.getString("hotswap.errorReadingFile") + ' ' + ioe);
    } catch (NoClassDefFoundError ncdfe) {
      throw new CommandException(Bundle.getString("hotswap.wrongClass"));
    } catch (VerifyError ve) {
      throw new CommandException(Bundle.getString("hotswap.verifyError") + ' ' + ve);
    } catch (UnsupportedClassVersionError ucve) {
      throw new CommandException(Bundle.getString("hotswap.versionError") + ' ' + ucve);
    } catch (ClassFormatError cfe) {
      throw new CommandException(Bundle.getString("hotswap.formatError") + ' ' + cfe);
    } catch (ClassCircularityError cce) {
      throw new CommandException(Bundle.getString("hotswap.circularity") + ' ' + cce);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
        }
      }
    }
  } // perform