⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logininfo.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.startit;

import java.io.*;
import java.util.*;
import java.lang.String;

public class LoginInfo {

  public static final int UNDEFINED = 2300;
  public static final int LOCAL     = 2301;
  public static final int RSH       = 2302;
  public static final int SSH       = 2303;

  public int remoteShellCommand;
  public String hostname;	  // may be NULL if remoteShellCommand == LOCAL
  public int hostType; 		  // may be NULL if remoteShellCommand == LOCAL
  public String username;	  // may be NULL if remoteShellCommand == LOCAL
  public String password;	  // may be NULL if remoteShellCommand == LOCAL

  // null constructor returns a local login
  public LoginInfo() {
    remoteShellCommand = LOCAL;
    hostType = Environment.getHostType();
  }

  public LoginInfo(String h, String u, String p, int r, int t) {
    remoteShellCommand = r;
    if (remoteShellCommand != LOCAL) {
      hostType = t;
      hostname = fixupHostname(h);
      username = "".equals(u) ? null : u; // empty string should be null
      password = p;

      if (username == null) {
	username = Environment.getUserName();
      }
    }
    else {
      hostType = Environment.getHostType();
    }
  }

  public static String fixupHostname(String h) {
    // set a reasonable default for an empty host
    if (h == null || h.equals("")) {
    	return "localhost";
    }
    return h;
  }

  /** Gives you a shell (a String you can exec) that you can send commands to.  
   * The command depends on what machine Startit is running on, as well as
   * what OS the target machine is (or if the shell is for the local machine).
   * (e.g. "rsh HOST exec sh", or "sh" if local, or "cmd.exe" if windows, etc.)
   *
   * @return a String suitable as an arg to exec(String cmd)
   */
  public String getLocalShellCommand() {
    if (remoteShellCommand != LOCAL && hostname == null) {
      StartitInfo.warningPrint("hostname required for remote execution");
      return null;
    }

    switch (Environment.getHostType()) {

    case Environment.UNKNOWN:
      System.err.println("ERROR: can't start app from unknown OS type");
      return null;
    

    case Environment.UNIX:
      switch(remoteShellCommand) {
      case RSH:
	// rsh %h %u?{-l %u}
	if (username == null) {
	  return "rsh " + hostname + " exec sh";
	}
	else {
	  return "rsh " + hostname + " -l " + username + " exec sh";
	}
      case LOCAL:
	return "sh";
      case SSH:
	if (username == null) {
	  return "ssh -o BatchMode=yes " + hostname + " exec sh";
	}
	else {
	  return "ssh -o BatchMode=yes " + hostname + " -l " + username + " exec sh";
	}
      default: // shouldn't fall through to here
	System.err.println("ERROR: unknown shell");
	return null;
      }
      
    case Environment.WINDOWS_ME:
    case Environment.WINDOWS_9x:
    case Environment.WINDOWS_NT:
      switch(remoteShellCommand) {
      case RSH:
	System.err.println("ERROR: can't use rsh from a Windows machine");
	return null;
      case LOCAL:
	if (Environment.getHostType() == Environment.WINDOWS_NT) {
	  return "cmd /Y";
	}
	else {
	  return "COMMAND.COM";
	}
      case SSH:
	if (username == null) {
	  StartitInfo.warningPrint("Username required for ssh");
	  return null;
	}
	//return "plink -batch -ssh -t -l " + username + " " + hostname + " exec sh";
	return "plink -batch -ssh -l " + username + " " + hostname + " exec sh";
      default: // shouldn't fall through to here
	System.err.println("ERROR: unknown shell");
	return null;
      }
    }

    // shouldn't fall through to here
    System.err.println("INTERNAL ERROR: LoginInfo:getLocalShellCommand");
    return null;
  }

  public String getRemoteLaunchCommand(String cmd) {
    switch(hostType) {
    case Environment.UNKNOWN:
      System.err.println("ERROR: can't start app on unknown OS type");
      return null;

    case Environment.UNIX:
      return "exec " + cmd;

    case Environment.WINDOWS_NT:
      return cmd;
      //   (used to be <"cmd /C " + cmd> - but why???

    case Environment.WINDOWS_ME:
    case Environment.WINDOWS_9x:
      return "start /m /w " + cmd;
      //   /m : Minimized
      //   /w : Waits untils the launched program is dead

    default: // shouldn't fall through to here
      return null;
    }
  }

  public String getRemoteExitCommand() {
    return getExitCommand(hostType);
  }

  static public String getLocalExitCommand() {
    return getExitCommand(Environment.getHostType());
  }

  // Necessary for windows machines where we can't use "exec" on the
  // last command, so we need to feed an explicit "exit" to the DOS
  // shell, which is still around
  static public String getExitCommand(int htype) {
    switch(htype) {
    case Environment.WINDOWS_ME:
    case Environment.WINDOWS_9x:
    case Environment.WINDOWS_NT:
      return "exit";

    default: // no other host type needs an explicit exit command
      // HERE  do it to be safe
      return "exit";
    }
  }

  public String getRemoteSetenvCommand(String var, String value) {
    if (var == null || value == null) return null;
    switch(hostType) {
    case Environment.UNIX:
      if (value == "") return "export unset " + var;
      else             return "export " + var + "=\"" + value + "\"";

    case Environment.WINDOWS_ME:
    case Environment.WINDOWS_9x:
    case Environment.WINDOWS_NT:
      return "set " + var + "=" + value;
    }
    return null;
  }

  public String getRemoteCdCommand(String dir) {
    if (dir == null) return null;
    switch(hostType) {
    case Environment.WINDOWS_ME:
    case Environment.WINDOWS_9x:
    case Environment.WINDOWS_NT:
      return "cd /d " + dir; // on windows, the /d allows you to specify drive (e.g. "d:/user")
    }
    return "cd " + dir;
  }

  public int getRemoteShellCommand() {
    return remoteShellCommand;
  }

  static public int shellFromString(String s) {
    s = s.toLowerCase();
    if (s.equals("local")) return LOCAL;
    else if (s.equals("ssh")) return SSH;
    else if (s.equals("rsh")) {
      if (Environment.isUnix()) {
	return RSH;
      }
      else {
	System.err.println("WARNING: rsh not supported on non-UNIX machines");
      }
    }
    
    return UNDEFINED;
  }

  /** Get host from a string like "user@host".  If there's no "@" return
   * the whole string.
   */
  static public String getHost(String login) {
    int at = login.indexOf('@');
    if (at < 0) return login;
    return login.substring(at+1);
  }

  /** Get user from a string like "user@host".  If there's no "@" return
   * the empty string.
   */
  static public String getUser(String login) {
    int at = login.indexOf('@');
    if (at < 0) return "";
    return login.substring(0, at);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -