Package bsh

Examples of bsh.Interpreter


    private Interpreter bshInterpreter;
 
  public BeanShellSampler()
  {
    try{
      bshInterpreter = new Interpreter();
    } catch (NoClassDefFoundError e){
      bshInterpreter=null;
    }
  }
View Full Code Here


            Debug.logVerbose("Evaluating -- " + expression, module);
        if (Debug.verboseOn())
            Debug.logVerbose("Using Context -- " + context, module);

        try {
            Interpreter bsh = makeInterpreter(context);
            // evaluate the expression
            o = bsh.eval(expression);
            if (Debug.verboseOn())
                Debug.logVerbose("Evaluated to -- " + o, module);

            // read back the context info
            NameSpace ns = bsh.getNameSpace();
            String[] varNames = ns.getVariableNames();
            for (String varName: varNames) {
                context.put(varName, bsh.get(varName));
            }
        } catch (EvalError e) {
            Debug.logError(e, "BSH Evaluation error.", module);
            throw e;
        }
View Full Code Here

        }
        return o;
    }

    public static Interpreter makeInterpreter(Map<String, ? extends Object> context) throws EvalError {
        Interpreter bsh = getMasterInterpreter(null);
        // Set the context for the condition
        if (context != null) {
            for (Map.Entry<String, ? extends Object> entry: context.entrySet()) {
                bsh.set(entry.getKey(), entry.getValue());
            }

            // include the context itself in for easier access in the scripts
            bsh.set("context", context);
        }

        return bsh;
    }
View Full Code Here

            BshUtil.masterClassManagers.putIfAbsent(classLoader, master);
            master = BshUtil.masterClassManagers.get(classLoader);
        }

        if (master != null) {
            Interpreter interpreter = new Interpreter(new StringReader(""), System.out, System.err,
                    false, new NameSpace(master, "global"), null, null);
            return interpreter;
        } else {
            Interpreter interpreter = new Interpreter();
            interpreter.setClassLoader(classLoader);
            return interpreter;
        }
    }
View Full Code Here

        }
    }

    public static Object runBshAtLocation(String location, Map<String, ? extends Object> context) throws GeneralException {
        try {
            Interpreter interpreter = makeInterpreter(context);

            Interpreter.ParsedScript script = null;
            script = parsedScripts.get(location);
            if (script == null) {
                synchronized (OfbizBshBsfEngine.class) {
                    script = parsedScripts.get(location);
                    if (script == null) {
                        URL scriptUrl = FlexibleLocation.resolveLocation(location);
                        if (scriptUrl == null) {
                            throw new GeneralException("Could not find bsh script at [" + location + "]");
                        }
                        Reader scriptReader = new InputStreamReader(scriptUrl.openStream());
                        script = interpreter.parseScript(location, scriptReader);
                        if (Debug.verboseOn()) Debug.logVerbose("Caching BSH script at: " + location, module);
                        parsedScripts.put(location, script);
                    }
                }
            }

            return interpreter.evalParsedScript(script);
        } catch (MalformedURLException e) {
            String errMsg = "Error loading BSH script at [" + location + "]: " + e.toString();
            Debug.logError(e, errMsg, module);
            throw new GeneralException(errMsg, e);
        } catch (ParseException e) {
View Full Code Here

                    ScriptFrameworkErrorType.UNKNOWN );
            }
            // Set class loader to be used for class files
            // and jar files
            Thread.currentThread().setContextClassLoader(cl);
            Interpreter interpreter = new Interpreter();

            interpreter.getNameSpace().clear();
            // Set class loader to be used by interpreter
            // to look for classes by source e.g. interpreter
            // will use this classloader to search classpath
            // for source file ( bla.java ) on import or reference
            interpreter.setClassLoader(cl);
            try {
                interpreter.set("XSCRIPTCONTEXT",
                    ScriptContext.createContext(m_xModel, m_xInvocContext,
                        m_xContext, m_xMultiComponentFactory));

                interpreter.set("ARGUMENTS", aParams);
            }
            catch (bsh.EvalError e) {
                // Framework error setting up context
                throw new ScriptFrameworkErrorException(
                    e.getMessage(), null,
                    metaData.getLanguageName(), metaData.getLanguage(),
                    ScriptFrameworkErrorType.UNKNOWN );
            }

            try {
                String source = null;
                Object result = null;

                ScriptEditorForBeanShell editor =
                    ScriptEditorForBeanShell.getEditor(
                       sourceUrl );

                if ( editor != null )
                {
                    result = editor.execute();

                    if (result == null)
                    {
                        return new Any(new Type(), null);
                    }
                    return result;
                }

                metaData.loadSource()
                source = metaData.getSource();

                if ( source == null || source.length() == 0 )
                {
                  throw new ScriptFrameworkErrorException(
                        "Failed to read script", null,
                      metaData.getLanguageName(), metaData.getLanguage(),
                      ScriptFrameworkErrorType.UNKNOWN );
                }
                result = interpreter.eval( source );

                if (result == null)
                {
                    return new Any(new Type(), null);
                }
View Full Code Here

        PrintStream origOut = System.out;
        PrintStream origErr = System.err;

        try
        {
            Interpreter engine = new Interpreter();

            if ( scriptOutput != null )
            {
                System.setErr( scriptOutput );
                System.setOut( scriptOutput );
                engine.setErr( scriptOutput );
                engine.setOut( scriptOutput );
            }

            if ( !Capabilities.haveAccessibility() )
            {
                try
                {
                    Capabilities.setAccessibility( true );
                }
                catch ( Exception e )
                {
                    if ( scriptOutput != null )
                    {
                        e.printStackTrace( scriptOutput );
                    }
                }
            }

            if ( classPath != null && !classPath.isEmpty() )
            {
                for ( Iterator it = classPath.iterator(); it.hasNext(); )
                {
                    String path = (String) it.next();
                    try
                    {
                        engine.getClassManager().addClassPath( new File( path ).toURI().toURL() );
                    }
                    catch ( IOException e )
                    {
                        throw new RuntimeException( "bad class path: " + path, e );
                    }
                }
            }

            if ( globalVariables != null )
            {
                for ( Iterator it = globalVariables.keySet().iterator(); it.hasNext(); )
                {
                    String variable = (String) it.next();
                    Object value = globalVariables.get( variable );
                    try
                    {
                        engine.set( variable, value );
                    }
                    catch ( EvalError e )
                    {
                        throw new RuntimeException( e );
                    }
                }
            }

            try
            {
                return engine.eval( script );
            }
            catch ( TargetError e )
            {
                throw new ScriptEvaluationException( e.getTarget() );
            }
View Full Code Here

   */
  protected Interpreter createInterpreter()
  {
    try
    {
      final Interpreter interpreter = new Interpreter();
      initializeInterpreter(interpreter);
      return interpreter;
    }
    catch (Throwable e)
    {
View Full Code Here

    protected Object executeBeanShellScript(String beanShellScript, Map context) throws EvalError {
        // Ensure there is something to execute.
        if (beanShellScript == null || beanShellScript.trim().equals("")) return null;

        // Initialize the BeanShell interpreter.
        Interpreter bshInterpreter = (Interpreter) _bshIntepreterThread.get();
        if (bshInterpreter == null) {
            bshInterpreter = new Interpreter();
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (loader != null) bshInterpreter.setClassLoader(loader);
            _bshIntepreterThread.set(bshInterpreter);
        }

        // Set context.
        Iterator contextIt = context.keySet().iterator();
        while (contextIt.hasNext()) {
            String contextVar = (String) contextIt.next();
            bshInterpreter.set(contextVar, context.get(contextVar));
        }

        // Launch the BeanShell script.
        return bshInterpreter.eval(beanShellScript);
    }
View Full Code Here

  private THashMap name2var = new THashMap ();

  public static Assignment readFromMatrix (VarSet vars, Reader in) throws IOException
  {
    Variable[] varr = vars.toVariableArray ();
    Interpreter interpreter = new Interpreter ();
    BufferedReader bIn = new BufferedReader (in);
    Assignment assn = new Assignment ();
    String line;

    while ((line = bIn.readLine ()) != null) {
      String[] fields = line.split ("\\s+");
      Object[] vals = new Object [fields.length];
      for (int i = 0; i < fields.length; i++) {
        try {
          vals[i] = interpreter.eval (fields[i]);
        } catch (EvalError e) {
          throw new RuntimeException ("Error reading line: "+line, e);
        }
      }
      assn.addRow (varr, vals);
View Full Code Here

TOP

Related Classes of bsh.Interpreter

Copyright © 2018 www.massapicom. 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.