Using properties in CLI scripts

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Some time ago I've found one interesting thread on Jboss.org forum (https://developer.jboss.org/thread/254496) discussing about one undocumented option available in the CLI which allows to use external properties in a script. I've used it to create a general purpose script to install any kind of Datasource. Let's see how to do it.

So at first the trick: you can pass a property file to a CLI script with the --properties argument as follows:

./jboss-cli.sh -c --properties=file.properties --file=script.cli 

Let's see in practice how to use it to create a Datasource. You have to prepare two files: the file createDataSource.cli which will be a general purpose script fit for any database:

set jdbcUrl=${jdbcUrl}  
set username=${username}  
set password=${password}  
set driver=${driver}  
set module=${module}  
set resource=${resource}
set jndiname=${jndiname}
set name=${name}
set resource=${resource}


module add --name=$module --resources=$resource --dependencies=javax.api,javax.transaction.api
 
/subsystem=datasources/jdbc-driver=$driver:add(driver-name=$driver,driver-module-name=$module)

data-source add --jndi-name=$jndiname --name=$name --connection-url=$jdbcUrl --driver-name=$driver --user-name=$username --password=$password

reload

As you can see, no reference is included about the specific database JDBC settings or driver. And now let's include the above properties in a configuration file named mysql.properties:

jdbcUrl=jdbc:mysql://localhost:3306/as7development  
username=jboss  
password=jboss  
driver=mysql  
module=com.mysql  
resource=/home/jboss/Downloads/mysql-connector-java-5.1.24-bin.jar
name=MySQLPool
jndiname=java:/MySqlDS

 Adapt the above properties to your need. Once done, execute from the shell:

./jboss-cli.sh -c --properties=mysql.properties --file=createDataSource.cli 

Now switching to another DB is just a matter of updating the properties. Here are the properties for PostgreSQL I have tested:

jdbcUrl=jdbc:postgresql://localhost/postgres  
username=jboss  
password=jboss  
driver=postgresql  
module=com.postgresql  
resource=/home/jboss/Downloads/postgresql-9.3-1101.jdbc41.jar
name=PostgreSQLPool
jndiname=java:/PostgreDS

 Tested as well with Oracle XE:

jdbcUrl=jdbc:oracle:thin:@localhost:1521:XE  
username=admin  
password=admin  
driver=oracle  
module=com.oracle  
resource=/home/jboss/Downloads/ojdbc6.jar
name=OraclePool
jndiname=java:/OracleDS

 Have fun with the CLI! Thanks to Stefan Maier for sharing this cool tip on JBoss.org!


Advertisement

Related articles available on mastertheboss.com

How do I get the list of MBeans with twiddle?

  JBoss provides a simple command line tool that allows for inte

How to count active session with twiddle ?

  There are some well known alternatives to count the number of

How do I run garbage collector from shell?

  You can suggest JBoss to run the garbage collector in differen

How do I check my application status from shell?

  This is a two process step: at first you need to retrieve the

How do you shut down multiple JBoss instances?

  Supposing you have configured 2 instances of JBoss, one on por

twiddle reference guide

Please note: twiddle is not available any more as default Command