You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@jakarta.apache.org by se...@apache.org on 2010/05/21 02:34:03 UTC
svn commit: r946837 -
/jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java
Author: sebb
Date: Fri May 21 00:34:03 2010
New Revision: 946837
URL: http://svn.apache.org/viewvc?rev=946837&view=rev
Log:
Add -show providers function
If cannot instantiate ScriptEngineManager, try to find out why
Modified:
jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java
Modified: jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java
URL: http://svn.apache.org/viewvc/jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java?rev=946837&r1=946836&r2=946837&view=diff
==============================================================================
--- jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java (original)
+++ jakarta/bsf/branches/bsf3.x/bsf-utils/src/main/java/org/apache/bsf/Main.java Fri May 21 00:34:03 2010
@@ -18,14 +18,19 @@
*/
package org.apache.bsf;
+import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
+import javax.imageio.spi.ServiceRegistry;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.Invocable;
@@ -37,7 +42,8 @@ import javax.script.ScriptException;
/**
* This is the main driver for BSF to be run on the command line
- * to eval scripts directly.
+ * to eval scripts directly. Also provides information on the
+ * engines, factories and providers.
*
* @author Sanjiva Weerawarana
* @author Matthew J. Duftler
@@ -46,6 +52,7 @@ import javax.script.ScriptException;
public class Main {
private static final String SHOW_ENGINE = "engine";
private static final String SHOW_FACTORIES = "factories";
+ private static final String SHOW_PROVIDERS = "providers";
private static final String ARG_IN = "-in";
private static final String ARG_LANG = "-lang";
private static final String ARG_EXT = "-ext";
@@ -74,7 +81,19 @@ public class Main {
String language = (String) argsTable.get(ARG_LANG);
String show = (String) argsTable.get(ARG_SHOW);
- ScriptEngineManager mgr = new ScriptEngineManager();
+ if (SHOW_PROVIDERS.equalsIgnoreCase(show)) {
+ processProviders(false);
+ return;
+ }
+ ScriptEngineManager mgr;
+ try {
+ mgr = new ScriptEngineManager();
+ } catch (Error e1) {
+ System.out.println("Could not create ScriptEngineManager: "+e1);
+ System.out.println("Checking which factories cannot be instantiated ...");
+ processProviders(true);
+ return;
+ }
final List engineFactories = mgr.getEngineFactories();
if (engineFactories.isEmpty()){
throw new RuntimeException("Could not find any engine factories");
@@ -100,15 +119,6 @@ public class Main {
throw new IllegalArgumentException("unable to determine language");
}
- Reader in;
-
- if (inFileName != null) {
- in = new FileReader(inFileName);
- } else {
- in = new InputStreamReader(System.in);
- inFileName = DEFAULT_IN_FILE_NAME;
- }
-
try {
ScriptEngine engine;
if (language != null) {
@@ -133,16 +143,77 @@ public class Main {
System.err.println("Engine supports Invocable interface.");
}
System.err.println();
+ return;
}
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("args", args);
+
+ Reader in;
+ if (inFileName != null) {
+ in = new FileReader(inFileName);
+ } else {
+ in = new InputStreamReader(System.in);
+ inFileName = DEFAULT_IN_FILE_NAME;
+ }
Object obj = engine.eval(in);
System.err.println("Result: " + obj);
+ in.close();
} catch (ScriptException e) {
e.printStackTrace();
}
}
+ /**
+ * Process provider files - check or print contents.
+ * @param check if true, check which factories cannot be instantiate; otherwise print file contents
+ * @throws IOException if the resource cannot be opened/read etc.
+ */
+ private static void processProviders(boolean check) throws IOException {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ HashSet set = new HashSet();
+ if (check){
+ System.out.println("Generating list of providers ...");
+ }
+ Enumeration en =cl.getResources("META-INF/services/javax.script.ScriptEngineFactory");
+ while(en.hasMoreElements()){
+ URL url = (URL) en.nextElement();
+ if (!check){
+ System.out.println(url);
+ }
+ BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
+ String line;
+ while((line = br.readLine()) != null){
+ if (check){
+ // Store the factory name
+ String nocomment=line.split("#", 2)[0].trim();
+ if (nocomment.length() > 0){
+ set.add(nocomment);
+ }
+ } else {
+ System.out.println(line);
+ }
+ }
+ br.close();
+ }
+ if (check){
+ System.out.println("Checking for provider errors ...");
+ Iterator iterator = ServiceRegistry.lookupProviders(ScriptEngineFactory.class, cl);
+ while(iterator.hasNext()){
+ try {
+ Object provider = iterator.next();
+ set.remove(provider.getClass().getName());
+ } catch (ThreadDeath td) { // must not ignore this
+ throw td;
+ // See BSF-30 - iterator may throw Error
+ } catch (Error error) {
+ System.out.println(error);
+ }
+ }
+ System.out.println("Following factories could not be instantiated:");
+ System.out.println(set.toString());
+ }
+ }
+
private static void showFactory(ScriptEngineFactory fac, boolean full) {
StringBuffer sb = new StringBuffer();
sb.append('[');
@@ -258,6 +329,8 @@ public class Main {
" " + SHOW_FACTORIES + " - list all engine factories and exit");
System.err.println(
" " +SHOW_ENGINE + " - list details of selected factory and engine");
+ System.err.println(
+ " " +SHOW_PROVIDERS + " - list details of providers (services files)");
System.err.println();
System.err.println();
System.err.println(
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@jakarta.apache.org
For additional commands, e-mail: notifications-help@jakarta.apache.org