users@glassfish.java.net

RE: JNDI from a service bundle

From: Martin Gainty <mgainty_at_hotmail.com>
Date: Fri, 29 Apr 2011 23:27:47 -0400

local-home or remote-home..it makes a difference on the lookup

hint: use
home = (ABTDLocalHome)context.lookup( "java:comp/env/ejb/ABTDLocalHome" )
for LocalHome

for RemoteHome
Object initialReference = context.lookup(
                                "ejb/jbs/ftob/ejb/td/ABTDHome" );
use jdbc for database connections only..

http://www.cs.unc.edu/Courses/jbs/lessons/enterprise/ejb_jndi/
Martin Gainty
______________________________________________
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas. Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett. Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs. Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut ętre privilégié. Si vous n'ętes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert ŕ l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement ętre sujets ŕ la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




From: eleduardo.solis_at_gmail.com
Date: Fri, 29 Apr 2011 13:21:40 -0500
Subject: JNDI from a service bundle
To: users_at_glassfish.java.net

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.