Package modTransf.script

Source Code of modTransf.script.CompiledScriptFactoryImpl

//Source file: H:\\temp\\generated\\ispuml\\mdaTransformation\\script\\CompiledScriptFactoryImpl.java

package modTransf.script;

import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.Compilable;
import javax.script.ScriptException;
import javax.script.CompiledScript;

/**
* Implementation of the script factory.
*/
public class CompiledScriptFactoryImpl
{

   /**
    * Log for scripts.
    */
   protected static Log log = LogFactory.getLog("modTransf.script.ScriptLog");


    /** The common script engine manager */
   protected ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

     /** The registered compilers by their names */
    protected Map compilers;

    /** The default script engine name*/
    protected String defaultScriptEngineName;
    /** The default script engine */
    protected Compilable defaultScriptEngine;


    /**
     * Default constructor.
     * Initialize the default compiler with ELScriptCompiler
     */
    public CompiledScriptFactoryImpl()
   {
     this.defaultScriptEngineName = "rhino";
   }

   /**
    * Constructor.
    * Create the factory and set the default ScriptEngine by its name.
    */
   public CompiledScriptFactoryImpl( String name)
  {
    this.defaultScriptEngineName = name;
  }

  /**
   * Compile the provided Script using the default language and return an
   * executable CompiledScript.
   *
   * @param script String The script to compile.
   * @return CompiledScript
   * @throws ScriptException
   */
  public CompiledScript compileScript(String script)
    throws ScriptException
  {
    return getDefaulCompiler().compile(script);
  }

   /**
    * Get the compiled expression of the expresion provided as a String, and using
    * the specyfied language.
    * If language is null, use the default language.
    * Throws NameNotFoundException if the language is not defined.
    * @param expr
    * @param language
    * @return CompiledExpression
    */
   public CompiledScript compileScript(String script, String language)
     throws ScriptException
   {
     if(language == null )
       return compileScript(script);

     Compilable compiler = lookupScriptEngine(language);

    try
    {
      return ((Compilable)compiler).compile(script);
    }
    catch(ClassCastException ex)
    {
      throw new ScriptException("Script engine for '"
                                + language + "' doesn't support CompiledScript." );
    }
   }

   /**
    * Lookup the compiler for the specified language.
    * Compilers are stored in a cache, so successive calls for the same language
    * will return the same compiler.
    * @param language String
    * @throws ScriptException
    * @return ScriptEngine
    */
   protected Compilable lookupScriptEngine( String language )
     throws ScriptException
   {
     // initialize the cache if needed
     if( compilers == null )
       compilers = new HashMap();

     // lookup in the cache
     Compilable compiler = (Compilable)compilers.get( language);
     if( compiler != null )
       return compiler;

     // No compiler created for this language. Lookup in ScriptManager
     compiler = (Compilable)scriptEngineManager.getEngineByName(language);
     if( compiler == null )
       throw new ScriptException("No compiler registered under name '" + language + "'.");

     // Add in cache
     compilers.put(language, compiler);
     return compiler;
   }

    /**
     * Create and register the default compiler as ELCompiler.
     */
    protected void createDefaultCompiler()
      throws ScriptException
    {
      defaultScriptEngine = lookupScriptEngine( defaultScriptEngineName );
    }

    /**
     * Load the default compiler.
     */
    protected Compilable getDefaulCompiler()
      throws ScriptException
    {
    if( defaultScriptEngine == null )
      createDefaultCompiler();

     return defaultScriptEngine;
    }


}
TOP

Related Classes of modTransf.script.CompiledScriptFactoryImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.