/*
* Created on 2004.3.25
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.jboss.fresh.shell.ejb;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.jboss.fresh.vfs.UserCtx;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.ctx.Context;
import org.jboss.fresh.io.Buffer;
import org.jboss.fresh.shell.EnvProperties;
import org.jboss.fresh.shell.Executable;
import org.jboss.fresh.shell.ProcessInfo;
import org.jboss.fresh.shell.Shell;
import org.jboss.fresh.shell.ShellStreamer;
import org.jboss.fresh.shell.ShellException;
import org.jboss.fresh.shell.impl.Process;
//import java.util.Iterator;
//import java.util.Map;
//import java.util.HashMap;
//import java.util.List;
/**
* @author strukelj
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class UnremoterShell implements Shell, ShellStreamer {
RemoteShell shell;
private boolean using = false;
private boolean collided = false;
// private boolean checkedout = false;
// private HashMap history = new HashMap();
// static Object smutex;
// static {
// smutex = new Object();
// }
static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(UnremoterShell.class);
public UnremoterShell(RemoteShell rshell) {
shell = rshell;
}
/*
public void checkout() {
log.debug("checkout: " + this);
synchronized(smutex) {
if(checkedout) {
log.error("Trying to check out an already checked out object !!!!", new Exception("Double checkout !!!!"));
log.error("Cached object history: ");
Iterator it = history.entrySet().iterator();
while(it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
if(ent.getKey()==this) {
List ls = (List)ent.getValue();
Iterator it2 = ls.iterator();
while(it2.hasNext()) {
log.error("========", (Throwable)it2.next());
}
}
}
}
history.put(this, new Exception("checkout - " + history.size()));
checkedout = true;
}
}
public void checkin() {
log.debug("checkin: " + this);
synchronized(smutex) {
if(!checkedout) {
log.error("Trying to checkin an object that was not checked out !!!!", new Exception("Checkin without checkout !!!!"));
log.error("Cached object history: ");
Iterator it = history.entrySet().iterator();
while(it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
if(ent.getKey()==this) {
List ls = (List)ent.getValue();
Iterator it2 = ls.iterator();
while(it2.hasNext()) {
log.error("========", (Throwable)it2.next());
}
}
}
}
history.put(this, new Exception("checkin - " + history.size()));
checkedout = false;
}
}
*/
synchronized void collide() {
// if(!checkedout) log.error("Using an object that has not been checkedout!", new Exception("Unchecked usage !!!!"));
if(using) {
log.error("Someone else is using it !!!!", new Exception("Collision !!!!"));
collided = true;
} else {
using = true;
}
}
synchronized void done() {
// if(!checkedout) log.error("Using an object that has not been checkedout!", new Exception("Unchecked usage !!!!"));
if (collided) {
log.error("Someone else was using it !!!!", new Exception("Someone collided with us !!!!"));
}
collided = false;
using = false;
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#execute(java.lang.String, boolean)
*/
public ProcessInfo execute(String cmdline, boolean ui)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
public ProcessInfo execute(String cmdline, List input, boolean ui)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#execute(org.jboss.fresh.shell.Executable, java.lang.String, java.lang.String[])
*/
public ProcessInfo execute(Executable exe, String exepath, String[] params)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#executeAsString(java.lang.String, boolean)
*/
/*
public String executeAsString(String cmdline, boolean ui)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
*/
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#executeAsObject(java.lang.String, boolean)
*/
public Object executeAsObject(String cmdline, boolean ui)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
public Object executeAsObject(String cmdline, List input, boolean ui)
throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#execute(java.lang.String)
*/
public ProcessInfo execute(String cmdline) throws ShellException {
collide();
try {
return shell.execute(cmdline);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
public ProcessInfo execute(String cmdline, List input) throws ShellException {
collide();
try {
return shell.execute(cmdline, input);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#executeAsString(java.lang.String)
*/
/*
public String executeAsString(String cmdline) throws ShellException {
collide();
try {
return shell.executeAsString(cmdline);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
*/
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#executeAsObject(java.lang.String)
*/
public Object executeAsObject(String cmdline) throws ShellException {
collide();
try {
return shell.executeAsObject(cmdline);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
public Object executeAsObject(String cmdline, List input) throws ShellException {
collide();
try {
return shell.executeAsObject(cmdline, input);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getEnvProperties()
*/
public Properties getEnvProperties() throws ShellException {
throw new RuntimeException("Method not implemented");
}
public EnvProperties getEnv() throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getEnvProperty(java.lang.String)
*/
public String getEnvProperty(String propname) throws ShellException {
collide();
try {
return shell.getEnvProperty(propname);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#setEnvProperty(java.lang.String, java.lang.String)
*/
public void setEnvProperty(String name, String value) throws ShellException {
collide();
try {
shell.setEnvProperty(name, value);
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#setROEnvProperty(java.lang.String, java.lang.String)
*/
public void setROEnvProperty(String name, String value) throws ShellException {
executeAsObject("rset " + name + "=" + value);
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getBuffer(java.lang.String, int)
*/
public Buffer getBuffer(String procid, int bufid) throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#_setPWD(java.lang.String)
*/
public void _setPWD(String path) {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getVFS()
*/
public VFS getVFS() throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getUserCtx()
*/
public UserCtx getUserCtx() throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#close()
*/
public void close() throws ShellException {
collide();
try {
shell.remove();
} catch(Exception ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#removeProcess(java.lang.String)
*/
public boolean removeProcess(String id) {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#createProcess(org.jboss.fresh.shell.Executable, java.lang.String[], java.lang.String, boolean)
*/
public Process createProcess(Executable exe, String[] params, String path,
boolean useThreadPool) throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getSessionID()
*/
public String getSessionID() throws ShellException {
collide();
try {
return shell.getSessionID();
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#loadExe(java.lang.String)
*/
public Executable loadExe(String command) throws ShellException {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#getContext()
*/
public Context getContext() {
throw new RuntimeException("Method not implemented");
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.Shell#using()
*/
public void using() throws ShellException {
collide();
try {
shell.getSessionID();
} catch(RemoteException ex) {
throw new ShellException(ex);
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.ShellStreamer#read(java.lang.String)
*/
public Object read(String id) throws IOException {
collide();
try {
return shell.read(id);
} catch(RemoteException ex) {
IOException ioe = new IOException("Exception occured while reading from StdOut for process " + id);
ioe.initCause(ex);
throw ioe;
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.ShellStreamer#readBuffer(java.lang.String, int)
*/
public LinkedList readBuffer(String id, int maxsize) throws IOException {
collide();
try {
return shell.readBuffer(id, maxsize);
} catch(RemoteException ex) {
IOException ioe = new IOException("Exception occured while reading from StdOut for process " + id);
ioe.initCause(ex);
throw ioe;
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.ShellStreamer#write(java.lang.String, java.lang.Object)
*/
public void write(String id, Object obj) throws IOException {
collide();
try {
shell.write(id, obj);
} catch(RemoteException ex) {
IOException ioe = new IOException("Exception occured while writing to StdIn of process " + id);
ioe.initCause(ex);
throw ioe;
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.ShellStreamer#writeBuffer(java.lang.String, java.util.LinkedList)
*/
public void writeBuffer(String id, LinkedList obj) throws IOException {
collide();
try {
shell.writeBuffer(id, obj);
} catch(RemoteException ex) {
IOException ioe = new IOException("Exception occured while writing to StdIn of process " + id);
ioe.initCause(ex);
throw ioe;
} finally {
done();
}
}
/* (non-Javadoc)
* @see org.jboss.fresh.shell.ShellStreamer#close(java.lang.String, int)
*/
public void close(String id, int streamid) throws IOException {
collide();
try {
shell.close(id, streamid);
} catch(RemoteException ex) {
IOException ioe = new IOException("Exception occured while closing " + (streamid==1 ? "StdIn" : streamid==2 ? "StdOut" : String.valueOf(streamid)) + " of process " + id);
ioe.initCause(ex);
throw ioe;
} finally {
done();
}
}
public RemoteShell getRemoteShell() {
return shell;
}
public boolean isValid() {
try {
using();
} catch (Exception ex) {
return false;
}
return true;
}
}