From: <one...@us...> - 2003-01-01 03:03:23
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers In directory sc8-pr-cvs1:/tmp/cvs-serv10832/cirrus/hibernate/helpers Modified Files: NamingHelper.java Log Message: arbitrary JNDI InitialContext properties may now be passed as hibernate.jndi.* Index: NamingHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/NamingHelper.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** NamingHelper.java 16 Sep 2002 04:50:50 -0000 1.7 --- NamingHelper.java 1 Jan 2003 03:03:17 -0000 1.8 *************** *** 2,6 **** --- 2,8 ---- package cirrus.hibernate.helpers; + import java.util.HashSet; import java.util.Hashtable; + import java.util.Iterator; import java.util.Properties; *************** *** 21,40 **** public static InitialContext getInitialContext(Properties props) throws NamingException { ! String jndiClass = props.getProperty(Environment.JNDI_CLASS); ! String jndiURL = props.getProperty(Environment.JNDI_URL); ! ! // we want to be able to just use the defaults, ! // if JNDI environment properties are not supplied ! Hashtable hash = new Hashtable(); ! if (jndiClass != null) hash.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass); ! if (jndiURL != null) hash.put(Context.PROVIDER_URL, jndiURL); ! try { ! return ( jndiURL==null && jndiClass==null ) ? new InitialContext() : new InitialContext(hash); } catch (NamingException e) { ! log.error("Could not obtain initial context: " + jndiURL, e); throw e; } --- 23,35 ---- 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; } *************** *** 80,83 **** --- 75,111 ---- } 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; } |