Module: Trinidad::DBPool

Defined in:
lib/trinidad_dbpool.rb

Class Method Summary collapse

Class Method Details

.create_resource(context, options, protocol = nil) {|resource| ... } ⇒ Object

Yields:

  • (resource)


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trinidad_dbpool.rb', line 9

def create_resource(context, options, protocol = nil)
  name = options.delete(:name) || options.delete(:jndi); url = options.delete(:url)
  if ! url.start_with?('jdbc:') && protocol
    url = "#{protocol}#{url}" unless url.start_with?(protocol)
  end

  resource_factory = options.key?(:factory) ? options.delete(:factory) :
    'org.apache.tomcat.jdbc.pool.DataSourceFactory'
  if ( pool = options.delete(:pool) ) && pool.to_s != 'jdbc' # pool: 'dbcp'
    begin
      load File.expand_path("../../trinidad-libs/tomcat-#{pool}.jar", __FILE__)
      resource_factory = nil
    rescue LoadError
      context.logger.warn "The `pool: #{pool}` option is not supported, please remove it"
    else
      context.logger.info "Using deprecated `pool: #{pool}` configuration option, consider removing it"
    end
  end

  driver = options.delete(:driver) || options.delete(:driverName) || options.delete(:driver_name)

  context.logger.debug "Using JDBC URL: #{url.inspect} for #{name}"

  # <Resource name="jdbc/MyDB"
  #           auth="Container"
  #           type="javax.sql.DataSource"
  #           url="jdbc:mysql://localhost:3306/mydb"
  #           driverClassName="com.mysql.jdbc.Driver"
  #           maxActive="100" maxIdle="30" maxWait="10000"
  #           username="root" password="secret" />
  resource = Trinidad::Tomcat::ContextResource.new
  resource.set_auth(options.delete(:auth)) if options.key?(:auth)
  resource.set_description(options.delete(:description)) if options.key?(:description)
  resource.set_name(name)
  resource.set_type(options.delete(:type) || 'javax.sql.DataSource')
  resource.set_property('factory', resource_factory) if resource_factory
  resource.set_property('driverClassName', driver)
  resource.set_property('url', url)
  camelizer = supported_properties_camelizer
  options.each do |key, value|
    resource.set_property(camelizer[key.to_s], value.to_s)
  end

  yield(resource) if block_given?

  context.naming_resources.add_resource(resource)
  context.naming_resources = resource.naming_resources

  resource
end