Announcement Announcement Module
Collapse
No announcement yet.
improve JNDI lookup in test phase Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • improve JNDI lookup in test phase

    Hello.

    I need to provide a dataSource via jndi-lookup to external (= not modifiable) classes.

    Writing the integration test, I noticed the SimpleNamingContextBuilder, and I immediatly loved it.

    But i suggest a small improvement: to auto-initialize it.

    I extended the class (but i don't like extend your classes), i attach the code as sample...

    Code:
    
        <bean
            id="jndiInitializer"
            class="[...].testutils.ExtendedPhasedSimpleNamingContextBuilder">
    
            <property name="objectsToBound">
                <map>
                    <entry
                        key="java:comp/env/jdbc/XYZ"
                        value-ref="dataSource" />
                </map>
            </property>
    
        </bean>
    Code:
    import java.util.Map;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.Phased;
    import org.springframework.mock.jndi.SimpleNamingContextBuilder;
    
    /**
     * A {@link SimpleNamingContextBuilder} with auto-initialization and auto-registration of jndi resources.
     * 
     * @author Add Value S.p.A. by Giovanni Dall'Oglio Risso
     */
    public class ExtendedPhasedSimpleNamingContextBuilder extends SimpleNamingContextBuilder implements Phased, InitializingBean, DisposableBean
    {
    
        private Map<String, Object> objectsToBound;
    
        public void destroy() throws Exception
        {
            if ( objectsToBound != null )
            {
                deactivate();
            }
        }
    
        public void afterPropertiesSet() throws Exception
        {
            if ( objectsToBound != null )
            {
                for ( String k : objectsToBound.keySet() )
                {
                    bind(k, objectsToBound.get(k));
                }
    
                activate();
            }
        }
    
        public int getPhase()
        {
            // this will start this bean
            // BEFORE the other beans in the Application Context
    
            return Integer.MIN_VALUE;
        }
    
        public Map<String, Object> getObjectsToBound()
        {
            return objectsToBound;
        }
    
        public void setObjectsToBound(Map<String, Object> objectsToBound)
        {
            this.objectsToBound = objectsToBound;
        }
    
    }

  • #2
    Hi Giovanni,

    Someone has already requested this feature in SPR-7873.

    So feel free to watch SPR-7873, vote for it, and/or add your comments there.

    Regards,

    Sam

    Comment

    Working...
    X