Class: RubyRest::Engine

Inherits:
Object
  • Object
show all
Includes:
Tools
Defined in:
lib/rubyrest/engine.rb

Overview

Objects of this class take a configuration as argument then launch a new server instance.

Constant Summary

Constants included from Tools

Tools::ATOM_DATE_FORMAT, Tools::ERRORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools

#error, #format_atom_date, #nvl, #parse_atom_date, #to_class, #to_gem_name, #to_module_name

Constructor Details

#initialize(original) ⇒ Engine

Creates a new RubyRest engine, for the specified hash of configuration data. The specified hash of params will be ‘interned’ so that config keys are accessible as symbols instead of strings.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rubyrest/engine.rb', line 21

def initialize( original )
  params = {}
  original.each{ |k,v| params[ k.intern ] = v }
  @service = params[ :service ]
  @prefix = params[ :prefix ]
  service_module = to_module_name( @prefix, @service )
  require @service
  @config = to_class( service_module, "config" ).new( params )
  @config[ :servicemodule ] = service_module
end

Instance Attribute Details

#configObject (readonly)

Enables external objects to read the engine configuration



15
16
17
# File 'lib/rubyrest/engine.rb', line 15

def config
  @config
end

Instance Method Details

#configure_databaseObject

Configures the database connectivity



55
56
57
58
59
60
61
62
# File 'lib/rubyrest/engine.rb', line 55

def configure_database
  @config.connect_to_database
  @config.setup_persistence
  if @config[ :destroy ] == true
    @config.init_schema
    @config.load_initial_data
  end
end

#startObject

Starts the engine. The following operations are accomplished:

1. configure the database
2. launch the webserver, in daemon mode unless false is

specified in the configuration



37
38
39
40
41
42
43
44
# File 'lib/rubyrest/engine.rb', line 37

def start
  configure_database if @config.has( :adapter )
  @server = RubyRest::Server.new( @config )
  @server.mount "/", CRUDServlet
  if @config.has( :daemon ) and @config[ :daemon ] == false 
    start_server 
  else start_daemon end
end

#start_daemonObject

Starts the server in another process



65
66
67
68
69
70
# File 'lib/rubyrest/engine.rb', line 65

def start_daemon
  @pid = fork do
    start_server
  end
  puts "#{self.to_s}: started process with pid=#{@pid}"
end

#start_serverObject

Starts the server



73
74
75
76
77
78
# File 'lib/rubyrest/engine.rb', line 73

def start_server
  [ "INT", "TERM" ].each { |signal|
    trap( signal ) { @server.shutdown }
  }
  @server.start
end

#stopObject

Shutdowns the current server, if existing. This is only useful when working in daemon mode.



48
49
50
51
52
# File 'lib/rubyrest/engine.rb', line 48

def stop
  return if @pid == nil
  Process.kill( 9, @pid )
  puts "#{self.to_s}: killed process with pid=#{@pid}"
end

#to_sObject

Returns the service name



81
82
83
# File 'lib/rubyrest/engine.rb', line 81

def to_s 
  "rubyrest: #{@service}"
end