From b3d1ef8671751d47617eaf4ac259b43eae45e53b Mon Sep 17 00:00:00 2001 From: Mark Struberg Date: Mon, 27 May 2024 19:00:40 +0200 Subject: [PATCH] OPENJPA-2922 disable JNDI lookup of EMF by default To use this feature please set the following system.property to true: emf_via_jndi_enabled --- .../org/apache/openjpa/kernel/localizer.properties | 3 +++ .../apache/openjpa/persistence/OpenJPAPersistence.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties b/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties index 35271cca8c..f5069ead58 100644 --- a/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties +++ b/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties @@ -137,6 +137,9 @@ end-trans-error: An exception occurred while ending the transaction. This \ not-bound: The file named "{0}" could not be found. naming-exception: A NamingException was thrown while obtaining the \ factory at "{0}" from JNDI. +jndi-disabled-exception: Accessing the EntityManagerFactory via JNDI has been \ + disabled. Set the system property {0} to 'true' if you want to use this \ + feature. attach-deleted: The object "{0}" with id "{1}" has been deleted and \ cannot be attached. not-detachable: The class "{0}" does not declare the "detachable" metadata \ diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java index cc7059dcfb..3db4973899 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java @@ -34,6 +34,7 @@ import org.apache.openjpa.lib.conf.ConfigurationProvider; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.util.ImplHelper; +import org.apache.openjpa.util.UserException; /** * Static helper methods for JPA users. @@ -44,6 +45,11 @@ */ public class OpenJPAPersistence { + /** + * Set this System property to 'true' if you want to enable the EntityManager via JNDI + */ + private static final String EMF_VIA_JNDI_ENABLED = "emf_via_jndi_enabled"; + private static final Localizer _loc = Localizer.forPackage(OpenJPAPersistence.class); @@ -137,6 +143,10 @@ public static OpenJPAEntityManagerFactory getEntityManagerFactory(Map map) { */ public static OpenJPAEntityManagerFactory createEntityManagerFactory (String jndiLocation, Context context) { + if (!"true".equalsIgnoreCase(System.getProperty(EMF_VIA_JNDI_ENABLED))) { + throw new UserException(_loc.get("jndi-disabled-exception", EMF_VIA_JNDI_ENABLED)); + } + if (jndiLocation == null) throw new NullPointerException("jndiLocation == null");