From 88172d1fb06fe9c9c765254885fa632ccded921a Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 25 Apr 2011 21:29:32 -0500 Subject: [PATCH] HHH-6117 - Figure out best way to handle SessionFactoryObjectFactory dealing with JNDI --- .../hibernate/internal/CoreMessageLogger.java | 7 +-- .../org/hibernate/ejb/util/NamingHelper.java | 48 ++++++++++--------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 45e0c8dc84..f7886782c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -23,7 +23,6 @@ */ package org.hibernate.internal; -import javax.naming.InvalidNameException; import javax.naming.NameNotFoundException; import javax.naming.NamingException; import javax.transaction.Synchronization; @@ -439,10 +438,6 @@ public interface CoreMessageLogger extends BasicLogger { @Message( value = "Could not bind JNDI listener", id = 127 ) void couldNotBindJndiListener(); - @LogMessage( level = WARN ) - @Message( value = "InitialContext did not implement EventContext", id = 128 ) - void initialContextDoesNotImplementEventContext(); - @LogMessage( level = INFO ) @Message( value = "Instantiating explicit connection provider: %s", id = 130 ) void instantiatingExplicitConnectionProvider(String providerClassName); @@ -900,7 +895,7 @@ public interface CoreMessageLogger extends BasicLogger { @LogMessage( level = WARN ) @Message( value = "Could not bind Ejb3Configuration to JNDI", id = 276 ) - void unableToBindEjb3ConfigurationToJndi( @Cause NamingException e ); + void unableToBindEjb3ConfigurationToJndi( @Cause JndiException e ); @LogMessage( level = WARN ) @Message( value = "Could not bind factory to JNDI", id = 277 ) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/util/NamingHelper.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/util/NamingHelper.java index c54bd07227..516d62ed5f 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/util/NamingHelper.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/util/NamingHelper.java @@ -22,19 +22,19 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.ejb.util; -import javax.naming.Context; -import javax.naming.InvalidNameException; -import javax.naming.NamingException; -import javax.naming.event.EventContext; + import javax.naming.event.NamespaceChangeListener; import javax.naming.event.NamingEvent; import javax.naming.event.NamingExceptionEvent; -import javax.naming.event.NamingListener; + +import org.jboss.logging.Logger; + import org.hibernate.ejb.AvailableSettings; import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.ejb.internal.EntityManagerMessageLogger; -import org.hibernate.internal.util.jndi.JndiHelper; -import org.jboss.logging.Logger; +import org.hibernate.service.jndi.JndiException; +import org.hibernate.service.jndi.JndiNameException; +import org.hibernate.service.jndi.internal.JndiServiceImpl; /** * @author Emmanuel Bernard @@ -44,32 +44,36 @@ public class NamingHelper { private static final EntityManagerMessageLogger LOG = Logger.getMessageLogger(EntityManagerMessageLogger.class, NamingHelper.class.getName()); - /** bind the configuration to the JNDI */ public static void bind(Ejb3Configuration cfg) { String name = cfg.getHibernateConfiguration().getProperty( AvailableSettings.CONFIGURATION_JNDI_NAME ); - if (name == null) LOG.debugf("No JNDI name configured for binding Ejb3Configuration"); + if ( name == null ) { + LOG.debug( "No JNDI name configured for binding Ejb3Configuration" ); + } else { - LOG.ejb3ConfigurationName(name); + LOG.ejb3ConfigurationName( name ); + // todo : instantiating the JndiService here is temporary until HHH-6159 is resolved. + JndiServiceImpl jndiService = new JndiServiceImpl( cfg.getProperties() ); try { - Context ctx = JndiHelper.getInitialContext( cfg.getProperties() ); - JndiHelper.bind( ctx, name, cfg ); - LOG.boundEjb3ConfigurationToJndiName(name); - ( (EventContext) ctx ).addNamingListener( name, EventContext.OBJECT_SCOPE, LISTENER ); + jndiService.bind( name, cfg ); + LOG.boundEjb3ConfigurationToJndiName( name ); + try { + jndiService.addListener( name, LISTENER ); + } + catch (Exception e) { + LOG.couldNotBindJndiListener(); + } } - catch (InvalidNameException ine) { - LOG.invalidJndiName(name, ine); + catch (JndiNameException e) { + LOG.invalidJndiName( name, e ); } - catch (NamingException ne) { - LOG.unableToBindEjb3ConfigurationToJndi(ne); - } - catch (ClassCastException cce) { - LOG.initialContextDoesNotImplementEventContext(); + catch (JndiException e) { + LOG.unableToBindEjb3ConfigurationToJndi( e ); } } } - private static final NamingListener LISTENER = new NamespaceChangeListener() { + private static final NamespaceChangeListener LISTENER = new NamespaceChangeListener() { public void objectAdded(NamingEvent evt) { LOG.debugf("An Ejb3Configuration was successfully bound to name: %s", evt.getNewBinding().getName()); }