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

    EJB3 RC5 & JNDI lookup in JSF WAR

    Frank Cornelis Apprentice


      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()
      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"));
      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);