geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r612939 - /geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java
Date Thu, 17 Jan 2008 19:22:25 GMT
Author: jdillon
Date: Thu Jan 17 11:22:23 2008
New Revision: 612939

URL: http://svn.apache.org/viewvc?rev=612939&view=rev
Log:
(GSHELL-48, GSHELL-49) Add file/URL support and error handling for --language

Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java

Modified: geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java?rev=612939&r1=612938&r2=612939&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-bsf/src/main/java/org/apache/geronimo/gshell/commands/bsf/ScriptCommand.java
Thu Jan 17 11:22:23 2008
@@ -19,10 +19,17 @@
 
 package org.apache.geronimo.gshell.commands.bsf;
 
+import java.io.File;
+import java.net.URI;
+import java.net.URL;
+import java.util.List;
+
 import jline.Terminal;
 import org.apache.bsf.BSFEngine;
 import org.apache.bsf.BSFManager;
+import org.apache.geronimo.gshell.clp.Argument;
 import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.command.CommandException;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
 import org.apache.geronimo.gshell.command.annotation.Requirement;
@@ -43,7 +50,7 @@
 
     private String language;
 
-    @Option(name="-l", aliases={"--language"}, required=true, description="Specify the scripting
language")
+    @Option(name="-l", aliases={"--language"}, description="Specify the scripting language")
     private void setLanguage(final String language) {
         assert language != null;
         
@@ -59,14 +66,33 @@
 
     @Option(name="-e", aliases={"--expression"}, description="Evaluate the given expression")
     private String expression;
+    
+    @Argument(description="A file or URL to execute")
+    private List<String> args;
 
     protected Object doExecute() throws Exception {
         //
         // TODO: When given a file/url, try to figure out language from ext if language not
given
         //       https://issues.apache.org/jira/browse/GSHELL-49
         //
+        
+        String path = null;
+        String filename = null;
+
+    	if (args != null) {
+    		path = args.get(0); // Only allowing one script for now
+    		filename = ((path.lastIndexOf('/')) == -1) ? path : path.substring(path.lastIndexOf('/')
+ 1) ; // Just the filename, please   		
+    	}
+    	
+    	if (language == null) {
+    		if (filename == null) {
+    			throw new CommandException("If a file/URL is not provided, language must" +
+    					" be specified using the -l (--language) option.");
+    		}
+    		language = BSFManager.getLangFromFilename(filename);   		
+    	}
 
-        BSFManager manager = new BSFManager();
+    	BSFManager manager = new BSFManager();
         final BSFEngine engine = manager.loadScriptingEngine(language);
 
         if (this.expression != null) {
@@ -76,14 +102,23 @@
 
             log.info("Expression result: " + obj);
         }
-        else {
-            // No expression, assume interactive (else we don't do anything)
-            interactive = true;
-
-            //
-            // TODO: This will change when file/URL processing is added
-            //       https://issues.apache.org/jira/browse/GSHELL-48
-            //
+        else if (path != null){
+        	log.info("Evaluating file: " + path);
+        	//Make a file.  Is it really a file?  Sweet, execute it.
+        	//Is it not a file?  Make a URI, convert that to a URL, or maybe just make a URL,
check on that.  Execute it.
+        	//Do it in a nifty way so mother would be proud.
+        	File pathFile = new File(path);
+        	URL pathURL;
+        	if (pathFile.isFile()) {
+        		pathURL = pathFile.toURL();
+        	} else {
+        		URI pathURI = new URI(path);
+        		pathURL = pathURI.toURL();
+        	}
+        	
+        	engine.exec(path, 1, 1, pathURL.getContent());
+        	
+        	log.info("Finished executing script: " + filename);
         }
 
         if (this.interactive) {



Mime
View raw message