Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/JRUBY-6970.rb

Overview

Work around for a bug in File.expand_path that doesn’t account for resources in jar paths.

Should solve this error:

Exception in thread "LogStash::Runner" org.jruby.exceptions.RaiseException:
(Errno::ENOENT) file:/home/jls/projects/logstash/build/data/unicode.data

Constant Summary collapse

JAR_RE =
/^(?:jar:)?file:(\/.*\.jar)!(\/.*$)/

Class Method Summary collapse

Class Method Details

.__zipcache(jarpath, path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/logstash/monkeypatches-for-performance.rb', line 46

def __zipcache(jarpath, path)
  @jarcache ||= Hash.new { |h,k| h[k] = {} }

  if @jarcache[jarpath].empty?
    #puts "Caching file entries for #{jarpath}"
    s = Time.now
    zip = java.util.zip.ZipFile.new(jarpath)
    zip.entries.each do |entry|
      #puts "Caching file entries for #{jarpath}: /#{entry.name}"
      # Prefix entry name with "/" because that's what the jar path looks
      # like in jruby: file://some.jar!/some/path
      @jarcache[jarpath]["/" + entry.name] = entry
    end
  end

  entry = @jarcache[jarpath][path]
  #puts "Serving cached file info #{path}: #{entry}"
  return entry
end

.exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logstash/monkeypatches-for-performance.rb', line 28

def exist?(path)
  #return mpp_exist?(path)
  # If path is in a jar (file://blah/foo.jar!/some/path)
  #   - create a cache for this jar of all files
  #   - return cached results only
  if RUBY_PLATFORM == "java" 
    m = JAR_RE.match(path)
    return mpp_exists?(path) if !m # not a jar file
    c = __zipcache(m[1], m[2]) # m[1] == the jar path
    return !c.nil?
  end
  return mpp_exists?(path)
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/logstash/monkeypatches-for-performance.rb', line 42

def exists?(path)
  return exist?(path)
end

.expand_path(path, dir = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/logstash/JRUBY-6970.rb', line 47

def expand_path(path, dir=nil)
  #p :expand_path => [path, dir]
  if path =~ /(jar:)?file:\/.*\.jar!/
    #p :expand_path_path => [path, dir]
    jar, resource = path.split("!", 2)
    #p :expand_path => [jar, resource]
    if resource.nil? || resource == ""
      # Nothing after the "!", nothing special to handle.
      return expand_path_JRUBY_6970(path, dir)
    else
      resource = expand_path_JRUBY_6970(resource, dir)
      return fix_jar_path(jar, resource)
    end
  elsif dir =~ /(jar:)?file:\/.*\.jar!/
    jar, dir = dir.split("!", 2)
    if dir.empty?
      # sometimes the original dir is just 'file:/foo.jar!'
      return File.join("#{jar}!", path) 
    end
    dir = expand_path_JRUBY_6970(path, dir)
    return fix_jar_path(jar, dir)
  else
    return expand_path_JRUBY_6970(path, dir)
  end
end

.expand_path_JRUBY_6970Object



45
# File 'lib/logstash/JRUBY-6970.rb', line 45

alias_method :expand_path_JRUBY_6970, :expand_path

.file?(path) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/monkeypatches-for-performance.rb', line 10

def file?(path)
  #return mpp_file?(path)
  # If path is in a jar (file://blah/foo.jar!/some/path)
  #   - create a cache for this jar of all files
  #   - return cached results only
  if RUBY_PLATFORM == "java" 
    m = JAR_RE.match(path)
    return mpp_file?(path) if !m # not a jar file
    c = __zipcache(m[1], m[2]) # m[1] == the jar path
    # ZipEntry has only 'isDirectory()' so I assume any
    # non-directories are files.
    rc = (!c.nil? && !c.isDirectory)
    #p path => rc
    return rc
  end
  return mpp_file?(path)
end

.mpp_exist?Object



6
# File 'lib/logstash/monkeypatches-for-performance.rb', line 6

alias_method :mpp_exist?, :exist?

.mpp_exists?Object



7
# File 'lib/logstash/monkeypatches-for-performance.rb', line 7

alias_method :mpp_exists?, :exists?

.mpp_file?Object

mpp == monkey patch for performance



5
# File 'lib/logstash/monkeypatches-for-performance.rb', line 5

alias_method :mpp_file?, :file?