Results 1 to 6 of 6

Thread: <jee:jndi-lookup/> of Tomcat Environment Entries

  1. #1
    Join Date
    Jan 2006
    Location
    Sydney, Australia
    Posts
    14

    Default <jee:jndi-lookup/> of Tomcat Environment Entries

    This should be in theory really easy but it hasn't been for me so I post of the forum as a last resort.

    Problem
    Want to be able to set an environment entry in Tomcat context.xml file such as
    Code:
    <Context ...>
      ...
      <Environment name="baseRmiUrl" value="rmi://whatever"
             type="java.lang.String" override="false"/>
      ...
    </Context>
    and then lookup and retrieve this environment entry inside my spring configuration.

    I've look at the documentation here which is about as clear as mud to me.

    Spring Documentation
    Code:
    Before...
    
    <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/MyDataSource"/>
        <property name="jndiEnvironment">
            <props>
                <prop key="foo">bar</prop>
            </props>
        </property>
    </bean>
    
    After...
    
    <jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
        <jee:environment>foo=bar</jee:environment>
    </jee:jndi-lookup>

    How do I get the environment entry and turn that into a string property so that I may reference it in a <bean> such as

    Code:
        <bean id="remoteAuthenticationService" parent="baseAuthRemoteServerService">
            <property name="serviceUrl">
                <value>${baseRmiUrl}/authenticationService</value>
            </property>
            <property name="serviceInterface">
                <value>com.tangler.service.AuthenticationService</value>
            </property>
        </bean>

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    In the past I have done this with a custom FactoryBean that turns Jndi entries into properties, and then uses the properties in a PropertyPlaceholderConfigurer. That approach is more general than you need for the one example you showed, but maybe you can use it? What you need is String concatenation in a bean property value - not something you get out of the box, as far as I know, except with a placeholder configurer.

  3. #3
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi Dave

    Quote Originally Posted by david_syer View Post
    In the past I have done this with a custom FactoryBean that turns Jndi entries into properties, and then uses the properties in a PropertyPlaceholderConfigurer. That approach is more general than you need for the one example you showed, but maybe you can use it?
    Where is this code so that I can use it? If you post it I will see if it is amenable to adding to the Spring reference documentation as an advanced example in the <jee:jndi-lookup/> section.

    Cheers
    Rick

  4. #4
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi crbaker

    Quote Originally Posted by crbaker View Post
    I've look[ed] at the documentation here which is about as clear as mud to me.
    Darn, muddy was not what I was aiming for. As you can see I've pinged Dave above to see if we can get the code in place to get you moving, and I will certainly amend the reference documentation inline with any suggestions you have with regard to making it less muddy

    Cheers
    Rick

  5. #5

    Default

    Quote Originally Posted by crbaker View Post
    Code:
        <bean id="remoteAuthenticationService" parent="baseAuthRemoteServerService">
            <property name="serviceUrl">
                <value>${baseRmiUrl}/authenticationService</value>
            </property>
            <property name="serviceInterface">
                <value>com.tangler.service.AuthenticationService</value>
            </property>
        </bean>
    How about

    Code:
        <bean id="remoteAuthenticationService" parent="baseAuthRemoteServerService">
            <property name="baseUrl">
                    <!-- This injects the environment String into a String property -->
                    <jee:jndi-lookup jndi-name="${java:comp/env/baseRmiUrl}" />
            </property>
            <property name="relativeServiceUrl">
                <value>/authenticationService</value>
            </property>
            <property name="serviceInterface">
                <value>com.tangler.service.AuthenticationService</value>
            </property>
        </bean>
    and then return the serviceUrl property as
    Code:
            public String getServiceUrl() {
                    return baseUrl + relativeServiceUrl;
            }
    Or, better yet, set the baseUrl property in the baseAuthRemoteServerService, to do it in a centralized way?
    Last edited by ASavitsky; Nov 3rd, 2006 at 02:44 PM.

  6. #6
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    There's an idea. Just use the jndi lookup to fill in a property value and shove it in a placeholder. Nothing fancy:

    Code:
    	<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="properties">
    			<bean class="java.util.Properties">
    				<constructor-arg>
    					<map>
    						<entry key="baseUrl">
    							<jee:jndi-lookup jndi-name="${java:comp/env/baseRmiUrl}" />
    						</entry>
    					</map>
    				</constructor-arg>
    			</bean>
    		</property>
    	</bean>
    That should work with the original service configuration from the first post. The factory bean I mentioned earlier was really a convenient way to load in many JNDI variables, but if there are only a couple and you know their names ahead of time, you can do it this way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •