I've been evaluating this option now for weeks and the short answer is: it is possible, however from my point of view not very convenient. I first introduced a new OSGi service called IDatabaseCredentialsService:
public
class DatabaseCredentialsService
implements IDatabaseCredentialsService {
public Map<String, String> getDBCredentioals() {
Map<String, String> credentials =
new HashMap<String, String>();
credentials.put("javax.persistence.jdbc.driver",
"org.postgresql.Driver");
credentials.put("javax.persistence.jdbc.url",
"jdbc:postgresql://localhost/testdb");
credentials.put("javax.persistence.jdbc.user",
"postgres");
credentials.put("javax.persistence.jdbc.password",
"mypasswd");
public
class CustomerServiceDAO {
private EntityManagerFactory
entityManagerFactory;
private EntityManagerFactoryBuilder
emfBuilder;
private IDatabaseCredentialsService
dbCredentials;
public Customer findCustomer() {
EntityManager entityManager =
entityManagerFactory
Customer c = entityManager.find(Customer.class, 123456);
private
void createEntityManagerFactory() {
assert
emfBuilder !=
null :
"emfBuilder != null";
assert
dbCredentials !=
null :
"dbCredentials != null";
Map<String, String> dbCredentioals =
dbCredentials.getDBCredentioals();
entityManagerFactory =
emfBuilder
.createEntityManagerFactory(dbCredentioals);
public
void bindDBConnectionService(IDatabaseCredentialsService dbService) {
dbCredentials = dbService;
if (emfBuilder !=
null) {
createEntityManagerFactory();
public
void bindEMFBuilder(EntityManagerFactoryBuilder builder) {
if (dbCredentials
!= null) {
createEntityManagerFactory();