1 Reply Latest reply on Dec 17, 2008 4:23 AM by Ralf Heydenreich

    locate services with service locator?

    Ralf Heydenreich Newbie

      Hi all,
      I have some servlets (lets say, ServA and ServB) on my JBoss 4.2.2.GA. Now I want to use the ServB from ServA. Both Servlets are registered in JNDI. The only way that works is a JNDI lookup in ServA to ServB. Injection via @Resource doesn't work. The old ServiceLocator pattern doesn't work, too (because no EJBHome exists).

      Is there a convenient method to write a generic service locator class which locates a given service and returns the correct service interface? One way I've tried is the ServiceLocator package from ALR, but this package is in a raw state and I didn't know how to use it (which configuration is needed). Any suggestions are welcome. The topics in this forum refer mostly to JSF or such things, which I don't need.

      TIA,
      Ralf.

        • 1. Re: locate services with service locator?
          Ralf Heydenreich Newbie

          I found my own solution:

           @SuppressWarnings("unchecked")
           public <T> T lookup(Class<T> clazz, final String pJndiName) {
           T cs = null;
           String jndiName = (pJndiName == null)
           ? jndiPrefix + clazz.getSimpleName() + "Bean/remote"
           : pJndiName;
          
           LOG.debug("Looking for "+jndiName);
           try {
           Object o = context.lookup(jndiName);
           if (o == null) {
           System.err.println("unable to find " + jndiName);
           } else {
           System.out.println("retrieved instance of "
           + o.getClass().getName());
           }
           cs = (T) PortableRemoteObject.narrow(o, clazz);
          
           } catch (NamingException e) {
           e.printStackTrace();
           }
           return cs;
           }
          


          Errorhandling has to be improved, but this version works for me.