package org.jboss.fresh.deployer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.naming.InitialContext;
import org.jboss.fresh.registry.RegistryContext;
import org.jboss.fresh.shell.*;
import org.jboss.fresh.shell.ejb.RemoteShellHome;
import org.jboss.fresh.shell.ejb.RemoteShell;
import org.jboss.fresh.io.IOUtils;
import org.jboss.fresh.util.TextLayout;
/** This is a simple service that sustains a file on the disk. It it it simply puts an integer specifying the number of invocations made from beginning of its running. */
public class ShellExecutorService extends ServiceModule implements ShellExecutorServiceMBean {
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ShellExecutorService.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("'['dd.MM' 'HH:mm:ss']'");
private String bfile;
private boolean qoe = false;
private String flog;
private String svcname;
private boolean exeOnStart;
private boolean useLocal;
private String bcode;
// Attributes ----------------------------------------------------
public String getName() {
return "Shell Exe Service";
}
public void setBatchFile(String file) {
bfile = file;
}
public String getBatchFile() {
return bfile;
}
public void setBatchCode(String code) {
bcode = code;
}
public String getBatchCode() {
return bcode;
}
public void setExecuteOnStart(boolean val) {
exeOnStart = val;
}
public boolean getExecuteOnStart() {
return exeOnStart;
}
public void setStopOnError(Boolean val) {
qoe = val.booleanValue();
}
public Boolean getStopOnError() {
return new Boolean(qoe);
}
public void setLogFile(String val) {
flog = val;
}
public String getLogFile() {
return flog;
}
public void setSvcName(String val) {
svcname = val;
}
public String getSvcName() {
return svcname;
}
public void setUseLocal(boolean val) {
useLocal = val;
}
public boolean getUseLocal() {
return useLocal;
}
public void doStart() throws Exception {
if (exeOnStart) execute();
}
private class Executor {
private boolean stopOnError = false;
private String eSvcname = "SystemShell";
private boolean eUseLocal = true;
public Executor() {
super();
this.stopOnError = qoe;
this.eUseLocal = useLocal;
this.eSvcname = svcname;
if (eSvcname == null) {
eSvcname = eUseLocal ? "SystemShell": "ShellSession";
}
}
// we open a log file
BufferedOutputStream bos = null;
// we open a batch file
BufferedReader in = null;
public void execute() throws Exception {
PrintWriter out = null;
if(bos != null)
out = new PrintWriter(new OutputStreamWriter(bos));
try {
if (eUseLocal) {
RegistryContext ctx = new RegistryContext();
SystemShell sysshell = (SystemShell) ctx.lookup(eSvcname);
Shell shell = sysshell.startSession(null, false);
try {
// now line by line
// And put all exceptions to log too.
String cmd = in.readLine();
while (cmd != null) {
cmd = cmd.trim();
if(out!=null) {
out.println(getTime() + " Executing: " + cmd);
out.flush();
}
if(! "".equals(cmd)) {
ProcessInfo pinf = shell.execute(cmd, true);
BufferedInputStream bin = new BufferedInputStream(new ShellConsoleInputStream(new ShellObjectReader(shell, pinf.procid)));
try {
IOUtils.copy(bin, bos, 50000);
} catch (Exception ex) {
try {
if(out != null) {
out.println(getTime() + " Exception has occured:");
ex.printStackTrace(out);
}
} catch (Exception e) {
e.printStackTrace();
}
if (stopOnError) {
throw ex;
}
}
}
cmd = in.readLine();
}
} finally {
try {
shell.close();
} catch (ShellException e) {
}
}
} else {
// we get RemoteShell
InitialContext ctx = new InitialContext();
RemoteShellHome home = (RemoteShellHome) ctx.lookup(eSvcname);
RemoteShell shell = home.create();
try {
// now line by line
// And put all exceptions to log too.
String cmd = in.readLine();
while (cmd != null) {
cmd = cmd.trim();
if(out != null) {
out.println(getTime() + " Executing: " + cmd);
out.flush();
}
if(! "".equals(cmd)) {
ProcessInfo pinf = shell.execute(cmd);
BufferedInputStream bin = new BufferedInputStream(new ShellConsoleInputStream(new ShellObjectReader(shell, pinf.procid)));
try {
IOUtils.copy(bin, bos, 50000);
} catch (Exception ex) {
try {
if(out!=null) {
out.println(getTime() + " Exception has occured:");
ex.printStackTrace(out);
}
} catch (Exception e) {
e.printStackTrace();
}
if (stopOnError) {
throw ex;
}
}
}
cmd = in.readLine();
}
} finally {
shell.remove();
}
}
} finally {
try {
in.close();
} catch (Exception ex) {
}
try {
if(out != null)
out.close();
} catch (Exception ex) {
}
}
}
}
public void execute() throws Exception {
// we open a log file
BufferedOutputStream bos = null;
if (flog != null) {
if (flog.equals("console")) {
bos = new BufferedOutputStream(System.out);
} else {
bos = new BufferedOutputStream(new FileOutputStream(flog, true));
}
}
if(bcode != null && bcode.length() > 1 && bfile != null) {
log.warn("Both batch file and batch code parameters are set. Using batch file!");
if(bos!=null) {
PrintWriter out = new PrintWriter(bos);
try {
out.println(getTime() + " Both batch file and batch code parameters are set. Using batch file!");
} catch (Exception e) {
e.printStackTrace();
}
try {
out.close();
} catch (Exception e) {
}
}
}
BufferedReader in = null;
if(bfile != null) {
// we open a batch file
try {
in = new BufferedReader(new FileReader(bfile));
} catch (Exception ex) {
if(bos!=null) {
PrintWriter out = new PrintWriter(bos);
try {
out.println(getTime() + " Exception has occured:");
ex.printStackTrace(out);
} catch (Exception e) {
e.printStackTrace();
}
try {
out.close();
} catch (Exception e) {
}
}
throw ex;
}
} else if(bcode != null) {
in = new BufferedReader(new StringReader(bcode));
}
Executor exe = new Executor();
exe.bos = bos;
exe.in = in;
exe.execute();
}
public void execute(Map params) throws Exception {
// we open a log file
BufferedOutputStream bos = null;
if (flog != null) {
if (flog.equals("console")) {
bos = new BufferedOutputStream(System.out);
} else {
bos = new BufferedOutputStream(new FileOutputStream(flog, true));
}
}
if(bcode != null && bcode.length() > 1 && bfile != null) {
log.warn("Both batch file and batch code parameters are set. Using batch file!");
if(bos!=null) {
PrintWriter out = new PrintWriter(bos);
try {
out.println(getTime() + " Both batch file and batch code parameters are set. Using batch file!");
} catch (Exception e) {
e.printStackTrace();
}
try {
out.close();
} catch (Exception e) {
}
}
}
// we open a batch file
BufferedReader in = null;
if(bfile != null) {
try {
in = populate(new BufferedReader(new FileReader(bfile)), params);
} catch (Exception ex) {
if(bos != null) {
PrintWriter out = new PrintWriter(bos);
try {
out.println(getTime() + " Exception has occured:");
ex.printStackTrace(out);
} catch (Exception e) {
e.printStackTrace();
}
try {
out.close();
} catch (Exception e) {
}
}
throw ex;
}
} else if(bcode != null) {
in = populate(new BufferedReader(new StringReader(bcode)), params);
}
Executor exe = new Executor();
exe.bos = bos;
exe.in = in;
exe.execute();
}
private BufferedReader populate(BufferedReader job, Map params) throws IOException {
if (params != null && !params.isEmpty()) {
StringBuffer sb = new StringBuffer();
for (String line = job.readLine(); line != null; line = job.readLine()) {
sb.append(line).append("\r\n");
}
TextLayout tl = new TextLayout("{","}");
tl.setPattern(sb.toString());
StringWriter sw = new StringWriter();
tl.bind(sw, params);
return new BufferedReader(new StringReader(sw.toString()));
}
return job;
}
protected static String getTime() {
return sdf.format(new Date());
}
}