You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by bestrafer <ze...@gmail.com> on 2011/04/20 11:32:01 UTC
Accessing osgi:service from service unit
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.
Re: Accessing osgi:service from service unit
Posted by Freeman Fang <fr...@gmail.com>.
On 2011-4-20, at 下午5:32, bestrafer wrote:
> 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)
>
Hi,
You're using JBI packaging, right?
How about you use OSGi packaging?
Or you can inject jndi object as OSGi service reference(as it's
already in OSGi registry) into your SU bean.xml and grap the jndi
object directly from your SU bean, this can bypass the jndi lookup
way. A related discussion[1] here should help.
[1]http://servicemix.396122.n5.nabble.com/Using-osgi-reference-in-a-service-unit-throws-error-td4313761.html
Freeman
>
> 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.
---------------------------------------------
Freeman Fang
FuseSource
Email:ffang@fusesource.com
Web: fusesource.com
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
Connect at CamelOne May 24-26
The Open Source Integration Conference