4 Replies Latest reply on Feb 8, 2006 9:06 AM by fcorneli

    EJB3 RC5 & JNDI lookup in JSF WAR

    fcorneli

      Hi,

      Since RC4 there is the JNDI ear prefixing which is very useful to prevent name clashes. But, when I try to call an EJB3 from within my JSF based WAR I have to do very crazy things to find out about the runtime name of the ear file. This because my ear file has a format like EARNAME-A_VERSION.ear. The A_VERSION is something I don't feel like hard coding into my JSF BB's, I need to find out about it at runtime. Isn't there something like an EAR local JNDI tree which all WARs within the same EAR can use to find their EAR local stuff? I already tried via 'java:comp', but this is not populated with the EJB3 beans at all.

      BTW: the runtime full EAR name detection code looks like (anyone, please, how do I avoid such f*cking things):

      private static final String EAR_PREFIX = "myapplication-deploy";

      private T getEJB3(Class clazz) {
      try {
      ClassLoader classLoader = Thread.currentThread()
      .getContextClassLoader();
      String fullEARPrefix = "";
      if (classLoader instanceof URLClassLoader) {
      URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
      URL[] urls = urlClassLoader.getURLs();
      for (URL url : urls) {
      String path = url.getPath();
      if (path.matches(".*" + EAR_PREFIX + ".*\\.ear.*")) {
      fullEARPrefix = path.substring(
      path.indexOf(EAR_PREFIX), path.indexOf(".ear"));
      break;
      }
      }
      }
      LOG.debug("full EAR prefix: " + fullEARPrefix);
      InitialContext initialContext = new InitialContext();
      T instance = (T) initialContext.lookup(fullEARPrefix
      + clazz.getSimpleName() + "/local");
      return instance;
      } catch (NamingException e) {
      throw new RuntimeException("naming error: " + e.getMessage(), e);
      }
      }

        • 1. Re: EJB3 RC5 & JNDI lookup in JSF WAR
          bill.burke

          why don't you just define your own jndi mappings for the EJBs within jboss.xml or @Local/RemoteBinding?

          • 2. Re: EJB3 RC5 & JNDI lookup in JSF WAR
            fcorneli

             

            "bill.burke@jboss.com" wrote:
            why don't you just define your own jndi mappings for the EJBs within jboss.xml or @Local/RemoteBinding?


            If I use @Local/RemoteBinding I lose the EAR prefix behaviour. I need to keep the EAR prefix because I have to deploy the same application multiple times, each time with a different EAR filename. Thus I need the EAR prefixes to scope my applications, but I don't want to have the prefix all over my WAR servlets. It should (IMHO) be possible to do an 'EAR scoped JNDI lookup' without too much pain.

            Frank.

            • 3. Re: EJB3 RC5 & JNDI lookup in JSF WAR
              bill.burke

              Ok, I think this is something we could do. Let me move this thread to EJB3 Design to see if it is something we *want* to do.

              • 4. Re: EJB3 RC5 & JNDI lookup in JSF WAR
                fcorneli

                It's getting even better. In case you're in a SAR, you have to do something like:

                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                if (classLoader instanceof UnifiedClassLoader) {
                 UnifiedClassLoader unifiedClassLoader = (UnifiedClassLoader) classLoader;
                 URL url = unifiedClassLoader.getURL();
                 String path = url.getPath();
                 if (path.matches(".*" + EAR_PREFIX + ".*\\.ear.*")) {
                 String fullEARPrefix = path.substring(path.indexOf(EAR_PREFIX), path
                 .indexOf(".ear"));
                 return fullEARPrefix;
                 }
                }