8 Replies Latest reply on Jul 26, 2006 5:25 AM by Nigel White

    Accessing correct bean version if multiple EARs deployed.

    Nigel White Newbie

      I need to be able to look up the appropriate EJB for the EAR file in which I am running.

      For example, we'll probably deploy named EAR files like Aspicio1-2-0.ear which will result in our Session Beans having a JNDI name of Aspicio1-2-0/BeanClassName.

      Some customers will continue to use the previous Apicio1-1-11.ear.

      In my code, I'll need to use

      String earName = ""; // How?
      new InitialContext.lookup(earName + "/BeanClassName/local");

      How can I set earName?

        • 1. Re: Accessing correct bean version if multiple EARs deployed
          jaikiran pai Master

          I havent thought whether there are any other ways of doing this, but the first thing that comes to my mind is passing the earName as an <env-entry>. You may bind the earName through the env-entry and later on lookup the same in your code:

          String earName = ctx.lookup("java:/comp/env/earName");
          ctx.lookup(earName + "BeanClassName/local");
          


          • 2. Re: Accessing correct bean version if multiple EARs deployed
            Nigel White Newbie

            In which of the many XML config files would this <env-entry> element go, and what would the syntax be?

            (Thanks for helping BTW!)

            • 3. Re: Accessing correct bean version if multiple EARs deployed
              Nigel White Newbie

              Ah, just a thought. Each EAR would have to have its own env-entry. So they'd have tro be called different names, so we're back where we started.

              We need to be able to enquire of the container what EAR fiel we are executing out of.

              • 4. Re: Accessing correct bean version if multiple EARs deployed
                jaikiran pai Master

                You can add it to the web.xml file. Here's an extract from the dtd of the web.xml:

                <!--
                The env-entry element contains the declaration of a web application's
                environment entry. The declaration consists of an optional
                description, the name of the environment entry, and an optional
                value. If a value is not specified, one must be supplied
                during deployment.
                -->
                <!ELEMENT env-entry (description?, env-entry-name, env-entry-value?,
                env-entry-type)>
                
                <!--
                The env-entry-name element contains the name of a web applications's
                environment entry. The name is a JNDI name relative to the
                java:comp/env context. The name must be unique within a web application.
                
                Example:
                
                <env-entry-name>minAmount</env-entry-name>
                
                Used in: env-entry
                -->
                <!ELEMENT env-entry-name (#PCDATA)>
                
                <!--
                The env-entry-type element contains the fully-qualified Java type of
                the environment entry value that is expected by the web application's
                code.
                
                The following are the legal values of env-entry-type:
                
                 java.lang.Boolean
                 java.lang.Byte
                 java.lang.Character
                 java.lang.String
                 java.lang.Short
                 java.lang.Integer
                 java.lang.Long
                 java.lang.Float
                 java.lang.Double
                
                Used in: env-entry
                -->
                <!ELEMENT env-entry-type (#PCDATA)>
                
                <!--
                The env-entry-value element contains the value of a web application's
                environment entry. The value must be a String that is valid for the
                constructor of the specified type that takes a single String
                parameter, or for java.lang.Character, a single character.
                
                Example:
                
                <env-entry-value>100.00</env-entry-value>
                
                Used in: env-entry
                -->
                <!ELEMENT env-entry-value (#PCDATA)>


                For more information, you can find the dtd at:
                http://java.sun.com/dtd/web-app_2_3.dtd

                So your entry will look like, in Apicio1-1-11.ear :

                <env-entry>
                 <description>Name of my application ear</description>
                 <env-entry-name>earName</env-entry-name>
                 <env-entry-value>Apicio1-1-11<env-entry-value>
                 <env-entry-type>java.lang.String</env-entry-type>
                </env-entry>


                And in Apicio1-2-0.ear it would be:

                <env-entry>
                 <description>Name of my application ear</description>
                 <env-entry-name>earName</env-entry-name>
                 <env-entry-value>Apicio1-2-0<env-entry-value>
                 <env-entry-type>java.lang.String</env-entry-type>
                </env-entry>


                You will have to change the value of this env-entry for each application ear, when you are deploying the application.

                Your code, however would remain the same:


                String earName = ctx.lookup("java:comp/env/earName");
                ctx.lookup(earName + "BeanClassName/local");


                So for Apicio1-2-0.ear, the lookup of env-entry would return the value Apicio1-2-0 and for Apicio1-1-11.ear, it would return Apicio1-1-11


                Try it out.



                • 5. Re: Accessing correct bean version if multiple EARs deployed
                  jaikiran pai Master

                  Sometime back a similar question was asked. On searching the JBossAS JIRA, found this:

                  http://jira.jboss.com/jira/browse/EJBTHREE-617

                  • 6. Re: Accessing correct bean version if multiple EARs deployed
                    Nigel White Newbie

                    Not quite the same. They were asking to snip any version information off so that you would just use Aspicio/BeanClassName/local.

                    I want to *preserve* version information, and have multiple versions of the EJBs in the JNDI tree.

                    It's just that inside the EAR, the code needs to know which is the appropriate one to look up.

                    • 7. Re: Accessing correct bean version if multiple EARs deployed
                      jaikiran pai Master

                      My understanding of http://jira.jboss.com/jira/browse/EJBTHREE-617 is that the user will be provided an option of configuring the name through some descriptor file like application.xml. The stripping of the version from the name might *just be an example*. I might be wrong, but this is what i understood out of it.

                      • 8. Re: Accessing correct bean version if multiple EARs deployed
                        Nigel White Newbie

                        I went with a utility class to look up beans.

                        The source for this class is generated by the ANT build script to prepend the name of the EAR file being built to the beginning of the JNDI name.

                        So I have an EJBUtil class which contains

                        public static Object getLocalEJB(String name) throws NamingException
                         {
                         return lookup("@ear_name@/" + name + "/local");
                         }
                        
                        


                        Which gets "fixed" by a filter in the build which knows the EAR file name.