You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemind.apache.org by hi...@jakarta.apache.org on 2004/07/01 00:55:27 UTC

[Jakarta HiveMind Wiki] Updated: UsingJNDIToObtainHiveMindServices

   Date: 2004-06-30T15:55:27
   Editor: NareshSikha <na...@schwab.com>
   Wiki: Jakarta HiveMind Wiki
   Page: UsingJNDIToObtainHiveMindServices
   URL: http://wiki.apache.org/jakarta-hivemind/UsingJNDIToObtainHiveMindServices

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -13,3 +13,48 @@
 I think it's a very good idea, where practical, to do this extra step ... it supports the ''Feedback'' principle. This is insurance against a change to a 3rd party library where an existing service's interface is changed (a bad idea!). Your existing code will fail ... but fail with a more sophisticated message. This is why I would object to a convienience method that takes just a service name.
 
 DanielFeist:  I agree completly and don't think the addition of another method which takes just the service-id is a good idea.  I just wanted to know if the passing of {{{Object.class}}} is acceptable if there is a situation, like in the example I gave or similair, where it is not possible to pass the interface expected.
+
+== Discussion ==
+
+I believe it is possible to infer the class name from the lookup request. Here is an example of using JNDI to discover the ThreadLocalStorage service.
+
+{{{
+Context c = new InitialContext();
+ThreadLocalStorage tls = (ThreadLocalStorage) c.lookup("service:" + ThreadLocalStorage.class.getName());
+}}}
+
+If there was a distinct service id, then that could be managed as well.
+
+{{{
+Context c = new InitialContext();
+ThreadLocalStorage tls = (ThreadLocalStorage) c.lookup("service:" + ThreadLocalStorage.class.getName() + "!some.service.id");
+}}}
+
+The internals of the lookup method could tokenize the string and do a forName on the class portion of the string.
+
+{{{
+public class serviceURLContext implements Context {
+  private Registry _registry;
+
+  public Object lookup(String name) throws NamingException {
+    name = name.substring(8).trim();
+
+    String serviceClassName = null;
+    String serviceId = null;
+
+    int i = name.indexOf("!");
+    if (i > 0) {
+      serviceClassName = name.substring(0, i);
+      serviceId = name.substring(i + 1);
+    } else {
+      serviceClassName = name;
+      serviceId = name;
+    }
+
+    Class serviceClass = Thread.currentThread().getContextClassLoader().
+      loadClass(serviceClassName);
+
+    return _registry.getService(serviceId, serviceClass);
+  }
+}
+}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org