Java Code Examples for org.apache.bsf.BSFManager#declareBean()

The following examples show how to use org.apache.bsf.BSFManager#declareBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FaenorInterface.java    From L2jBrasil with GNU General Public License v3.0 6 votes vote down vote up
public void addPetData(BSFManager context, int petID, int levelStart, int levelEnd, Map<String, String> stats)
throws BSFException
  {
      L2PetData[] petData = new L2PetData[levelEnd - levelStart + 1];
      int value           = 0;
      for (int level = levelStart; level <= levelEnd; level++)
      {
          petData[level - 1]  = new L2PetData();
          petData[level - 1].setPetID(petID);
          petData[level - 1].setPetLevel(level);

       context.declareBean("level", new Double(level), Double.TYPE);
          for (String stat : stats.keySet())
          {
		value = ((Number)Expression.eval(context, "beanshell", stats.get(stat))).intValue();
              petData[level - 1].setStat(stat, value);
          }
       context.undeclareBean("level");
      }

  }
 
Example 2
Source File: BSFReportPreProcessor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public MasterReport performPreDataProcessing( final MasterReport definition,
    final DefaultFlowController flowController ) throws ReportProcessingException {
  if ( preDataScript == null || language == null || StringUtils.isEmpty( preDataScript, true ) ) {
    return definition;
  }

  try {
    final BSFManager interpreter = new BSFManager();
    interpreter.declareBean( "definition", definition, MasterReport.class ); //$NON-NLS-1$
    interpreter.declareBean( "flowController", flowController, DefaultFlowController.class ); //$NON-NLS-1$
    final Object o = interpreter.eval( getLanguage(), "expression", 1, 1, preDataScript );
    if ( o instanceof MasterReport == false ) {
      throw new ReportDataFactoryException( "Not a MasterReport" );
    }
    return (MasterReport) o; //$NON-NLS-1$

  } catch ( BSFException e ) {
    throw new ReportDataFactoryException( "Failed to initialize the BSF-Framework", e );
  }
}
 
Example 3
Source File: BSFReportPreProcessor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public MasterReport performPreProcessing( final MasterReport definition, final DefaultFlowController flowController )
  throws ReportProcessingException {
  if ( script == null || language == null || StringUtils.isEmpty( script, true ) ) {
    return definition;
  }

  try {
    final BSFManager interpreter = new BSFManager();
    interpreter.declareBean( "definition", definition, MasterReport.class ); //$NON-NLS-1$
    interpreter.declareBean( "flowController", flowController, DefaultFlowController.class ); //$NON-NLS-1$
    final Object o = interpreter.eval( getLanguage(), "expression", 1, 1, script );
    if ( o instanceof MasterReport == false ) {
      throw new ReportDataFactoryException( "Not a MasterReport" );
    }
    return (MasterReport) o; //$NON-NLS-1$

  } catch ( BSFException e ) {
    throw new ReportDataFactoryException( "Failed to initialize the BSF-Framework", e );
  }
}
 
Example 4
Source File: BSFReportPreProcessor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public SubReport performPreDataProcessing( final SubReport definition, final DefaultFlowController flowController )
  throws ReportProcessingException {
  if ( script == null || language == null ) {
    return definition;
  }

  try {
    final BSFManager interpreter = new BSFManager();
    interpreter.declareBean( "definition", definition, MasterReport.class ); //$NON-NLS-1$
    interpreter.declareBean( "flowController", flowController, DefaultFlowController.class ); //$NON-NLS-1$
    final Object o = interpreter.eval( getLanguage(), "expression", 1, 1, preDataScript );
    if ( o instanceof SubReport == false ) {
      throw new ReportDataFactoryException( "Not a SubReport" );
    }
    return (SubReport) o; //$NON-NLS-1$

  } catch ( BSFException e ) {
    throw new ReportDataFactoryException( "Failed to initialize the BSF-Framework", e );
  }
}
 
Example 5
Source File: BSFReportPreProcessor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public SubReport performPreProcessing( final SubReport definition, final DefaultFlowController flowController )
  throws ReportProcessingException {
  if ( script == null || language == null ) {
    return definition;
  }

  try {
    final BSFManager interpreter = new BSFManager();
    interpreter.declareBean( "definition", definition, SubReport.class ); //$NON-NLS-1$
    interpreter.declareBean( "flowController", flowController, DefaultFlowController.class ); //$NON-NLS-1$
    final Object o = interpreter.eval( getLanguage(), "expression", 1, 1, script );
    if ( o instanceof SubReport == false ) {
      throw new ReportDataFactoryException( "Not a MasterReport" );
    }
    return (SubReport) o; //$NON-NLS-1$

  } catch ( BSFException e ) {
    throw new ReportDataFactoryException( "Failed to initialize the BSF-Framework", e );
  }
}
 
Example 6
Source File: TestBshBSF.java    From beanshell with Apache License 2.0 5 votes vote down vote up
/** The main method.
 * @param args the arguments
 * @throws BSFException the BSF exception */
@Test
public void test_bsf_engine() throws BSFException {
    final BSFManager mgr = new BSFManager();
    // register beanshell with the BSF framework
    final String[] extensions = {"bsh"};
    BSFManager.registerScriptingEngine("beanshell",
            "bsh.util.BeanShellBSFEngine", extensions);
    mgr.declareBean("foo", "fooString", String.class);
    mgr.declareBean("bar", "barString", String.class);
    mgr.registerBean("gee", "geeString");
    final BSFEngine beanshellEngine = mgr.loadScriptingEngine("beanshell");
    String script = "foo + bar + bsf.lookupBean(\"gee\")";
    Object result = beanshellEngine.eval("Test eval...", -1, -1, script);
    assertTrue(result.equals("fooStringbarStringgeeString"));
    // test apply()
    final Vector<String> names = new Vector<>();
    names.addElement("name");
    final Vector<String> vals = new Vector<>();
    vals.addElement("Pat");
    script = "name + name";
    result = beanshellEngine.apply("source string...", -1, -1, script,
            names, vals);
    assertTrue(result.equals("PatPat"));
    result = beanshellEngine.eval("Test eval...", -1, -1, "name");
    // name should not be set
    assertTrue(result == null);
    // Verify the primitives are unwrapped
    result = beanshellEngine.eval("Test eval...", -1, -1, "1+1");
    assertTrue(result instanceof Integer
            && ((Integer) result).intValue() == 2);
}
 
Example 7
Source File: BSFSpecTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Test
public void testVariablePassing() throws BSFException {
    // tag::bsf_variable_passing[]
    BSFManager manager = new BSFManager();
    manager.declareBean("xyz", 4, Integer.class);
    Object answer = manager.eval("groovy", "test.groovy", 0, 0, "xyz + 1");
    assertEquals(5, answer);
    // end::bsf_variable_passing[]
}
 
Example 8
Source File: BSFSpecTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccess() throws BSFException {
    // tag::bsf_access[]
    BSFManager manager = new BSFManager();
    BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
    manager.declareBean("myvar", "hello", String.class);
    Object myvar = manager.lookupBean("myvar");
    String result = (String) bsfEngine.call(myvar, "reverse", new Object[0]);
    assertEquals("olleh", result);
    // end::bsf_access[]
}
 
Example 9
Source File: ScriptManager.java    From PADListener with GNU General Public License v2.0 5 votes vote down vote up
/** Creates a new instance of ScriptManager */
public ScriptManager(Framework framework) {
    try {
        _bsfManager = new BSFManager();
        _bsfManager.declareBean("framework", framework, framework.getClass());
        _bsfManager.declareBean("out", System.out, System.out.getClass());
        _bsfManager.declareBean("err", System.err, System.out.getClass());
    } catch (BSFException bsfe) {
        _logger.severe("Declaring a bean should not throw an exception! " + bsfe);
    }
}
 
Example 10
Source File: ScriptableDataFactory.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Initializes the Bean-Scripting Framework manager.
 *
 * @param interpreter
 *          the BSF-Manager that should be initialized.
 * @throws BSFException
 *           if an error occurred.
 */
protected void initializeInterpreter( final BSFManager interpreter ) throws BSFException {
  dataRowWrapper = new LegacyDataRowWrapper();
  interpreter.declareBean( "dataRow", dataRowWrapper, DataRow.class ); //$NON-NLS-1$
  interpreter.declareBean( "configuration", getConfiguration(), Configuration.class ); //$NON-NLS-1$
  interpreter.declareBean( "contextKey", getContextKey(), ResourceKey.class ); //$NON-NLS-1$
  interpreter.declareBean( "resourceManager", getResourceManager(), ResourceManager.class ); //$NON-NLS-1$
  interpreter.declareBean( "resourceBundleFactory", getResourceBundleFactory(), ResourceBundleFactory.class ); //$NON-NLS-1$
  interpreter.declareBean( "dataFactoryContext", getDataFactoryContext(), ResourceBundleFactory.class ); //$NON-NLS-1$
  if ( script != null ) {
    interpreter.exec( getLanguage(), "startup-script", 1, 1, getScript() ); //$NON-NLS-1$
  }
}
 
Example 11
Source File: AbstractChartExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected JFreeChart postProcessChart( final JFreeChart originalChart ) {
  if ( postProcessingLanguage == null || postProcessingScript == null ) {
    return originalChart;
  }

  final LegacyDataRowWrapper legacyDataRowWrapper = new LegacyDataRowWrapper();
  final WrapperExpressionRuntime runtimeWrapper = new WrapperExpressionRuntime();
  runtimeWrapper.update( null, getRuntime() );
  legacyDataRowWrapper.setParent( getDataRow() );
  try {
    final BSFManager interpreter = new BSFManager();
    interpreter.declareBean( "chartExpression", this, getClass() ); //$NON-NLS-1$
    interpreter.declareBean( "chart", originalChart, JFreeChart.class ); //$NON-NLS-1$
    interpreter.declareBean( "runtime", runtimeWrapper, ExpressionRuntime.class ); //$NON-NLS-1$
    interpreter.declareBean( "dataRow", legacyDataRowWrapper, DataRow.class ); //$NON-NLS-1$
    final Object o = interpreter.eval( postProcessingLanguage, "expression", 1, 1, postProcessingScript ); //$NON-NLS-1$
    if ( o instanceof JFreeChart ) {
      return (JFreeChart) o;
    }
    return originalChart;
  } catch ( BSFException e ) {
    // this is not nice
    AbstractChartExpression.logger.warn( "Failed to evaluate post processing script", e );
  } finally {
    legacyDataRowWrapper.setParent( null );
    runtimeWrapper.update( null, null );
  }
  return originalChart;
}
 
Example 12
Source File: BSFExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Initializes the Bean-Scripting Framework manager.
 *
 * @param interpreter
 *          the BSF-Manager that should be initialized.
 * @throws BSFException
 *           if an error occured.
 */
protected void initializeInterpreter( final BSFManager interpreter ) throws BSFException {
  dataRowWrapper = new LegacyDataRowWrapper();
  runtimeWrapper = new WrapperExpressionRuntime();
  runtimeWrapper.update( getDataRow(), getRuntime() );
  interpreter.declareBean( "runtime", runtimeWrapper, ExpressionRuntime.class ); //$NON-NLS-1$
  interpreter.declareBean( "dataRow", dataRowWrapper, DataRow.class ); //$NON-NLS-1$
  if ( script != null ) {
    interpreter.exec( getLanguage(), "script", 1, 1, getScript() ); //$NON-NLS-1$
  }
}