You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by rtselvan <rt...@gmail.com> on 2009/11/05 07:45:28 UTC

custom resource factory

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 this message in context: http://old.nabble.com/custom-resource-factory-tp26208589p26208589.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: custom resource factory

Posted by Selvan Ramasamy <rt...@gmail.com>.
with your approach, say there are 100 clients and you will have 100 times
the same application deployed with different datasource..

so we are avoiding it to deploy the 100 instances by creating the data
source dynamically.. there is a lot of more to make your code multi-tenant
than just the data source.


On Fri, Nov 6, 2009 at 11:01 AM, Quintin Beukes <qu...@skywalk.co.za>wrote:

> clients added? I haven't worked with clouds before.
>
> Though I would think a data source goes paired with an app, goes paired
> with
> a database. Or at least it gets deployed in some way or another? Meaning
> it's just a "deploy an lookup"?
>
> I know I'm misunderstanding it though.
>
> Quintin Beukes
>
>
> On Fri, Nov 6, 2009 at 8:28 PM, Selvan Ramasamy <rt...@gmail.com>
> wrote:
>
> > Generally, you would do this if you are a service provider (cloud hosted)
> > with many clients on the same application instance and new clients are
> > added
> > time to time without having taking the server down.
> >
> > On Fri, Nov 6, 2009 at 10:01 AM, Quintin Beukes <quintin@skywalk.co.za
> > >wrote:
> >
> > > Hey,
> > >
> > > This is off topic. I'm just curious to learn new things. Why do you
> need
> > to
> > > create data sources dynamically?
> > >
> > > Quintin Beukes
> > >
> > >
> > > On Fri, Nov 6, 2009 at 5:20 PM, rtselvan <rt...@gmail.com> wrote:
> > >
> > > >
> > > > Thanks Andy.
> > > >
> > > > your solution would work great, I was thinking to create the data
> > sources
> > > > on
> > > > demand with JNDI interception but this should work as well. I will
> test
> > > it
> > > > out later today.
> > > >
> > > > As long as we use openEJB then, we should be good with this solution.
> > For
> > > > other app servers, we might have to look into different options.
> > > >
> > > > Thanks
> > > > /selvan
> > > >
> > > >
> > > >
> > > > Andy Gumbrecht wrote:
> > > > >
> > > > > Selvan,
> > > > >
> > > > > 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
> > > > >              return;
> > > > >          }
> > > > >
> > > > >          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),
> > > > > "new://Resource?type=DataSource");
> > > > >              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),
> > > > > "org.hsqldb.jdbc.JDBCDriver");
> > > > >              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),
> > > > > "org.apache.derby.jdbc.EmbeddedDriver");
> > > > >              env.put(String.format("%1$sJdbcUrl", dsname),
> > > > > String.format("jdbc:derby:%1$s/%2$s;create=true",
> > > > > f.getPath().replace("\\", "/"), m.toLowerCase()));
> > > > >          }
> > > > >
> > > > >          if (null != resource) {
> > > > >
> > > > >              try {
> > > > >
> > > > >
> SystemInstance.get().getComponent(Assembler.class).createResource(new
> > > > > ConfigurationFactory().configureService(resource,
> > ResourceInfo.class));
> > > > >                  Thread.yield();
> > > > >              } 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 this message in context:
> > > >
> http://old.nabble.com/custom-resource-factory-tp26208589p26230797.html
> > > > Sent from the OpenEJB User mailing list archive at Nabble.com.
> > > >
> > > >
> > >
> >
>

Re: custom resource factory

Posted by Quintin Beukes <qu...@skywalk.co.za>.
clients added? I haven't worked with clouds before.

Though I would think a data source goes paired with an app, goes paired with
a database. Or at least it gets deployed in some way or another? Meaning
it's just a "deploy an lookup"?

I know I'm misunderstanding it though.

Quintin Beukes


On Fri, Nov 6, 2009 at 8:28 PM, Selvan Ramasamy <rt...@gmail.com> wrote:

> Generally, you would do this if you are a service provider (cloud hosted)
> with many clients on the same application instance and new clients are
> added
> time to time without having taking the server down.
>
> On Fri, Nov 6, 2009 at 10:01 AM, Quintin Beukes <quintin@skywalk.co.za
> >wrote:
>
> > Hey,
> >
> > This is off topic. I'm just curious to learn new things. Why do you need
> to
> > create data sources dynamically?
> >
> > Quintin Beukes
> >
> >
> > On Fri, Nov 6, 2009 at 5:20 PM, rtselvan <rt...@gmail.com> wrote:
> >
> > >
> > > Thanks Andy.
> > >
> > > your solution would work great, I was thinking to create the data
> sources
> > > on
> > > demand with JNDI interception but this should work as well. I will test
> > it
> > > out later today.
> > >
> > > As long as we use openEJB then, we should be good with this solution.
> For
> > > other app servers, we might have to look into different options.
> > >
> > > Thanks
> > > /selvan
> > >
> > >
> > >
> > > Andy Gumbrecht wrote:
> > > >
> > > > Selvan,
> > > >
> > > > 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
> > > >              return;
> > > >          }
> > > >
> > > >          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),
> > > > "new://Resource?type=DataSource");
> > > >              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),
> > > > "org.hsqldb.jdbc.JDBCDriver");
> > > >              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),
> > > > "org.apache.derby.jdbc.EmbeddedDriver");
> > > >              env.put(String.format("%1$sJdbcUrl", dsname),
> > > > String.format("jdbc:derby:%1$s/%2$s;create=true",
> > > > f.getPath().replace("\\", "/"), m.toLowerCase()));
> > > >          }
> > > >
> > > >          if (null != resource) {
> > > >
> > > >              try {
> > > >
> > > > SystemInstance.get().getComponent(Assembler.class).createResource(new
> > > > ConfigurationFactory().configureService(resource,
> ResourceInfo.class));
> > > >                  Thread.yield();
> > > >              } 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 this message in context:
> > > http://old.nabble.com/custom-resource-factory-tp26208589p26230797.html
> > > Sent from the OpenEJB User mailing list archive at Nabble.com.
> > >
> > >
> >
>

Re: custom resource factory

Posted by Selvan Ramasamy <rt...@gmail.com>.
Generally, you would do this if you are a service provider (cloud hosted)
with many clients on the same application instance and new clients are added
time to time without having taking the server down.

On Fri, Nov 6, 2009 at 10:01 AM, Quintin Beukes <qu...@skywalk.co.za>wrote:

> Hey,
>
> This is off topic. I'm just curious to learn new things. Why do you need to
> create data sources dynamically?
>
> Quintin Beukes
>
>
> On Fri, Nov 6, 2009 at 5:20 PM, rtselvan <rt...@gmail.com> wrote:
>
> >
> > Thanks Andy.
> >
> > your solution would work great, I was thinking to create the data sources
> > on
> > demand with JNDI interception but this should work as well. I will test
> it
> > out later today.
> >
> > As long as we use openEJB then, we should be good with this solution. For
> > other app servers, we might have to look into different options.
> >
> > Thanks
> > /selvan
> >
> >
> >
> > Andy Gumbrecht wrote:
> > >
> > > Selvan,
> > >
> > > 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
> > >              return;
> > >          }
> > >
> > >          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),
> > > "new://Resource?type=DataSource");
> > >              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),
> > > "org.hsqldb.jdbc.JDBCDriver");
> > >              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),
> > > "org.apache.derby.jdbc.EmbeddedDriver");
> > >              env.put(String.format("%1$sJdbcUrl", dsname),
> > > String.format("jdbc:derby:%1$s/%2$s;create=true",
> > > f.getPath().replace("\\", "/"), m.toLowerCase()));
> > >          }
> > >
> > >          if (null != resource) {
> > >
> > >              try {
> > >
> > > SystemInstance.get().getComponent(Assembler.class).createResource(new
> > > ConfigurationFactory().configureService(resource, ResourceInfo.class));
> > >                  Thread.yield();
> > >              } 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 this message in context:
> > http://old.nabble.com/custom-resource-factory-tp26208589p26230797.html
> > Sent from the OpenEJB User mailing list archive at Nabble.com.
> >
> >
>

Re: custom resource factory

Posted by Quintin Beukes <qu...@skywalk.co.za>.
Hey,

This is off topic. I'm just curious to learn new things. Why do you need to
create data sources dynamically?

Quintin Beukes


On Fri, Nov 6, 2009 at 5:20 PM, rtselvan <rt...@gmail.com> wrote:

>
> Thanks Andy.
>
> your solution would work great, I was thinking to create the data sources
> on
> demand with JNDI interception but this should work as well. I will test it
> out later today.
>
> As long as we use openEJB then, we should be good with this solution. For
> other app servers, we might have to look into different options.
>
> Thanks
> /selvan
>
>
>
> Andy Gumbrecht wrote:
> >
> > Selvan,
> >
> > 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
> >              return;
> >          }
> >
> >          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),
> > "new://Resource?type=DataSource");
> >              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),
> > "org.hsqldb.jdbc.JDBCDriver");
> >              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),
> > "org.apache.derby.jdbc.EmbeddedDriver");
> >              env.put(String.format("%1$sJdbcUrl", dsname),
> > String.format("jdbc:derby:%1$s/%2$s;create=true",
> > f.getPath().replace("\\", "/"), m.toLowerCase()));
> >          }
> >
> >          if (null != resource) {
> >
> >              try {
> >
> > SystemInstance.get().getComponent(Assembler.class).createResource(new
> > ConfigurationFactory().configureService(resource, ResourceInfo.class));
> >                  Thread.yield();
> >              } 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 this message in context:
> http://old.nabble.com/custom-resource-factory-tp26208589p26230797.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>

Re: custom resource factory

Posted by rtselvan <rt...@gmail.com>.
Thanks Andy.

your solution would work great, I was thinking to create the data sources on
demand with JNDI interception but this should work as well. I will test it
out later today. 

As long as we use openEJB then, we should be good with this solution. For
other app servers, we might have to look into different options.

Thanks
/selvan



Andy Gumbrecht wrote:
> 
> Selvan,
> 
> 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
>              return;
>          }
> 
>          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), 
> "new://Resource?type=DataSource");
>              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), 
> "org.hsqldb.jdbc.JDBCDriver");
>              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), 
> "org.apache.derby.jdbc.EmbeddedDriver");
>              env.put(String.format("%1$sJdbcUrl", dsname), 
> String.format("jdbc:derby:%1$s/%2$s;create=true", 
> f.getPath().replace("\\", "/"), m.toLowerCase()));
>          }
> 
>          if (null != resource) {
> 
>              try {
>                  
> SystemInstance.get().getComponent(Assembler.class).createResource(new 
> ConfigurationFactory().configureService(resource, ResourceInfo.class));
>                  Thread.yield();
>              } 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 this message in context: http://old.nabble.com/custom-resource-factory-tp26208589p26230797.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: custom resource factory

Posted by Andy Gumbrecht <an...@orprovision.com>.
Selvan,

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
             return;
         }

         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), 
"new://Resource?type=DataSource");
             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), 
"org.hsqldb.jdbc.JDBCDriver");
             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), 
"org.apache.derby.jdbc.EmbeddedDriver");
             env.put(String.format("%1$sJdbcUrl", dsname), 
String.format("jdbc:derby:%1$s/%2$s;create=true", 
f.getPath().replace("\\", "/"), m.toLowerCase()));
         }

         if (null != resource) {

             try {
                 
SystemInstance.get().getComponent(Assembler.class).createResource(new 
ConfigurationFactory().configureService(resource, ResourceInfo.class));
                 Thread.yield();
             } 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
>