[WSO2] How to Add a Datasource by Commandline

Every server at WSO2 (i.e Application Server, Data services Server) has a common way of exposing a database to its applications. That is using a data source. Sometimes, we do not prefer to create datasource in server by logging into the Management Console. Programmatically, we should be able to create a data source.

Admin Services exposed by the server 

There are various admin services exposed by the server as SOAP. We can leverage one of such admin service called “NDataSourceAdmin” to manage data-sources programmatically.
This admin service has following operations.

Calling Admin Services

We can call the SOAP admin services by sending a relevant soap request with the action header set to the operation name we are calling. For an example if we want to add a datasource to the server,
SOAP action: addDataSource

SOAP request to be sent when adding a datasource

Following is a sample data-source wrapped in a SOAP envelope.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://org.apache.axis2/xsd" xmlns:xsd1="http://services.core.ndatasource.carbon.wso2.org/xsd" xmlns:xsd2="http://core.ndatasource.carbon.wso2.org/xsd">
<xsd:addDataSource xmlns:xsd="http://org.apache.axis2/xsd"
                  <validationQuery>SELECT 1</validationQuery>

Following are meanings of important tags on the request

  •  <xsd1:dsXMLConfiguration: Data source configuration falls under here as a CDATA tag.
  • <url: database url
  • <Username: username used to authenticate with DB
  • <password: password of above user
  • <driverClassName: class name of the driver implementation used to connect with DB
  • <type: type of the database  (i.e RDBMS)
  • <description: description for the data-source
  • <jndiConfig<name: JNDI name by which data source should be exposed
  • <name: name of the data source

Sending the soap request by commandline

Save above soap request to a file and name it as “MyCarbonDataSource.xml”. Using curl we can send the request as below. Invoke curl command from where MyCarbonDataSource.xml exists.

curl -k -u admin:admin --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:addDataSource" --data @MyCarbonDataSource.xml

Verifying if data source is added and active

In order to verify if the data source is added, navigate to following location using Management Console registry browser (Home>>Registry>>Browse).


You will see that new data source is added to the registry

Then go to Configure >> Data Sources and verify that it is active

Now your DataSource is ready...!!

Hasitha Hiranya

No comments:

Post a Comment