From: Juergen H. <jho...@us...> - 2006-04-21 00:14:23
|
Update of /cvsroot/springframework/spring/src/org/springframework/jndi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19629/src/org/springframework/jndi Modified Files: Tag: mbranch-1-2 JndiObjectFactoryBean.java JndiTemplate.java Added Files: Tag: mbranch-1-2 TypeMismatchNamingException.java Log Message: backported fixes and enhancements from 2.0 M4 (HEAD) Index: JndiObjectFactoryBean.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jndi/JndiObjectFactoryBean.java,v retrieving revision 1.13 retrieving revision 1.13.2.1 diff -C2 -d -r1.13 -r1.13.2.1 *** JndiObjectFactoryBean.java 13 Oct 2005 21:12:35 -0000 1.13 --- JndiObjectFactoryBean.java 21 Apr 2006 00:13:49 -0000 1.13.2.1 *************** *** 1,11 **** /* ! * Copyright 2002-2005 the original author or authors. ! * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at ! * * http://www.apache.org/licenses/LICENSE-2.0 ! * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, --- 1,11 ---- /* ! * Copyright 2002-2006 the original author or authors. ! * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at ! * * http://www.apache.org/licenses/LICENSE-2.0 ! * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, *************** *** 64,67 **** --- 64,69 ---- private boolean cache = true; + private Object defaultObject; + private Object jndiObject; *************** *** 108,120 **** /** * Look up the JNDI object and store it. */ ! public void afterPropertiesSet() throws NamingException { super.afterPropertiesSet(); if (this.proxyInterface != null) { // We need a proxy and a JndiObjectTargetSource. this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this); } else { if (!this.lookupOnStartup || !this.cache) { --- 110,140 ---- /** + * Specify a default object to fall back to if the JNDI lookup fails. + * Default is none. + * <p>This can be an arbitrary bean reference or literal value. + * It is typically used for literal values in scenarios where the JNDI environment + * might define specific config settings but those are not required to be present. + * <p>Note: This is only supported for lookup on startup. + * @see #setLookupOnStartup + */ + public void setDefaultObject(Object defaultObject) { + this.defaultObject = defaultObject; + } + + + /** * Look up the JNDI object and store it. */ ! public void afterPropertiesSet() throws IllegalArgumentException, NamingException { super.afterPropertiesSet(); if (this.proxyInterface != null) { + if (this.defaultObject != null) { + throw new IllegalArgumentException("'defaultObject' is not supported in combination with proxyInterface"); + } // We need a proxy and a JndiObjectTargetSource. this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this); } + else { if (!this.lookupOnStartup || !this.cache) { *************** *** 122,127 **** "Cannot deactivate 'lookupOnStartup' or 'cache' without specifying a 'proxyInterface'"); } // Locate specified JNDI object. ! this.jndiObject = lookup(); } } --- 142,183 ---- "Cannot deactivate 'lookupOnStartup' or 'cache' without specifying a 'proxyInterface'"); } + if (this.defaultObject != null && getExpectedType() != null && + !getExpectedType().isInstance(this.defaultObject)) { + throw new IllegalArgumentException("Default object [" + this.defaultObject + + "] of type [" + this.defaultObject.getClass().getName() + + "] is not of expected type [" + getExpectedType().getName() + "]"); + } // Locate specified JNDI object. ! this.jndiObject = lookupWithFallback(); ! } ! } ! ! /** ! * Lookup variant that that returns the specified "defaultObject" ! * (if any) in case of lookup failure. ! * @return the located object, or the "defaultObject" as fallback ! * @throws NamingException in case of lookup failure without fallback ! * @see #setDefaultObject ! */ ! protected Object lookupWithFallback() throws NamingException { ! try { ! return lookup(); ! } ! catch (TypeMismatchNamingException ex) { ! // Always let TypeMismatchNamingException through - ! // we don't want to fall back to the defaultObject in this case. ! throw ex; ! } ! catch (NamingException ex) { ! if (this.defaultObject != null) { ! if (logger.isDebugEnabled()) { ! logger.debug("JNDI lookup failed - returning specified default object instead", ex); ! } ! else if (logger.isInfoEnabled()) { ! logger.debug("JNDI lookup failed - returning specified default object instead: " + ex); ! } ! return this.defaultObject; ! } ! throw ex; } } Index: JndiTemplate.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jndi/JndiTemplate.java,v retrieving revision 1.13 retrieving revision 1.13.2.1 diff -C2 -d -r1.13 -r1.13.2.1 *** JndiTemplate.java 13 Oct 2005 21:12:35 -0000 1.13 --- JndiTemplate.java 21 Apr 2006 00:13:49 -0000 1.13.2.1 *************** *** 1,11 **** /* ! * Copyright 2002-2005 the original author or authors. ! * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at ! * * http://www.apache.org/licenses/LICENSE-2.0 ! * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, --- 1,11 ---- /* ! * Copyright 2002-2006 the original author or authors. ! * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at ! * * http://www.apache.org/licenses/LICENSE-2.0 ! * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, *************** *** 21,24 **** --- 21,25 ---- import javax.naming.Context; import javax.naming.InitialContext; + import javax.naming.NameNotFoundException; import javax.naming.NamingException; *************** *** 116,120 **** */ public Object lookup(final String name) throws NamingException { ! if (logger.isInfoEnabled()) { logger.debug("Looking up JNDI object with name [" + name + "]"); } --- 117,121 ---- */ public Object lookup(final String name) throws NamingException { ! if (logger.isDebugEnabled()) { logger.debug("Looking up JNDI object with name [" + name + "]"); } *************** *** 123,127 **** Object located = ctx.lookup(name); if (located == null) { ! throw new NamingException( "JNDI object with [" + name + "] not found: JNDI implementation returned null"); } --- 124,128 ---- Object located = ctx.lookup(name); if (located == null) { ! throw new NameNotFoundException( "JNDI object with [" + name + "] not found: JNDI implementation returned null"); } *************** *** 146,150 **** Object jndiObject = lookup(name); if (requiredType != null && !requiredType.isInstance(jndiObject)) { ! throw new NamingException( "Object [" + jndiObject + "] available at JNDI location [" + name + "] is not assignable to [" + requiredType.getName() + "]"); --- 147,151 ---- Object jndiObject = lookup(name); if (requiredType != null && !requiredType.isInstance(jndiObject)) { ! throw new TypeMismatchNamingException( "Object [" + jndiObject + "] available at JNDI location [" + name + "] is not assignable to [" + requiredType.getName() + "]"); *************** *** 160,165 **** */ public void bind(final String name, final Object object) throws NamingException { ! if (logger.isInfoEnabled()) { ! logger.info("Binding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { --- 161,166 ---- */ public void bind(final String name, final Object object) throws NamingException { ! if (logger.isDebugEnabled()) { ! logger.debug("Binding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { *************** *** 179,184 **** */ public void rebind(final String name, final Object object) throws NamingException { ! if (logger.isInfoEnabled()) { ! logger.info("Rebinding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { --- 180,185 ---- */ public void rebind(final String name, final Object object) throws NamingException { ! if (logger.isDebugEnabled()) { ! logger.debug("Rebinding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { *************** *** 196,201 **** */ public void unbind(final String name) throws NamingException { ! if (logger.isInfoEnabled()) { ! logger.info("Unbinding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { --- 197,202 ---- */ public void unbind(final String name) throws NamingException { ! if (logger.isDebugEnabled()) { ! logger.debug("Unbinding JNDI object with name [" + name + "]"); } execute(new JndiCallback() { --- NEW FILE: TypeMismatchNamingException.java --- /* * Copyright 2002-2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.jndi; import javax.naming.NamingException; /** * Exception thrown if a type mismatch is encountered for an object * located in a JNDI environment. Thrown by JndiTemplate. * * @author Juergen Hoeller * @since 1.2.8 * @see JndiTemplate#lookup(String, Class) */ public class TypeMismatchNamingException extends NamingException { /** * Construct a new TypeMismatchNamingException. * @param explanation the explanation text */ public TypeMismatchNamingException(String explanation) { super(explanation); } } |