You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@aries.apache.org by Harald Wellmann <ha...@gmx.de> on 2011/06/29 00:34:35 UTC
Re: declaring a JNDI datasource
Hi Daniel,
there's a tutorial for setting up Aries + OpenJPA + JNDI + Datasource I
wrote last autumn:
http://hwellmann.blogspot.com/2010/09/openjpa-and-osgi.html
The version numbers are out of date by now, but if you know the basics
about JPA in classic web containers like Tomcat, this post may help you
to understand some of the differences when using JPA with OSGi and Aries.
Am 28.06.2011 16:40, schrieb Daniel Plappert:
> I am new to OSGI and currently trying to understand its mechanism. I
> want to create a service (bundle), which creates a JDNI datasource
> and provides this datasource to other services. What I've read so far
> is, OSGI "itself" (felix/equinox) can't use JNDI, but aries can.
> That's why I am asking.
>
OSGi Core doesn't know about JNDI. The OSGi Enterprise Spec defines an
OSGi JNDI service, and Aries provides an implementation.
> What I am trying to do is to provide a flexible service, let's say a
> JDNIDatasourceService. This service creates a JNDI datasource, which
> connection parameters can be configured "from the outside".
JNDI datasource? You mean a JDBC datasource registered in a JNDI naming
context...
> I don't
> what to deploy the bundle with a persistence.xml, in which the
> username, password and so on are hard coded. What I want is the same
> possible mechanism like i.e. a tomcat server provides. It is possible
> to declare a JNDI datasource with tomcat so that all my webapps can
> access this datasource.
Forget about Tomcat for a moment - Tomcat is not an OSGi web container.
You can write a tiny bundle that creates a datasource (in plain old Java
code) and then registers this datasource as an OSGi service.
Aries JNDI assigns a JNDI name to all OSGi services, so your persistence
bundle can simply access the datasource by a JNDI name like
osgi:service/javax.sql.DataSource, without depending on any driver and
connection details.
Some more pointers:
Simple sample with OSGi + Derby + OpenJPA + Aries JNDI + Aries JPA:
http://code.google.com/p/osgi-enterprise/source/browse/#hg%2Fjpa
Aries Blog Sample
http://aries.apache.org/modules/samples/blog-sample.html
Aries JNDI:
http://aries.apache.org/modules/jndiproject.html
Hope that helps,
Harald
Re: declaring a JNDI datasource
Posted by Harald Wellmann <ha...@gmx.de>.
Am 30.06.2011 08:55, schrieb Daniel Plappert:
>
> What I understood so far is: I have to create a service that provides
> the datasource. But, what if the datasource has to be configured by
> someone else? Not me, for e.g. if I want to "sell" an application
> based on OSGI, the datasource must be configured by the buyer's
> admin, not me. That's why I came up with tomcat (I know it's not an
> OSGI web container). With tomcat the admin can declare a jdbc
> datasource which can be accessed via JNDI in the webapp. My question
> is, is it possible to do something similar with OSGI? The datasource
> must be configurable.
>
Sure - you can configure a datasource in any Java EE web container, but
the Java EE spec does not define the implementation details. Tomcat's
configuration doesn't work for GlassFish, and vice versa.
The OSGi specs do not define how to configure the OSGi container or
individual services, but they do define a Configuration Admin service
which lets you configure services at runtime.
See this presentation for an overview (in German):
http://www.iks-gmbh.com/files/pdf/jax09_osgi_vortrag_der_iks.pdf
The OSGi Enterprise Spec defines a JDBC Service providing a
DataSourceFactory. (This is not implemented in Aries AFAIK. Eclipse
Gemini has a JDBC Service, but they only support Derby out of the box,
and you have to write our own provider for other JDBC drivers, as most
of them are not OSGi-ready.)
> Is creating i.e. a database.properties file which is read by my OSGI
> service the only way to achieve this goal?
>
Certainly not the only way, but maybe the easiest one. I like my
applications to have one central config file (properties or XML, as the
case may be), so why not put the datasource properties into the same
file with the rest of your configuration rather than using a different
(albeit standardized) method to configure your datasource.
The "best" solution depends on how flexible your system is required to
be. Should your customers be able to switch between PostgreSQL, MySQL
and Derby? Then they need to select and configure the appropriate JDBC
driver, and the JDBC Service might help.
Do you need the full dynamics of OSGi, with bundles coming and going at
runtime? Then a central config file may not work...
Regards,
Harald
Re: declaring a JNDI datasource
Posted by Daniel Plappert <pl...@denkformat.de>.
Hi Harald,
Thanks for your reply.
What I understood so far is: I have to create a service that provides the datasource. But, what if the datasource has to be configured by someone else? Not me, for e.g. if I want to "sell" an application based on OSGI, the datasource must be configured by the buyer's admin, not me. That's why I came up with tomcat (I know it's not an OSGI web container). With tomcat the admin can declare a jdbc datasource which can be accessed via JNDI in the webapp. My question is, is it possible to do something similar with OSGI? The datasource must be configurable.
Is creating i.e. a database.properties file which is read by my OSGI service the only way to achieve this goal?
I hope I could explain what my current problem is. Thanks again for your help!
Daniel
Am 29.06.2011 um 00:34 schrieb Harald Wellmann:
> Hi Daniel,
>
> there's a tutorial for setting up Aries + OpenJPA + JNDI + Datasource I wrote last autumn:
>
> http://hwellmann.blogspot.com/2010/09/openjpa-and-osgi.html
>
> The version numbers are out of date by now, but if you know the basics about JPA in classic web containers like Tomcat, this post may help you to understand some of the differences when using JPA with OSGi and Aries.
>
> Am 28.06.2011 16:40, schrieb Daniel Plappert:
>> I am new to OSGI and currently trying to understand its mechanism. I
>> want to create a service (bundle), which creates a JDNI datasource
>> and provides this datasource to other services. What I've read so far
>> is, OSGI "itself" (felix/equinox) can't use JNDI, but aries can.
>> That's why I am asking.
>>
>
> OSGi Core doesn't know about JNDI. The OSGi Enterprise Spec defines an OSGi JNDI service, and Aries provides an implementation.
>
>
>> What I am trying to do is to provide a flexible service, let's say a
>> JDNIDatasourceService. This service creates a JNDI datasource, which
>> connection parameters can be configured "from the outside".
>
> JNDI datasource? You mean a JDBC datasource registered in a JNDI naming context...
>
> > I don't
>> what to deploy the bundle with a persistence.xml, in which the
>> username, password and so on are hard coded. What I want is the same
>> possible mechanism like i.e. a tomcat server provides. It is possible
>> to declare a JNDI datasource with tomcat so that all my webapps can
>> access this datasource.
>
> Forget about Tomcat for a moment - Tomcat is not an OSGi web container.
>
> You can write a tiny bundle that creates a datasource (in plain old Java code) and then registers this datasource as an OSGi service.
>
> Aries JNDI assigns a JNDI name to all OSGi services, so your persistence bundle can simply access the datasource by a JNDI name like osgi:service/javax.sql.DataSource, without depending on any driver and connection details.
>
> Some more pointers:
>
> Simple sample with OSGi + Derby + OpenJPA + Aries JNDI + Aries JPA:
> http://code.google.com/p/osgi-enterprise/source/browse/#hg%2Fjpa
>
> Aries Blog Sample
> http://aries.apache.org/modules/samples/blog-sample.html
>
> Aries JNDI:
> http://aries.apache.org/modules/jndiproject.html
>
> Hope that helps,
> Harald
>