javaapache-tomeeopenejbtomee-8

TomEE custom property provider


I am running my application in TomEE 8. I want to read some property from my DB and pass it to application.

I saw one example from https://rmannibucau.wordpress.com/2014/08/06/tomee-and-more-advanced-resource-configuration/ . may be it is old. it is not working . while deploying application it expect the Resource Type. But example doesn't have Type in resource tag.

<Resource id="..." properties-provider="com.foo.MyPropertiesReader" />

What is the latest/correct way to load my custom properties to my application from my DB ?


Solution

  • I believe that Romain was trying to be concise in his examples. In his examples, it can be surmised that he was using javax.sql.DataSource or DataSource. Despite that, though, this will work for any type of resource, even custom resources. There are also already implementations that you can check out:

    In the page you mentioned, Romain also notes that your class can either implement org.apache.openejb.api.resource.PropertiesResourceProvider, or supply a Properties provides(); method.

    Here is a small example:

    org.superbiz.provider.MyPropertiesReader.java

    package org.superbiz.provider;
    
    import org.apache.openejb.api.resource.PropertiesResourceProvider;
    import org.apache.openejb.testng.PropertiesBuilder;
    
    import java.util.Properties;
    
    public class MyPropertiesReader implements PropertiesResourceProvider {
        public Properties provides() {
            return new PropertiesBuilder()
                    .p("JdbcDriver", "org.hsqldb.jdbcDriver")
                    .p("JdbcUrl", "jdbc:hsqldb:mem:moviedb")
                    .build();
        }
    }
    

    src/main/webapp/WEB-INF/resources.xml

    <resources>
        <Resource id="movieDatabase"
                  type="DataSource"
                  properties-provider="org.superbiz.provider.MyPropertiesReader"/>
    </resources>
    

    These are the key snippets that I hope will help clear your doubts. Implementation of the datasource is left for you to code. :)