Resources are something basic in JavaEE but in TomEE and OpenEJB it is quite more powerful than it looks like.
Basically you have two main ways to define a resource in TomEE or OpenEJB:
In the first case you’ll surely do something like:
<Resource id="resourceId" aResourceAttribute="attrValue" anotherResourceAttribute="anotherValue"> attribute1 = value1 attribute2 = value2 </Resource>
In the second case you’ll do:
resourceId = new://Resource?aResourceAttribute=attrValue1&anotherResourceAttribute=anotherValue resourceId.attribute1 = value1 resourceId.attribute2 = value2
If code doesn’t make it obvious: both are the same: a line declaring what we want (a resource, with an id and some resource config) and some attributes values.
If you prefix the id by openejb:Resource/ you’ll get the internal JNDI name of the resource.
Attributes depend on the resource (I speak about attribute1, attribute2 etc in the previous sample). They globally define the state of the resources (they are often resource attributes as we’ll see).
Note: there are some internal attributes to change some behavior but I’ll not deal with it, they depend on the resource type (DataSource…).
The resource configuration attributes define how the resource will be instantiated. Here are the main ones:
- type: a known type. It will be used to find defaults and use them as base if you don’t override all attributes
- provider: the path to the default provider (it has defaults)
- class-name: the factory type (qualified name)
- factory-name: the factory method name
If you don’t define a factory-name a “new” will be done no the class-name class.
So what does it mean?
It means when you declare a datasource this way:
<Resource id="jdbc/db" type="DataSource"> JdbcUrld = jdbc:hsqldb:mem:db </Resource>
OpenEJB then use the defaults of type DataSource (you can find them here http://svn.apache.org/repos/asf/tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml in Service Provider “Default JDBC Database”). You can see there are defaults for all the attributes – that’s why you don’t need to set them and resource configuration attributes define you’ll use DataSourceFactory to create the datasource.
Note: “constructor” attribute just define the parameter matching to ensure the ordering of them if “create” method has parameters.
Ok but do I really need to know it? You can cause it can help you to get custom resources defined or injected. For instance if you have a class org.superbiz.Configuration with a setter “setUrl” and another one “setName” you can define it this way:
<Resource id="config" class-name="org.superbiz.Configuration"> Url = http://foo.com/bar Name = bar </Resource>
And you can then simply get it injected using:
@Resource(name = "config") private Configuration configuration;
If you want to see how provider (defaults) mecanism works you can read http://tomee.apache.org/examples/dynamic-datasource-routing/README.html