import java.io.IOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.tizen.common.TizenPlatformConstants;
import org.tizen.common.core.application.InstallPathConfig;
import org.tizen.common.sdb.command.message.CommandErrorException;
import org.tizen.sdblib.exception.ShellCommandUnresponsiveException;
import org.tizen.sdblib.exception.TimeoutException;
-
public class SdbCommand {
+ protected final Logger logger = LoggerFactory.getLogger(getClass());
+
public static final int DEFAULT_TIMEOUT = 60000;
+ private final String SEPARATOR = System.getProperty("line.separator");
+
private IDevice device = null;
private ITizenConsoleManager console = null;
private String endLine = "";
private String commandOutput = "";
private CommandOutputReceiver receiver = null;
private String sdbPath;
-
+ private boolean showCommand = false;
+
public SdbCommand(IDevice device) {
this(device, null);
}
-
+
public SdbCommand(IDevice device, ITizenConsoleManager console) {
this(device, console, new CommandOutputReceiver(console));
}
-
+
public SdbCommand(IDevice device, ITizenConsoleManager console, CommandOutputReceiver receiver) {
this.device = device;
this.console = console;
this.receiver = receiver;
-
+
sdbPath = InstallPathConfig.getSDBPath();
}
-
- public void runCommand(String command ) throws Exception {
- if (console != null) {
- console.println("$ " + command);
- }
+
+ public void runCommand(String command) throws Exception {
+ print("$ " + command);
device.executeShellCommand(command, receiver);
this.endLine = receiver.getEndLine();
this.commandOutput = receiver.getCommandOutput();
}
-
+
public void runLaunchCommand(String command) throws IOException {
runLaunchCommand(command, true);
}
-
+
private Process runHostCommandImpl(String command, String sdbCommand, boolean isBlock, IShellOutputReceiver userReceiver, boolean isLaunch) throws IOException {
+ print("$ " + sdbCommand);
+
Process process = null;
-
- if (console != null) {
- console.println(String.format("$ %s", sdbCommand));
- }
-
IShellOutputReceiver r = (userReceiver != null) ? userReceiver : this.receiver;
if ( isBlock ) {
- if (isLaunch) {
+ if ( isLaunch ) {
device.executeLaunchCommand( command, r );
} else {
device.executeHostCommand( command, r );
}
- if (userReceiver == null) {
+ if ( userReceiver == null ) {
this.endLine = this.receiver.getEndLine();
this.commandOutput = this.receiver.getCommandOutput();
}
- }
- else {
+ } else {
process = Runtime.getRuntime().exec(sdbCommand);
}
-
+
return process;
}
-
+
public Process runLaunchCommand(String command, boolean isBlock) throws IOException {
final String sdbCommand = String.format("%s -s %s launch %s", sdbPath, device.getSerialNumber(), command);
-
return runHostCommandImpl(command, sdbCommand, isBlock, null, true);
}
-
+
public Process runHostCommand(String command, boolean isBlock) throws IOException {
return runHostCommand(command, isBlock, null);
}
-
+
public Process runHostCommand(String command, boolean isBlock, IShellOutputReceiver receiver) throws IOException {
final String sdbCommand = String.format("%s -s %s %s", sdbPath, device.getSerialNumber(), command);
-
return runHostCommandImpl(command, sdbCommand, isBlock, receiver, false);
}
-
+
public String returnExecuteCommand(String command) {
-
final String sdbCommand = String.format("%s -s %s shell %s", sdbPath, device.getSerialNumber(), command);
-
return HostUtil.returnExecute(sdbCommand);
}
- public CommandErrorType runCommand(String command, CommandErrorType messages ) throws CommandErrorException, TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
+ public CommandErrorType runCommand(String command, CommandErrorType messages) throws CommandErrorException, TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
return runCommand(command, messages, DEFAULT_TIMEOUT);
}
- public CommandErrorType runCommand(String command, CommandErrorType errorMessages, int timeout ) throws CommandErrorException, TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException{
- if (console != null) {
- console.println("$ " + command);
- }
+ public CommandErrorType runCommand(String command, CommandErrorType errorMessages, int timeout) throws CommandErrorException, TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException{
+ print("$ " + command);
device.executeShellCommand( makeCommandWithExitcode(command), receiver, timeout);
this.endLine = receiver.getEndLine();
this.commandOutput = receiver.getCommandOutput();
-
String endLine = receiver.getEndLine();
int exitcode = parseExitcode(endLine);
-
errorMessages.findErrorType(exitcode, command);
errorMessages.setCommandOutput(receiver.getCommandOutput());
errorMessages.makeException();
-
return errorMessages;
}
-
+
public String getEndLine() {
return endLine;
}
-
+
public String getCommandOutput() {
return commandOutput;
}
-
+
public String[] getResultLineStrings() {
String strs = commandOutput.toString();
if ( StringUtil.isEmpty(strs) ) {
return new String[0];
}
- String [] str = StringUtil.split( strs, System.getProperty("line.separator") );
-
+ String[] str = StringUtil.split( strs, SEPARATOR );
return str;
}
-
+
private int parseExitcode(String line) {
int exitcode = -1;
if ( line.startsWith(TizenPlatformConstants.CMD_RESULT_PREFIX) ) {
}
return exitcode;
}
-
+
private String makeCommandWithExitcode(String command) {
return command + TizenPlatformConstants.CMD_SUFFIX;
}
+
+ public boolean isShowCommand() {
+ return showCommand;
+ }
+
+ public void setShowCommand(boolean showCommand) {
+ this.showCommand = showCommand;
+ }
+
+ private void print(String message) {
+ logger.debug(message);
+ if ( isShowCommand() && console != null ) {
+ console.println(message);
+ }
+ }
}