com.sector91.wit.Scripting.java Source code

Java tutorial

Introduction

Here is the source code for com.sector91.wit.Scripting.java

Source

// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //

////   ///   /// ///       
//////  ////   // ////  //// 
/// ////  ////  //  ////  //// 
///  //// /////  //        ///  
///  //// ///// //  //// ////// 
//   /// /////  //  ////  ////  
// //// ///// //  ////  ////   
/////////////  ////  ////   
////////////   ///   ///    
///// /////   ////  ////    
///// /////   //// ///// // 
////  ////    /// ///// //  
///// /////   ////////////   
////  ////     ////  ////    

// The Web framework with class.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //

// Copyright (c) 2013 Adam R. Nelson
//
// 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 com.sector91.wit;

import java.util.List;
import java.util.NavigableMap;
import java.util.TreeMap;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

public final class Scripting {

    private Scripting() {
    }

    public static final List<String> HINT_EXTENSIONS = ImmutableList.of("bsh", "java", "groovy", "js", "py", "rb");

    public static NavigableMap<String, ScriptEngineFactory> scriptEngines(ClassLoader cl) {
        final NavigableMap<String, ScriptEngineFactory> map = new TreeMap<>();
        final ScriptEngineManager mgr = new ScriptEngineManager(cl);
        for (ScriptEngineFactory factory : mgr.getEngineFactories()) {
            for (String ext : factory.getExtensions()) {
                map.put(ext, factory);
            }
        }
        return Maps.unmodifiableNavigableMap(map);
    }

    public static String scriptHintMessage(String ext, ClassLoader cl) {
        switch (ext) {
        case "bsh":
        case "java":
            try {
                Class.forName("bsh.Interpreter", false, cl);
                return "BeanShell scripting support requires BeanShell v2.0b5 or greater."
                        + " It appears that you are using an older version.";
            } catch (ClassNotFoundException ex) {
                return "BeanShell scripting support requires BeanShell v2.0b5 or greater."
                        + " No BeanShell JAR could be found.";
            }
        case "groovy":
            return "Groovy scripting support requires the Groovy JAR file.";
        case "js":
            return "JavaScript support should be built into the JVM... Something may be weird about"
                    + " your Java setup.";
        case "py":
            return "Python scripting support requires a Jython JAR with JSR-223 support"
                    + " (Version 2.5.1 or newer).";
        case "rb":
            return "Ruby scripting support requires a recent version of JRuby that includes" + " jruby-engine.";
        default:
            return "The script extension '" + ext + "' is not recognized, and no installed JSR-223"
                    + " ScriptEngine supports it.";
        }
    }
}