Mari_Yaguchi
Mari_Yaguchi

Reputation: 457

Use WSO2 Data Source inside a class mediator

is there any way to use the WSO2 Data Source inside a class mediator? I tried to googling this, but found nothing. Is it possible to do this?

Upvotes: 1

Views: 1071

Answers (2)

poohdedoo
poohdedoo

Reputation: 1268

You can access your data source directly using JNDI data source if you expose your data source as JNDI data source.

//imports
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;

try {
    Hashtable environment = new Hashtable();
    environment.put("java.naming.factory.initial", "org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory");
    Context initContext = new InitialContext(environment);
    DataSource ds = (DataSource)initContext.lookup("jdbc/MyCarbonDataSource");
    if (result != null) {
        // Do your work here
        conn = ds.getConnection();

       st = conn.createStatement();
       rs = st.executeQuery("SELECT * FROM Customer");
    } else {
       //handle it
    }
} catch (NamingException e) {
    e.printStackTrace();
}

Please refer this for further details

Upvotes: 1

Rajeev Sampath
Rajeev Sampath

Reputation: 2747

To do so, make it an OSGi bundle and get the data source service injected to it through declarative services (write a new class that acts as the bundle activator). The reference name of the data source service is org.wso2.carbon.ndatasource. The service interface is org.wso2.carbon.ndatasource.core.DataSourceService. Once the service is injected, you'll be able to access the available data sources through this.

The maven dependency

<dependency>
         <groupId>org.wso2.carbon</groupId>
         <artifactId>org.wso2.carbon.ndatasource.core</artifactId>
         <version>{set.carbon.platform.version.here}</version>
</dependency>

When you deploy this osgi bundle, place it inside dropins folder so that carbon can identify it as an osgi bundle.

some resources that may be helpful for implementing this:

  1. http://wso2-oxygen-tank.10903.n7.nabble.com/how-does-WSO2-implement-the-Declarative-Service-td9292.html
  2. http://subashsdm.blogspot.com/2013/01/publish-wso2-governance-registry-events.html

Upvotes: 0

Related Questions