Hi
The goal is to get a reference to the configured datasource through JNDI.
In service unit (non-OSGI) I try:
InitialContext namingContext = new InitialContext();
DataSource dataSource = (DataSource)
namingContext.lookup("osgi:services/javax.sql.DataSource/(osgi.jndi.service.name=MyDataSource)");
Connection con = dataSource.getConnection();
and got Exception:
javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an
application resource file: java.naming.factory.initial
at
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at
javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at
javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
Here is my configuration in SMX4.3
Budles successfully deployed:
postgres driver as osgi bundle
c3p0 pool as osgi bundle
c3p0-postgres osgi fragment to inject the driver dependency to the pool
C3P0 datasource is configured as osgi service through jndi.xml and hot
deployed. I can see this service in the OSGI registry:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<cm:property-placeholder persistent-id="example.db"/>
<bean id="datasourceBean"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
...
</bean>
<service id="datasourceService" interface="javax.sql.DataSource"
ref="datasourceBean">
<service-properties>
<entry key="osgi.jndi.service.name"
value="MyDataSource"></entry>
</service-properties>
</service>
</blueprint>
Any clue why I can't get initial context from SU?
Regards,
Anatoliy
--
View this message in context:
http://servicemix.396122.n5.nabble.com/Accessing-osgi-service-from-service-unit-tp4315330p4315330.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.