Top Level Namespace

Defined Under Namespace

Classes: AjpRailsDispatcher, AjpRailsRequest, AjpRailsResponse, RailsRunner

Constant Summary collapse

OPTION_NAMES_TO_INTERNAL_NAMES =

Maps long option names to internal hash keys.

{
  'port' => 'AJP_PORT',
  'host' => 'BIND_IP',
  'environment' => 'RAILS_ENV',
  'location' => 'APP_LOCATION',
  'directory' => 'APP_DIRECTORY',
  'prefix' => 'DISPATCHER_PREFIX',
  'suffix' => 'DISPATCHER_SUFFIX',
  'jvm-route' => 'LOAD_BALANCE_ID',
  'serverid' => 'LOAD_BALANCE_ID',
  'daemon' => 'IS_DAEMON'
}
DEFAULT_OPTIONS =
{
  OPTION_NAMES_TO_INTERNAL_NAMES['environment'] =>
    ENV['RAILS_ENV'] || 'production',
  OPTION_NAMES_TO_INTERNAL_NAMES['port'] => Net::AJP13::Constants::DEFAULT_PORT,
  OPTION_NAMES_TO_INTERNAL_NAMES['host'] => '127.0.0.1',
  OPTION_NAMES_TO_INTERNAL_NAMES['location'] => '/',
  OPTION_NAMES_TO_INTERNAL_NAMES['directory'] => '.'
}

Instance Method Summary collapse

Instance Method Details

#parse_optionsObject

Parses command line options.

Merges external configuration file into DEFAULT_OPTIONS if a file specified, and merges command line options, and returns merged options.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ajp-rails/rails-runner.rb', line 72

def parse_options
  opts = DEFAULT_OPTIONS.dup

  cmd_opts = {}
  parser = OptionParser.new
  parser.on('-p PORT', '--port=PORT', 
            "Listens to the specified port.\n" +
            "Default: #{opts[OPTION_NAMES_TO_INTERNAL_NAMES['port']]}") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['port']] = v
  }
  parser.on('-h IP', '--host=IP',
            "Binds rails to the specified ip.\n" +
            "Default: #{opts[OPTION_NAMES_TO_INTERNAL_NAMES['host']]}") {
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['host']] = v
  }
  parser.on('-e RAILS_ENV', '--environment=RAILS_ENV',
            "Specifies the environment to run this server under (test/development/production).\n" + 
            "Default: ENV['RAILS_ENV'] || 'production'") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['environment']] = v
  }
  parser.on('-l LOCATION', '--location=LOCATION',
            "The base of the application's virtual path.\n" +
            "Default: /") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['location']] = v
  }
  parser.on('-d DIRECTORY', '--directory=DIRECTORY',
            "The base of the application's physical path\n" +
            "Default: #{opts[OPTION_NAMES_TO_INTERNAL_NAMES['directory']]}") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['directory']] = v
  }
  parser.on('--prefix=PREFIX',
            "The prefix of the ajp-mounted URLs.") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['prefix']] = v
  }
  parser.on('--suffix=SUFFIX',
            "The suffix of the ajp-mounted URLs.") { |v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['suffix']] = v
  }
  parser.on('--serverid=ID',
            "The unique ID to identify this rails process," +
	    " which is for sticky session. The ID can contain only" +
	    " [a-z][A-Z][0-9], and case insensitive.") { |id|
    raise ArgumentError, 'Server ID can contain only [a-z][A-Z][0-9].' unless
      /\A[[:alnum:]]+\Z/ =~ id
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['serverid']] = id
  }
  parser.on('--jvm-route=ID',
            "This is equal to --serverid") { |id|
    raise ArgumentError, 'Server ID can contain only [a-z][A-Z][0-9].' unless
      /\A[[:alnum:]]+\Z/ =~ id
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['jvm-route']] = id
  }
  parser.on('--daemon', "Makes Rails run as a daemon") {|v|
    cmd_opts[OPTION_NAMES_TO_INTERNAL_NAMES['daemon']] = v
  }

  parser.on('-c FILE', '--config=FILE', 
            'Reads options from the specified file. ' +
            'The file must be YAML file.') {|value|
    cmd_opts['config'] = value
  }
  parser.parse(ARGV)

  if path = cmd_opts['config']
    cmd_opts.delete('config')
    if FileTest::file? path then
      require 'yaml'
      YAML.load_file(path).each do |key, value|
        name = OPTION_NAMES_TO_INTERNAL_NAMES[key]
        opts[name || key] = value
      end
    else
      warn("Not a exsiting regular file: #{path}")
      exit(1)
    end
  end

  opts.merge(cmd_opts)
end