Announcement Announcement Module
Collapse
No announcement yet.
Set property value from a jndi lookup result Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Set property value from a jndi lookup result

    In configuring an ejb stateless session bean proxy you can supply a jndiTemplate. This takes a provider url. I do not wish to hard code the URL here, but wish to obtain a JNDI env String specifying the provider url. Does spring offer any way to set a property value based on the result of a JNDI env lookup???

  • #2
    Specifying a JndiTemplate does enable you to provide the informations necessary for performing a jndi lookup.
    As I understand you want to perform a jndi lookup to retrieve the necessary informations to create the JndiTemplate. That sounds somewhat recursive to me, but maybe I did not understand your purpose correctly. If that is the case, please provide some more details.

    Regards,
    Andreas

    Comment


    • #3
      Yes I agree it is abstracting it one more layer, but we do not wish to put any deployment related information such as provider url's in the applicationContext.xml. Instead we would rather have the environment supply this type of information so that it may be configured through the admin tool for example - if a JBOSS server went down, we could link a new Provider URL to the resource and reload the app, rather than have to go in and edit the source. So I would like to obtain the provider url as a JNDI lookup and then use this lookup value in my JNDI Template.

      Comment


      • #4
        And where do you want to retrieve the parameters for the first JNDI lookup from?
        Some initial information has to be provided in the client. And you do not need to hardcode it. If it is in the application context (maybe even extracted via PropertyPlaceHolderConfigurer) it is easily changed without touching the program itself.

        Regards,
        Andreas

        Comment


        • #5
          I know the names of the Environment variables for the first jndi lookup - we can expect these to always remain the same. They are a requirement for the web application and must be provided by the container - whether these are linked to some global entry or simply supplied in the context.xml does not matter to me - as long as they are set.

          <Environment name="javaNamingFactoryInitial" value="org.jnp.interfaces.NamingContextFactory" type="java.lang.String" />
          <Environment name="javaNamingProviderUrl" value="jnp://host:1099" type="java.lang.String" />
          <Environment name="javaNamingFactoryUrlPkgs" value="org.jboss.naming:org.jnp.interface" type="java.lang.String" />

          I tried the following:

          Code:
          <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
             <constructor-arg index="0">
                <props>
                   <prop key="java.naming.factory.initial">
                      <bean class="org.springframework.jndi.JndiObjectFactoryBean">
                         <property name="resourceRef" value="true" />
                         <property name="jndiName" value="javaNamingFactoryInitial" />
                       </bean>
                    </prop>
                    <prop key="java.naming.provider.url">
                        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
                             <property name="resourceRef" value="true" />
                             <property name="jndiName" value="javaNamingProviderUrl" />
                        </bean>
                    </prop>
                    <prop key="java.naming.factory.url.pkgs">
                        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
                             <property name="resourceRef" value="true" />
                             <property name="jndiName" value="javaNamingFactoryUrlPkgs" />
                        </bean>
                    </prop>
               </props>		
          </constructor-arg>
          </bean>

          Comment


          • #6
            Normally you would use a PropertyPlaceholderConfigurer to do this. But you typically use a property file or the Java environment to specify values, not JNDI.

            Another suggestion would be to have the value you want be a JndiObjectFactoryBean. The problem with that is you'd have to have to admin url for that bean. But a PropertyPlaceholderConfigurer actually fits here, since you can't configure the admin server on the fly. So you would just use a regular property file.

            Comment


            • #7
              What error did you get? Since you are using resourceRef set to true, are those values properly set in your EJB or servlet descriptors?

              Comment


              • #8
                The error I get is: The content of element type "prop" must match "null". I simply have the Environment entries declared in my web applications context.xml file.

                So you think I should create a PropertyPlaceHolderConfigurer which does my Environment lookups to replace the values??

                Comment


                • #9
                  Ok I looked at the Spring Beans DTD, my bad. Prop is defined as <prop key="xxx">#PCDATA</prop>. I was attempting to set the value based on the result of my jndiObjectFactoryBean which is a String - this is not allowed. Hmm, I wish they allowed beans to be nested in the <prop> as long as the call to the bean results in a String.

                  Comment


                  • #10
                    What I did for creating properties for JNDI was create a class with static method which returns a property object. That kept the names of the properties out of my XML files. If you did that you could pass the results of the jndi lookup into that method. This method takes url, username and password. If username is null, then only the url is added to the properties.

                    Code:
                    <bean id="myProperties" class="com.mycompany.jndi.JndiUtils" factory-method="createJBossProperties">
                      <constructor-arg><!-- url --></constructor-arg>
                      <constructor-arg><!-- username --></constructor-arg>
                      <constructor-arg><!-- password --></constructor-arg>
                    </bean>

                    Comment

                    Working...
                    X