You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Andy Gumbrecht <an...@orprovision.com> on 2009/10/14 10:37:32 UTC
Dynamic datasources
Hi all,
Just thought I should share this snippet for creating DataSource
resources either before or after an embedded OpenEJB has started.
Thanks go out to David Blevins for pointing me in the right direction.
The method can be fed a non-null environment (env) properties which will
be seeded before use in the InitialContext call.
If the 'env' parameter is null then it is assumed that an InitialContext
has been created for OpenEJB, in which case a live Resource is created.
Obviously this is quite specific to my requirements, but can be easily
modified.
private static final Log log = LogFactory.getLog(Session.class);
private static final Set<String> _dataSources = new HashSet<String>();
/**
* Creates a DataSource resource for OpenEJB.
* @param env If not null the properties are seeds with parameters
to create the DataSource .
* @param managed Is this a JTA managed or unmanaged DataSource.
* @param f 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 Exception If anything goes wrong.
*/
private static void addDataSourceConfig(Properties env, final boolean
managed, final File f, final String name) throws Exception {
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");
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));
} catch (OpenEJBException e) {
log.error("error:", e);
throw new Exception(String.format("Failed to add
DataSource %1$s%2$s", name, m), e);
}
}
_dataSources.add(String.format("%1$s%2$s", name, m));
}
Best regards,
Andy.
PS. Something I also do which might be useful to someone is to create
and load an application dynamically after OpenEJB has started.
//Dynamically load application
SystemInstance.get().getComponent(Assembler.class).createApplication(new ConfigurationFactory().configureApplication(
new File("my.ejb.jar")
));