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.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/traject/util.rb', line 77

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_solrj_jars(settings) ⇒ Object

Requires solrj jar(s) from settings['solrj.jar_dir'] 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.



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

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
    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