8 Replies Latest reply on Apr 28, 2011 4:53 PM by viczi

    Referencing to global JNDI with @Resource annotation in a servlet

    viczi

      Hello!

       

      I would like to access to a global JNDI resource (created with JNDIBindingServiceMgr) from a servlet in a WAR component of the EAR application (JBoss AS 5.1).

      I would like to use the @Resource annotation, but it doesn't work. The value of the attribute is always null.

       

      Given the following:

       

      - The global JNDI name is: jbossconfig/string (I see with the JNDIView MBean list() method result)

      - The local JNDI name is: s, defined in the jboss-web.xml with the following config:

       

      <resource-ref>

              <res-ref-name>s</res-ref-name>

              <res-type>java.lang.String</res-type>

              <jndi-name>jbossconfig/string</jndi-name>

          </resource-ref>

       

      The resource annotation is in the servlet:

       

      @Resource(name = "s")

      private String s;

       

      And the value of the s is null.

       

      Interresting facts:

      - I could read the value from the global JNDI with lookup: ctx.lookup("jbossconfig/string");

      - I could read the value form the LOCAL JNDI (!!!) with lookup: ctx.lookup("java:comp/env/s");

      - @Resource(mappedName="jbossconfig/string") works (But it depends on global name - not on logical name, so it is not a good solution.)

      - I tried the resource-ref in web.xml, it doesn't work - it is not suprise, because the @Resource annotation is the equivalent of the resource-ref entry in web.xml

      - I tried to use the ResourceLink to link to the global jndi without success

       

      So, how can I access a global jndi with Resource annotation from servlet? Is it possible? (With mapped name it works!)

       

       

      I've attached the war file which contains the source of the servlet, too. And I've attached the jndi-binding-service.xml, which defines the global jndi names. Just copy both of them to the deploy directory, and try the /jbossconfig url.

       

       

      Thank you,

       

      Istvan

        • 1. Referencing to global JNDI with @Resource annotation in a servlet
          jaikiran

          What's the version of the web-app xsd in web.xml? It should be 2.5 minimum for injection to work in servlets.

          • 2. Referencing to global JNDI with @Resource annotation in a servlet
            viczi

            Yes, the version is 2.5, this is the root tag of the web.xml:

             

            <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

            • 3. Referencing to global JNDI with @Resource annotation in a servlet
              jaikiran

              Please post the exact code in the servlet and also can you try this against JBoss AS 6.0.0.Final?

              • 4. Re: Referencing to global JNDI with @Resource annotation in a servlet
                viczi

                Hello!

                 

                I've attached the war file which contains the source of the servlet, too. And I've attached the jndi-binding-service.xml, which defines the global jndi names. Just copy both of them to the deploy directory, and try the /jbossconfig url.

                 

                It doesn't work in JBoss AS 6.0.0.Final.

                 

                Source of the servlet:

                 

                 

                @Resource(name = "s")
                private String s;
                
                @Override
                    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                            throws ServletException, IOException {
                
                     Context c;
                     try {
                            c = new InitialContext();
                            System.out.println("Global lookup: " + c.lookup("jbossconfig/string"));
                            System.out.println("Local lookup: " + c.lookup("java:comp/env/s"));
                     } catch (NamingException ex) {
                        ex.printStackTrace();
                     }
                
                     System.out.println("DI: " + s);
                }
                

                 

                 

                The result is:

                 

                Global lookup: Hello, JNDI!

                Local lookup: Hello, JNDI!

                DI: null

                • 5. Re: Referencing to global JNDI with @Resource annotation in a servlet
                  jaikiran

                  Istvan Viczian wrote:

                   


                  - The local JNDI name is: s, defined in the jboss-web.xml with the following config:

                   

                  <resource-ref>

                          <res-ref-name>s</res-ref-name>

                          <res-type>java.lang.String</res-type>

                          <jndi-name>jbossconfig/string</jndi-name>

                      </resource-ref>

                   

                  Change it to:

                   

                  <jboss-web>
                      <env-entry>
                          <env-entry-name>s</env-entry-name>
                          <env-entry-type>java.lang.String</env-entry-type>
                          <jndi-name>jbossconfig/string</jndi-name>
                      </env-entry>
                  </jboss-web> 
                  

                   

                  A @Resource of type java.lang.String is an env-entry.

                  • 6. Re: Referencing to global JNDI with @Resource annotation in a servlet
                    viczi

                    Hello!

                     

                    Thank you very much, Ill' give it a try!

                     

                    But what about the EJB layer? I use the following, see above. As you see, it is a resource-ref, and doesn't work! What should I use in the EJB layer? resource-ref, or resource-env-ref? As I see, there is no "env-entry" there.

                     

                    <?xml version="1.0" encoding="UTF-8"?>

                    <jboss>

                        <enterprise-beans>

                            <session>

                                <ejb-name>JBossConfigBean</ejb-name>

                     

                                <resource-ref>

                                    <res-ref-name>jbossconfig/string</res-ref-name>

                                    <res-type>java.lang.String</res-type>

                                    <jndi-name>jbossconfig/string</jndi-name>

                                </resource-ref>

                            </session>

                        </enterprise-beans>

                    </jboss>

                    • 7. Referencing to global JNDI with @Resource annotation in a servlet
                      jaikiran

                      For java.lang.String type, it should always be a env-entry in the deployment descriptors (doesn't matter whether it's web or EJB).

                       

                       

                      Istvan Viczian wrote:

                       

                      As I see, there is no "env-entry" there.

                       

                      You mean, you don't see an env-entry element in the jboss.xml xsd? It's there and you should be able to use it similar to the jboss-web.xml.

                      • 8. Re: Referencing to global JNDI with @Resource annotation in a servlet
                        viczi

                        Hello!

                         

                        In the jboss_5_1.xsd (http://www.jboss.org/j2ee/schema/jboss_5_1.xsd) I don't find the env-entry. The XML validator shows error.

                         

                        I've read the specification, and the is no solution to access the variables defined in the application server, in the GLOBAL JNDI. With the @Resource annotation you can access to simple variables (String, Character, Byte, Short, Integer, Long, Boolean, Double and Float), but it can be defined only in the ejb-jar.xml, with env-entry tag, not in application server.

                         

                        Interresting thing, that JBoss 5.1 and 6 works different.