Class: Jettywrapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jettywrapper.rb

Overview

Jettywrapper is a Singleton class, so you can only create one jetty instance at a time.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fedora_homeObject

Where is fedora located? Default is jetty_home/fedora



14
15
16
# File 'lib/jettywrapper.rb', line 14

def fedora_home
  @fedora_home
end

#jetty_homeObject

Where is jetty located?



10
11
12
# File 'lib/jettywrapper.rb', line 10

def jetty_home
  @jetty_home
end

#pidObject

If Jettywrapper is running, what pid is it running as?



8
9
10
# File 'lib/jettywrapper.rb', line 8

def pid
  @pid
end

#portObject

What port should jetty start on? Default is 8888



9
10
11
# File 'lib/jettywrapper.rb', line 9

def port
  @port
end

#quietObject

Keep quiet about jetty output?



12
13
14
# File 'lib/jettywrapper.rb', line 12

def quiet
  @quiet
end

#solr_homeObject

Where is solr located? Default is jetty_home/solr



13
14
15
# File 'lib/jettywrapper.rb', line 13

def solr_home
  @solr_home
end

#startup_waitObject

After jetty starts, how long to wait until starting the tests?



11
12
13
# File 'lib/jettywrapper.rb', line 11

def startup_wait
  @startup_wait
end

Class Method Details

.configure(params = {}) ⇒ Object

Set the jetty parameters. It accepts a Hash of symbols.

Parameters:

  • params (Hash<Symbol>) (defaults to: {})
  • :jetty_home (Symbol)

    Required. Where is jetty located?

  • :jetty_port (Symbol)

    What port should jetty start on? Default is 8888

  • :startup_wait (Symbol)

    After jetty starts, how long to wait before running tests? If you don’t let jetty start all the way before running the tests, they’ll fail because they can’t reach jetty.

  • :solr_home (Symbol)

    Where is solr? Default is jetty_home/solr

  • :fedora_home (Symbol)

    Where is fedora? Default is jetty_home/fedora/default

  • :quiet (Symbol)

    Keep quiet about jetty output? Default is true.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jettywrapper.rb', line 31

def configure(params = {})
  hydra_server = self.instance
  hydra_server.quiet = params[:quiet].nil? ? true : params[:quiet]
  if defined?(Rails.root)
   base_path = Rails.root
  else
   raise "You must set either RAILS_ROOT or :jetty_home so I know where jetty is" unless params[:jetty_home]
  end
  hydra_server.jetty_home = params[:jetty_home] || File.expand_path(File.join(base_path, 'jetty'))
  hydra_server.solr_home = params[:solr_home]  || File.join( hydra_server.jetty_home, "solr")
  hydra_server.fedora_home = params[:fedora_home] || File.join( hydra_server.jetty_home, "fedora","default")
  hydra_server.port = params[:jetty_port] || 8888
  hydra_server.startup_wait = params[:startup_wait] || 5
  return hydra_server
end

.wrap(params = {}) ⇒ Object

Wrap the tests. Startup jetty, yield to the test task, capture any errors, shutdown jetty, and return the error.

Examples:

Using this method in a rake task

require 'jettywrapper'
desc "Spin up jetty and run tests against it"
task :newtest do
  jetty_params = { 
    :jetty_home => "/path/to/jetty", 
    :quiet => false, 
    :jetty_port => 8983, 
    :startup_wait => 30
  }
  error = Jettywrapper.wrap(jetty_params) do   
    Rake::Task["rake:spec"].invoke 
    Rake::Task["rake:cucumber"].invoke 
  end 
  raise "test failures: #{error}" if error
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jettywrapper.rb', line 65

def wrap(params = {})
  error = false
  jetty_server = self.instance
  jetty_server.quiet = params[:quiet] || true
  jetty_server.jetty_home = params[:jetty_home]
  jetty_server.solr_home = params[:solr_home]
  jetty_server.port = params[:jetty_port] || 8888
  jetty_server.startup_wait = params[:startup_wait] || 5
  jetty_server.fedora_home = params[:fedora_home] || File.join( jetty_server.jetty_home, "fedora","default")

  begin
    # puts "starting jetty on #{RUBY_PLATFORM}"
    jetty_server.start
    sleep jetty_server.startup_wait
    yield
  rescue
    error = $!
    puts "*** Error starting hydra-jetty: #{error}"
  ensure
    # puts "stopping jetty server"
    jetty_server.stop
  end

  return error
end

Instance Method Details

#jetty_commandObject

What command is being run to invoke jetty?



94
95
96
# File 'lib/jettywrapper.rb', line 94

def jetty_command
  "java -Djetty.port=#{@port} -Dsolr.solr.home=#{@solr_home} -Dfedora.home=#{@fedora_home} -jar start.jar"
end

#jruby_raise_error?Boolean

Not Windows

Returns:

  • (Boolean)


134
135
136
# File 'lib/jettywrapper.rb', line 134

def jruby_raise_error?
  raise 'JRuby requires that you start solr manually, then run "rake spec" or "rake features"' if defined?(JRUBY_VERSION)
end

#platform_specific_startObject

start jetty for *nix



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jettywrapper.rb', line 114

def platform_specific_start
  puts "Starting Jetty on windows"
  Dir.chdir(@jetty_home) do
    @pid = Process.create(
          :app_name         => jetty_command,
          :creation_flags   => Process::DETACHED_PROCESS,
          :process_inherit  => false,
          :thread_inherit   => true,
          :cwd              => "#{@jetty_home}"
       ).process_id
  end
end

#platform_specific_stopObject

stop jetty for *nix



128
129
130
131
# File 'lib/jettywrapper.rb', line 128

def platform_specific_stop
  Process.kill(1, @pid)
  Process.wait
end

#startObject



98
99
100
101
102
103
104
# File 'lib/jettywrapper.rb', line 98

def start
  puts "jetty_home: #{@jetty_home}"
  puts "solr_home: #{@solr_home}"
  puts "fedora_home: #{@fedora_home}"
  puts "jetty_command: #{jetty_command}"
  platform_specific_start
end

#stopObject



106
107
108
# File 'lib/jettywrapper.rb', line 106

def stop
  platform_specific_stop
end