Module: Traject::Util

Defined in:
lib/traject/util.rb

Overview

Just some internal utility methods

Class Method Summary collapse

Class Method Details

.exception_to_log_message(e) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/traject/util.rb', line 7

def self.exception_to_log_message(e)
  indent = "    "

  msg  = indent + "Exception: " + e.class.name + ": " + e.message + "\n"
  msg += indent + e.backtrace.first + "\n"

  if (e.respond_to?(:getRootCause) && e.getRootCause && e != e.getRootCause )
    caused_by = e.getRootCause
    msg += indent + "Caused by\n"
    msg += indent + caused_by.class.name + ": " + caused_by.message + "\n"
    msg += indent + caused_by.backtrace.first + "\n"
  end

  return msg
end

.extract_caller_location(str) ⇒ Object

From ruby #caller method, you get an array. Pass one line of the array here, get just file and line number out.



25
26
27
# File 'lib/traject/util.rb', line 25

def self.extract_caller_location(str)
  str.split(':in `').first
end

.jruby_ensure_init!(feature = nil) ⇒ Object

just does a ‘require ’java’‘ but rescues the exception if we aren’t jruby, and raises a better error message.

Pass in a developer-presentable name of a feature to include in the error message if you want.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/traject/util.rb', line 123

def self.jruby_ensure_init!(feature = nil)
  begin
    require 'java'
  rescue LoadError => e
    feature ||= "A traject feature is in use that"
    msg = if feature
      "#{feature} requires jruby, but you do not appear to be running under jruby. We recommend `chruby` for managing multiple ruby installs."
    end
    raise LoadError.new(msg)
  end
end

.require_marc4j_jars(settings) ⇒ Object

Requires marc4j jar(s) from settings if given, otherwise uses jars bundled with traject gem in ./vendor

Have to pass in a settings arg, so we can check it for specified jar dir.

Tries not to do the dirglob and require if marc4j has already been loaded. Will define global constants with classes MarcPermissiveStreamReader and MarcXmlReader if not already defined.

This is all a bit janky, maybe there’s a better way to do this? We do want a ‘require’ method defined somewhere utility, so multiple classes can use it, including extra gems. This method IS used by extra gems, so should be considered part of the API – after it’s called, those top-level globals should be available, and marc4j should be loaded.



43
44
45
46
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
72
# File 'lib/traject/util.rb', line 43

def self.require_marc4j_jars(settings)
  jruby_ensure_init!

  tries = 0
  begin
    tries += 1

    org.marc4j

    # java_import which we'd normally use weirdly doesn't work
    # from a class method. https://github.com/jruby/jruby/issues/975
    Object.const_set("MarcPermissiveStreamReader",  org.marc4j.MarcPermissiveStreamReader) unless defined? ::MarcPermissiveStreamReader
    Object.const_set("MarcXmlReader",  org.marc4j.MarcXmlReader) unless defined? ::MarcXmlReader
  rescue NameError  => e
    # /Users/jrochkind/code/solrj-gem/lib"

    include_jar_dir = File.expand_path("../../vendor/marc4j/lib", File.dirname(__FILE__))

    jardir = settings["marc4j.jar_dir"] || include_jar_dir
    Dir.glob("#{jardir}/*.jar") do |x|
      require x
    end

    if tries > 1
      raise LoadError.new("Can not find Marc4J java classes")
    else
      retry
    end
  end
end

.require_solrj_jars(settings) ⇒ Object

Requires solrj jar(s) from settings if given, otherwise uses jars bundled with traject gem in ./vendor

Have to pass in a settings arg, so we can check it for specified jar dir.

Tries not to do the dirglob and require if solrj has already been loaded. Will define global constants with classes HttpSolrServer and SolrInputDocument if not already defined.

This is all a bit janky, maybe there’s a better way to do this? We do want a ‘require’ method defined somewhere utility, so multiple classes can use it, including extra gems. This method may be used by extra gems, so should be considered part of the API – after it’s called, those top-level globals should be available, and solrj should be loaded.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/traject/util.rb', line 88

def self.require_solrj_jars(settings)
  jruby_ensure_init!

  tries = 0
  begin
    tries += 1

    org.apache.solr
    org.apache.solr.client.solrj

    # java_import which we'd normally use weirdly doesn't work
    # from a class method. https://github.com/jruby/jruby/issues/975
    Object.const_set("HttpSolrServer", org.apache.solr.client.solrj.impl.HttpSolrServer) unless defined? ::HttpSolrServer
    Object.const_set("SolrInputDocument", org.apache.solr.common.SolrInputDocument) unless defined? ::SolrInputDocument
  rescue NameError  => e
    # /Users/jrochkind/code/solrj-gem/lib"

    included_jar_dir = File.expand_path("../../vendor/solrj/lib", File.dirname(__FILE__))

    jardir = settings["solrj.jar_dir"] || included_jar_dir
    Dir.glob("#{jardir}/*.jar") do |x|
      require x
    end
    if tries > 1
      raise LoadError.new("Can not find SolrJ java classes")
    else
      retry
    end
  end
end