Class: StompServer::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/stomp_server_ng.rb

Overview

Module level configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurator

Returns a new instance of Configurator.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/stomp_server_ng.rb', line 139

def initialize

  @opts = nil
  @defaults = {
    #
    # For clarity maintain the same order here as below in the 'getopts' 
    # method!!!!
    #
    :auth => false,                 # -a
    :host => "127.0.0.1",           # -b
    :checkpoint => 0,               # -c
    :config => 'stompserver.conf',  # -C
    :debug => false,                # -d
    :logdir => 'log',               # -D
    :log_level => 'error',          # -l
    :logfile => 'stompserver.log',  # -L
    :port => 61613,                 # -p
    :pidfile => 'stompserver.pid',  # -P
    :queue => 'memory',             # -q
    :storage => ".stompserver",     # -s
    :session_cache => 0,            # -S
    :working_dir => Dir.getwd,      # -w
    :dbyml => 'database.yml',       # -y
    :daemon => false                # -z
  }
  # Get a crude logger
  @@log = Logger.new(STDOUT)

  # Show version numbers regardless
  StompServer::LogHelper.showversion(@@log)

  # Options handling
  @opts = getopts()   # get and merge the options

  # Finalize logger level handling
  @@log.debug "Logger Level Requested: #{@opts[:log_level].upcase}"
  StompServer::LogHelper.set_loglevel(@opts)

  # Show Load Path
  StompServer::LogHelper.showloadpath(@@log)

  @@log.level = StompServer::LogHelper.get_loglevel()

  # Turn on $DEBUG for extra debugging info if requested
  if opts[:debug]
    $DEBUG=true
    @@log.debug "-d / --debug set, $DEBUG is true"
  end

  # Configuration is complete!
  @@log.debug("#{self.class} Configuration complete")
end

Instance Attribute Details

#optsObject

The final options, merged from the defaults, the config file, and the command line. – Should this be ‘read’ only after construction ????



137
138
139
# File 'lib/stomp_server_ng.rb', line 137

def opts
  @opts
end

Instance Method Details

#getoptsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/stomp_server_ng.rb', line 192

def getopts()

  # New Options Parser
  opts_parser = OptionParser.new

  # Empty Hash for parser values
  hopts = {}

  # :auth
  opts_parser.on("-a", "--auth", String, 
    "Require client authorization") {|a| 
    hopts[:auth] = true}

  # :host
  opts_parser.on("-b", "--host=ADDR", String, 
    "Change the host (default: localhost)") {|a| 
    hopts[:host] = a}

  # :checkpoint
  opts_parser.on("-c", "--checkpoint=SECONDS", Integer, 
    "Time between checkpointing the queues in seconds (default: 0)") {|c| 
    hopts[:checkpoint] = c}

  # :config
  opts_parser.on("-C", "--config=CONFIGFILE", String, 
    "Configuration File (default: stompserver.conf)") {|c| 
    hopts[:config] = c}

  # :debug
  opts_parser.on("-d", "--debug", String, 
    "Turn on debug messages") {|d| 
    hopts[:debug] = true}

  # :logdir
  opts_parser.on("-D", "--logdir=LOGDIR", String, 
    "Log file directory  (default: log)") {|d| 
    hopts[:logdir] = d} # new

  # :log_level
  opts_parser.on("-l", "--log_level=LEVEL", String, 
    "Logger Level (default: ERROR)") {|l| 
    hopts[:log_level] = l}

  # :logfile
  opts_parser.on("-L", "--logfile=LOGFILE", String, 
    "Log file name (default: stompserver.log") {|l| 
    hopts[:logfile] = l} # new

  # :port
  opts_parser.on("-p", "--port=PORT", Integer, 
    "Change the port (default: 61613)") {|p| 
    hopts[:port] = p}

  # :pidfile
  opts_parser.on("-P", "--pidfile=PIDFILE", Integer, 
    "PID file name (default: stompserver.pid)") {|p| 
    hopts[:pidfile] = p} # new

  # :queue
  opts_parser.on("-q", "--queuetype=QUEUETYPE", String, 
    "Queue type (memory|dbm|activerecord|file) (default: memory)") {|q| 
    hopts[:queue] = q}

  # :storage
  opts_parser.on("-s", "--storage=DIR", String, 
    "Change the storage directory (default: .stompserver, relative to working_dir)") {|s| 
    hopts[:storage] = s}

  # :session_cache
  opts_parser.on("-S", "--session_cache=SIZE", Integer, 
    "session ID cache size (default: 0, disable session ID cache)") {|s| 
    hopts[:session_cache] = s}

  # :working_dir
  opts_parser.on("-w", "--working_dir=DIR", String, 
    "Change the working directory (default: current directory)") {|s| 
    hopts[:working_dir] = s}

  # :dbyml
  opts_parser.on("-y", "--dbyml=YMLFILE", String, 
    "Database .yml file name (default: database.yml)") {|y| 
    hopts[:dbyml] = y}

  # :daemon
  opts_parser.on("-z", "--daemon", String, 
    "Daemonize server process") {|d| 
    hopts[:daemon] = true}

  # Handle help if required
  opts_parser.on("-h", "--help", "Show this message") do
    puts opts_parser
    exit
  end

  opts_parser.parse(ARGV)

  # Handle the config file
  config_found = false
  # Load a default config file first if it exists.
  loaded_opts = {}
  full_path_default = File.expand_path(@defaults[:config])
  if File.exists?(full_path_default)
    @@log.debug("Loading config file defaults from: #{full_path_default}")
    loaded_opts.merge!(YAML.load_file(full_path_default))
    @defaults[:config] = full_path_default
    config_found = true
  end
  # If a command line specified config file exists, overlay any new
  # parameters in that file.
  if hopts[:config]
    full_path_cl = File.expand_path(hopts[:config])
    if File.exists?(full_path_cl)
      @@log.debug("Loading config file overrides from: #{full_path_cl}")
      loaded_opts.merge!(YAML.load_file(full_path_cl))
      hopts[:config] = full_path_cl
      config_found = true
    end
  end
  @@log.warn("No configuration file found.") unless config_found

  # Run basic required merges on all the options
  opts = {}                         # set to empty
  opts = opts.merge(@defaults)      # 01 = merge in defaults
  opts = opts.merge(loaded_opts)    # 02 = merge in loaded from config file
  opts = opts.merge(hopts)          # 03 = merge in command line options

  # Last but not least: Miscellaneous file definitions
  opts[:etcdir] = File.join(opts[:working_dir],'etc')           # Define ':etcdir'
  opts[:storage] = File.join(opts[:working_dir],opts[:storage]) # Override! ':storage'
  opts[:logdir] = File.join(opts[:working_dir],opts[:logdir])   # Override! ':logdir'
  opts[:logfile] = File.join(opts[:logdir],opts[:logfile])      # Override! ':logfile'
  opts[:pidfile] = File.join(opts[:logdir],opts[:pidfile])      # Override! ':pidfile'
  # :dbyml will be a full path and file name
  unless File.exists?(File.expand_path(opts[:dbyml]))
    opts[:dbyml] = File.join(opts[:etcdir],opts[:dbyml])        # Override! ':dbyml'
  else
    opts[:dbyml] = File.expand_path(opts[:dbyml])
  end

  # Authorization - working file
  if opts[:auth]
    opts[:passwd] = File.join(opts[:etcdir],'.passwd')
  end
  
  # Return merged values (in Hash)
  return opts
end