remove dependency ShellImpl from JobController

pull/1171/head
gongdewei 5 years ago
parent a1d4a39a11
commit 5acfe6ada4

@ -5,23 +5,23 @@ import com.taobao.arthas.core.shell.ShellServer;
import com.taobao.arthas.core.shell.cli.CliToken;
import com.taobao.arthas.core.shell.cli.CliTokens;
import com.taobao.arthas.core.shell.future.Future;
import com.taobao.arthas.core.shell.handlers.shell.CloseHandler;
import com.taobao.arthas.core.shell.handlers.shell.CommandManagerCompletionHandler;
import com.taobao.arthas.core.shell.handlers.shell.FutureHandler;
import com.taobao.arthas.core.shell.handlers.shell.InterruptHandler;
import com.taobao.arthas.core.shell.handlers.shell.ShellLineHandler;
import com.taobao.arthas.core.shell.handlers.shell.SuspendHandler;
import com.taobao.arthas.core.shell.handlers.shell.*;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.shell.session.impl.SessionImpl;
import com.taobao.arthas.core.shell.system.ExecStatus;
import com.taobao.arthas.core.shell.system.Job;
import com.taobao.arthas.core.shell.system.JobController;
import com.taobao.arthas.core.shell.system.JobListener;
import com.taobao.arthas.core.shell.system.impl.InternalCommandManager;
import com.taobao.arthas.core.shell.system.impl.JobControllerImpl;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.shell.term.impl.TermImpl;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.FileUtils;
import com.taobao.arthas.core.util.LogUtil;
import com.taobao.middleware.logger.Logger;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.util.Date;
import java.util.List;
@ -78,7 +78,7 @@ public class ShellImpl implements Shell {
@Override
public synchronized Job createJob(List<CliToken> args) {
Job job = jobController.createJob(commandManager, args, this);
Job job = jobController.createJob(commandManager, args, session, new ShellJobHandler(this), term);
return job;
}
@ -179,4 +179,53 @@ public class ShellImpl implements Shell {
public Job getForegroundJob() {
return currentForegroundJob;
}
private class ShellJobHandler implements JobListener {
ShellImpl shell;
public ShellJobHandler(ShellImpl shell) {
this.shell = shell;
}
@Override
public void onForeground(Job job) {
shell.setForegroundJob(job);
//reset stdin handler to job's origin handler
//shell.term().stdinHandler(job.process().getStdinHandler());
}
@Override
public void onBackground(Job job) {
resetAndReadLine();
}
@Override
public void onTerminated(Job job) {
if (!job.isRunInBackground()){
resetAndReadLine();
}
// save command history
Term term = shell.term();
if (term instanceof TermImpl) {
List<int[]> history = ((TermImpl) term).getReadline().getHistory();
FileUtils.saveCommandHistory(history, new File(Constants.CMD_HISTORY_FILE));
}
}
@Override
public void onSuspend(Job job) {
if (!job.isRunInBackground()){
resetAndReadLine();
}
}
private void resetAndReadLine() {
//reset stdin handler to echo handler
//shell.term().stdinHandler(null);
shell.setForegroundJob(null);
shell.readline();
}
}
}

@ -2,8 +2,9 @@ package com.taobao.arthas.core.shell.system;
import com.taobao.arthas.core.shell.cli.CliToken;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.impl.ShellImpl;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.shell.system.impl.InternalCommandManager;
import com.taobao.arthas.core.shell.term.Term;
import java.util.List;
import java.util.Set;
@ -33,10 +34,12 @@ public interface JobController {
*
* @param commandManager command manager
* @param tokens the command tokens
* @param shell the current shell
* @param session the current session
* @param jobHandler job event handler
* @param term telnet term
* @return the created job
*/
Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, ShellImpl shell);
Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, Session session, JobListener jobHandler, Term term);
/**
* Close the controller and terminate all the underlying jobs, a closed controller does not accept anymore jobs.

@ -1,21 +1,18 @@
package com.taobao.arthas.core.shell.system.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import com.taobao.arthas.core.GlobalOptions;
import com.taobao.arthas.core.shell.cli.CliToken;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.impl.ShellImpl;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.shell.system.Job;
import com.taobao.arthas.core.shell.system.JobListener;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.util.LogUtil;
import com.taobao.middleware.logger.Logger;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Job Controller
*
@ -53,8 +50,8 @@ public class GlobalJobControllerImpl extends JobControllerImpl {
}
@Override
public Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, ShellImpl shell) {
final Job job = super.createJob(commandManager, tokens, shell);
public Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, Session session, JobListener jobHandler, Term term) {
final Job job = super.createJob(commandManager, tokens, session, jobHandler, term);
/*
* job

@ -8,30 +8,20 @@ import com.taobao.arthas.core.shell.command.internal.StdoutHandler;
import com.taobao.arthas.core.shell.command.internal.TermHandler;
import com.taobao.arthas.core.shell.future.Future;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.impl.ShellImpl;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.shell.system.Job;
import com.taobao.arthas.core.shell.system.JobController;
import com.taobao.arthas.core.shell.system.JobListener;
import com.taobao.arthas.core.shell.system.Process;
import com.taobao.arthas.core.shell.system.impl.ProcessImpl.ProcessOutput;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.shell.term.impl.TermImpl;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.FileUtils;
import com.taobao.arthas.core.util.TokenUtils;
import io.termd.core.function.Function;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
@ -61,16 +51,16 @@ public class JobControllerImpl implements JobController {
}
@Override
public Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, ShellImpl shell) {
public Job createJob(InternalCommandManager commandManager, List<CliToken> tokens, Session session, JobListener jobHandler, Term term) {
int jobId = idGenerator.incrementAndGet();
StringBuilder line = new StringBuilder();
for (CliToken arg : tokens) {
line.append(arg.raw());
}
boolean runInBackground = runInBackground(tokens);
Process process = createProcess(tokens, commandManager, jobId, shell.term());
Process process = createProcess(tokens, commandManager, jobId, term);
process.setJobId(jobId);
JobImpl job = new JobImpl(jobId, this, process, line.toString(), runInBackground, shell.session(), new ShellJobHandler(shell));
JobImpl job = new JobImpl(jobId, this, process, line.toString(), runInBackground, session, jobHandler);
jobs.put(jobId, job);
return job;
}
@ -232,53 +222,4 @@ public class JobControllerImpl implements JobController {
close(null);
}
private class ShellJobHandler implements JobListener {
ShellImpl shell;
public ShellJobHandler(ShellImpl shell) {
this.shell = shell;
}
@Override
public void onForeground(Job job) {
shell.setForegroundJob(job);
//reset stdin handler to job's origin handler
//shell.term().stdinHandler(job.process().getStdinHandler());
}
@Override
public void onBackground(Job job) {
resetAndReadLine();
}
@Override
public void onTerminated(Job job) {
if (!job.isRunInBackground()){
resetAndReadLine();
}
// save command history
Term term = shell.term();
if (term instanceof TermImpl) {
List<int[]> history = ((TermImpl) term).getReadline().getHistory();
FileUtils.saveCommandHistory(history, new File(Constants.CMD_HISTORY_FILE));
}
}
@Override
public void onSuspend(Job job) {
if (!job.isRunInBackground()){
resetAndReadLine();
}
}
private void resetAndReadLine() {
//reset stdin handler to echo handler
//shell.term().stdinHandler(null);
shell.setForegroundJob(null);
shell.readline();
}
}
}

Loading…
Cancel
Save