private ProcessExecutionResults startNode(File basedir) { if (log.isDebugEnabled()) { log.debug("Starting node at " + basedir); } File binDir = new File(basedir, "bin"); File startScript; SystemInfo systemInfo = SystemInfoFactory.createSystemInfo(); ProcessExecution startScriptExe; if (systemInfo.getOperatingSystemType() == OperatingSystemType.WINDOWS) { startScript = new File(binDir, "cassandra.bat"); startScriptExe = createProcessExecution(null, startScript); } else { startScript = new File(binDir, "cassandra"); startScriptExe = createProcessExecution(null, startScript); startScriptExe.addArguments(Arrays.asList("-p", "cassandra.pid")); } startScriptExe.setWaitForCompletion(0); ProcessExecutionResults results = systemInfo.executeProcess(startScriptExe); if (log.isDebugEnabled()) { log.debug(startScript + " returned with exit code [" + results.getExitCode() + "]"); } return results; }
private PluginContext createPluginContext(String pluginName) { SystemInfo sysInfo = SystemInfoFactory.createSystemInfo(); File dataDir = new File(pluginContainerConfiguration.getDataDirectory(), pluginName); File tmpDir = pluginContainerConfiguration.getTemporaryDirectory(); String pcName = pluginContainerConfiguration.getContainerName(); PluginContext context = new PluginContext(pluginName, sysInfo, tmpDir, dataDir, pcName); return context; }
/** * Creates a ProcessExecution for the specified file for the current platform. If the current * platform is Windows and the file name ends with ".bat" or ".cmd", the file will be assumed to * be a Windows batch file, and the process execution will be initialized accordingly. Note, if * the file is a UNIX script, its first line must be a valid #! reference to a script interpreter * (e.g. #!/bin/sh), otherwise it will fail to execute. The returned ProcessExecution will have a * non-null arguments list, an environment map that is a copy of the current process's * environment, and a working directory set to its executable's parent directory. * * @param prefix a prefix command line that should be prepended to the executable's command line * (e.g. "/usr/bin/nohup /usr/bin/sudo -u jboss -g jboss"). any files on the command line * should be absolute paths. if null, no prefix command line will be prepended * @param file an executable or a batch file * @return a process execution */ public static ProcessExecution createProcessExecution(String prefix, File file) { SystemInfo systemInfo = SystemInfoFactory.createSystemInfo(); ProcessExecution processExecution; List<String> prefixArgs; if (prefix != null) { // TODO (ips, 04/27/10): Ideally, the prefix should be a String[], not a String. prefixArgs = Arrays.asList(prefix.split("[ \t]+")); } else { prefixArgs = Collections.emptyList(); } String executable; List<String> args = new ArrayList<String>(); if (systemInfo.getOperatingSystemType() == OperatingSystemType.WINDOWS && isBatchFile(file)) { // Windows batch files cannot be executed directly - they must be passed as arguments to // cmd.exe, e.g. // "C:\Windows\System32\cmd.exe /c C:\opt\jboss-as\bin\run.bat". executable = getCmdExeFile().getPath(); args.add("/c"); args.addAll(prefixArgs); args.add(file.getPath()); } else { // UNIX if (prefixArgs.isEmpty()) { executable = file.getPath(); } else { executable = prefixArgs.get(0); if (prefixArgs.size() > 1) { args.addAll(prefixArgs.subList(1, prefixArgs.size())); } args.add(file.getPath()); } } processExecution = new ProcessExecution(executable); processExecution.setArguments(args); // Start out with a copy of our own environment, since Windows needs // certain system environment variables to find DLLs, etc., and even // on UNIX, many scripts will require certain environment variables // (PATH, JAVA_HOME, etc.). // TODO (ips, 04/27/12): We probably should not just do this by default. Map<String, String> envVars = new LinkedHashMap<String, String>(System.getenv()); processExecution.setEnvironmentVariables(envVars); // Many scripts (e.g. JBossAS scripts) assume their working directory is the directory // containing the script. processExecution.setWorkingDirectory(file.getParent()); return processExecution; }
private void processArguments(AgentMain agent, String[] args) { PrintWriter out = agent.getOut(); String sopts = "deop::sv"; LongOpt[] lopts = { new LongOpt("disable", LongOpt.NO_ARGUMENT, null, 'd'), new LongOpt("enable", LongOpt.NO_ARGUMENT, null, 'e'), new LongOpt("os", LongOpt.NO_ARGUMENT, null, 'o'), new LongOpt("ps", LongOpt.OPTIONAL_ARGUMENT, null, 'p'), new LongOpt("shutdown", LongOpt.NO_ARGUMENT, null, 's'), new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v') }; Getopt getopt = new Getopt("native", args, sopts, lopts); int code; while ((code = getopt.getopt()) != -1) { switch (code) { case ':': case '?': case 1: { out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax())); break; } case 'd': { SystemInfoFactory.disableNativeSystemInfo(); out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_DISABLE_DONE)); break; } case 'e': { SystemInfoFactory.enableNativeSystemInfo(); out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_ENABLE_DONE)); break; } case 'o': { SystemInfo sysInfo = SystemInfoFactory.createSystemInfo(); // careful - I chose to only output things that I know the non-native Java sysinfo can // support out.println( MSG.getMsg( AgentI18NResourceKeys.NATIVE_OS_OUTPUT, sysInfo.getOperatingSystemName(), sysInfo.getOperatingSystemVersion(), sysInfo.getHostname())); break; } case 'p': { SystemInfo sysInfo = SystemInfoFactory.createSystemInfo(); String verboseOpt = getopt.getOptarg(); boolean verbose = (verboseOpt != null) && verboseOpt.equals(MSG.getMsg(AgentI18NResourceKeys.NATIVE_VERBOSE)); try { List<ProcessInfo> processes = sysInfo.getAllProcesses(); if (verbose) { out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_PS_OUTPUT_VERBOSE_HEADER)); } else { out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_PS_OUTPUT_SHORT_HEADER)); } for (ProcessInfo p : processes) { if (verbose) { out.println( MSG.getMsg( AgentI18NResourceKeys.NATIVE_PS_OUTPUT_VERBOSE, p.getPid(), p.getParentPid(), p.getBaseName(), Arrays.toString(p.getCommandLine()))); } else { out.println( MSG.getMsg( AgentI18NResourceKeys.NATIVE_PS_OUTPUT_SHORT, p.getPid(), p.getName())); } } } catch (Exception e) { out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_NOT_SUPPORTED)); } break; } case 's': { if (!agent.isStarted()) { SystemInfoFactory.shutdown(); SystemInfoFactory.disableNativeSystemInfo(); out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_SHUTDOWN_DONE)); } else { out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_SHUTDOWN_FAILED_AGENT_STARTED)); } break; } case 'v': { out.println(SystemInfoFactory.getNativeSystemInfoVersion()); break; } } } if ((getopt.getOptind() + 1) < args.length) { out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax())); } return; }