コード例 #1
0
ファイル: LoadService.java プロジェクト: joernh/jruby
  protected LoadServiceResource tryResourceFromJarURL(
      SearchState state, String baseName, SuffixType suffixType) {
    // if a jar or file URL, return load service resource directly without further searching
    LoadServiceResource foundResource = null;
    if (baseName.startsWith("jar:")) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        try {
          URL url = new URL(namePlusSuffix);
          debugLogTry("resourceFromJarURL", url.toString());
          if (url.openStream() != null) {
            foundResource = new LoadServiceResource(url, namePlusSuffix);
            debugLogFound(foundResource);
          }
        } catch (FileNotFoundException e) {
        } catch (MalformedURLException e) {
          throw runtime.newIOErrorFromException(e);
        } catch (IOException e) {
          throw runtime.newIOErrorFromException(e);
        }
        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          break; // end suffix iteration
        }
      }
    } else if (baseName.startsWith("file:") && baseName.indexOf("!/") != -1) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        try {
          String jarFile = namePlusSuffix.substring(5, namePlusSuffix.indexOf("!/"));
          JarFile file = new JarFile(jarFile);
          String filename = namePlusSuffix.substring(namePlusSuffix.indexOf("!/") + 2);
          String canonicalFilename = canonicalizePath(filename);

          debugLogTry("resourceFromJarURL", canonicalFilename.toString());
          if (file.getJarEntry(canonicalFilename) != null) {
            foundResource =
                new LoadServiceResource(
                    new URL("jar:file:" + jarFile + "!/" + canonicalFilename), namePlusSuffix);
            debugLogFound(foundResource);
          }
        } catch (Exception e) {
        }
        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          break; // end suffix iteration
        }
      }
    }

    return foundResource;
  }
コード例 #2
0
  public ByteList doReceiveNonblock(ThreadContext context, int length) {
    Ruby runtime = context.runtime;
    Channel channel = getChannel();

    if (!(channel instanceof SelectableChannel)) {
      if (runtime.is1_9()) {
        throw runtime.newErrnoEAGAINReadableError(
            channel.getClass().getName() + " does not support nonblocking");
      } else {
        throw runtime.newErrnoEAGAINError(
            channel.getClass().getName() + " does not support nonblocking");
      }
    }

    SelectableChannel selectable = (SelectableChannel) channel;

    synchronized (selectable.blockingLock()) {
      boolean oldBlocking = selectable.isBlocking();

      try {
        selectable.configureBlocking(false);

        try {
          return doReceive(context, length);
        } finally {
          selectable.configureBlocking(oldBlocking);
        }

      } catch (IOException e) {
        throw runtime.newIOErrorFromException(e);
      }
    }
  }
コード例 #3
0
ファイル: Tempfile.java プロジェクト: rkh/jruby
  @JRubyMethod(required = 1, optional = 1, visibility = PRIVATE)
  @Override
  public IRubyObject initialize(IRubyObject[] args, Block block) {
    Ruby runtime = getRuntime();
    IRubyObject basename = args[0];
    IRubyObject dir = defaultTmpDir(runtime, args);

    File tmp = null;
    synchronized (tmpFileLock) {
      while (true) {
        try {
          if (counter == -1) {
            counter = RND.nextInt() & 0xffff;
          }
          counter++;

          // We do this b/c make_tmpname might be overridden
          IRubyObject tmpname =
              callMethod(
                  runtime.getCurrentContext(),
                  "make_tmpname",
                  new IRubyObject[] {basename, runtime.newFixnum(counter)});
          tmp =
              JRubyFile.create(
                  getRuntime().getCurrentDirectory(),
                  new File(dir.convertToString().toString(), tmpname.convertToString().toString())
                      .getPath());
          if (tmp.createNewFile()) {
            tmpFile = tmp;
            path = tmp.getPath();
            try {
              tmpFile.deleteOnExit();
            } catch (NullPointerException npe) {
              // See JRUBY-4624.
              // Due to JDK bug, NPE could be thrown
              // when shutdown is in progress.
              // Do nothing.
            } catch (IllegalStateException ise) {
              // do nothing, shutdown in progress
            }
            initializeOpen();
            referenceSet.put(reaper = new Reaper(this, runtime, tmpFile, openFile), Boolean.TRUE);
            return this;
          }
        } catch (IOException e) {
          throw runtime.newIOErrorFromException(e);
        }
      }
    }
  }
コード例 #4
0
  public static IRScope loadScriptFromFile(
      Ruby runtime,
      InputStream inStream,
      File resourcePath,
      String resourceName,
      boolean isAbsolute) {
    String name = getFilenameFromPathAndName(resourcePath, resourceName, isAbsolute);
    try {
      Class clazz = loadCompiledScriptFromClass(runtime, inStream);

      try {
        Method method = clazz.getMethod("loadIR", Ruby.class, String.class);
        return (IRScope) method.invoke(null, runtime, name);
      } catch (Exception e) {
        if (runtime.getDebug().isTrue()) {
          e.printStackTrace();
        }
        throw runtime.newLoadError(
            name + " is not compiled Ruby; use java_import to load normal classes");
      }
    } catch (IOException e) {
      throw runtime.newIOErrorFromException(e);
    } catch (LinkageError le) {
      if (runtime.getDebug().isTrue()) {
        le.printStackTrace();
      }
      throw runtime.newLoadError(
          "Linkage error loading compiled script; you may need to recompile '" + name + "': " + le);
    } finally {
      try {
        inStream.close();
      } catch (IOException ioe) {
        throw runtime.newIOErrorFromException(ioe);
      }
    }
  }
コード例 #5
0
ファイル: Readline.java プロジェクト: moses/jruby
  @JRubyMethod(name = "readline", module = true, visibility = Visibility.PRIVATE)
  public static IRubyObject s_readline(
      ThreadContext context, IRubyObject recv, IRubyObject prompt, IRubyObject add_to_hist)
      throws IOException {
    Ruby runtime = context.getRuntime();
    ConsoleHolder holder = getHolder(runtime);
    if (holder.readline == null) {
      initReadline(runtime, holder); // not overridden, let's go
    }

    IRubyObject line = runtime.getNil();
    String v = null;
    while (true) {
      try {
        holder.readline.getTerminal().disableEcho();
        v = holder.readline.readLine(prompt.toString());
        break;
      } catch (IOException ioe) {
        if (RubyIO.restartSystemCall(ioe)) {
          // This is for JRUBY-2988, since after a suspend the terminal seems
          // to need to be reinitialized. Since we can't easily detect suspension,
          // initialize after every readline. Probably not fast, but this is for
          // interactive terminals anyway...so who cares?
          try {
            holder.readline.getTerminal().initializeTerminal();
          } catch (Exception e) {
          }
          continue;
        }
        throw runtime.newIOErrorFromException(ioe);
      } finally {
        holder.readline.getTerminal().enableEcho();
      }
    }

    if (null != v) {
      if (add_to_hist.isTrue()) {
        holder.readline.getHistory().addToHistory(v);
      }

      /* Explicitly use UTF-8 here. c.f. history.addToHistory using line.asUTF8() */
      line = RubyString.newUnicodeString(recv.getRuntime(), v);
    }
    return line;
  }
コード例 #6
0
ファイル: LoadService.java プロジェクト: joernh/jruby
  protected LoadServiceResource tryResourceFromJarURLWithLoadPath(
      String namePlusSuffix, String loadPathEntry) {
    LoadServiceResource foundResource = null;

    JarFile current = jarFiles.get(loadPathEntry);
    boolean isFileJarUrl = loadPathEntry.startsWith("file:") && loadPathEntry.indexOf("!/") != -1;
    String after =
        isFileJarUrl ? loadPathEntry.substring(loadPathEntry.indexOf("!/") + 2) + "/" : "";
    String before =
        isFileJarUrl ? loadPathEntry.substring(0, loadPathEntry.indexOf("!/")) : loadPathEntry;

    if (null == current) {
      try {
        if (loadPathEntry.startsWith("jar:")) {
          current = new JarFile(loadPathEntry.substring(4));
        } else if (loadPathEntry.endsWith(".jar")) {
          current = new JarFile(loadPathEntry);
        } else {
          current = new JarFile(loadPathEntry.substring(5, loadPathEntry.indexOf("!/")));
        }
        jarFiles.put(loadPathEntry, current);
      } catch (ZipException ignored) {
        if (runtime.getInstanceConfig().isDebug()) {
          runtime
              .getErr()
              .println("ZipException trying to access " + loadPathEntry + ", stack trace follows:");
          ignored.printStackTrace(runtime.getErr());
        }
      } catch (FileNotFoundException ignored) {
      } catch (IOException e) {
        throw runtime.newIOErrorFromException(e);
      }
    }
    String canonicalEntry = after + namePlusSuffix;
    if (current != null) {
      debugLogTry("resourceFromJarURLWithLoadPath", current.getName() + "!/" + canonicalEntry);
      if (current.getJarEntry(canonicalEntry) != null) {
        try {
          if (loadPathEntry.endsWith(".jar")) {
            foundResource =
                new LoadServiceResource(
                    new URL("jar:file:" + loadPathEntry + "!/" + canonicalEntry),
                    "/" + namePlusSuffix);
          } else if (loadPathEntry.startsWith("file:")) {
            foundResource =
                new LoadServiceResource(
                    new URL("jar:" + before + "!/" + canonicalEntry),
                    loadPathEntry + "/" + namePlusSuffix);
          } else {
            foundResource =
                new LoadServiceResource(
                    new URL("jar:file:" + loadPathEntry.substring(4) + "!/" + namePlusSuffix),
                    loadPathEntry + namePlusSuffix);
          }
          debugLogFound(foundResource);
        } catch (MalformedURLException e) {
          throw runtime.newIOErrorFromException(e);
        }
      }
    }

    return foundResource;
  }
コード例 #7
0
ファイル: OpenFile.java プロジェクト: rkh/jruby
  public void finalize(Ruby runtime, boolean raise) {
    try {
      ChannelDescriptor main = null;
      ChannelDescriptor pipe = null;

      // Recent JDKs shut down streams in the parent when child
      // terminates, so we can't trust that they'll be open for our
      // close. Check for that.

      boolean isProcess = process != null;

      synchronized (this) {
        Stream ps = pipeStream;
        if (ps != null) {
          pipe = ps.getDescriptor();

          try {
            // check for closed channel due to child exit
            if (isProcess && ps.getChannel().isOpen() || !isProcess) {
              ps.fflush();
              ps.fclose();
            }
          } finally {
            // make sure the pipe stream is set to null
            pipeStream = null;
          }
        }
        Stream ms = mainStream;
        if (ms != null) {
          // TODO: Ruby logic is somewhat more complicated here, see comments after
          main = ms.getDescriptor();
          try {
            // check for closed channel due to child exit
            if (isProcess && ms.getChannel().isOpen() || !isProcess) {
              if (pipe == null && isWriteBuffered()) {
                ms.fflush();
              }
              ms.fclose();
            }
          } catch (BadDescriptorException bde) {
            if (main == pipe) {
            } else {
              throw bde;
            }
          } finally {
            // make sure the main stream is set to null
            mainStream = null;
          }
        }
      }
    } catch (IOException ex) {
      if (raise) {
        throw runtime.newIOErrorFromException(ex);
      }
    } catch (BadDescriptorException ex) {
      if (raise) {
        throw runtime.newErrnoEBADFError();
      }
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }