|
From: <cr...@us...> - 2009-01-21 13:33:57
|
Revision: 4898
http://jnode.svn.sourceforge.net/jnode/?rev=4898&view=rev
Author: crawley
Date: 2009-01-21 13:33:53 +0000 (Wed, 21 Jan 2009)
Log Message:
-----------
Tidy up CommandShell's resource script execution + de-static'd Emu and friends.
Modified Paths:
--------------
trunk/distr/src/emu/org/jnode/emu/EditEmu.java
trunk/distr/src/emu/org/jnode/emu/ShellEmu.java
trunk/shell/src/emu/org/jnode/emu/Emu.java
trunk/shell/src/shell/org/jnode/shell/CommandShell.java
Modified: trunk/distr/src/emu/org/jnode/emu/EditEmu.java
===================================================================
--- trunk/distr/src/emu/org/jnode/emu/EditEmu.java 2009-01-21 13:32:57 UTC (rev 4897)
+++ trunk/distr/src/emu/org/jnode/emu/EditEmu.java 2009-01-21 13:33:53 UTC (rev 4898)
@@ -10,14 +10,21 @@
* @author Levente S\u00e1ntha
*/
public class EditEmu extends Emu {
+
+ public EditEmu(File root) throws EmuException {
+ super(root);
+ }
+
public static void main(String[] argv) throws Exception {
if (argv.length == 0 || argv[0].startsWith("-")) {
System.err.println("Usage: editEmu <file> [<jnode-home>]");
return;
}
- initEnv(argv.length > 1 ? new File(argv[1]) : null);
-
+ new EditEmu(argv.length > 1 ? new File(argv[1]) : null).run(new File(argv[0]));
+ }
+
+ private void run(File file) throws Exception {
SwingTextScreenConsoleManager cm = new SwingTextScreenConsoleManager();
final TextScreenConsole console = cm.createConsole(
null,
@@ -26,7 +33,6 @@
ConsoleManager.CreateOptions.NO_LINE_EDITTING));
TextEditor te = new TextEditor(console);
- File f = new File(argv[0]);
- te.loadFile(f);
+ te.loadFile(file);
}
}
Modified: trunk/distr/src/emu/org/jnode/emu/ShellEmu.java
===================================================================
--- trunk/distr/src/emu/org/jnode/emu/ShellEmu.java 2009-01-21 13:32:57 UTC (rev 4897)
+++ trunk/distr/src/emu/org/jnode/emu/ShellEmu.java 2009-01-21 13:33:53 UTC (rev 4898)
@@ -11,13 +11,20 @@
* @author Levente S\u00e1ntha
*/
public class ShellEmu extends Emu {
+
+ public ShellEmu(File root) throws EmuException {
+ super(root);
+ }
public static void main(String[] argv) throws Exception {
if (argv.length > 0 && argv[0].startsWith("-")) {
System.err.println("Usage: shellemu [<jnode-home>]");
return;
}
- initEnv(argv.length > 0 ? new File(argv[0]) : null);
+ new ShellEmu(argv.length > 0 ? new File(argv[0]) : null).run();
+ }
+
+ private void run() throws Exception {
TextScreenConsoleManager cm = new SwingTextScreenConsoleManager();
new Thread(new CommandShell(cm.createConsole(
"Console 1",
Modified: trunk/shell/src/emu/org/jnode/emu/Emu.java
===================================================================
--- trunk/shell/src/emu/org/jnode/emu/Emu.java 2009-01-21 13:32:57 UTC (rev 4897)
+++ trunk/shell/src/emu/org/jnode/emu/Emu.java 2009-01-21 13:33:53 UTC (rev 4898)
@@ -59,7 +59,7 @@
};
// FIXME configuring a hard-coded list of command plugins is a bad idea.
- private static final String[] PLUGIN_NAMES = new String[]{
+ private static final String[] PLUGIN_NAMES = new String[] {
"org.jnode.shell.command",
"org.jnode.shell.command.posix",
"org.jnode.shell.command.driver.console",
@@ -69,38 +69,36 @@
};
/**
- * Initialize a minimal subset of JNode services to allow us to run JNode commands.
+ * The constructor initializes a minimal subset of JNode services to allow us to run JNode commands.
*
* @param root the notional JNode sandbox root directory or <code>null</code>.
* @throws EmuException
*/
- public static void initEnv(File root) throws EmuException {
- if (true) {
- if (root == null) {
- root = new File("").getAbsoluteFile();
- System.err.println("Assuming that the JNode root is '" + root + "'");
- }
- InitialNaming.setNameSpace(new BasicNameSpace());
+ public Emu(File root) throws EmuException {
+ if (root == null) {
+ root = new File("").getAbsoluteFile();
+ System.err.println("Assuming that the JNode root is '" + root + "'");
+ }
+ InitialNaming.setNameSpace(new BasicNameSpace());
- try {
- InitialNaming.bind(DeviceManager.NAME, DeviceManager.INSTANCE);
- AliasManager aliasMgr =
- new DefaultAliasManager(new DummyExtensionPoint()).createAliasManager();
- SyntaxManager syntaxMgr =
- new DefaultSyntaxManager(new DummyExtensionPoint()).createSyntaxManager();
- for (String pluginName : PLUGIN_NAMES) {
- configurePluginCommands(root, pluginName, aliasMgr, syntaxMgr);
- }
- System.setProperty("jnode.invoker", "thread");
- System.setProperty("jnode.interpreter", "redirecting");
- System.setProperty("jnode.debug", "true");
- InitialNaming.bind(AliasManager.NAME, aliasMgr);
- InitialNaming.bind(ShellManager.NAME, new DefaultShellManager());
- InitialNaming.bind(SyntaxManager.NAME, syntaxMgr);
- InitialNaming.bind(HelpFactory.NAME, new DefaultHelpFactory());
- } catch (NamingException ex) {
- throw new EmuException("Problem setting up InitialNaming bindings", ex);
+ try {
+ InitialNaming.bind(DeviceManager.NAME, DeviceManager.INSTANCE);
+ AliasManager aliasMgr =
+ new DefaultAliasManager(new DummyExtensionPoint()).createAliasManager();
+ SyntaxManager syntaxMgr =
+ new DefaultSyntaxManager(new DummyExtensionPoint()).createSyntaxManager();
+ for (String pluginName : PLUGIN_NAMES) {
+ configurePluginCommands(root, pluginName, aliasMgr, syntaxMgr);
}
+ System.setProperty("jnode.invoker", "thread");
+ System.setProperty("jnode.interpreter", "redirecting");
+ System.setProperty("jnode.debug", "true");
+ InitialNaming.bind(AliasManager.NAME, aliasMgr);
+ InitialNaming.bind(ShellManager.NAME, new DefaultShellManager());
+ InitialNaming.bind(SyntaxManager.NAME, syntaxMgr);
+ InitialNaming.bind(HelpFactory.NAME, new DefaultHelpFactory());
+ } catch (NamingException ex) {
+ throw new EmuException("Problem setting up InitialNaming bindings", ex);
}
}
@@ -113,7 +111,7 @@
* @param syntaxMgr the syntax manager to be populated
* @throws EmuException
*/
- private static void configurePluginCommands(File root, String pluginName, AliasManager aliasMgr,
+ private void configurePluginCommands(File root, String pluginName, AliasManager aliasMgr,
SyntaxManager syntaxMgr) throws EmuException {
XMLElement pluginDescriptor = loadPluginDescriptor(root, pluginName);
extractAliases(pluginDescriptor, aliasMgr);
@@ -127,7 +125,7 @@
* @param syntaxMgr the syntax manager to be populated.
* @throws EmuException
*/
- private static void extractSyntaxBundles(XMLElement pluginDescriptor, SyntaxManager syntaxMgr)
+ private void extractSyntaxBundles(XMLElement pluginDescriptor, SyntaxManager syntaxMgr)
throws EmuException {
XMLElement syntaxesDescriptor = findExtension(pluginDescriptor, SyntaxManager.SYNTAXES_EP_NAME);
if (syntaxesDescriptor == null) {
@@ -157,7 +155,7 @@
* @param aliasMgr the alias manager to be populated.
* @throws EmuException
*/
- private static void extractAliases(XMLElement pluginDescriptor, AliasManager aliasMgr) {
+ private void extractAliases(XMLElement pluginDescriptor, AliasManager aliasMgr) {
XMLElement aliasesDescriptor = findExtension(pluginDescriptor, AliasManager.ALIASES_EP_NAME);
if (aliasesDescriptor == null) {
return;
@@ -178,7 +176,7 @@
* @param epName the extension point's name
* @return
*/
- private static XMLElement findExtension(XMLElement pluginDescriptor, String epName) {
+ private XMLElement findExtension(XMLElement pluginDescriptor, String epName) {
for (XMLElement child : pluginDescriptor.getChildren()) {
if (child.getName().equals("extension") &&
epName.equals(child.getStringAttribute("point"))) {
@@ -197,7 +195,7 @@
* @return the loaded plugin descriptor or <code>null</code>
* @throws EmuException
*/
- private static XMLElement loadPluginDescriptor(File root, String pluginName)
+ private XMLElement loadPluginDescriptor(File root, String pluginName)
throws EmuException {
File file = null;
for (String projectName : ALL_PROJECTS) {
Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-01-21 13:32:57 UTC (rev 4897)
+++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-01-21 13:33:53 UTC (rev 4898)
@@ -267,31 +267,9 @@
// Here, we are running in the CommandShell (main) Thread
// so, we can register ourself as the current shell
// (it will also be the current shell for all children Thread)
+
+ configureShell();
- // FIXME - At one point, the 'current shell' had something to do with
- // dispatching keyboard input to the right application. Now this is
- // handled by the console layer. Is 'current shell' a meaningful /
- // useful concept anymore?
- try {
- ShellUtils.getShellManager().registerShell(this);
-
- ShellUtils.registerCommandInvoker(DefaultCommandInvoker.FACTORY);
- ShellUtils.registerCommandInvoker(ThreadCommandInvoker.FACTORY);
- ShellUtils.registerCommandInvoker(ProcletCommandInvoker.FACTORY);
- ShellUtils.registerCommandInvoker(IsolateCommandInvoker.FACTORY);
- ShellUtils.registerCommandInterpreter(DefaultInterpreter.FACTORY);
- ShellUtils
- .registerCommandInterpreter(RedirectingInterpreter.FACTORY);
- } catch (NameNotFoundException e1) {
- e1.printStackTrace();
- }
-
- // Configure the shell based on Syetsm properties.
- setupFromProperties();
-
- // Now become interactive
- ownThread = Thread.currentThread();
-
// Run commands from the JNode command line first
final String cmdLine = System.getProperty(CMDLINE_PROPERTY_NAME, "");
final StringTokenizer tok = new StringTokenizer(cmdLine);
@@ -320,7 +298,7 @@
try {
if (jnode_ini.exists()) {
runCommandFile(jnode_ini);
- } else {
+ } else if (getClass().getResource(name) != null) {
runCommandResource(name);
}
} catch (ShellException ex) {
@@ -340,7 +318,7 @@
try {
if (shell_ini.exists()) {
runCommandFile(shell_ini);
- } else {
+ } else if (getClass().getResource(name) != null) {
runCommandResource(name);
}
} catch (ShellException ex) {
@@ -374,6 +352,28 @@
}
}
+ public void configureShell() {
+ try {
+ ShellUtils.getShellManager().registerShell(this);
+
+ ShellUtils.registerCommandInvoker(DefaultCommandInvoker.FACTORY);
+ ShellUtils.registerCommandInvoker(ThreadCommandInvoker.FACTORY);
+ ShellUtils.registerCommandInvoker(ProcletCommandInvoker.FACTORY);
+ ShellUtils.registerCommandInvoker(IsolateCommandInvoker.FACTORY);
+ ShellUtils.registerCommandInterpreter(DefaultInterpreter.FACTORY);
+ ShellUtils
+ .registerCommandInterpreter(RedirectingInterpreter.FACTORY);
+ } catch (NameNotFoundException e1) {
+ e1.printStackTrace();
+ }
+
+ // Configure the shell based on System properties.
+ setupFromProperties();
+
+ // Now become interactive
+ ownThread = Thread.currentThread();
+ }
+
private void setupFromProperties() {
debugEnabled = Boolean.parseBoolean(System.getProperty(
DEBUG_PROPERTY_NAME, DEBUG_DEFAULT));
@@ -819,8 +819,7 @@
boolean enabled = setHistoryEnabled(false);
try {
CommandInterpreter interpreter = createInterpreter(new FileReader(file));
- // FIXME throw ShellException if interpreter not found
- return (interpreter == null) ? -1 : interpreter.interpret(this, file);
+ return interpreter.interpret(this, file);
} catch (IOException ex) {
throw new ShellException("Cannot open command file: " + ex.getMessage(), ex);
} finally {
@@ -835,15 +834,11 @@
// FIXME throw ShellException if resource or interpreter not found
InputStream input = getClass().getResourceAsStream(resource);
if (input == null) {
- result = -1; // resource doesn't exist
+ throw new ShellException("Cannot find resource '" + resource + "'");
} else {
CommandInterpreter interpreter = createInterpreter(new InputStreamReader(input));
- if (interpreter == null) {
- result = -1; // no interpreter !
- } else {
- Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource));
- result = interpreter.interpret(this, reader);
- }
+ Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource));
+ result = interpreter.interpret(this, reader);
}
return result;
} finally {
@@ -856,11 +851,13 @@
final BufferedReader br = new BufferedReader(reader);
CommandInterpreter interpreter;
String line = br.readLine();
- if (line == null) {
- interpreter = null; // stream is empty
- } else if (line.startsWith("#!")) {
+ if (line != null && line.startsWith("#!")) {
String name = line.substring(2);
interpreter = ShellUtils.createInterpreter(name);
+ if (interpreter == null) {
+ throw new ShellException("Cannot execute script: no '" +
+ name + "' interpreter is registered");
+ }
} else {
interpreter = this.interpreter;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|