Class: AtlConfig::ServerXML

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

Class Method Summary collapse

Class Method Details

.dbinfo(cfgxml, dsname) ⇒ DBInfo

Extracts database info from a datasource definition in a Tomcat server.xml files.

Parameters:

  • cfgxml (String)

    XML Text of conf/server.xml file

  • dsname (String)

    Name of datasource defined in XML

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/atl_config/serverxml.rb', line 9

def self.dbinfo(cfgxml, dsname)
  config = Nokogiri::XML(cfgxml)
  dsname = dsname.gsub(/^java:comp\/env\//, '')
  datasource = config.xpath("/Server/Service/Engine[@name='Standalone']/Host[@name='localhost']/Context/Resource[@name='#{dsname}']").first
  if datasource then
    r = DBInfo.new
    r.user = datasource.xpath("@username")&.to_s
    r.password = datasource.xpath("@password")&.to_s
    url = datasource.xpath("@url")&.to_s
    if url then
      urlparts = AtlConfig::JDBCURL.parse(url)
      r.dbtype = urlparts[:dbtype]
      r.host = urlparts[:host]
      r.port = if urlparts[:port] then urlparts[:port].to_i
        else
          case r.dbtype
          when 'postgresql'
            5432
          when 'mysql'
            3306
          else
            raise "Unhandled db type #{r.dbtype}"
          end
        end
      r.database = urlparts[:database]
    end
    r
  else
    nil
  end
end