Ejemplo n.º 1
0
  public static int symbolToInterestOps(
      Ruby ruby, SelectableChannel channel, IRubyObject interest) {
    if (interest == ruby.newSymbol("r")) {
      if ((channel.validOps() & SelectionKey.OP_ACCEPT) != 0) {
        return SelectionKey.OP_ACCEPT;
      } else {
        return SelectionKey.OP_READ;
      }
    } else if (interest == ruby.newSymbol("w")) {
      if (channel instanceof SocketChannel && !((SocketChannel) channel).isConnected()) {
        return SelectionKey.OP_CONNECT;
      } else {
        return SelectionKey.OP_WRITE;
      }
    } else if (interest == ruby.newSymbol("rw")) {
      int interestOps = 0;

      /* nio4r emulates the POSIX behavior, which is sloppy about allowed modes */
      if ((channel.validOps() & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0) {
        interestOps |= symbolToInterestOps(ruby, channel, ruby.newSymbol("r"));
      }

      if ((channel.validOps() & (SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT)) != 0) {
        interestOps |= symbolToInterestOps(ruby, channel, ruby.newSymbol("w"));
      }

      return interestOps;
    } else {
      throw ruby.newArgumentError("invalid interest type: " + interest);
    }
  }
Ejemplo n.º 2
0
  public static IRubyObject doAccept(RubySocket sock, ThreadContext context, boolean ex) {
    Ruby runtime = context.runtime;

    Channel channel = sock.getChannel();

    try {
      if (channel instanceof ServerSocketChannel) {
        ServerSocketChannel serverChannel = (ServerSocketChannel) sock.getChannel();

        SocketChannel socket = serverChannel.accept();

        if (socket == null) {
          // This appears to be undocumented in JDK; null as a sentinel value
          // for a nonblocking accept with nothing available. We raise for Ruby.
          // indicates that no connection is available in non-blocking mode
          if (!ex) return runtime.newSymbol("wait_readable");
          throw runtime.newErrnoEAGAINReadableError("accept(2) would block");
        }

        RubySocket rubySocket = new RubySocket(runtime, runtime.getClass("Socket"));
        rubySocket.initFromServer(runtime, sock, socket);

        return runtime.newArray(
            rubySocket,
            new Addrinfo(runtime, runtime.getClass("Addrinfo"), socket.getRemoteAddress()));
      }
      throw runtime.newErrnoENOPROTOOPTError();
    } catch (IllegalBlockingModeException e) {
      // indicates that no connection is available in non-blocking mode
      if (!ex) return runtime.newSymbol("wait_readable");
      throw runtime.newErrnoEAGAINReadableError("accept(2) would block");
    } catch (IOException e) {
      throw sockerr(runtime, e.getLocalizedMessage(), e);
    }
  }
Ejemplo n.º 3
0
 public static IRubyObject interestOpsToSymbol(Ruby ruby, int interestOps) {
   switch (interestOps) {
     case SelectionKey.OP_READ:
     case SelectionKey.OP_ACCEPT:
       return ruby.newSymbol("r");
     case SelectionKey.OP_WRITE:
     case SelectionKey.OP_CONNECT:
       return ruby.newSymbol("w");
     case SelectionKey.OP_READ | SelectionKey.OP_CONNECT:
     case SelectionKey.OP_READ | SelectionKey.OP_WRITE:
       return ruby.newSymbol("rw");
     default:
       throw ruby.newArgumentError("unknown interest op combination");
   }
 }
Ejemplo n.º 4
0
 public final RubySymbol getSymbol(Ruby runtime, int index, String name) {
   RubySymbol symbol = symbols[index];
   if (symbol == null) {
     return symbols[index] = runtime.newSymbol(name);
   }
   return symbol;
 }
Ejemplo n.º 5
0
  /**
   * Just enough configuration settings (most don't make sense in Java) to run the rubytests unit
   * tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and <code>EXEEXT
   * </code>.
   */
  public void load(Ruby runtime, boolean wrap) {
    RubyModule configModule;

    configModule = runtime.defineModule("RbConfig");
    RubyKernel.autoload(
        runtime.getObject(),
        runtime.newSymbol("Config"),
        runtime.newString("rbconfig/obsolete.rb"));

    configModule.defineAnnotatedMethods(RbConfigLibrary.class);

    RubyHash configHash = RubyHash.newHash(runtime);
    configModule.defineConstant("CONFIG", configHash);

    String[] versionParts;
    versionParts = Constants.RUBY_VERSION.split("\\.");

    setConfig(configHash, "MAJOR", versionParts[0]);
    setConfig(configHash, "MINOR", versionParts[1]);
    setConfig(configHash, "TEENY", versionParts[2]);
    setConfig(configHash, "ruby_version", versionParts[0] + '.' + versionParts[1] + ".0");
    // Rubygems is too specific on host cpu so until we have real need lets default to universal
    // setConfig(configHash, "arch", System.getProperty("os.arch") + "-java" +
    // System.getProperty("java.specification.version"));
    setConfig(
        configHash, "arch", "universal-java" + System.getProperty("java.specification.version"));

    normalizedHome = getNormalizedHome(runtime);

    // Use property for binDir if available, otherwise fall back to common bin default
    String binDir = SafePropertyAccessor.getProperty("jruby.bindir");
    if (binDir == null) {
      binDir = new NormalizedFile(normalizedHome, "bin").getPath();
    }
    setConfig(configHash, "bindir", binDir);

    setConfig(configHash, "RUBY_INSTALL_NAME", jrubyScript());
    setConfig(configHash, "RUBYW_INSTALL_NAME", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "ruby_install_name", jrubyScript());
    setConfig(configHash, "rubyw_install_name", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "SHELL", jrubyShell());
    setConfig(configHash, "prefix", normalizedHome);
    setConfig(configHash, "exec_prefix", normalizedHome);

    setConfig(configHash, "host_os", getOSName());
    setConfig(configHash, "host_vendor", System.getProperty("java.vendor"));
    setConfig(configHash, "host_cpu", getArchitecture());

    setConfig(configHash, "target_os", getOSName());

    setConfig(configHash, "target_cpu", getArchitecture());

    String jrubyJarFile = "jruby.jar";
    URL jrubyPropertiesUrl = Ruby.getClassLoader().getResource("/org/jruby/Ruby.class");
    if (jrubyPropertiesUrl != null) {
      Pattern jarFile =
          Pattern.compile("jar:file:.*?([a-zA-Z0-9.\\-]+\\.jar)!" + "/org/jruby/Ruby.class");
      Matcher jarMatcher = jarFile.matcher(jrubyPropertiesUrl.toString());
      jarMatcher.find();
      if (jarMatcher.matches()) {
        jrubyJarFile = jarMatcher.group(1);
      }
    }
    setConfig(configHash, "LIBRUBY", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_ALIASES", jrubyJarFile);

    setConfig(configHash, "build", Constants.BUILD);
    setConfig(configHash, "target", Constants.TARGET);

    String shareDir = new NormalizedFile(normalizedHome, "share").getPath();
    String includeDir = new NormalizedFile(normalizedHome, "lib/native/" + getOSName()).getPath();

    String vendorDirGeneral = getVendorDirGeneral(runtime);
    String siteDirGeneral = getSiteDirGeneral(runtime);
    String rubySharedLibDir = getRubySharedLibDir(runtime);
    String rubyLibDir = getRubyLibDir(runtime);
    String archDir = getArchDir(runtime);
    String vendorDir = getVendorDir(runtime);
    String vendorLibDir = getVendorLibDir(runtime);
    String vendorArchDir = getVendorArchDir(runtime);
    String siteDir = getSiteDir(runtime);
    String siteLibDir = getSiteLibDir(runtime);
    String siteArchDir = getSiteArchDir(runtime);
    String sysConfDir = getSysConfDir(runtime);

    setConfig(configHash, "libdir", vendorDirGeneral);
    setConfig(configHash, "rubylibprefix", vendorDirGeneral + "/ruby");
    setConfig(configHash, "rubylibdir", rubyLibDir);
    setConfig(configHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(configHash, "vendordir", vendorDir);
      setConfig(configHash, "vendorlibdir", vendorLibDir);
      setConfig(configHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(configHash, "sitedir", siteDir);
    setConfig(configHash, "sitelibdir", siteLibDir);
    setConfig(configHash, "sitearchdir", siteArchDir);
    setConfig(configHash, "sitearch", "java");
    setConfig(configHash, "archdir", archDir);
    setConfig(configHash, "topdir", archDir);
    setConfig(configHash, "includedir", includeDir);
    setConfig(configHash, "configure_args", "");
    setConfig(configHash, "datadir", shareDir);
    setConfig(configHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(configHash, "sysconfdir", sysConfDir);
    setConfig(configHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    setConfig(configHash, "DLEXT", "jar");
    if (getRubygemsDir(runtime) != null) {
      setConfig(configHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    if (Platform.IS_WINDOWS) {
      setConfig(configHash, "EXEEXT", ".exe");
    } else {
      setConfig(configHash, "EXEEXT", "");
    }

    setConfig(configHash, "ridir", new NormalizedFile(shareDir, "ri").getPath());

    // These will be used as jruby defaults for rubygems if found
    String gemhome = SafePropertyAccessor.getProperty("jruby.gem.home");
    String gempath = SafePropertyAccessor.getProperty("jruby.gem.path");
    if (gemhome != null) setConfig(configHash, "default_gem_home", gemhome);
    if (gempath != null) setConfig(configHash, "default_gem_path", gempath);

    setConfig(configHash, "joda-time.version", Constants.JODA_TIME_VERSION);
    setConfig(configHash, "tzdata.version", Constants.TZDATA_VERSION);

    RubyHash mkmfHash = RubyHash.newHash(runtime);

    setConfig(mkmfHash, "libdir", vendorDirGeneral);
    setConfig(mkmfHash, "arch", "java");
    setConfig(mkmfHash, "rubylibdir", rubyLibDir);
    setConfig(mkmfHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(mkmfHash, "vendordir", vendorDir);
      setConfig(mkmfHash, "vendorlibdir", vendorLibDir);
      setConfig(mkmfHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(mkmfHash, "sitedir", siteDir);
    setConfig(mkmfHash, "sitelibdir", siteLibDir);
    setConfig(mkmfHash, "sitearchdir", siteArchDir);
    setConfig(mkmfHash, "sitearch", "java");
    setConfig(mkmfHash, "archdir", archDir);
    setConfig(mkmfHash, "topdir", archDir);
    setConfig(mkmfHash, "configure_args", "");
    setConfig(mkmfHash, "datadir", new NormalizedFile(normalizedHome, "share").getPath());
    setConfig(mkmfHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(mkmfHash, "sysconfdir", sysConfDir);
    setConfig(mkmfHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    if (getRubygemsDir(runtime) != null) {
      setConfig(mkmfHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    setupMakefileConfig(configModule, mkmfHash);

    runtime.getLoadService().load("jruby/kernel/rbconfig.rb", false);
  }
Ejemplo n.º 6
0
  public static void createTemplate(Ruby runtime, RubyModule mASN1) {
    mTemplate = runtime.defineModuleUnder("Template", mASN1);
    RubyModule mParser = runtime.defineModuleUnder("Parser", mTemplate);
    cTemplateValue =
        mTemplate.defineClassUnder(
            "Value", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);

    CODEC = runtime.newSymbol("codec");
    OPTIONS = runtime.newSymbol("options");
    DEFAULT = runtime.newSymbol("default");
    NAME = runtime.newSymbol("name");
    TYPE = runtime.newSymbol("type");
    OPTIONAL = runtime.newSymbol("optional");
    TAG = runtime.newSymbol("tag");
    TAGGING = runtime.newSymbol("tagging");
    LAYOUT = runtime.newSymbol("layout");
    MIN_SIZE = runtime.newSymbol("min_size");

    CODEC_PRIMITIVE = runtime.newSymbol("PRIMITIVE");
    CODEC_TEMPLATE = runtime.newSymbol("TEMPLATE");
    CODEC_SEQUENCE = runtime.newSymbol("SEQUENCE");
    CODEC_SET = runtime.newSymbol("SET");
    CODEC_SEQUENCE_OF = runtime.newSymbol("SEQUENCE_OF");
    CODEC_SET_OF = runtime.newSymbol("SET_OF");
    CODEC_ANY = runtime.newSymbol("ANY");
    CODEC_CHOICE = runtime.newSymbol("CHOICE");

    mTemplate.defineAnnotatedMethods(RubyAsn1Template.class);
    mParser.defineAnnotatedMethods(TemplateParser.class);
    cTemplateValue.defineAnnotatedMethods(RubyAsn1Template.class);
  }
Ejemplo n.º 7
0
 @Override
 public IRubyObject interpret(
     Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
   return runtime.newSymbol(super.interpret(runtime, context, self, aBlock).toString());
 }