Forum Home » Fuse Distributions » Fuse ESB

Thread: Calling DataSource via JNDI in ServiceMix 4.4.1

 

Permlink Replies: 3 - Last Post: Apr 19, 2012 2:10 AM Last Post By: mikevoxcap Threads: [ Previous | Next ]
mikevoxcap

Posts: 4
Registered: 04/11/12
Calling DataSource via JNDI in ServiceMix 4.4.1
Posted: Apr 11, 2012 6:52 PM
  Click to reply to this thread Reply
I have tried about every example to get this working, but I finally need to turn to the forum for help. Here is my setup:

  • Local ServiceMix 4.4.0
  • JDK 1.6
  • Windows

As per some of the forum posts, I have created a bundle that exposes the datasource.

/DSproj/src/main/resources/META-INF/spring/jndi.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<bean id="myDSBean" class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
<property name="databaseName" value="MyDB" />
<property name="password" value="pass" />
<property name="portNumber" value="1433" />
<property name="serverName" value="server" />
<property name="user" value="uer" />
</bean>

<osgi:service id="myDSService" ref="myDSBean">
<osgi:interfaces>
<value>javax.sql.DataSource</value>
<value>net.sourceforge.jtds.jdbcx.JtdsDataSource</value>
</osgi:interfaces>
<osgi:service-properties>
<entry key="osgi.jndi.service.name" value="MyDS" />
</osgi:service-properties>
</osgi:service>
</beans>

/DSProj/src/main/resoruces/jndi.properties

java.naming.factory.initial=org.apache.xbean.spring.jndi.SpringInitialContextFactory

I am able to install this bundle and start it successfully in ServiceMix. Now, I have created a second bundle that contains Hibernate entities and DAO. I want to expose these DAOs to other service bundles. I create a second service bundle, which is currently the one having issues.

/EntityProj/src/main/resources/jndi.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="jndi"
class="org.apache.xbean.spring.jndi.SpringInitialContextFactory"
factory-method="makeInitialContext">
</bean>

</beans>

/EntityProj/src/main/resources/jndi.properties

java.naming.factory.initial=org.apache.xbean.spring.jndi.SpringInitialContextFactory

/EntityProj/src/main/resources/META-INF/spring/my-datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<jee:jndi-lookup id="dataSource"
jndi-name="osgi:services/javax.sql.DataSource/(osgi.jndi.service.name=MyDS)"
expected-type="javax.sql.DataSource" />

...

When I install the second project, I am now getting the following exception:

Caused by: javax.naming.NamingException: scheme osgi not recognized
at org.apache.xbean.spring.jndi.DefaultContext.lookup(DefaultContext.java:131)

My thought right now is that the bundle for DAOs and entities should use a different initial context implementation over SpringInitialContextFactory, but wasn't sure if I would run into the exception I got above.

I am really at a loss here. Any help would be appreciated.
mikevoxcap

Posts: 4
Registered: 04/11/12
Re: Calling DataSource via JNDI in ServiceMix 4.4.1
Posted: Apr 12, 2012 4:34 PM   in response to: mikevoxcap in response to: mikevoxcap
  Click to reply to this thread Reply
Planning to try the example here:

https://github.com/cschneider/Karaf-Tutorial

Hopefully this resolves my issue. Thanks!
mikevoxcap

Posts: 4
Registered: 04/11/12
Re: Calling DataSource via JNDI in ServiceMix 4.4.1
Posted: Apr 12, 2012 7:51 PM   in response to: mikevoxcap in response to: mikevoxcap
  Click to reply to this thread Reply
Fixed my issue. I switched from using JEE to do the JNDI lookup and moved to having everything in the persistence.xml.
mikevoxcap

Posts: 4
Registered: 04/11/12
Re: Calling DataSource via JNDI in ServiceMix 4.4.1
Posted: Apr 19, 2012 2:10 AM   in response to: mikevoxcap in response to: mikevoxcap
  Click to reply to this thread Reply
Okay, I moved to trying to do this with a blueprint rather than doing this via spring configurations. It is really disappointing how long it takes to configure something that I expect to take no more than an hour (it has taken me well over 40 hours between pouring through documentation and trying different combinations). Here is where I am at.

First, I have a blueprint that defines the datasource I am using:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<bean id="myDSBean" class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
<property name="databaseName" value="test" />
<property name="password" value="password" />
<property name="portNumber" value="1433" />
<property name="serverName" value="10.10.10.10" />
<property name="user" value="sa" />
</bean>

<service interface="javax.sql.DataSource" ref="myDSBean">
<service-properties>
<entry key="osgi.jndi.service.name" value="MyDS"/>
</service-properties>
</service>

</blueprint>

I am able to deploy this and it starts up fine.

Next, I have my bundle. The bundle has the following files:

/src/main/resources/jpa/EachEntity.xml

These contain the entity mappings. These worked fine with a Spring configuration. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">

<package>com.mycompany.data.entity</package>
<entity class="MyEntity" metadata-complete="false" />
</entity-mappings>

/src/main/resources/META-INF/persistence.xml

Here is a snippet:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>osgi:services/javax.sql.DataSource/(osgi.jndi.service.name=MyDS)</non-jta-data-source>
<mapping-file>jpa/MyEntity.xml</mapping-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.default_schema" value="dbo" />
</properties>
</persistence-unit>
</persistence>

/src/main/resources/OSGI-INF/blueprint.xml

This is the blueprint for the service that I am exposing.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://cxf.apache.org/blueprint/jaxws
http://cxf.apache.org/schemas/blueprint/jaxws.xsd
http://cxf.apache.org/blueprint/core
http://cxf.apache.org/schemas/blueprint/core.xsd">

<bean id="myEntityDAO" class="com.mycompany.data.dao.jpa.MyEntityJpaDAO">
<jpa:context property="entityManager" unitname="persistenceUnit" />
</bean>

<jaxws:endpoint
xmlns:nmgr="http://www.mycompany.com/service/MyService/"
id="myService"
address="/MyService"
serviceName="nmgr:MyService"
endpointName="nmgr:MyServiceSOAP"
implementor="#myServiceImpl"/>

<bean id="myServiceImpl"
class="com.mycompany.service.myservice.impl.MyServiceImpl">
<property name="myEntityDAO" ref="myEntityDAO" />
</bean>
</blueprint>

pom.xml

My felix configuration:

<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>!com.mycompany.schemas.*</Export-Package>
<Private-Package>
com.mycompany.*
</Private-Package>
<Import-Package>
javassist.util.proxy,
javax.sql,
javax.persistence.spi,
javax.persistence.metamodel,
javax.persistence.criteria,
org.hibernate;version=${hibernate.version},
org.hibernate.proxy;version=${hibernate.version},
org.hibernate.ejb;version=${hibernate.version},
org.springframework.aop;version=${spring.version},
org.springframework.aop.framework;version=${spring.version},
org.aopalliance.aop,
javax.naming.*,
*
</Import-Package>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
</instructions>
</configuration>

Result

First, the blueprint of the service has errors for <jpa:context /> and <jpa:unit />

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jpa:context'.

Second, when the bundle is deployed, there is no web service and the only log I see is:

2012-04-18 20:50:36,642 | WARN | l Console Thread | container | ? ? | 170 - org.apache.aries.jpa.container - 0.3.0 | There are no providers available.

Doesnt look like any of the beans are initialized from the blueprint.xml.

Also, I am looking for opinions / information / options on the following:

1) Can I use spring's component scanning mechanism in a blueprint? I was not sure if I was forced to use <jpa:context /> and <jpa:unit />.

2) Looking at the documentation for aries, it also looks like the transactions schema forces me to define transactions at the DAO level. Is this true?

3) Can I refer to blueprint.xml beans from the spring configured beans?

Again, your help is appreciated. I really want to feel comfortable with and be an advocate for this product.