After a lot of trial and error, I managed to get this working. I figured I'd
post my findings here for future reference for others. My goal was to create a
DataSource for a MySQL database connection pool in JNDI that I could use from
legacy libraries.
One stumbling block in particular was the name of the JNDI object. In
particular, it cannot contain any / characters (contrary to somewhat standard
"jdbc/MyDataSource" names). Line 44 of OSGiServicesContext ensures there is
only a single / separating the interface class name from the filter, so the
filter can not contain additional /'s.
I installed the MySQL driver with:
osgi:install -s wrap:mvn:mysql/mysql-connector-java/5.1.14
And then copied the following jndi.xml into the ServiceMix deploy directory:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
id="datasourceBean">
<property name="serverName"
value="localhost"></property>
<property name="databaseName"
value="database"></property>
<property name="port" value="3306"></property>
<property name="user" value="********"></property>
<property name="password" value="********"></property>
</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>
The status of each is:
[ 183] [Resolved ] [ ] [ ] [ 60] Sun Microsystems'
JDBC Driver for MySQL (5.1.14)
[ 184] [Active ] [Created ] [ ] [ 60] jndi.xml (0.0.0)
Then, in my code I can do:
InitialContext namingContext = new InitialContext();
DataSource dataSource = (DataSource)
namingContext.lookup("osgi:services/javax.sql.DataSource/(osgi.jndi.service.name=MyDataSource)");
Thanks again for your help!
Dan
On Feb 4, 2011, at 7:04 PM, Gert Vanthienen wrote:
> Dan,
>
>
> I'm not entirely sure what you're trying to achieve here, but for many
> purposes, ithe OSGi Service Registry can be used for setting up shared
> objects. For example, you can use a blueprint or spring xml file to
> create and register a DataSource instance. Something like...
>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="datasource"
> class="org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource"/>
>
> <service ref="datasource">
> <interfaces>
> <value>javax.sql.DataSource</value>
> <value>javax.sql.ConnectionPoolDataSource</value>
> </interfaces>
> </service>
>
> </blueprint>
>
> In your own application bundle, you can use Blueprint or Spring DM
> again to access the DataSource that is living in the OSGi Service
> Registry, but if your application wants, it can also access the same
> object instance using JNDI as explained in
> http://servicemix.apache.org/SMX4NMR/7-jndi-integration.html. Not
> entirely sure if I'm actually answering your question here, so feel
> free to let me know if this was not the information you were looking
> for...
>
>
> Regards,
>
> Gert Vanthienen
> ------------------------
> FuseSource
> Web: http://fusesource.com
> Blog: http://gertvanthienen.blogspot.com/
>
>
>
> On Fri, Feb 4, 2011 at 10:43 PM, Dan Powell <[email protected]> wrote:
>> What is the procedure for configuring a JDBC DataSource registered in JNDI?
>> Do I need to require any particular bundles? Where do I configure the
>> Spring bean? The org.springframework.jdbc bundle doesn't appear to be
>> included by default, but even after installing that bundle I still can't get
>> my JNDI beans to deploy.
>>
>> Thanks!
>>
>> Dan