org.jruby.embed.ScriptingContainer Java Examples

The following examples show how to use org.jruby.embed.ScriptingContainer. 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: TestColumnCaster.java    From embulk-parser-jsonl with MIT License 5 votes vote down vote up
@Before
public void createResource()
{
    jruby = new ScriptingContainer();
    thrown = new DataException("any");
    Value[] kvs = new Value[2];
    kvs[0] = ValueFactory.newString("k");
    kvs[1] = ValueFactory.newString("v");
    mapValue = ValueFactory.newMap(kvs);
    parser = new TimestampParser(jruby, "%Y-%m-%d %H:%M:%S.%N", DateTimeZone.UTC);
}
 
Example #2
Source File: HbaseInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws InterpreterException {
  this.scriptingContainer  = new ScriptingContainer(LocalContextScope.SINGLETON);
  this.writer = new StringWriter();
  scriptingContainer.setOutput(this.writer);

  if (!Boolean.parseBoolean(getProperty(HBASE_TEST_MODE))) {
    String hbaseHome = getProperty(HBASE_HOME);
    String rubySrc = getProperty(HBASE_RUBY_SRC);
    Path absRubySrc = Paths.get(hbaseHome, rubySrc).toAbsolutePath();

    logger.info("Home:" + hbaseHome);
    logger.info("Ruby Src:" + rubySrc);

    File f = absRubySrc.toFile();
    if (!f.exists() || !f.isDirectory()) {
      throw new InterpreterException("HBase ruby sources is not available at '" + absRubySrc
          + "'");
    }

    logger.info("Absolute Ruby Source:" + absRubySrc.toString());
    // hirb.rb:41 requires the following system properties to be set.
    Properties sysProps = System.getProperties();
    sysProps.setProperty(HBASE_RUBY_SRC, absRubySrc.toString());

    Path absHirbPath = Paths.get(hbaseHome, "bin/hirb.rb");
    try {
      FileInputStream fis = new FileInputStream(absHirbPath.toFile());
      this.scriptingContainer.runScriptlet(fis, "hirb.rb");
      fis.close();
    } catch (IOException e) {
      throw new InterpreterException(e.getCause());
    }
  }
}
 
Example #3
Source File: RubyOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(OperatorContext context)
{
  sc = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
  for (String s : setupScripts) {
    EvalUnit unit = sc.parse(s);
    unit.run();
  }
  if (type == Type.EVAL) {
    unit = sc.parse(script);
  }
}
 
Example #4
Source File: ScriptingEngine.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * getScriptingContainer
 * 
 * @return
 */
public synchronized ScriptingContainer getScriptingContainer()
{
	if (this._scriptingContainer == null)
	{
		this._scriptingContainer = this.createScriptingContainer(LocalContextScope.SINGLETON);
	}

	return this._scriptingContainer;
}
 
Example #5
Source File: ScriptingEngine.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public synchronized ScriptingContainer getInitializedScriptingContainer()
{
	ScriptingContainer sc = getScriptingContainer();
	if (!initialized)
	{
		sc.runScriptlet("require 'ruble'"); //$NON-NLS-1$
		initialized = true;
	}
	return sc;
}
 
Example #6
Source File: MultiValuedHeadersTest.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
  // Silence logging.
  System.setProperty(DEFAULT_LOG_LEVEL_KEY, "WARN");

  // Build the Rack servlet.
  ScriptingContainer ruby = new ScriptingContainer();
  IRubyObject application = ruby.parse(CLASSPATH, "application.rb").run();
  RackServlet servlet = new RackServlet(new JRubyRackApplication(application));

  server = new ExampleServer(servlet, "/*");
  server.start();
  client = new DefaultHttpClient();
  localhost = new HttpHost("localhost", server.getPort());
}
 
Example #7
Source File: HeaderFlushingTest.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
  // Silence logging.
  System.setProperty(DEFAULT_LOG_LEVEL_KEY, "WARN");

  // Build the Rack servlet.
  ScriptingContainer ruby = new ScriptingContainer();
  IRubyObject application = ruby.parse(CLASSPATH, "application.rb").run();
  RackServlet servlet = new RackServlet(new JRubyRackApplication(application));

  server = new ExampleServer(servlet, "/*");
  server.start();
  client = new DefaultHttpClient();
  localhost = new HttpHost("localhost", server.getPort());
}
 
Example #8
Source File: ExampleService.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
private IRubyObject createApplication() {
  // There's a lot you could do here; for now, we just read a rackup file from the classpath,
  // then build a Rack application based on it.
  ScriptingContainer container = new ScriptingContainer();
  container.put("builder_script", readResource("config.ru"));
  return container.parse("require 'rack'; Rack::Builder.new_from_string(builder_script)").run();
}
 
Example #9
Source File: ExampleServerTest.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
  // Silence logging.
  System.setProperty(DEFAULT_LOG_LEVEL_KEY, "WARN");

  // Build the Rack servlet.
  ScriptingContainer ruby = new ScriptingContainer();
  IRubyObject application = ruby.parse("lambda { |env| [200, {}, ['Hello, World!']] }").run();
  RackServlet servlet = new RackServlet(new JRubyRackApplication(application));

  server = new ExampleServer(servlet, "/*");
  server.start();
  client = new DefaultHttpClient();
  localhost = new HttpHost("localhost", server.getPort());
}
 
Example #10
Source File: TestStringCast.java    From embulk-parser-jsonl with MIT License 4 votes vote down vote up
@Before
public void createResource()
{
    jruby = new ScriptingContainer();
}
 
Example #11
Source File: ScriptingEngine.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * createScriptingContainer
 * 
 * @param scope
 * @return
 */
private ScriptingContainer createScriptingContainer(LocalContextScope scope)
{
	// ScriptingContainer result = new ScriptingContainer(scope, LocalVariableBehavior.PERSISTENT);
	ScriptingContainer result = new ScriptingContainer(scope, LocalVariableBehavior.TRANSIENT);

	try
	{
		File jrubyHome = null;
		Bundle jruby = Platform.getBundle("org.jruby"); //$NON-NLS-1$
		// try just exploding the jruby lib dir
		URL url = FileLocator.find(jruby, new Path("lib"), null); //$NON-NLS-1$

		if (url != null)
		{
			File lib = ResourceUtil.resourcePathToFile(url);
			// Ok, now use the parent of exploded lib dir as JRuby Home
			jrubyHome = lib.getParentFile();
		}
		else
		{
			// Ok, just assume the plugin is unpacked and pass the root of the plugin as JRuby Home
			jrubyHome = FileLocator.getBundleFile(jruby);
		}

		result.setHomeDirectory(jrubyHome.getAbsolutePath());

		// TODO Generate two containers? A global one for loading bundles, a threadsafe one for executing
		// commands/snippets/etc?
		// Pre-load 'ruble' framework files!
		List<String> loadPaths = result.getLoadPaths();
		loadPaths.addAll(0, getContributedLoadPaths());
		result.setLoadPaths(loadPaths);
	}
	catch (IOException e)
	{
		String message = MessageFormat.format(Messages.ScriptingEngine_Error_Setting_JRuby_Home,
				new Object[] { e.getMessage() });

		IdeLog.logError(ScriptingActivator.getDefault(), message, e);
		ScriptLogger.logError(message);
	}

	return result;
}
 
Example #12
Source File: CompassV2.java    From opoopress with Apache License 2.0 4 votes vote down vote up
private void runScriptlet(String script) {
    log.debug(script);
    new ScriptingContainer().runScriptlet(script);
}
 
Example #13
Source File: Compass.java    From opoopress with Apache License 2.0 4 votes vote down vote up
private void runScriptlet(String script) {
    log.debug(script);
    new ScriptingContainer().runScriptlet(script);
}
 
Example #14
Source File: ExampleServerTest.java    From rack-servlet with Apache License 2.0 4 votes vote down vote up
@Provides IRubyObject provideApplication() {
  ScriptingContainer ruby = new ScriptingContainer();
  return ruby.parse("lambda { |env| [200, {}, ['Hello, World!']] }").run();
}
 
Example #15
Source File: JRubyRackBodyIteratorTest.java    From rack-servlet with Apache License 2.0 4 votes vote down vote up
@Before public void setUp() {
  scriptingContainer = new ScriptingContainer();
  scriptingContainer.runScriptlet(PathType.CLASSPATH, "enumerable_with_close.rb");
}