Java Code Examples for org.jruby.embed.LocalContextScope

The following examples show how to use org.jruby.embed.LocalContextScope. These examples are extracted from open source projects. 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 Project: zeppelin   Source File: HbaseInterpreter.java    License: 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 2
/**
 * getScriptingContainer
 * 
 * @return
 */
public synchronized ScriptingContainer getScriptingContainer()
{
	if (this._scriptingContainer == null)
	{
		this._scriptingContainer = this.createScriptingContainer(LocalContextScope.SINGLETON);
	}

	return this._scriptingContainer;
}
 
Example 3
/**
 * 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;
}