All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alogic.terminal.xscript.XCmd Maven / Gradle / Ivy
package com.alogic.terminal.xscript;
import java.util.ArrayList;
import java.util.List;
import com.alogic.terminal.Command;
import com.alogic.xscript.plugins.Segment;
import org.apache.commons.lang3.StringUtils;
import com.alogic.terminal.Resolver;
import com.alogic.terminal.Terminal;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
/**
* 执行普通指令
*
* @author yyduan
*
* @since 1.6.9.9
* @version 1.6.9.9 [20170829 duanyy]
* - 增加ssh改密码功能
*
* @version 1.6.13.38 [20210224 duanyy]
* - 增加shellMode参数
*/
public class XCmd extends Segment {
protected String pid = "$xshell";
protected String id = "$x-cmd";
/**
* 指令
*/
protected List cmds = new ArrayList();
protected String lineId = "$line";
protected boolean shellMode = true;
public XCmd(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p){
super.configure(p);
pid = PropertiesConstants.getString(p,"pid", pid , true);
lineId = PropertiesConstants.getString(p,"line",lineId,true);
id = PropertiesConstants.getString(p,"id","$" + this.getXmlTag(),true);
shellMode = PropertiesConstants.getBoolean(p,"shellMode",true,true);
String value = PropertiesConstants.getRaw(p,"value","");
if (StringUtils.isNotEmpty(value)){
String [] list = value.split(";");
for (String one:list){
if (StringUtils.isNotEmpty(one)){
cmds.add(one);
}
}
}
}
@Override
protected void onExecute(final XsObject root,final XsObject current,final LogicletContext ctx,final ExecuteWatcher watcher){
Terminal t = ctx.getObject(pid);
if (t == null){
throw new BaseException("core.e1001","It must be in a xshell context,check your together script.");
}
final XCmd self = this;
final boolean log2log = this.children.isEmpty();
Resolver resolver = new Resolver() {
@Override
public Object resolveBegin(String cmd) {
return cmd;
}
@Override
public void resolveLine(Object cookies, String content) {
if (log2log){
log(content);
}else {
if (StringUtils.isNotEmpty(content)) {
ctx.SetValue(lineId, content);
self.onSuperExecute(root, current, ctx, watcher);
}
}
}
@Override
public void resolveEnd(Object cookies) {
}
};
int ret = 0;
for (String cmd:cmds){
String transformed = ctx.transform(cmd);
if (StringUtils.isNotEmpty(transformed)){
ret = t.exec(new Command.Simple(resolver,shellMode,transformed));
if (ret != 0){
break;
}
}
}
ctx.SetValue(id,String.valueOf(ret));
}
protected void onSuperExecute(XsObject root,XsObject current,
final LogicletContext ctx,final ExecuteWatcher watcher){
super.onExecute(root,current,ctx,watcher);
}
}