1 2 Previous Next 17 Replies Latest reply on May 22, 2013 3:03 PM by frolovmx

    Get ear name

    klejs

      Hi,

      Is it possible from within a deployed (as an ear) ejb3 application to get the name of the ear file that the application is packaged in?

      Thanks in advance

        • 1. Re: Get ear name
          wluque

          any idea?

          Thanks!!!

          • 2. Re: Get ear name
            jaikiran

            I dont think there's a way. Any specific reason you want to do this?

            • 3. Re: Get ear name
              benc

              I don't know about klejs's reason, for me it is for JNDI lookups because the default path starts with the ear name. Our build scripts include build number, date, etc in the ear name. It would be nice for the application to be able to determine what the ear name is dynamically instead of having to hard code it somewhere and update it for every new build.

              • 4. Re: Get ear name
                jaikiran

                 

                "benc" wrote:
                for me it is for JNDI lookups because the default path starts with the ear name. Our build scripts include build number, date, etc in the ear name.


                You are right, the default jndi name includes the ear name as well. However, you can override this default jndi name and bind the beans to specific jndi-names of your choice either by using the jboss.xml file or by using annotations (look for @RemoteBinding or @LocalBinding). This way, you don't have to depend on the ear file name.


                • 5. Re: Get ear name
                  rpiaggio

                   

                  private static String getEarName() {
                   URL url = Thread.currentThread().getContextClassLoader().getResource("");
                   Pattern p = Pattern.compile("(?i)/([^\\\\/:\\*\\?<>\\|]+)\\.ear/");
                   Matcher m = p.matcher(url.getPath());
                   if (m.find()) {
                   return m.group(1) + "/";
                   } else {
                   return "";
                   }
                  }

                  It's a bit of a hack, but it's the only way I could find.

                  I don't want to be annotating every single bean, it's error-prone. Wish they would standardize bean naming.


                  • 6. Re: Get ear name
                    sverker

                    Unforturnatly I've seen that this method doesn't always work, e.g. from an JMX bean.

                     

                    The reason why I need to get the ear name is that I have a set of modules that are used to build multiple ear files. Hence the jndi names gets different, and that is a feature, but when lookin up the jndi names from beans inside the jar I need to find out which ear is being used. I've searched for a solution but can't find anything.

                    • 7. Re: Get ear name
                      jaikiran

                      Which exact version of JBoss AS?

                      • 8. Re: Get ear name
                        sverker
                        5.1.0.GA
                        • 9. Re: Get ear name
                          jaikiran

                          sverker wrote:

                           

                          but when lookin up the jndi names from beans inside the jar I need to find out which ear is being used. I've searched for a solution but can't find anything.

                          If you use the @org.jboss.ejb3.annotation.RemoteBinding (or org.jboss.ejb3.annotation.LocalBinding or their xml equivalents), you will no longer need to know the ear (or the jar) name. See this example http://www.jboss.org/file-access/default/members/jbossejb3/freezone/docs/tutorial/1.0.7/html/JNDI_Bindings.html

                          • 10. Re: Get ear name
                            sverker
                            Yes, I know about that but then I don't have the benefit of having the ejb names scoped. I.e. if I have two ear's with the same ejb inside on the same jboss instance then the names collide if I've bound them hard with RemoteBinding or LocalBinding.
                            • 11. Re: Get ear name
                              laurent4x_x52

                              Hi Sverker;

                              I've been struggling with the same exact need for 2 days...

                              Did you find something ???

                               

                              Or could the team come up with some easily configurable (not in the code - rather be in xml) way to handle this ???

                              More than once we have to deal with deploying ears with same ejbs.. same jarsetc...

                              but needed with different names.

                               

                              Or is there something we are missing ??

                              My guest is that we are many guys facing this problem... (just check number of posts relating to this..)

                               

                              Do you find something ?? Help will be appreciated, sincerly !

                               

                              Happy New Year to all !

                              Cheers

                              • 12. Re: Get ear name
                                sverker

                                I did a workaround. In pom.xml the application.finalName property is defined to the name the ear will have and is used in build section finalName tag. I have a mbean which is configured via a *-service.xml file deployed in the ear. In there I have an attribute definition like this:

                                 

                                <attribute name="EarName">${application.finalName}</attribute>

                                 

                                Maven will replace the macro with the property value. Then I have a ServiceLocator singleton class which is used by all clients to lookup the services, it has a method like this:

                                 


                                private static String getEarName() {
                                        if(earName == null) {
                                            // This will lookup the name of the ear from our mbean
                                            MBeanServer mBeanServer = MBeanServerLocator.locateJBoss();
                                            try {
                                                earName = (String) mBeanServer.invoke( new ObjectName("com.limetransit.sms:service=LimeManagement"),
                                                        "getEarName",
                                                        new Object[] {},
                                                        new String[] {}
                                                );
                                            } catch(Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                        return earName + "/";
                                }

                                 

                                May not be the most beautiful solution but it works at least...

                                • 13. Re: Get ear name
                                  sverker

                                  The ServiceLocator has methods like this for all Service beans:

                                   

                                  public final SystemStatusService getSystemStatusService(Properties env) throws NamingException {
                                          return (SystemStatusService) getContext(env).lookup(getEarName() + "SystemStatusService/remote");
                                  }

                                  • 14. Re: Get ear name
                                    lkasprzy

                                    Is exist some way to get ear name in Jboss 7.1 version ? As I know the RemoteBinding annotation don't work in 7 version. I have about 15 ears in many diffrent environments with diffrent versions ear  (with timestamps) and composed with diffrent version of modules. This versions is changing often, so I have to programatically inject ear name and also module name.

                                    1 2 Previous Next