diff src/main/java/de/intevation/lada/util/data/EntityManagerProducer.java @ 438:f0ad10e0e1b1

Moved Repositories, etc. into data package.
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 06 Feb 2015 17:55:54 +0100
parents
children 3ec358698b4d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/util/data/EntityManagerProducer.java	Fri Feb 06 17:55:54 2015 +0100
@@ -0,0 +1,64 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out
+ * the documentation coming with IMIS-Labordaten-Application for details.
+ */
+package de.intevation.lada.util.data;
+
+import javax.annotation.Resource;
+import javax.ejb.LocalBean;
+import javax.ejb.SessionContext;
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.validation.UnexpectedTypeException;
+
+
+@Stateless
+@LocalBean
+public class EntityManagerProducer {
+
+    @Resource
+    private SessionContext ctx;
+
+    private String jndiPath = "java:app/entitymanager/";
+
+    /**
+     * Constructor for multi-tenancy entity manager delegate.
+     * Default jndi search path is 'java:app/entitymanager'.
+     */
+    public EntityManagerProducer() {
+    }
+
+    /**
+     * Constructor for multi-tenancy entity manager delegate.
+     *
+     * @param jndiEnv The jndi path to search for datasources.
+     *     Defaults to 'java:app/entitymanager'.
+     */
+    public EntityManagerProducer(String jndiPath) {
+        this.jndiPath = jndiPath;
+    }
+
+    /**
+     * Create an entity manager for a datasource.
+     *
+     * @throws UnexpectedTypeException
+     * @param dataSourceName The jndi name of the datasource.
+     * @return The entity manager for the datasource.
+     */
+    public EntityManager entityManager(String dataSourceName) {
+
+        EntityManager entityManager = 
+            (EntityManager) this.ctx.lookup(this.jndiPath + dataSourceName);
+
+        if (entityManager == null) {
+            throw new UnexpectedTypeException("Unknown data source name '" +
+                dataSourceName + "'.");
+        }
+
+        return entityManager;
+
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)