|
From: <cr...@us...> - 2008-09-28 14:49:35
|
Revision: 4587
http://jnode.svn.sourceforge.net/jnode/?rev=4587&view=rev
Author: crawley
Date: 2008-09-28 14:49:21 +0000 (Sun, 28 Sep 2008)
Log Message:
-----------
First cut checkin of an isolate command invoker
Modified Paths:
--------------
trunk/shell/descriptors/org.jnode.shell.xml
trunk/shell/src/shell/org/jnode/shell/AsyncCommandInvoker.java
trunk/shell/src/shell/org/jnode/shell/CommandRunner.java
trunk/shell/src/shell/org/jnode/shell/CommandShell.java
trunk/shell/src/shell/org/jnode/shell/CommandThreadImpl.java
trunk/shell/src/shell/org/jnode/shell/ThreadCommandInvoker.java
trunk/shell/src/test/org/jnode/test/shell/CompletionTest.java
trunk/shell/src/test/org/jnode/test/shell/DefaultSyntaxCompletionTest.java
Added Paths:
-----------
trunk/core/src/core/org/jnode/vm/isolate/link/ObjectLinkMessage.java
trunk/shell/src/shell/org/jnode/shell/isolate/
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandInvoker.java
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandLauncher.java
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocket.java
trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocketImpl.java
trunk/shell/src/shell/org/jnode/shell/proclet/ProcletCommandInvoker.java
Removed Paths:
-------------
trunk/shell/src/shell/org/jnode/shell/ProcletCommandInvoker.java
Added: trunk/core/src/core/org/jnode/vm/isolate/link/ObjectLinkMessage.java
===================================================================
--- trunk/core/src/core/org/jnode/vm/isolate/link/ObjectLinkMessage.java (rev 0)
+++ trunk/core/src/core/org/jnode/vm/isolate/link/ObjectLinkMessage.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,24 @@
+package org.jnode.vm.isolate.link;
+
+public class ObjectLinkMessage extends LinkMessageImpl {
+
+ private final Object obj;
+
+ private ObjectLinkMessage(Object cr) {
+ this.obj = cr;
+ }
+
+ public static ObjectLinkMessage newMessage (Object obj) {
+ return new ObjectLinkMessage(obj);
+ }
+
+ @Override
+ public Object extract() {
+ return obj;
+ }
+
+ @Override
+ LinkMessageImpl CloneMessage() {
+ return new ObjectLinkMessage(obj);
+ }
+}
Modified: trunk/shell/descriptors/org.jnode.shell.xml
===================================================================
--- trunk/shell/descriptors/org.jnode.shell.xml 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/descriptors/org.jnode.shell.xml 2008-09-28 14:49:21 UTC (rev 4587)
@@ -23,6 +23,7 @@
<export name="org.jnode.shell.help.argument.*"/>
<export name="org.jnode.shell.help.def.*"/>
<export name="org.jnode.shell.io.*"/>
+ <export name="org.jnode.shell.isolate.*"/>
<export name="org.jnode.shell.proclet.*"/>
<export name="org.jnode.shell.syntax.*"/>
</library>
Modified: trunk/shell/src/shell/org/jnode/shell/AsyncCommandInvoker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/AsyncCommandInvoker.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/AsyncCommandInvoker.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -148,7 +148,7 @@
cr.run();
} else {
try {
- threadProcess = createThread(cmdLine, cr);
+ threadProcess = createThread(cr);
} catch (Exception ex) {
throw new ShellInvocationException(
"Exception while creating command thread", ex);
@@ -178,14 +178,15 @@
throw new ShellFailureException("unexpected internal command");
}
try {
- return createThread(cmdLine, cr);
+ return createThread(cr);
} catch (Exception ex) {
throw new ShellInvocationException(
"Exception while creating command thread", ex);
}
}
- abstract CommandThread createThread(CommandLine cmdLine, CommandRunner cr);
+ protected abstract CommandThread createThread(CommandRunner cr)
+ throws ShellInvocationException;
public void keyPressed(KeyboardEvent ke) {
// disabling Ctrl-C since currently we have no safe method for killing a
Modified: trunk/shell/src/shell/org/jnode/shell/CommandRunner.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -42,11 +42,11 @@
* @author cr...@jn...
*
*/
-class CommandRunner implements Runnable {
+public class CommandRunner implements Runnable {
private final CommandShell shell;
private final CommandInvoker invoker;
- final CommandIO[] ios;
- final Class<?> cx;
+ private final CommandIO[] ios;
+ final Class<?> targetClass;
final Method method;
final Object[] args;
final CommandInfo cmdInfo;
@@ -55,11 +55,11 @@
private int rc;
public CommandRunner(CommandShell shell, CommandInvoker invoker,
- CommandInfo cmdInfo, Class<?> cx, Method method, Object[] args,
+ CommandInfo cmdInfo, Class<?> targetClass, Method method, Object[] args,
CommandIO[] ios) {
this.shell = shell;
this.invoker = invoker;
- this.cx = cx;
+ this.targetClass = targetClass;
this.method = method;
this.cmdInfo = cmdInfo;
this.commandLine = null;
@@ -71,7 +71,7 @@
CommandInfo cmdInfo, CommandLine commandLine, CommandIO[] ios) {
this.shell = shell;
this.invoker = invoker;
- this.cx = null;
+ this.targetClass = null;
this.method = null;
this.args = null;
this.cmdInfo = cmdInfo;
@@ -85,16 +85,16 @@
try {
if (method != null) {
try {
- // This saves the Command instance that has the commandline state
+ // This saves the Command instance that has the command line state
// associated in a thread-local so that the Abstract.execute(String[])
// method can get hold of it. This is the magic that allows a command
// that implements 'main' as "new MyCommand().execute(args)" to get the
- // parsed commandline arguments, etc.
+ // parsed command line arguments, etc.
AbstractCommand.saveCurrentCommand(cmdInfo.getCommandInstance());
// Call the command's entry point method reflectively
Object obj = Modifier.isStatic(method.getModifiers()) ? null
- : cx.newInstance();
+ : targetClass.newInstance();
AccessController.doPrivileged(new InvokeAction(method, obj,
args));
} finally {
@@ -109,7 +109,7 @@
// bounce us to the older execute(CommandLine, InputStream, PrintStream,
// PrintStream) method.
Command cmd = cmdInfo.createCommandInstance();
- cmd.initialize(commandLine, ios);
+ cmd.initialize(commandLine, getIos());
cmd.execute();
}
ok = true;
@@ -124,7 +124,7 @@
IOException savedEx = null;
// Make sure that we try to flush all IOs, even if some of the flushes
// result in IOExceptions.
- for (CommandIO io : ios) {
+ for (CommandIO io : getIos()) {
try {
io.flush();
} catch (IOException ex) {
@@ -182,4 +182,28 @@
}
}
+ public Method getMethod() {
+ return method;
+ }
+
+ public Class<?> getTargetClass() {
+ return targetClass;
+ }
+
+ public Object[] getArgs() {
+ return args;
+ }
+
+ public CommandIO[] getIos() {
+ return ios;
+ }
+
+ public CommandLine getCommandLine() {
+ return commandLine;
+ }
+
+ public String getCommandName() {
+ return commandLine != null ? commandLine.getCommandName() : null;
+ }
+
}
Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -62,6 +62,8 @@
import org.jnode.shell.io.FanoutWriter;
import org.jnode.shell.io.NullInputStream;
import org.jnode.shell.io.NullOutputStream;
+import org.jnode.shell.isolate.IsolateCommandInvoker;
+import org.jnode.shell.proclet.ProcletCommandInvoker;
import org.jnode.shell.syntax.ArgumentBundle;
import org.jnode.shell.syntax.CommandSyntaxException;
import org.jnode.shell.syntax.SyntaxManager;
@@ -269,6 +271,7 @@
ShellUtils.registerCommandInvoker(DefaultCommandInvoker.FACTORY);
ShellUtils.registerCommandInvoker(ThreadCommandInvoker.FACTORY);
ShellUtils.registerCommandInvoker(ProcletCommandInvoker.FACTORY);
+ ShellUtils.registerCommandInvoker(IsolateCommandInvoker.FACTORY);
ShellUtils.registerCommandInterpreter(DefaultInterpreter.FACTORY);
ShellUtils
.registerCommandInterpreter(RedirectingInterpreter.FACTORY);
@@ -282,7 +285,7 @@
// Now become interactive
ownThread = Thread.currentThread();
- // Run commands from the JNode commandline first
+ // Run commands from the JNode command line first
final String cmdLine = System.getProperty(CMDLINE_PROPERTY_NAME, "");
final StringTokenizer tok = new StringTokenizer(cmdLine);
Modified: trunk/shell/src/shell/org/jnode/shell/CommandThreadImpl.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandThreadImpl.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/CommandThreadImpl.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -33,7 +33,7 @@
/**
* @param group the parent group for the thread
- * @param runner the runnable that implements the command
+ * @param runner the runnable that will run the command
* @param name a thread name
* @param size the threads stack size
*/
@@ -45,16 +45,17 @@
/**
* @param group the parent group for the thread
- * @param runner the runnable that implements the command
+ * @param runner the Runnable that will run the command
+ * @param name the thread name
*/
- public CommandThreadImpl(ThreadGroup group, Runnable runner) {
- super(group, runner);
+ public CommandThreadImpl(ThreadGroup group, Runnable runner, String name) {
+ super(group, runner, name);
this.runner = runner;
}
/**
- * @param runner the runnable that implements the command
- * @param name a thread name
+ * @param runner the Runnable that will run the command
+ * @param name the thread name
*/
public CommandThreadImpl(Runnable runner, String name) {
super(runner, name);
Deleted: trunk/shell/src/shell/org/jnode/shell/ProcletCommandInvoker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/ProcletCommandInvoker.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/ProcletCommandInvoker.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -1,74 +0,0 @@
-/*
- * $Id: ThreadCommandInvoker.java 3374 2007-08-02 18:15:27Z lsantha $
- *
- * JNode.org
- * Copyright (C) 2003-2007 JNode.org
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
- * License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; If not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-package org.jnode.shell;
-
-import org.jnode.shell.io.CommandIO;
-import org.jnode.shell.proclet.ProcletContext;
-import org.jnode.shell.proclet.ProcletIOContext;
-import org.jnode.vm.VmSystem;
-
-/**
- * This command invoker runs commands in their own proclet, giving each one its
- * own stdin,out,err etcetera.
- *
- * @author cr...@jn...
- */
-public class ProcletCommandInvoker extends AsyncCommandInvoker {
-
- public static final Factory FACTORY = new Factory() {
- public CommandInvoker create(CommandShell shell) {
- return new ProcletCommandInvoker(shell);
- }
-
- public String getName() {
- return "proclet";
- }
- };
-
- private static boolean initialized;
-
- public ProcletCommandInvoker(CommandShell commandShell) {
- super(commandShell);
- init();
- }
-
- public String getName() {
- return "proclet";
- }
-
- CommandThreadImpl createThread(CommandLine cmdLine, CommandRunner cr) {
- CommandIO[] ios = cmdLine.getStreams();
- return ProcletContext.createProclet(cr, null, null,
- new Object[] {
- ios[0].getInputStream(),
- ios[1].getPrintStream(),
- ios[2].getPrintStream()},
- cmdLine.getCommandName());
- }
-
- private static synchronized void init() {
- if (!initialized) {
- VmSystem.switchToExternalIOContext(new ProcletIOContext());
- initialized = true;
- }
- }
-}
Modified: trunk/shell/src/shell/org/jnode/shell/ThreadCommandInvoker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/ThreadCommandInvoker.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/shell/org/jnode/shell/ThreadCommandInvoker.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -58,7 +58,7 @@
return "thread";
}
- CommandThreadImpl createThread(CommandLine cmdLine, CommandRunner cr) {
- return new CommandThreadImpl(cr, cmdLine.getCommandName());
+ protected CommandThreadImpl createThread(CommandRunner cr) {
+ return new CommandThreadImpl(cr, cr.getCommandName());
}
}
Added: trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandInvoker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandInvoker.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandInvoker.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,68 @@
+/*
+ * $Id: ThreadCommandInvoker.java 3374 2007-08-02 18:15:27Z lsantha $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2007 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.shell.isolate;
+
+import java.io.IOException;
+
+import org.jnode.shell.AsyncCommandInvoker;
+import org.jnode.shell.CommandInvoker;
+import org.jnode.shell.CommandRunner;
+import org.jnode.shell.CommandShell;
+import org.jnode.shell.CommandThread;
+import org.jnode.shell.ShellInvocationException;
+
+/**
+ * This command invoker runs commands in their own isolates.
+ *
+ * @author cr...@jn...
+ */
+public class IsolateCommandInvoker extends AsyncCommandInvoker {
+
+ public static final Factory FACTORY = new Factory() {
+ public CommandInvoker create(CommandShell shell) {
+ return new IsolateCommandInvoker(shell);
+ }
+
+ public String getName() {
+ return "isolate";
+ }
+ };
+
+ public IsolateCommandInvoker(CommandShell commandShell) {
+ super(commandShell);
+ }
+
+ @Override
+ public String getName() {
+ return "isolate";
+ }
+
+ @Override
+ protected CommandThread createThread(CommandRunner cr)
+ throws ShellInvocationException {
+ try {
+ return new IsolateCommandThreadImpl(cr);
+ } catch (IOException ex) {
+ throw new ShellInvocationException(ex);
+ }
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandLauncher.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandLauncher.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandLauncher.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,26 @@
+package org.jnode.shell.isolate;
+
+import javax.isolate.Isolate;
+import javax.isolate.Link;
+
+import org.jnode.shell.CommandRunner;
+import org.jnode.vm.isolate.link.ObjectLinkMessage;
+
+public class IsolateCommandLauncher {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Link cl = Isolate.getLinks()[0];
+ CommandRunner cr;
+ try {
+ ObjectLinkMessage message = (ObjectLinkMessage) cl.receive();
+ cr = (CommandRunner) message.extract();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return;
+ }
+ cr.run();
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateCommandThreadImpl.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,128 @@
+package org.jnode.shell.isolate;
+
+import java.io.Closeable;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.Socket;
+import java.util.Properties;
+
+import javax.isolate.Isolate;
+import javax.isolate.Link;
+import javax.isolate.StreamBindings;
+
+import org.jnode.shell.CommandRunner;
+import org.jnode.shell.CommandThread;
+import org.jnode.shell.ShellInvocationException;
+import org.jnode.shell.ThreadExitListener;
+import org.jnode.shell.io.CommandIO;
+import org.jnode.util.ReaderInputStream;
+import org.jnode.util.WriterOutputStream;
+import org.jnode.vm.isolate.link.ObjectLinkMessage;
+
+/**
+ * This class implements the CommandThread API for commands run in their
+ * own private isolates.
+ *
+ * @author cr...@jn...
+ */
+public class IsolateCommandThreadImpl implements CommandThread {
+
+ private final Isolate isolate;
+ private final CommandRunner cr;
+
+ public IsolateCommandThreadImpl(CommandRunner cr) throws IOException {
+ this.cr = cr;
+ CommandIO[] ios = cr.getIos();
+ Properties properties = System.getProperties();
+ StreamBindings streamBindings = createStreamBindings(ios);
+ isolate = new Isolate(streamBindings, properties,
+ "org.jnode.shell.isolate.IsolateCommandLauncher", new String[0]);
+ }
+
+ private StreamBindings createStreamBindings(CommandIO[] ios) throws IOException {
+ // FIXME if there are more than 3 CommandIOs, they should be passed
+ // to the isolate via a link message.
+ if (ios.length > 3) {
+ throw new RuntimeException("> 3 CommandIOs not implemented yet");
+ }
+ StreamBindings streamBindings = new StreamBindings();
+ Closeable in = ios[0].findBaseStream();
+ if (in instanceof FileInputStream) {
+ streamBindings.setIn((FileInputStream) in);
+ } else {
+ streamBindings.setIn(createSocketForInput(in));
+ }
+ Closeable out = ios[1].findBaseStream();
+ if (out instanceof FileOutputStream) {
+ streamBindings.setOut((FileOutputStream) out);
+ } else {
+ streamBindings.setOut(createSocketForOutput(out));
+ }
+ Closeable err = ios[2].findBaseStream();
+ if (err instanceof FileOutputStream) {
+ streamBindings.setErr((FileOutputStream) err);
+ } else {
+ streamBindings.setErr(createSocketForOutput(err));
+ }
+ return streamBindings;
+ }
+
+ private Socket createSocketForInput(Closeable closeable) throws IOException {
+ InputStream in;
+ if (closeable instanceof Reader) {
+ in = new ReaderInputStream((Reader) closeable);
+ } else {
+ in = (InputStream) closeable;
+ }
+ return new IsolateSocket(in);
+ }
+
+ private Socket createSocketForOutput(Closeable closeable) throws IOException {
+ OutputStream out;
+ if (closeable instanceof Writer) {
+ out = new WriterOutputStream((Writer) closeable);
+ } else {
+ out = (OutputStream) closeable;
+ }
+ return new IsolateSocket(out);
+ }
+
+ @Override
+ public int getReturnCode() {
+ // TODO Auto-generated method stub
+ return -1;
+ }
+
+ @Override
+ public boolean isAlive() {
+ return isolate.hasStarted() && !isolate.hasExited();
+ }
+
+ @Override
+ public void start(ThreadExitListener listener) throws ShellInvocationException {
+ try {
+ Link cl = Link.newLink(Isolate.currentIsolate(), isolate);
+ isolate.start(cl);
+ ObjectLinkMessage msg = ObjectLinkMessage.newMessage(this.cr);
+ cl.send(msg);
+ } catch (Exception ex) {
+ throw new ShellInvocationException("Cannot start isolate", ex);
+ }
+ }
+
+ @Override
+ public void stop(ThreadDeath threadDeath) {
+ // FIXME - not sure what status argument should be ... but this is moot
+ // at the moment because VmIsolate.halt does nothing with it anyway.
+ isolate.halt(0 /* FIXME */);
+ }
+
+ public void waitFor() {
+ // TODO implement me
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocket.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocket.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocket.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,45 @@
+/*
+ * $Id: Shell.java 4556 2008-09-13 08:02:20Z crawley $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.jnode.shell.isolate;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketException;
+
+/**
+ * This special purpose socket type is a light-weight wrapper for an
+ * input or output stream for use in local isolate communication. One
+ * of these sockets is created in the 'connected' state, and does not
+ * support the normal socket connection mechanisms.
+ *
+ * @author cr...@jn...
+ */
+public class IsolateSocket extends Socket {
+
+ public IsolateSocket(InputStream in) throws SocketException {
+ super(new IsolateSocketImpl(in));
+ }
+
+ public IsolateSocket(OutputStream out) throws SocketException {
+ super(new IsolateSocketImpl(out));
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocketImpl.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocketImpl.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/isolate/IsolateSocketImpl.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,159 @@
+/*
+ * $Id: Shell.java 4556 2008-09-13 08:02:20Z crawley $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+ package org.jnode.shell.isolate;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketImpl;
+import java.net.UnknownHostException;
+
+/**
+ * The implementation class for IsolateSoccket.
+ *
+ * @author cr...@jn...
+ */
+public class IsolateSocketImpl extends SocketImpl {
+
+ private final InputStream in;
+ private final OutputStream out;
+ private boolean closed;
+ private final InetAddress dummyAddress;
+
+ private IsolateSocketImpl(InputStream in, OutputStream out) {
+ super();
+ this.in = in;
+ this.out = out;
+ InetAddress d;
+ try {
+ d = InetAddress.getByAddress(new byte[]{0, 0, 0, 0});
+ } catch (UnknownHostException ex) {
+ d = null;
+ }
+ this.dummyAddress = d;
+ }
+
+ public IsolateSocketImpl(InputStream in) {
+ this(in, null);
+ }
+
+ public IsolateSocketImpl(OutputStream out) {
+ this(null, out);
+ }
+
+ @Override
+ protected void accept(SocketImpl s) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected int available() throws IOException {
+ if (in == null) {
+ throw new SocketException("socket is closed");
+ }
+ return in.available();
+ }
+
+ @Override
+ protected InetAddress getInetAddress() {
+ // We have to return something non-null because the generic Socket code uses
+ // a non-null address to indicate that the socket is connected.
+ return dummyAddress;
+ }
+
+ @Override
+ protected void bind(InetAddress host, int port) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected synchronized void close() throws IOException {
+ if (!closed) {
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.close();
+ }
+ closed = true;
+ }
+ }
+
+ @Override
+ protected void connect(String host, int port) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void connect(InetAddress host, int port) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void connect(SocketAddress address, int timeout) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void create(boolean stream) throws IOException {
+ // Do nothing,
+ }
+
+ @Override
+ protected synchronized InputStream getInputStream() throws IOException {
+ if (closed || in == null) {
+ throw new SocketException("socket is closed");
+ }
+ return in;
+ }
+
+ @Override
+ protected synchronized OutputStream getOutputStream() throws IOException {
+ if (closed || out == null) {
+ throw new SocketException("socket is closed");
+ }
+ return out;
+ }
+
+ @Override
+ protected void listen(int backlog) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void sendUrgentData(int data) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object getOption(int optID) throws SocketException {
+ // no options supported
+ return null;
+ }
+
+ @Override
+ public void setOption(int optID, Object value) throws SocketException {
+ // no options supported
+ }
+}
Added: trunk/shell/src/shell/org/jnode/shell/proclet/ProcletCommandInvoker.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/proclet/ProcletCommandInvoker.java (rev 0)
+++ trunk/shell/src/shell/org/jnode/shell/proclet/ProcletCommandInvoker.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -0,0 +1,78 @@
+/*
+ * $Id: ThreadCommandInvoker.java 3374 2007-08-02 18:15:27Z lsantha $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2007 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.shell.proclet;
+
+import org.jnode.shell.AsyncCommandInvoker;
+import org.jnode.shell.CommandInvoker;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.CommandRunner;
+import org.jnode.shell.CommandShell;
+import org.jnode.shell.CommandThreadImpl;
+import org.jnode.shell.io.CommandIO;
+import org.jnode.vm.VmSystem;
+
+/**
+ * This command invoker runs commands in their own proclet, giving each one its
+ * own stdin,out,err etcetera.
+ *
+ * @author cr...@jn...
+ */
+public class ProcletCommandInvoker extends AsyncCommandInvoker {
+
+ public static final Factory FACTORY = new Factory() {
+ public CommandInvoker create(CommandShell shell) {
+ return new ProcletCommandInvoker(shell);
+ }
+
+ public String getName() {
+ return "proclet";
+ }
+ };
+
+ private static boolean initialized;
+
+ public ProcletCommandInvoker(CommandShell commandShell) {
+ super(commandShell);
+ init();
+ }
+
+ public String getName() {
+ return "proclet";
+ }
+
+ protected CommandThreadImpl createThread(CommandRunner cr) {
+ CommandIO[] ios = cr.getIos();
+ return ProcletContext.createProclet(cr, null, null,
+ new Object[] {
+ ios[0].getInputStream(),
+ ios[1].getPrintStream(),
+ ios[2].getPrintStream()},
+ cr.getCommandName());
+ }
+
+ private static synchronized void init() {
+ if (!initialized) {
+ VmSystem.switchToExternalIOContext(new ProcletIOContext());
+ initialized = true;
+ }
+ }
+}
Modified: trunk/shell/src/test/org/jnode/test/shell/CompletionTest.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/CompletionTest.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/test/org/jnode/test/shell/CompletionTest.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -35,11 +35,11 @@
import org.jnode.shell.CommandShell;
import org.jnode.shell.DefaultCommandInvoker;
import org.jnode.shell.DefaultInterpreter;
-import org.jnode.shell.ProcletCommandInvoker;
import org.jnode.shell.RedirectingInterpreter;
import org.jnode.shell.ShellUtils;
import org.jnode.shell.ThreadCommandInvoker;
import org.jnode.shell.alias.AliasManager;
+import org.jnode.shell.proclet.ProcletCommandInvoker;
import org.jnode.shell.syntax.ArgumentSyntax;
import org.jnode.shell.syntax.EmptySyntax;
import org.jnode.shell.syntax.OptionSyntax;
Modified: trunk/shell/src/test/org/jnode/test/shell/DefaultSyntaxCompletionTest.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/DefaultSyntaxCompletionTest.java 2008-09-27 17:05:24 UTC (rev 4586)
+++ trunk/shell/src/test/org/jnode/test/shell/DefaultSyntaxCompletionTest.java 2008-09-28 14:49:21 UTC (rev 4587)
@@ -35,11 +35,11 @@
import org.jnode.shell.CommandShell;
import org.jnode.shell.DefaultCommandInvoker;
import org.jnode.shell.DefaultInterpreter;
-import org.jnode.shell.ProcletCommandInvoker;
import org.jnode.shell.RedirectingInterpreter;
import org.jnode.shell.ShellUtils;
import org.jnode.shell.ThreadCommandInvoker;
import org.jnode.shell.alias.AliasManager;
+import org.jnode.shell.proclet.ProcletCommandInvoker;
import org.jnode.test.shell.syntax.TestAliasManager;
import org.jnode.test.shell.syntax.TestSyntaxManager;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|