You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/05/20 01:04:28 UTC

svn commit: r407929 - in /geronimo/sandbox/gshell/trunk: gshell-cli/ gshell-commands/ gshell-commands/scripting-commands/ gshell-commands/scripting-commands/src/ gshell-commands/scripting-commands/src/main/ gshell-commands/scripting-commands/src/main/j...

Author: jdillon
Date: Fri May 19 16:04:27 2006
New Revision: 407929

URL: http://svn.apache.org/viewvc?rev=407929&view=rev
Log:
An initial stab at scripting support

Added:
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/script.properties
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/components.xml
Modified:
    geronimo/sandbox/gshell/trunk/gshell-cli/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-commands/pom.xml

Modified: geronimo/sandbox/gshell/trunk/gshell-cli/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-cli/pom.xml?rev=407929&r1=407928&r2=407929&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-cli/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-cli/pom.xml Fri May 19 16:04:27 2006
@@ -32,7 +32,13 @@
             <artifactId>standard-commands</artifactId>
             <version>${pom.version}</version>
         </dependency>
-        
+
+        <dependency>
+            <groupId>${pom.groupId}</groupId>
+            <artifactId>scripting-commands</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
+
         <dependency>
             <groupId>classworlds</groupId>
             <artifactId>classworlds</artifactId>

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/pom.xml?rev=407929&r1=407928&r2=407929&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/pom.xml Fri May 19 16:04:27 2006
@@ -35,6 +35,7 @@
     
     <modules>
         <module>standard-commands</module>
+        <module>scripting-commands</module>
     </modules>
     
 </project>

Added: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/pom.xml?rev=407929&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/pom.xml (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/pom.xml Fri May 19 16:04:27 2006
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- $Id -->
+
+<project
+    xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    
+    <modelVersion>4.0.0</modelVersion>
+    
+    <parent>
+        <groupId>org.apache.geronimo.gshell</groupId>
+        <artifactId>gshell-commands</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+    
+    <artifactId>scripting-commands</artifactId>
+    <name>GShell Commands :: scripting</name>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>bsf</groupId>
+            <artifactId>bsf</artifactId>
+            <version>2.3.0</version>
+            <type>jar</type>
+            <scope>compile</scope>
+        </dependency>
+    </dependencies>
+    
+</project>

Added: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java?rev=407929&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java Fri May 19 16:04:27 2006
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.commands.scripting;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.gshell.console.Console;
+import org.apache.bsf.BSFEngine;
+
+/**
+ * ???
+ *
+ * @version $Id: IO.java 399599 2006-05-04 08:13:57Z jdillon $
+ */
+public class InteractiveInterpreter
+    implements Runnable
+{
+    private static final Log log = LogFactory.getLog(InteractiveInterpreter.class);
+
+    private BSFEngine engine;
+
+    private Console console;
+
+    private String prompt;
+
+    public InteractiveInterpreter(final Console console, final BSFEngine engine, final String prompt) {
+        assert console != null;
+        assert engine != null;
+        assert prompt != null;
+
+        this.console = console;
+        this.engine = engine;
+        this.prompt = prompt;
+    }
+
+    public void run() {
+        log.info("Running...");
+
+        while (true) {
+            try {
+                String line;
+
+                while ((line = console.readLine(prompt)) != null) {
+                    log.debug("Read line: " + line);
+
+                    //
+                    // TODO: Need to handle an "exit" of some sort... ctrl-d or something... ?
+                    //
+                    
+                    // Just ignore blank lines
+                    if (line.trim().equals("")) {
+                        continue;
+                    }
+
+                    engine.exec("<unknown>", 1, 1, line);
+                }
+
+            }
+            catch (Exception e) {
+                log.error("Unhandled failure", e);
+            }
+        }
+    }
+}

Added: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java?rev=407929&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java Fri May 19 16:04:27 2006
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.commands.scripting;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.PosixParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.lang.StringUtils;
+
+import org.apache.geronimo.gshell.command.Command;
+import org.apache.geronimo.gshell.command.CommandSupport;
+import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.console.JLineConsole;
+import org.apache.bsf.BSFManager;
+import org.apache.bsf.BSFEngine;
+
+/**
+ * Provides generic scripting language integration via <a href="http://http://jakarta.apache.org/bsf">BSF</a>.
+ *
+ * @version $Id: CatCommand.java 399599 2006-05-04 08:13:57Z jdillon $
+ */
+public class ScriptCommand
+    extends CommandSupport
+{
+    private String language;
+
+    private boolean interactive = false;
+
+    private String expression
+            ;
+
+    public ScriptCommand() {
+        super("script");
+    }
+
+    protected int doExecute(final String[] args) throws Exception {
+        assert args != null;
+
+        //
+        // TODO: Optimize, move common code to CommandSupport
+        //
+
+        IO io = getIO();
+
+        Options options = new Options();
+
+        options.addOption(OptionBuilder.withLongOpt("help")
+            .withDescription("Display this help message")
+            .create('h'));
+
+        options.addOption(OptionBuilder.withLongOpt("lang")
+            .withDescription("Specify the scripting language")
+            .hasArg()
+            .create('l'));
+
+        options.addOption(OptionBuilder.withLongOpt("expression")
+            .withDescription("Evaluate the given expression")
+            .hasArg()
+            .create('e'));
+
+        options.addOption(OptionBuilder.withLongOpt("interactive")
+            .withDescription("Run interactive mode")
+            .create('i'));
+
+        CommandLineParser parser = new PosixParser();
+        CommandLine line = parser.parse(options, args);
+
+        if (line.hasOption('h')) {
+            io.out.println(getName() + " -- scripting language integration");
+            io.out.println();
+
+            HelpFormatter formatter = new HelpFormatter();
+            formatter.printHelp(
+                io.out,
+                80, // width (FIXME: Should pull from gshell.columns variable)
+                getName() + " [options]",
+                "",
+                options,
+                4, // left pad
+                4, // desc pad
+                "",
+                false); // auto usage
+
+            io.out.println();
+
+            return Command.SUCCESS;
+        }
+
+        if (line.hasOption('l')) {
+            this.language = line.getOptionValue('l');
+        }
+
+        if (line.hasOption('e')) {
+            this.expression = line.getOptionValue('e');
+        }
+
+        if (line.hasOption('i')) {
+            this.interactive = true;
+        }
+
+        //
+        // TODO: When given a file/url, try to figure out language from ext if language not given
+        //
+
+        if (language == null) {
+            throw new RuntimeException("Must specify a language");
+        }
+
+        if (!BSFManager.isLanguageRegistered(language)) {
+            throw new RuntimeException("Language is not registered: " + language);
+        }
+
+        BSFManager manager = new BSFManager();
+        BSFEngine engine = manager.loadScriptingEngine(language);
+
+        if (this.expression != null) {
+            log.info("Evaluating expression: " + expression);
+
+            Object obj = engine.eval("<unknown>", 1, 1, expression);
+
+            log.info("Expression result: " + obj);
+        }
+
+        if (this.interactive) {
+            String prompt = "script:" + language + ">";
+            InteractiveInterpreter interp = new InteractiveInterpreter(new JLineConsole(getIO()), engine, prompt);
+            interp.run();
+        }
+
+        return Command.SUCCESS;
+    }
+}

Added: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/script.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/script.properties?rev=407929&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/script.properties (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/script.properties Fri May 19 16:04:27 2006
@@ -0,0 +1,12 @@
+##
+## $Id$
+##
+
+class=org.apache.geronimo.gshell.commands.scripting.ScriptCommand
+
+name=script
+
+#aliases=
+
+category=scripting
+

Added: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/components.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/components.xml?rev=407929&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/components.xml (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/resources/META-INF/org.apache.geronimo.gshell/components.xml Fri May 19 16:04:27 2006
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<!-- $Id: components.xml 406799 2006-05-16 02:03:13Z jdillon $ -->
+
+<beans>
+
+    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
+    </bean>
+    
+    <bean id="script" class="org.apache.geronimo.gshell.commands.scripting.ScriptCommand" singleton="false">
+    </bean>
+    
+</beans>