Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.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.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2009 Yoko Harada <yokolet@gmail.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 
 package org.jruby.embed.jsr223;
 
 import java.util.List;
This is a substitute of javax.script.ScriptEngineManager. With this script engine manager, you can avoid two known troubles. One this happens on OS X JDK 5 which tries to load AppleScriptEngine and ends up in the exception. Another one happens when you use livetribe version of javax.script and GLOBAL_SCOPE. The livetribe javax.script has a bug to handle GLOBAL_SCOPE.

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 public class JRubyScriptEngineManager {
     private static final String service = "META-INF/services/javax.script.ScriptEngineFactory";
     private Bindings globalMap;
 
     public JRubyScriptEngineManager() throws ScriptException {
         this(null);
     }
 
     public JRubyScriptEngineManager(ClassLoader loaderthrows ScriptException {
         init(loader);
     }
 
     private void init(ClassLoader loaderthrows ScriptException {
          = new HashMap();
          = new HashMap();
          = new HashMap();
          = new SimpleBindings();
         try {
              =
                 (HashSet<ScriptEngineFactory>) (new ServiceFinder(loader)).getServices();
             if (.isEmpty()) {
                 ..println("no factory");
             }
             prepareMaps();
         } catch (Exception e) {
             e.printStackTrace();
             throw new ScriptException(e);
         }
     }
 
     private void prepareMaps() {
         for (ScriptEngineFactory factory : ) {
             List<Stringnames = factory.getNames();
             for (String name : names) {
                 .put(namefactory);
             }
             List<Stringextensions = factory.getExtensions();
             for (String extension : extensions) {
                 .put(extensionfactory);
             }
             List<StringmimeTypes = factory.getMimeTypes();
             for (String mimeType : mimeTypes) {
                .put(mimeTypefactory);
            }
        }
    }
    public void setBindings(Bindings b) {
        if (b == null) {
            throw new IllegalArgumentException("Null bindings");
        }
         = b;
    }
    public Bindings getBindings() {
        return ;
    }
    public void put(String keyObject value) {
        .put(keyvalue);
    }
    public Object get(String key) {
        return .get(key);
    }
    public ScriptEngine getEngineByName(String shortName) {
        if (shortName == null) {
            throw new NullPointerException("Null shortName");
        }
        ScriptEngineFactory factory = .get(shortName);
        if (factory == null) {
            throw new IllegalArgumentException("No engine for " + shortName);
        }
        ScriptEngine engine = factory.getScriptEngine();
        return engine;
    }
    public ScriptEngine getEngineByExtension(String extension) {
        if (extension == null) {
            throw new NullPointerException("Null extension");
        }
        ScriptEngineFactory factory = .get(extension);
        if (factory == null) {
            throw new IllegalArgumentException("No engine for " + extension);
        }
        ScriptEngine engine = factory.getScriptEngine();
        return engine;
    }
    public ScriptEngine getEngineByMimeType(String mimeType) {
        if (mimeType == null) {
            throw new NullPointerException("Null mimeType");
        }
        ScriptEngineFactory factory = .get(mimeType);
        if (factory == null) {
            throw new IllegalArgumentException("No engine for " + mimeType);
        }
        ScriptEngine engine = factory.getScriptEngine();
        return engine;
    }
        List<ScriptEngineFactoryl = new ArrayList();
        for (ScriptEngineFactory factory : ) {
            l.add(factory);
        }
        return Collections.unmodifiableList(l);
    }
    public void registerEngineName(String nameScriptEngineFactory factory) {
        if (name == null || factory == null) {
            throw new NullPointerException("name and/or factory is null.");
        }
        .put(namefactory);
    }
    public void registerEngineMimeType(String typeScriptEngineFactory factory) {
        if (type == null || factory == null) {
            throw new NullPointerException("type and/or factory is null.");
        }
        .put(typefactory);
    }
    public void registerEngineExtension(String extensionScriptEngineFactory factory) {
        if (extension == null || factory == null) {
            throw new NullPointerException("extension and/or factory is null.");
        }
        .put(extensionfactory);
    }
New to GrepCode? Check out our FAQ X