I have following jruby code that uses java class javax.naming.InitialContext
:
if RUBY_PLATFORM == "java"
require 'java'
import javax.naming.InitialContext
module JndiProperties
def self.getProperty(name)
begin
env.lookup(name).to_s
rescue
nil
end
end
def self.[](name)
getProperty(name)
end
private
def self.env
context = InitialContext.new
environment = context.lookup 'java:comp/env'
environment
end
end
else
module JndiProperties
def self.getProperty(name)
nil
end
def self.[](name)
getProperty(name)
end
end
end
I use this module in database.yml to configure database connection. E.g.:
username: <%= JndiProperties['ANTARCTICLE_DB_USER'] || 'root' %>
When I try to run rails application, i get uninitialized constant JndiProperties::InitialContext
. If i try to use this module from irb, it will work as expected.