mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-27 05:45:26 +00:00
HHH-5632 - Import initial services work
This commit is contained in:
parent
1a522bb968
commit
03c004bd13
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
import org.hibernate.cache.CacheException;
|
import org.hibernate.cache.CacheException;
|
||||||
import org.hibernate.cache.RegionFactory;
|
import org.hibernate.cache.RegionFactory;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||||
import org.infinispan.manager.EmbeddedCacheManager;
|
import org.infinispan.manager.EmbeddedCacheManager;
|
||||||
import org.infinispan.util.logging.Log;
|
import org.infinispan.util.logging.Log;
|
||||||
@ -65,7 +65,7 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws
|
|||||||
String name = ConfigurationHelper.getString(CACHE_MANAGER_RESOURCE_PROP, properties, null);
|
String name = ConfigurationHelper.getString(CACHE_MANAGER_RESOURCE_PROP, properties, null);
|
||||||
if (name == null)
|
if (name == null)
|
||||||
throw new CacheException("Configuration property " + CACHE_MANAGER_RESOURCE_PROP + " not set");
|
throw new CacheException("Configuration property " + CACHE_MANAGER_RESOURCE_PROP + " not set");
|
||||||
return locateCacheManager(name, NamingHelper.getJndiProperties(properties));
|
return locateCacheManager(name, JndiHelper.extractJndiProperties(properties));
|
||||||
}
|
}
|
||||||
|
|
||||||
private EmbeddedCacheManager locateCacheManager(String jndiNamespace, Properties jndiProperties) {
|
private EmbeddedCacheManager locateCacheManager(String jndiNamespace, Properties jndiProperties) {
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
import org.hibernate.cache.CacheException;
|
import org.hibernate.cache.CacheException;
|
||||||
import org.hibernate.cfg.Settings;
|
import org.hibernate.cfg.Settings;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||||
import org.jboss.cache.CacheManager;
|
import org.jboss.cache.CacheManager;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -69,7 +69,7 @@ public void start(Settings settings, Properties properties) throws CacheExceptio
|
|||||||
if (name == null)
|
if (name == null)
|
||||||
throw new CacheException("Configuration property " + CACHE_FACTORY_RESOURCE_PROP + " not set");
|
throw new CacheException("Configuration property " + CACHE_FACTORY_RESOURCE_PROP + " not set");
|
||||||
|
|
||||||
CacheManager cf = locateCacheFactory( name, NamingHelper.getJndiProperties( properties ) );
|
CacheManager cf = locateCacheFactory( name, JndiHelper.extractJndiProperties( properties ) );
|
||||||
setCacheFactory( cf );
|
setCacheFactory( cf );
|
||||||
|
|
||||||
super.start(settings, properties);
|
super.start(settings, properties);
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
import org.hibernate.cache.CacheException;
|
import org.hibernate.cache.CacheException;
|
||||||
import org.hibernate.cfg.Settings;
|
import org.hibernate.cfg.Settings;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.internal.util.config.ConfigurationHelper;
|
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||||
import org.jboss.cache.Cache;
|
import org.jboss.cache.Cache;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -74,7 +74,7 @@ protected Cache createSharedCache(Settings settings, Properties properties) {
|
|||||||
if (name == null)
|
if (name == null)
|
||||||
throw new CacheException("Configuration property " + CACHE_RESOURCE_PROP + " not set");
|
throw new CacheException("Configuration property " + CACHE_RESOURCE_PROP + " not set");
|
||||||
|
|
||||||
return locateCache( name, NamingHelper.getJndiProperties( properties ) );
|
return locateCache( name, JndiHelper.extractJndiProperties( properties ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.util.StringHelper;
|
import org.hibernate.util.StringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,7 +66,7 @@ public final void start(Properties properties) throws CacheException {
|
|||||||
if ( StringHelper.isEmpty( jndiNamespace ) ) {
|
if ( StringHelper.isEmpty( jndiNamespace ) ) {
|
||||||
throw new CacheException( "No JNDI namespace specified for cache" );
|
throw new CacheException( "No JNDI namespace specified for cache" );
|
||||||
}
|
}
|
||||||
cache = locateCache( jndiNamespace, NamingHelper.getJndiProperties( properties ) );
|
cache = locateCache( jndiNamespace, JndiHelper.extractJndiProperties( properties ) );
|
||||||
prepare( properties );
|
prepare( properties );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -72,7 +72,7 @@ public void configure(Properties props) throws HibernateException {
|
|||||||
pass = props.getProperty( Environment.PASS );
|
pass = props.getProperty( Environment.PASS );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ds = ( DataSource ) NamingHelper.getInitialContext( props ).lookup( jndiName );
|
ds = ( DataSource ) JndiHelper.getInitialContext( props ).lookup( jndiName );
|
||||||
}
|
}
|
||||||
catch ( Exception e ) {
|
catch ( Exception e ) {
|
||||||
log.error( "Could not find datasource: " + jndiName, e );
|
log.error( "Could not find datasource: " + jndiName, e );
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves {@link SessionFactory} instances during <tt>JNDI<tt> look-ups as well as during deserialization
|
* Resolves {@link SessionFactory} instances during <tt>JNDI<tt> look-ups as well as during deserialization
|
||||||
@ -109,8 +109,8 @@ public static void addInstance(String uid, String name, SessionFactory instance,
|
|||||||
log.info("Factory name: " + name);
|
log.info("Factory name: " + name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Context ctx = NamingHelper.getInitialContext(properties);
|
Context ctx = JndiHelper.getInitialContext(properties);
|
||||||
NamingHelper.bind(ctx, name, instance);
|
JndiHelper.bind(ctx, name, instance);
|
||||||
log.info("Bound factory to JNDI name: " + name);
|
log.info("Bound factory to JNDI name: " + name);
|
||||||
( (EventContext) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER);
|
( (EventContext) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER);
|
||||||
}
|
}
|
||||||
@ -135,7 +135,7 @@ public static void removeInstance(String uid, String name, Properties properties
|
|||||||
log.info("Unbinding factory from JNDI name: " + name);
|
log.info("Unbinding factory from JNDI name: " + name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Context ctx = NamingHelper.getInitialContext(properties);
|
Context ctx = JndiHelper.getInitialContext(properties);
|
||||||
ctx.unbind(name);
|
ctx.unbind(name);
|
||||||
log.info("Unbound factory from JNDI name: " + name);
|
log.info("Unbound factory from JNDI name: " + name);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.internal.util.jndi;
|
||||||
|
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception indicating trouble accessing JNDI
|
||||||
|
*
|
||||||
|
* @author Steve Ebersole
|
||||||
|
*/
|
||||||
|
public class JndiException extends HibernateException {
|
||||||
|
public JndiException(String string, Throwable root) {
|
||||||
|
super( string, root );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,242 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.internal.util.jndi;
|
||||||
|
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.naming.Context;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.Name;
|
||||||
|
import javax.naming.NameNotFoundException;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
public final class JndiHelper {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger( JndiHelper.class );
|
||||||
|
|
||||||
|
private JndiHelper() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a hodge-podge of properties, extract out the ones relevant for JNDI interaction.
|
||||||
|
*
|
||||||
|
* @param properties
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked" })
|
||||||
|
public static Properties extractJndiProperties(Map configurationValues) {
|
||||||
|
final Properties jndiProperties = new Properties();
|
||||||
|
|
||||||
|
for ( Map.Entry entry : (Set<Map.Entry>) configurationValues.entrySet() ) {
|
||||||
|
if ( !String.class.isInstance( entry.getKey() ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final String propertyName = (String) entry.getKey();
|
||||||
|
final Object propertyValue = entry.getValue();
|
||||||
|
if ( propertyName.startsWith( Environment.JNDI_PREFIX ) ) {
|
||||||
|
// write the IntialContextFactory class and provider url to the result only if they are
|
||||||
|
// non-null; this allows the environmental defaults (if any) to remain in effect
|
||||||
|
if ( Environment.JNDI_CLASS.equals( propertyName ) ) {
|
||||||
|
if ( propertyValue != null ) {
|
||||||
|
jndiProperties.put( Context.INITIAL_CONTEXT_FACTORY, propertyValue );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ( Environment.JNDI_URL.equals( propertyName ) ) {
|
||||||
|
if ( propertyValue != null ) {
|
||||||
|
jndiProperties.put( Context.PROVIDER_URL, propertyValue );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
final String passThruPropertyname = propertyName.substring( Environment.JNDI_PREFIX.length() + 1 );
|
||||||
|
jndiProperties.put( passThruPropertyname, propertyValue );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return jndiProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do a JNDI lookup. Mainly we are handling {@link NamingException}
|
||||||
|
*
|
||||||
|
* @param jndiName The namespace of the object to locate
|
||||||
|
* @param context The context in which to resolve the namespace.
|
||||||
|
*
|
||||||
|
* @return The located object; may be null.
|
||||||
|
*
|
||||||
|
* @throws JndiException if a {@link NamingException} occurs
|
||||||
|
*/
|
||||||
|
public static Object locate(String jndiName, Context context) {
|
||||||
|
try {
|
||||||
|
return context.lookup( jndiName );
|
||||||
|
}
|
||||||
|
catch ( NamingException e ) {
|
||||||
|
throw new JndiException( "Unable to lookup JNDI name [" + jndiName + "]", e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind val to name in ctx, and make sure that all intermediate contexts exist.
|
||||||
|
*
|
||||||
|
* @param ctx the root context
|
||||||
|
* @param name the name as a string
|
||||||
|
* @param val the object to be bound
|
||||||
|
*
|
||||||
|
* @throws JndiException if a {@link NamingException} occurs
|
||||||
|
*/
|
||||||
|
public static void bind(String jndiName, Object value, Context context) {
|
||||||
|
try {
|
||||||
|
log.trace( "binding : " + jndiName );
|
||||||
|
context.rebind( jndiName, value );
|
||||||
|
}
|
||||||
|
catch ( Exception initialException ) {
|
||||||
|
// We had problems doing a simple bind operation. This could very well be caused by missing intermediate
|
||||||
|
// contexts, so we attempt to create those intermmediate contexts and bind again
|
||||||
|
Name n = tokenizeName( jndiName, context );
|
||||||
|
Context intermediateContextBase = context;
|
||||||
|
while ( n.size() > 1 ) {
|
||||||
|
final String intermediateContextName = n.get( 0 );
|
||||||
|
|
||||||
|
Context intermediateContext = null;
|
||||||
|
try {
|
||||||
|
log.trace( "intermediate lookup: " + intermediateContextName );
|
||||||
|
intermediateContext = (Context) intermediateContextBase.lookup( intermediateContextName );
|
||||||
|
}
|
||||||
|
catch ( NameNotFoundException handledBelow ) {
|
||||||
|
// ok as we will create it below if not found
|
||||||
|
}
|
||||||
|
catch ( NamingException e ) {
|
||||||
|
throw new JndiException( "Unaniticipated error doing intermediate lookup", e );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( intermediateContext != null ) {
|
||||||
|
log.trace( "Found interediate context: " + intermediateContextName );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
log.trace( "Creating subcontext: " + intermediateContextName );
|
||||||
|
try {
|
||||||
|
intermediateContext = intermediateContextBase.createSubcontext( intermediateContextName );
|
||||||
|
}
|
||||||
|
catch ( NamingException e ) {
|
||||||
|
throw new JndiException( "Error creating intermediate context [" + intermediateContextName + "]", e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
intermediateContextBase = intermediateContext;
|
||||||
|
n = n.getSuffix( 1 );
|
||||||
|
}
|
||||||
|
log.trace( "binding: " + n );
|
||||||
|
try {
|
||||||
|
intermediateContextBase.rebind( n, value );
|
||||||
|
}
|
||||||
|
catch ( NamingException e ) {
|
||||||
|
throw new JndiException( "Error performing intermediate bind [" + n + "]", e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.debug( "Bound name: " + jndiName );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Name tokenizeName(String jndiName, Context context) {
|
||||||
|
try {
|
||||||
|
return context.getNameParser( "" ).parse( jndiName );
|
||||||
|
}
|
||||||
|
catch ( NamingException e ) {
|
||||||
|
throw new JndiException( "Unable to tokenize JNDI name [" + jndiName + "]", e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// todo : remove these once we get the services in place and integrated into the SessionFactory
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static InitialContext getInitialContext(Properties props) throws NamingException {
|
||||||
|
|
||||||
|
Hashtable hash = extractJndiProperties(props);
|
||||||
|
log.info("JNDI InitialContext properties:" + hash);
|
||||||
|
try {
|
||||||
|
return hash.size()==0 ?
|
||||||
|
new InitialContext() :
|
||||||
|
new InitialContext(hash);
|
||||||
|
}
|
||||||
|
catch (NamingException e) {
|
||||||
|
log.error("Could not obtain initial context", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind val to name in ctx, and make sure that all intermediate contexts exist.
|
||||||
|
*
|
||||||
|
* @param ctx the root context
|
||||||
|
* @param name the name as a string
|
||||||
|
* @param val the object to be bound
|
||||||
|
* @throws NamingException
|
||||||
|
*/
|
||||||
|
public static void bind(Context ctx, String name, Object val) throws NamingException {
|
||||||
|
try {
|
||||||
|
log.trace("binding: " + name);
|
||||||
|
ctx.rebind(name, val);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Name n = ctx.getNameParser("").parse(name);
|
||||||
|
while ( n.size() > 1 ) {
|
||||||
|
String ctxName = n.get(0);
|
||||||
|
|
||||||
|
Context subctx=null;
|
||||||
|
try {
|
||||||
|
log.trace("lookup: " + ctxName);
|
||||||
|
subctx = (Context) ctx.lookup(ctxName);
|
||||||
|
}
|
||||||
|
catch (NameNotFoundException nfe) {}
|
||||||
|
|
||||||
|
if (subctx!=null) {
|
||||||
|
log.debug("Found subcontext: " + ctxName);
|
||||||
|
ctx = subctx;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
log.info("Creating subcontext: " + ctxName);
|
||||||
|
ctx = ctx.createSubcontext(ctxName);
|
||||||
|
}
|
||||||
|
n = n.getSuffix(1);
|
||||||
|
}
|
||||||
|
log.trace("binding: " + n);
|
||||||
|
ctx.rebind(n, val);
|
||||||
|
}
|
||||||
|
log.debug("Bound name: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,7 @@
|
|||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.util.ExternalSessionFactoryConfig;
|
import org.hibernate.util.ExternalSessionFactoryConfig;
|
||||||
|
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ public void start() throws HibernateException {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
log.info("stopping service");
|
log.info("stopping service");
|
||||||
try {
|
try {
|
||||||
InitialContext context = NamingHelper.getInitialContext( buildProperties() );
|
InitialContext context = JndiHelper.getInitialContext( buildProperties() );
|
||||||
( (SessionFactory) context.lookup(boundName) ).close();
|
( (SessionFactory) context.lookup(boundName) ).close();
|
||||||
//context.unbind(boundName);
|
//context.unbind(boundName);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
import javax.transaction.Transaction;
|
import javax.transaction.Transaction;
|
||||||
|
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template implementation of {@link TransactionManagerLookup} where the
|
* Template implementation of {@link TransactionManagerLookup} where the
|
||||||
@ -54,7 +54,7 @@ public abstract class JNDITransactionManagerLookup implements TransactionManager
|
|||||||
*/
|
*/
|
||||||
public TransactionManager getTransactionManager(Properties props) throws HibernateException {
|
public TransactionManager getTransactionManager(Properties props) throws HibernateException {
|
||||||
try {
|
try {
|
||||||
return (TransactionManager) NamingHelper.getInitialContext(props).lookup( getName() );
|
return (TransactionManager) JndiHelper.getInitialContext(props).lookup( getName() );
|
||||||
}
|
}
|
||||||
catch (NamingException ne) {
|
catch (NamingException ne) {
|
||||||
throw new HibernateException( "Could not locate TransactionManager", ne );
|
throw new HibernateException( "Could not locate TransactionManager", ne );
|
||||||
|
@ -38,9 +38,9 @@
|
|||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
import org.hibernate.TransactionException;
|
import org.hibernate.TransactionException;
|
||||||
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
import org.hibernate.jdbc.JDBCContext;
|
import org.hibernate.jdbc.JDBCContext;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.util.NamingHelper;
|
|
||||||
import org.hibernate.util.JTAHelper;
|
import org.hibernate.util.JTAHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,7 +109,7 @@ public void configure(Properties props) throws HibernateException {
|
|||||||
*/
|
*/
|
||||||
protected final InitialContext resolveInitialContext(Properties properties) {
|
protected final InitialContext resolveInitialContext(Properties properties) {
|
||||||
try {
|
try {
|
||||||
return NamingHelper.getInitialContext( properties );
|
return JndiHelper.getInitialContext( properties );
|
||||||
}
|
}
|
||||||
catch ( NamingException ne ) {
|
catch ( NamingException ne ) {
|
||||||
throw new HibernateException( "Could not obtain initial context", ne );
|
throw new HibernateException( "Could not obtain initial context", ne );
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
import javax.transaction.xa.XAResource;
|
import javax.transaction.xa.XAResource;
|
||||||
|
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.util.NamingHelper;
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TransactionManagerLookup implementation intended for use with WebSphere
|
* TransactionManagerLookup implementation intended for use with WebSphere
|
||||||
@ -180,7 +180,7 @@ public class TransactionAdapter implements Transaction {
|
|||||||
private TransactionAdapter(Properties props) {
|
private TransactionAdapter(Properties props) {
|
||||||
try {
|
try {
|
||||||
if ( extendedJTATransaction == null ) {
|
if ( extendedJTATransaction == null ) {
|
||||||
extendedJTATransaction = NamingHelper.getInitialContext( props )
|
extendedJTATransaction = JndiHelper.getInitialContext( props )
|
||||||
.lookup( "java:comp/websphere/ExtendedJTATransaction" );
|
.lookup( "java:comp/websphere/ExtendedJTATransaction" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,144 +0,0 @@
|
|||||||
/*
|
|
||||||
* Hibernate, Relational Persistence for Idiomatic Java
|
|
||||||
*
|
|
||||||
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
|
|
||||||
* indicated by the @author tags or express copyright attribution
|
|
||||||
* statements applied by the authors. All third-party contributions are
|
|
||||||
* distributed under license by Red Hat Middleware LLC.
|
|
||||||
*
|
|
||||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
|
||||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
|
||||||
* Lesser General Public License, as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
||||||
* for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
|
||||||
* along with this distribution; if not, write to:
|
|
||||||
* Free Software Foundation, Inc.
|
|
||||||
* 51 Franklin Street, Fifth Floor
|
|
||||||
* Boston, MA 02110-1301 USA
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.hibernate.util;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Hashtable;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import javax.naming.Context;
|
|
||||||
import javax.naming.InitialContext;
|
|
||||||
import javax.naming.Name;
|
|
||||||
import javax.naming.NameNotFoundException;
|
|
||||||
import javax.naming.NamingException;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
|
|
||||||
public final class NamingHelper {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(NamingHelper.class);
|
|
||||||
|
|
||||||
public static InitialContext getInitialContext(Properties props) throws NamingException {
|
|
||||||
|
|
||||||
Hashtable hash = getJndiProperties(props);
|
|
||||||
log.info("JNDI InitialContext properties:" + hash);
|
|
||||||
try {
|
|
||||||
return hash.size()==0 ?
|
|
||||||
new InitialContext() :
|
|
||||||
new InitialContext(hash);
|
|
||||||
}
|
|
||||||
catch (NamingException e) {
|
|
||||||
log.error("Could not obtain initial context", e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bind val to name in ctx, and make sure that all intermediate contexts exist.
|
|
||||||
*
|
|
||||||
* @param ctx the root context
|
|
||||||
* @param name the name as a string
|
|
||||||
* @param val the object to be bound
|
|
||||||
* @throws NamingException
|
|
||||||
*/
|
|
||||||
public static void bind(Context ctx, String name, Object val) throws NamingException {
|
|
||||||
try {
|
|
||||||
log.trace("binding: " + name);
|
|
||||||
ctx.rebind(name, val);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
Name n = ctx.getNameParser("").parse(name);
|
|
||||||
while ( n.size() > 1 ) {
|
|
||||||
String ctxName = n.get(0);
|
|
||||||
|
|
||||||
Context subctx=null;
|
|
||||||
try {
|
|
||||||
log.trace("lookup: " + ctxName);
|
|
||||||
subctx = (Context) ctx.lookup(ctxName);
|
|
||||||
}
|
|
||||||
catch (NameNotFoundException nfe) {}
|
|
||||||
|
|
||||||
if (subctx!=null) {
|
|
||||||
log.debug("Found subcontext: " + ctxName);
|
|
||||||
ctx = subctx;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
log.info("Creating subcontext: " + ctxName);
|
|
||||||
ctx = ctx.createSubcontext(ctxName);
|
|
||||||
}
|
|
||||||
n = n.getSuffix(1);
|
|
||||||
}
|
|
||||||
log.trace("binding: " + n);
|
|
||||||
ctx.rebind(n, val);
|
|
||||||
}
|
|
||||||
log.debug("Bound name: " + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transform JNDI properties passed in the form <tt>hibernate.jndi.*</tt> to the
|
|
||||||
* format accepted by <tt>InitialContext</tt> by triming the leading "<tt>hibernate.jndi</tt>".
|
|
||||||
*/
|
|
||||||
public static Properties getJndiProperties(Properties properties) {
|
|
||||||
|
|
||||||
HashSet specialProps = new HashSet();
|
|
||||||
specialProps.add(Environment.JNDI_CLASS);
|
|
||||||
specialProps.add(Environment.JNDI_URL);
|
|
||||||
|
|
||||||
Iterator iter = properties.keySet().iterator();
|
|
||||||
Properties result = new Properties();
|
|
||||||
while ( iter.hasNext() ) {
|
|
||||||
String prop = (String) iter.next();
|
|
||||||
if ( prop.indexOf(Environment.JNDI_PREFIX) > -1 && !specialProps.contains(prop) ) {
|
|
||||||
result.setProperty(
|
|
||||||
prop.substring( Environment.JNDI_PREFIX.length()+1 ),
|
|
||||||
properties.getProperty(prop)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String jndiClass = properties.getProperty(Environment.JNDI_CLASS);
|
|
||||||
String jndiURL = properties.getProperty(Environment.JNDI_URL);
|
|
||||||
// we want to be able to just use the defaults,
|
|
||||||
// if JNDI environment properties are not supplied
|
|
||||||
// so don't put null in anywhere
|
|
||||||
if (jndiClass != null) result.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass);
|
|
||||||
if (jndiURL != null) result.put(Context.PROVIDER_URL, jndiURL);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private NamingHelper() {}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
import org.hibernate.ejb.AvailableSettings;
|
import org.hibernate.ejb.AvailableSettings;
|
||||||
import org.hibernate.ejb.Ejb3Configuration;
|
import org.hibernate.ejb.Ejb3Configuration;
|
||||||
|
import org.hibernate.internal.util.jndi.JndiHelper;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -56,8 +57,8 @@ public static void bind(Ejb3Configuration cfg) {
|
|||||||
log.info( "Ejb3Configuration name: {}", name );
|
log.info( "Ejb3Configuration name: {}", name );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Context ctx = org.hibernate.util.NamingHelper.getInitialContext( cfg.getProperties() );
|
Context ctx = JndiHelper.getInitialContext( cfg.getProperties() );
|
||||||
org.hibernate.util.NamingHelper.bind( ctx, name, cfg );
|
JndiHelper.bind( ctx, name, cfg );
|
||||||
log.info( "Bound Ejb3Configuration to JNDI name: {}", name );
|
log.info( "Bound Ejb3Configuration to JNDI name: {}", name );
|
||||||
( (EventContext) ctx ).addNamingListener( name, EventContext.OBJECT_SCOPE, LISTENER );
|
( (EventContext) ctx ).addNamingListener( name, EventContext.OBJECT_SCOPE, LISTENER );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user