Skip to content

Commit

Permalink
OPENJPA-2922 disable JNDI lookup of EMF by default
Browse files Browse the repository at this point in the history
To use this feature please set the following
system.property to true: emf_via_jndi_enabled
  • Loading branch information
struberg committed May 27, 2024
1 parent 823ffc5 commit b3d1ef8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit b3d1ef8

Please sign in to comment.