tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Gumbrecht <>
Subject Re: custom resource factory
Date Thu, 05 Nov 2009 08:38:50 GMT

This is what I am currently doing - you will have to extract what you 
need from the example. If the OpenEJB instance is not already running 
then I seed the Properties '_env' for use in the InitialContext, or 
provide 'null' to configure a Resource for the running embedded instance 
(see 'SystemInstance'). If you need to do this in a stand alone instance 
then just create a deploy bean to perform this action.

I call the method twice - for managed and unmanaged (and keep track of 
what is already configured).

addDataSourceConfig(_env, true, f, deployment);
addDataSourceConfig(_env, false, f, deployment);

      * Creates a DataSource resource for OpenEJB.
      * @param env     If not null the properties are seeded with 
parameters to create the DataSource .
      * @param managed Is this a JTA managed or unmanaged DataSource.
      * @param f       Root File location for the database - Is specific 
for file based databases such as Derby, etc.
      * @param name    DataSource name - Will prepend to either 
[name]Managed or [name]Unmanaged.
      * @throws StateException If anything goes wrong.
     private static void addDataSourceConfig(Properties env, final 
boolean managed, final File f, final String name) throws StateException {

         final String m = (managed ? "Managed" : "Unmanaged");

         if (_dataSources.contains(String.format("%1$s%2$s", name, m))) {
             //Already configured for OpenEJB

         final Resource resource = (null == env ? new 
Resource(String.format("%1$s%2$s", name, m), "DataSource") : null);
         final String dsname;

         if (null != resource) {
             env = resource.getProperties();
             dsname = "";
         } else {
             env.put(String.format("%1$s%2$s", name, m), 
             dsname = String.format("%1$s%2$s.", name, m);

         env.put(String.format("%1$sJtaManaged", dsname), (managed ? 
"true" : "false"));
         env.put(String.format("%1$sUserName", dsname), "SA");

         if (isHSQL()) {
             env.put(String.format("%1$sJdbcDriver", dsname), 
             env.put(String.format("%1$sJdbcUrl", dsname), 
String.format("jdbc:hsqldb:file:%1$s/%2$s", f.getPath().replace("\\", 
"/"), m.toLowerCase()));
         } else {
             env.put(String.format("%1$sJdbcDriver", dsname), 
             env.put(String.format("%1$sJdbcUrl", dsname), 
f.getPath().replace("\\", "/"), m.toLowerCase()));

         if (null != resource) {

             try {
ConfigurationFactory().configureService(resource, ResourceInfo.class));
             } catch (OpenEJBException e) {
                 log.error("error:", e);
                 throw new StateException(String.format("Failed to add 
DataSource %1$s%2$s", name, m), e);

         _dataSources.add(String.format("%1$s%2$s", name, m));

On 05.11.2009 07:45, rtselvan wrote:
> I am trying to implement a multi-tenant application using openEJB container,
> I need to be able to create the datasource dynamically for the JPA
> (hibernate JPA). so I wrote a custom jndi object factory but it never get
> called.
> Is the custom resource factory supported in openEJB? or any hidden
> documentation available out there for it?
> Thanks
> /selvan

View raw message