#!/bin/sh # vim:ft=ruby:sw=2:ts=2:et # # BSD License # # Copyright (c) 2007, Ivan Tarasov , Sun Microsystems, Inc. # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the Sun Microsystems, Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # servers.yaml example: # --- # localhost: # host: localhost # port: 8686 # user: admin # password: top-secret # secure: false # # dev: # host: net.hack # port: 8686 # user: admin # password: KERNOD-WEL # secure: true # trust-options: # trust-any: true # # prod: # host: foobar # port: 8686 # user: admin # password: '(define eternity (lambda (x) (eternity x)))' # secure: true # trust-options: # trust-any: false # trust-store: trust.jks # trust-store-password: '(eternity `())' # pools.yaml example: # # --- # reports: # data_source_class: oracle.jdbc.pool.OracleDataSource # is_connection_validation_required: true # is_isolation_level_guaranteed: false # res_type: javax.sql.DataSource # properties: # User: reportsuser # Password: unspeakable # URL: jdbc:oracle:thin:@dbserver:1525:reports # # wiki: # data_source_class: oracle.jdbc.pool.OracleDataSource # is_connection_validation_required: true # is_isolation_level_guaranteed: false # res_type: javax.sql.DataSource # properties: # User: kiwi # Password: 'kryliev.net' # URL: jdbc:oracle:thin:@wiki:1522:wikidb # change the CLASSPATH to point to the javaee.jar and appserv-ext.jar export CLASSPATH=/opt/glassfish/lib/javaee.jar:/opt/glassfish/lib/appserv-ext.jar #export JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.config.file=logging.properties -Djavax.net.debug=ssl" #export JAVA_OPTS="$JAVA_OPTS -Djavax.net.ssl.trustStore=./trust.jks -Djavax.net.ssl.trustStorePassword=123456" jruby -- /dev/stdin $* << "END_OF_SCRIPT" require 'java' require 'yaml' require 'optparse' require 'ostruct' HashMap = java.util.HashMap AppserverConnectionSource = com.sun.appserv.management.client.AppserverConnectionSource TLSParams = com.sun.appserv.management.client.TLSParams TrustAnyTrustManager = com.sun.appserv.management.client.TrustAnyTrustManager TrustStoreTrustManager = com.sun.appserv.management.client.TrustStoreTrustManager X509TrustManager = javax.net.ssl.X509TrustManager JString = java.lang.String def parse_options options = OpenStruct.new options.force = false opts_parser = OptionParser.new do |opts| opts.banner = < Create the connection pools described in on the given server. Application servers' configurations are listed in `servers.yaml'. BANNER opts.separator '' opts.on('-f', '--force', 'Force pool/JDBC resource recreation when they already exist.') do |f| options.force = f end opts.on_tail('-h', '--help', 'This help message.') do puts opts exit 0 end end opts_parser.parse! ARGV if ARGV.length != 2 then $stderr.puts opts_parser exit 1 end options.server_name = ARGV[0] options.pools_yaml = ARGV[1] options end def get_server_configuration(server_name) servers = YAML.load(IO.read('servers.yaml')) servers[server_name] end def connect_and_get_domain_root(host, port, user, password, secure, trust_options) def create_tls_params(trust_options) if trust_options.nil? then nil else if trust_options['trust-any'] then trust_managers = TrustAnyTrustManager.getInstanceArray else store = java.io.File.new(trust_options['trust-store']) store_password = JString.new(trust_options['trust-store-password']).to_char_array trust_managers = X509TrustManager[1].new(TrustStoreTrustManager.new(store, store_password)) end TLSParams.new(trust_managers, nil) end end tls_params = secure ? create_tls_params(trust_options) : nil conn = AppserverConnectionSource.new(AppserverConnectionSource::PROTOCOL_RMI, host, port, user, password, tls_params, nil) conn.getJMXConnector(false) conn.domain_root end def configure_pool(domain_config, pool_name, pool_params, force) def add_resource_ref(domain_config, jndi_name) servers = domain_config.server_config_map servers.each do |server_name, server| server.create_resource_ref_config jndi_name, true end end reject_params = { :data_source_class => nil, :properties => nil } configured_pools = domain_config.JDBCConnectionPoolConfigMap configured_resources = domain_config.JDBCResourceConfigMap resource_name = "jdbc/#{pool_name}" pool_exists = configured_pools.contains_key(pool_name) resource_exists = configured_resources.contains_key(resource_name) need_to_force = (pool_exists or resource_exists) if need_to_force and !force then puts "Found existing connection pool or JDBC resource for pool `#{pool_name}', skipping..." else if force then if resource_exists then puts "Found existing JDBC resource `#{resource_name}', removing..." domain_config.removeJDBCResourceConfig resource_name end if pool_exists then puts "Found existing connection pool `#{pool_name}', removing..." domain_config.removeJDBCConnectionPoolConfig pool_name end end datasource_class = pool_params[:data_source_class.to_s] puts "Creating pool `#{pool_name}' with DataSource class `#{datasource_class}'..." pool = domain_config.createJDBCConnectionPoolConfig(pool_name, datasource_class, nil) pool_params.each_pair do |param, value| param_setter = "#{param}=".to_sym unless reject_params.has_key?(param) pool.send(param_setter, value) if pool.respond_to? param_setter end end if pool_params.has_key? :properties.to_s pool_params[:properties.to_s].each_pair { |k,v| pool.set_property_value k, v } end puts "Creating JDBC Resource `#{resource_name}' for pool `#{pool_name}'..." domain_config.createJDBCResourceConfig(resource_name, pool_name, HashMap.new( { com.sun.appserv.management.config.ResourceConfigKeys::ENABLED_KEY => true })) add_resource_ref domain_config, resource_name end end options = parse_options server = get_server_configuration options.server_name if server.nil? $stderr.puts "Cannot find server configuration for server `#{options.server_name}'." exit 2 end domain_root = connect_and_get_domain_root(server['host'], server['port'], server['user'], server['password'], server['secure'], server['trust-options']) domain_config = domain_root.domain_config pools = YAML.load(IO.read(options.pools_yaml)) pools.each_pair do |pool_name,params| configure_pool(domain_config, pool_name, params, options.force) end END_OF_SCRIPT