users@glassfish.java.net

Re: JNDI from a service bundle

From: Sahoo <sanjeeb.sahoo_at_oracle.com>
Date: Sat, 30 Apr 2011 13:00:25 +0530

Hi Eduardo,

I can't reproduce this. We have tests where we do JNDI lookup from pure
OSGi bundles and they work. I even tried doing the same from an SCR
component and it works both in GF 3.1 and 3.2-SNAPSHOT. From the stack
trace it appears that one of our internal service tracker got a
notification about the service, but the service object was found to be
null. I can't explain how that's happening. Can you supply us the test
case? For your reference, the test case I just developed to test this
scenario is available at

https://svn.java.net/svn/glassfish~svn/trunk/fighterfish/test/testapp/test.app15

You can just checkout that url, build and try out yourself.

We have one bug about looking up JNDI from a BundleActivator, but that's
only applicable when a bundle is deployed using "asadmin deploy
--type=osgi" command as discussed in
http://java.net/jira/browse/GLASSFISH-16412. That bug has been fixed in
glassfish trunk. I am sure you are not affected by that bug as you said
you are deploying via autodeploy/bundles/.

Thanks,
Sahoo

On Friday 29 April 2011 11:51 PM, Eduardo Solis wrote:
> Hi there!
>
> Got a question. I am trying to create an osgi service (using
> declarative services). what I want this service to do is to provide
> the JNDI lookup for the datasource and hand-out connections to other
> bundles and to other J2EE clients (servlet). I created the skeleton of
> the service and its service declaration and things work fine.
>
> public interface IConnectionGetter {
>
> public Connection getConnection();
> }
>
>
> public class ConnectionGetterImpl implements IConnectionGetter {
> private static final String MYCLASS =
> ConnectionGetterImpl.class.getName();
> private static Logger logger = Logger.getLogger(MYCLASS);
>
> /*
> * (non-Javadoc)
> *
> * @see com.acs.connector.IConnectionGetter#getConnection()
> */
> @Override
> public Connection getConnection() {
> String method = "getConnection";
> logger.entering(MYCLASS, method);
> Connection conn = null;
> logger.exiting(MYCLASS, method);
> return conn;
> }
> }
>
> This works fine, I can see the service in the registry and the servlet
> can get to it (using @Resource to get it). Now, when I actually code
> the JNDI lookup
>
> public class ConnectionGetterImpl implements IConnectionGetter {
> private static final String MYCLASS =
> ConnectionGetterImpl.class.getName();
> private static Logger logger = Logger.getLogger(MYCLASS);
>
> /*
> * (non-Javadoc)
> *
> * @see com.acs.connector.IConnectionGetter#getConnection()
> */
> @Override
> public Connection getConnection() {
> String method = "getConnection";
> logger.entering(MYCLASS, method);
> Connection conn = null;
> try {
> InitialContext context = new InitialContext();
> DataSource src = (DataSource) context.lookup("jdbc/testConnection");
> conn = src.getConnection();
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (SQLException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> logger.exiting(MYCLASS, method);
> return conn;
> }
> }
>
>
> As soon as I add the InitialContext I get this when I deploy the
> bundle (autodeploy/bundles)
>
> [#|2011-04-29T12:02:40.509-0500|SEVERE|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=96;_ThreadName=Thread-1;|java.lang.NullPointerException
> at
> com.sun.hk2.component.ExistingSingletonInhabitant.<init>(ExistingSingletonInhabitant.java:57)
> at
> org.jvnet.hk2.osgiadapter.HK2Main$HK2ServiceTrackerCustomizer.addingService(HK2Main.java:273)
> at
> org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:896)
> at
> org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:261)
> at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:233)
> at
> org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:840)
> at
> org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:871)
> at
> org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:733)
> at
> org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:662)
> at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:3769)
> at org.apache.felix.framework.Felix.access$000(Felix.java:80)
> at org.apache.felix.framework.Felix$2.serviceChanged(Felix.java:722)
> at
> org.apache.felix.framework.ServiceRegistry.registerService(ServiceRegistry.java:107)
> at org.apache.felix.framework.Felix.registerService(Felix.java:2854)
> at
> org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:251)
> at
> org.apache.felix.scr.impl.manager.AbstractComponentManager.registerService(AbstractComponentManager.java:408)
> at
> org.apache.felix.scr.impl.manager.AbstractComponentManager.registerComponentService(AbstractComponentManager.java:419)
> at
> org.apache.felix.scr.impl.manager.AbstractComponentManager$Unsatisfied.activate(AbstractComponentManager.java:1003)
> at
> org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:298)
> at
> org.apache.felix.scr.impl.manager.AbstractComponentManager$1.doRun(AbstractComponentManager.java:138)
> at
> org.apache.felix.scr.impl.ComponentActivatorTask.run(ComponentActivatorTask.java:67)
> at
> org.apache.felix.scr.impl.ComponentActorThread.run(ComponentActorThread.java:96)
> at java.lang.Thread.run(Thread.java:662)
> |#]
>
>
> I found an example from Sahoo where he was doing the same lookup from
> an activator only his was a wab rather than just a simple bundle.
>
> Has anyone done this or is there an article somewhere on how to do this?
>
> Thanks!
>
> Eduardo Solis.