Search code examples
jakarta-eeibm-mobilefirst

Using underlying Java EE Resources in MobileFirst Foundation


Looking at the Java Adapter documentation and samples, I don't see any examples of delegating like DataSource configuration to the underlying Java EE server. Instead things like Driver and authentication are in Adapter properties. Is using Java EE Resources a supported pattern?

It definitely seems non-ideal and tedious to have to manage resource details within each adapter, especially if multiple adapters might use the same resources. Not to mention potentially having sensitive properties in adapter configuration files.

Or is the idea that the people managing these things will know nothing about the Java EE server, and instead only know how to use the MFP console?


Solution

  • I have been able to do this in my Java EE server (WebSphere Liberty). Described in a blog post, recreated here.

    Define the JNDI DataSource in your Java EE server. (e.g. Configuring relational database connectivity in Liberty)

    Put the JNDI name itself in Adapter configuration parameters:

    <property name="dataSource.jndiName" 
              displayName="DataSource JNDI Name" defaultValue="jdbc/myDataSource"/>
    

    In your adapter Java code, use the following simple code to obtain that DataSource:

        try {
            javax.naming.InitialContext ctx = new javax.naming.InitialContext();
            this.dataSource = (javax.sql.DataSource) ctx.lookup(dataSourceJndiName);
        }
        catch (javax.naming.NamingException e) {
            throw new RuntimeException(
                "Unable to locate specified DataSource: " + dataSourceJndiName);
        }
    

    Where dataSourceJndiName is obtained from the MobileFirst ConfigurationAPI:

    configApi.getPropertyValue("dataSource.jndiName");