You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by elMateo <so...@gmail.com> on 2012/03/16 10:10:45 UTC

Connect a bundle to a data base

Hello all,

I need to connect a bundle to a MySQL data base and I don't know how I can
do it.

I know that I can't do the same that in a normal Java application, but I
don't find the correct form to do this.

Could anybody help me?

Thanks in advance, Jesus

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
Am 21.03.2012 19:38, schrieb Donald Whytock:
> Understood. The biggest argument for what I described is probably 
> getting away from third-party packages. I tend to go barebones to 
> avoid complicated dependencies and keep my footprint small. Of course, 
> if someone wrote something like this and it became sufficiently 
> established, it would be a third-party package itself... Don 
That was also one my concerns ... and blueprint is at least standardized.

Christian

-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by Donald Whytock <dw...@gmail.com>.
On Wed, Mar 21, 2012 at 12:17 PM, Christian Schneider
<ch...@die-schneider.net> wrote:
> I also wondered if I should start writing something like this. The problem
> is that DataSources and in the same way jms ConnectionFactories are
> configured
> very differently. It is not only a url and usernam, password in some cases.
> So this can be quite complicated.

It can be done somewhat generically, with the service either having
custom configuration methods for each DBMS, or alternately having a
generic method that returns a configuration POJO structured for the
DBMS.

It can also be done similar to how Apache Camel does it, with a fully
configurable URI; the required driver would be the resource type, and
whatever values are needed can be specified as URI parameters.  Of
course, Camel has an endpoint pool and a locator to connect user to
endpoint.

For a first-pass solution, I think I'd go with the POJO.

> This solution is extremly simple and I think good enough for most
> situations. So while it would be nice to have a service like you described I
> did not really miss it till now.

Understood.  The biggest argument for what I described is probably
getting away from third-party packages.  I tend to go barebones to
avoid complicated dependencies and keep my footprint small.

Of course, if someone wrote something like this and it became
sufficiently established, it would be a third-party package itself...

Don

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
I also wondered if I should start writing something like this. The 
problem is that DataSources and in the same way jms ConnectionFactories 
are configured
very differently. It is not only a url and usernam, password in some 
cases. So this can be quite complicated.

I found a very elegant solution using blueprint and the karaf blueprint 
deployer though. You can define the DataSource bean in the blueprint 
file and export it as a service.
Blueprint is flexible enough so you can define almost any DataSource 
like this. The Karaf blueprint deployer then uses bnd to create the 
necessary package imports on the fly.
It is even possible to externalize username / password with the config 
admin service but as the blueprint file can be changed on the fly and is 
small I did not even do that.

I have put some examples on github:
https://github.com/cschneider/Karaf-Tutorial/tree/master/db/datasource

This solution is extremly simple and I think good enough for most 
situations. So while it would be nice to have a service like you 
described I did not really miss it till now.

Christian

Am 21.03.2012 14:38, schrieb Donald Whytock:
> What you probably want in the long run is a datasource-generating
> service, to which you pass the database URI.  You can put things in
> the properties of the service to indicate whether it's Oracle, mySQL,
> MSSQL, etc., so that you can have multiple generating services at the
> same time.
>
> Don
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


-- 

Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by je...@gmail.com.
Hi...
Your driver class is not visible from the bundle where you want to invoke dynamically (class.forname) the class.....logic....
Exposing a real osgi service as suggested wisely by neil is the only simple way to avoid that error...

Jerome
---- Envoyé avec BlackBerry® d'Orange ----

-----Original Message-----
From: elMateo <so...@gmail.com>
Date: Wed, 21 Mar 2012 18:35:03 
To: <us...@felix.apache.org>
Reply-To: users@felix.apache.org
Subject: Re: Connect a bundle to a data base

I'm struggling with this exception of
Apache java.lang.NoClassDefFoundError:
com/mysql/jdbc/jdbc2/optional/MysqlDataSource. Because I have the MySQL
Connect/J bundle running in Apache Felix. Sometimes I have another
exception, java.lang.NoClassDefFoundError: javax/sql/DataSource, and I
don't know why :(

On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> The solution described by Neil should allow that and should be easy to
> implement. Using the service to connect your other bundles to the
> datasource even makes them
> very indepentent of the implementation of the "bridge bundle". A user
> bundle could import the DataSource from my solution with blueprint or the
> handwritten java bridge bundle
> and would not have to be changed in any way.
>
> What exactly are you struggling with?
>
> Christian
>
> Am 21.03.2012 15:43, schrieb elMateo:
>
>  What I want it's only a bundle that work as a bridge to  whatever
>> database.
>> The other bundles access to this one to retrieve any information from
>> external databases. But I don't get this :(
>>
>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>  wrote:
>>
>>  What you probably want in the long run is a datasource-generating
>>> service, to which you pass the database URI.  You can put things in
>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>> MSSQL, etc., so that you can have multiple generating services at the
>>> same time.
>>>
>>> Don
>>>
>>> ------------------------------**------------------------------**
>>> ---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>
> --
>
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
Thanks a lot for this!!

I think that my problem is with config files

Best regards!

On Thu, Mar 22, 2012 at 5:39 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> As this may be interesting for others too I have created an example on how
> to instantiate a DataSource and publish it as a service.
> I also used the config admin service so you can define the properties
> outside of the bundle.
>
> See
> https://github.com/cschneider/**Karaf-Tutorial/tree/master/db/**
> datasource-mysql<https://github.com/cschneider/Karaf-Tutorial/tree/master/db/datasource-mysql>
>
> As you see I did not have to define much for the maven bundle plugin.
>
> Christian
>
>
> Am 22.03.2012 15:56, schrieb elMateo:
>
>> ManifestLocation is for indicate exactly where is the manisfest file.
>>  Removeheader is used for one of the test to try the application, it isn't
>> important.
>>
>> On Thu, Mar 22, 2012 at 1:29 PM, Christian Schneider<
>> chris@die-schneider.net>  wrote:
>>
>>  Some questions /remarks:
>>>
>>> Why do you use<manifestLocation>  and<_removeheaders>? I never used those
>>> till now.
>>> I think you do not have to export any packages as you just provide a
>>> service.
>>>
>>>  Ok, I'm going to test this.
>>
>>  Do you initialize the DataSource in java code? If yes then you probably
>>> do
>>> not need any dynamic imports and you typically do not need to define
>>> Import-Package as the plugin normally does the correct thing.
>>>
>>>  Yes, I do this: DataSource dataSource = new
>> com.mysql.jdbc.jdbc2.optional.**MysqlDataSource();
>>
>>  Can you upload your whole project somewhere?
>>>
>>>  Sorry, I don't have any server to upload my project. But my Activator
>> class
>> only do this:
>>
>>                   DataSource dataSource = new
>> com.mysql.jdbc.jdbc2.optional.**MysqlDataSource();
>>                   registration =
>> bc.registerService(DataSource.**class.getName(), dataSource, null);
>>
>> And when I start the bundle, Apache Felix show me exceptions write above.
>>
>> Thanks a lot for yours answers :) !
>>
>>  Christian
>>>
>>>
>>>
>>>
>>>
>>> Am 22.03.2012 11:50, schrieb elMateo:
>>>
>>>  This is an extract of my pom file:
>>>>
>>>>            <plugin>
>>>> <groupId>org.apache.felix</****groupId>
>>>> <artifactId>maven-bundle-****plugin</artifactId>
>>>> <extensions>true</extensions>
>>>> <configuration>
>>>> <manifestLocation>META-INF</****manifestLocation>
>>>> <instructions>
>>>> <_removeheaders>Ignore-****Package</_removeheaders>
>>>> <Bundle-SymbolicName>****bundleDBConnector</Bundle-****SymbolicName>
>>>> <Bundle-Name>****bundleDBConnector</Bundle-****Name>
>>>> <Bundle-Activator>com.****database.bundle.Activator</****
>>>> Bundle-Activator>
>>>> <Export-Package>com.database.****bundle;-split-package:=merge-****
>>>> last</Export-Package>
>>>> <Import-Package>org.osgi.****framework</Import-Package>
>>>> <DynamicImport-Package>javax.****sql.*,com.mysql.jdbc.jdbc2.**
>>>> optional.*</DynamicImport-****Package>
>>>>
>>>> </instructions>
>>>> </configuration>
>>>> </plugin>
>>>>
>>>> Is it correct?? Uff, still it appears the same exception :(
>>>>
>>>> On Thu, Mar 22, 2012 at 10:23 AM, elMateo<so...@gmail.com>
>>>> wrote:
>>>>
>>>>  Yes, I'm developing my application with Maven. I'm going to read this
>>>>
>>>>> tutorial. Maybe I'm doing something wrong
>>>>>
>>>>>
>>>>> On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider<
>>>>> chris@die-schneider.net>   wrote:
>>>>>
>>>>>  In the bundle that creates the DataSource you have to have an
>>>>>
>>>>>> Import-Package statement in the Manifest for each package you use.
>>>>>> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is
>>>>>> quite
>>>>>> hard to do that by hand. If you use maven to build your bundle then
>>>>>> you should use the maven bundle plugin which analyzes your java code
>>>>>> and
>>>>>> creates the necessary statements for the Manifest.
>>>>>>
>>>>>> In this tutorial I describe how to do it:
>>>>>> http://www.liquid-reality.de/******display/liquid/2011/02/15/****<http://www.liquid-reality.de/****display/liquid/2011/02/15/**>
>>>>>> <http://www.liquid-reality.**de/**display/liquid/2011/02/**15/**<http://www.liquid-reality.de/**display/liquid/2011/02/15/**>
>>>>>> >
>>>>>> Karaf+Tutorial+Part+1+-+******Installation+and+First+******
>>>>>> application<
>>>>>> http://www.liquid-**reality.**de/display/liquid/**<http://reality.de/display/liquid/**>
>>>>>> 2011/02/15/Karaf+Tutorial+****Part+1+-+Installation+and+**
>>>>>> First+application<http://www.**liquid-reality.de/display/**
>>>>>> liquid/2011/02/15/Karaf+**Tutorial+Part+1+-+**Installation+and+First+
>>>>>> **application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>>>>>> >
>>>>>>
>>>>>>
>>>>>> The tutorial is about karaf but the bundle creation is the same if you
>>>>>> use pure felix. Only the deployment may be different.
>>>>>>
>>>>>> Christian
>>>>>>
>>>>>>
>>>>>> Am 21.03.2012 18:35, schrieb elMateo:
>>>>>>
>>>>>>  I'm struggling with this exception of
>>>>>>
>>>>>>> Apache java.lang.******NoClassDefFoundError:
>>>>>>> com/mysql/jdbc/jdbc2/optional/******MysqlDataSource. Because I have
>>>>>>> the
>>>>>>>
>>>>>>>
>>>>>>> MySQL
>>>>>>> Connect/J bundle running in Apache Felix. Sometimes I have another
>>>>>>> exception, java.lang.******NoClassDefFoundError:
>>>>>>> javax/sql/DataSource,
>>>>>>>
>>>>>>> and I
>>>>>>>
>>>>>>> don't know why :(
>>>>>>>
>>>>>>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>>>>>>> chris@die-schneider.net>    wrote:
>>>>>>>
>>>>>>>  The solution described by Neil should allow that and should be easy
>>>>>>> to
>>>>>>>
>>>>>>>  implement. Using the service to connect your other bundles to the
>>>>>>>> datasource even makes them
>>>>>>>> very indepentent of the implementation of the "bridge bundle". A
>>>>>>>> user
>>>>>>>> bundle could import the DataSource from my solution with blueprint
>>>>>>>> or
>>>>>>>> the
>>>>>>>> handwritten java bridge bundle
>>>>>>>> and would not have to be changed in any way.
>>>>>>>>
>>>>>>>> What exactly are you struggling with?
>>>>>>>>
>>>>>>>> Christian
>>>>>>>>
>>>>>>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>>>>>>
>>>>>>>>  What I want it's only a bundle that work as a bridge to  whatever
>>>>>>>>
>>>>>>>>  database.
>>>>>>>>
>>>>>>>>> The other bundles access to this one to retrieve any information
>>>>>>>>> from
>>>>>>>>> external databases. But I don't get this :(
>>>>>>>>>
>>>>>>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dwhytock@gmail.com
>>>>>>>>> >
>>>>>>>>>  wrote:
>>>>>>>>>
>>>>>>>>>  What you probably want in the long run is a datasource-generating
>>>>>>>>>
>>>>>>>>>  service, to which you pass the database URI.  You can put things
>>>>>>>>> in
>>>>>>>>>
>>>>>>>>>> the properties of the service to indicate whether it's Oracle,
>>>>>>>>>> mySQL,
>>>>>>>>>> MSSQL, etc., so that you can have multiple generating services at
>>>>>>>>>> the
>>>>>>>>>> same time.
>>>>>>>>>>
>>>>>>>>>> Don
>>>>>>>>>>
>>>>>>>>>> ------------------------------********------------------------**
>>>>>>>>>> --**
>>>>>>>>>> --**--**
>>>>>>>>>> ---------
>>>>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.******apac**
>>>>>>>>>> he.org<
>>>>>>>>>> http://apache.org**>
>>>>>>>>>> <users-unsubscribe@**felix.**a**pache.org <http://apache.org><
>>>>>>>>>> http://felix.apache.**org <http://felix.apache.org>><
>>>>>>>>>>
>>>>>>>>>> users-unsubscribe@**felix.**apache.org <http://felix.apache.org><
>>>>>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>>>>>> >
>>>>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>  --
>>>>>>>>>>
>>>>>>>>>>  Christian Schneider
>>>>>>>>>
>>>>>>>> http://www.liquid-reality.de
>>>>>>>>
>>>>>>>> Open Source Architect
>>>>>>>> Talend Application Integration Division http://www.talend.com
>>>>>>>>
>>>>>>>>
>>>>>>>> ------------------------------********------------------------**
>>>>>>>> --**--**
>>>>>>>> --**---------
>>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.******apac**he.org<
>>>>>>>> http://apache.org**>
>>>>>>>> <users-unsubscribe@**felix.**a**pache.org <http://apache.org><
>>>>>>>> http://felix.apache.**org <http://felix.apache.org>><
>>>>>>>>
>>>>>>>> users-unsubscribe@**felix.**apache.org <http://felix.apache.org><
>>>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>>>> >
>>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>  --
>>>>>>>>
>>>>>>> Christian Schneider
>>>>>> http://www.liquid-reality.de
>>>>>>
>>>>>> Open Source Architect
>>>>>> Talend Application Integration Division http://www.talend.com
>>>>>>
>>>>>>
>>>>>> ------------------------------******--------------------------**--**
>>>>>> --**---------
>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.****apac**he.org<
>>>>>> http://apache.org**>
>>>>>> <users-unsubscribe@**felix.**apache.org <http://felix.apache.org><
>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>> >
>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>
>>>>>>
>>>>>>
>>>>>>  --
>>> Christian Schneider
>>> http://www.liquid-reality.de
>>>
>>> Open Source Architect
>>> Talend Application Integration Division http://www.talend.com
>>>
>>>
>>> ------------------------------****----------------------------**
>>> --**---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>> <us...@felix.apache.org>
>>> >
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
As this may be interesting for others too I have created an example on 
how to instantiate a DataSource and publish it as a service.
I also used the config admin service so you can define the properties 
outside of the bundle.

See
https://github.com/cschneider/Karaf-Tutorial/tree/master/db/datasource-mysql

As you see I did not have to define much for the maven bundle plugin.

Christian


Am 22.03.2012 15:56, schrieb elMateo:
> ManifestLocation is for indicate exactly where is the manisfest file.
>   Removeheader is used for one of the test to try the application, it isn't
> important.
>
> On Thu, Mar 22, 2012 at 1:29 PM, Christian Schneider<
> chris@die-schneider.net>  wrote:
>
>> Some questions /remarks:
>>
>> Why do you use<manifestLocation>  and<_removeheaders>? I never used those
>> till now.
>> I think you do not have to export any packages as you just provide a
>> service.
>>
> Ok, I'm going to test this.
>
>> Do you initialize the DataSource in java code? If yes then you probably do
>> not need any dynamic imports and you typically do not need to define
>> Import-Package as the plugin normally does the correct thing.
>>
> Yes, I do this: DataSource dataSource = new
> com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
>
>> Can you upload your whole project somewhere?
>>
> Sorry, I don't have any server to upload my project. But my Activator class
> only do this:
>
>                    DataSource dataSource = new
> com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
>                    registration =
> bc.registerService(DataSource.class.getName(), dataSource, null);
>
> And when I start the bundle, Apache Felix show me exceptions write above.
>
> Thanks a lot for yours answers :) !
>
>> Christian
>>
>>
>>
>>
>>
>> Am 22.03.2012 11:50, schrieb elMateo:
>>
>>> This is an extract of my pom file:
>>>
>>>             <plugin>
>>> <groupId>org.apache.felix</**groupId>
>>> <artifactId>maven-bundle-**plugin</artifactId>
>>> <extensions>true</extensions>
>>> <configuration>
>>> <manifestLocation>META-INF</**manifestLocation>
>>> <instructions>
>>> <_removeheaders>Ignore-**Package</_removeheaders>
>>> <Bundle-SymbolicName>**bundleDBConnector</Bundle-**SymbolicName>
>>> <Bundle-Name>**bundleDBConnector</Bundle-**Name>
>>> <Bundle-Activator>com.**database.bundle.Activator</**Bundle-Activator>
>>> <Export-Package>com.database.**bundle;-split-package:=merge-**
>>> last</Export-Package>
>>> <Import-Package>org.osgi.**framework</Import-Package>
>>> <DynamicImport-Package>javax.**sql.*,com.mysql.jdbc.jdbc2.**
>>> optional.*</DynamicImport-**Package>
>>> </instructions>
>>> </configuration>
>>> </plugin>
>>>
>>> Is it correct?? Uff, still it appears the same exception :(
>>>
>>> On Thu, Mar 22, 2012 at 10:23 AM, elMateo<so...@gmail.com>   wrote:
>>>
>>>   Yes, I'm developing my application with Maven. I'm going to read this
>>>> tutorial. Maybe I'm doing something wrong
>>>>
>>>>
>>>> On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider<
>>>> chris@die-schneider.net>   wrote:
>>>>
>>>>   In the bundle that creates the DataSource you have to have an
>>>>> Import-Package statement in the Manifest for each package you use.
>>>>> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is
>>>>> quite
>>>>> hard to do that by hand. If you use maven to build your bundle then
>>>>> you should use the maven bundle plugin which analyzes your java code and
>>>>> creates the necessary statements for the Manifest.
>>>>>
>>>>> In this tutorial I describe how to do it:
>>>>> http://www.liquid-reality.de/****display/liquid/2011/02/15/**<http://www.liquid-reality.de/**display/liquid/2011/02/15/**>
>>>>> Karaf+Tutorial+Part+1+-+****Installation+and+First+****application<
>>>>> http://www.liquid-**reality.de/display/liquid/**
>>>>> 2011/02/15/Karaf+Tutorial+**Part+1+-+Installation+and+**
>>>>> First+application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>>>>>
>>>>> The tutorial is about karaf but the bundle creation is the same if you
>>>>> use pure felix. Only the deployment may be different.
>>>>>
>>>>> Christian
>>>>>
>>>>>
>>>>> Am 21.03.2012 18:35, schrieb elMateo:
>>>>>
>>>>>   I'm struggling with this exception of
>>>>>> Apache java.lang.****NoClassDefFoundError:
>>>>>> com/mysql/jdbc/jdbc2/optional/****MysqlDataSource. Because I have the
>>>>>>
>>>>>> MySQL
>>>>>> Connect/J bundle running in Apache Felix. Sometimes I have another
>>>>>> exception, java.lang.****NoClassDefFoundError: javax/sql/DataSource,
>>>>>> and I
>>>>>>
>>>>>> don't know why :(
>>>>>>
>>>>>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>>>>>> chris@die-schneider.net>    wrote:
>>>>>>
>>>>>>   The solution described by Neil should allow that and should be easy to
>>>>>>
>>>>>>> implement. Using the service to connect your other bundles to the
>>>>>>> datasource even makes them
>>>>>>> very indepentent of the implementation of the "bridge bundle". A user
>>>>>>> bundle could import the DataSource from my solution with blueprint or
>>>>>>> the
>>>>>>> handwritten java bridge bundle
>>>>>>> and would not have to be changed in any way.
>>>>>>>
>>>>>>> What exactly are you struggling with?
>>>>>>>
>>>>>>> Christian
>>>>>>>
>>>>>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>>>>>
>>>>>>>   What I want it's only a bundle that work as a bridge to  whatever
>>>>>>>
>>>>>>>   database.
>>>>>>>> The other bundles access to this one to retrieve any information from
>>>>>>>> external databases. But I don't get this :(
>>>>>>>>
>>>>>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>>>>>>   wrote:
>>>>>>>>
>>>>>>>>   What you probably want in the long run is a datasource-generating
>>>>>>>>
>>>>>>>>   service, to which you pass the database URI.  You can put things in
>>>>>>>>> the properties of the service to indicate whether it's Oracle,
>>>>>>>>> mySQL,
>>>>>>>>> MSSQL, etc., so that you can have multiple generating services at
>>>>>>>>> the
>>>>>>>>> same time.
>>>>>>>>>
>>>>>>>>> Don
>>>>>>>>>
>>>>>>>>> ------------------------------******--------------------------**
>>>>>>>>> --**--**
>>>>>>>>> ---------
>>>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.****apac**he.org<
>>>>>>>>> http://apache.org**>
>>>>>>>>> <users-unsubscribe@**felix.**apache.org<http://felix.apache.org><
>>>>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>   --
>>>>>>>>>
>>>>>>>> Christian Schneider
>>>>>>> http://www.liquid-reality.de
>>>>>>>
>>>>>>> Open Source Architect
>>>>>>> Talend Application Integration Division http://www.talend.com
>>>>>>>
>>>>>>>
>>>>>>> ------------------------------******--------------------------**--**
>>>>>>> --**---------
>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.****apac**he.org<
>>>>>>> http://apache.org**>
>>>>>>> <users-unsubscribe@**felix.**apache.org<http://felix.apache.org><
>>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>   --
>>>>> Christian Schneider
>>>>> http://www.liquid-reality.de
>>>>>
>>>>> Open Source Architect
>>>>> Talend Application Integration Division http://www.talend.com
>>>>>
>>>>>
>>>>> ------------------------------****----------------------------**
>>>>> --**---------
>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>>> <us...@felix.apache.org>
>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>
>>>>>
>>>>>
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
ManifestLocation is for indicate exactly where is the manisfest file.
 Removeheader is used for one of the test to try the application, it isn't
important.

On Thu, Mar 22, 2012 at 1:29 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> Some questions /remarks:
>
> Why do you use <manifestLocation> and <_removeheaders>? I never used those
> till now.
> I think you do not have to export any packages as you just provide a
> service.
>

Ok, I'm going to test this.

>
> Do you initialize the DataSource in java code? If yes then you probably do
> not need any dynamic imports and you typically do not need to define
> Import-Package as the plugin normally does the correct thing.
>
Yes, I do this: DataSource dataSource = new
com.mysql.jdbc.jdbc2.optional.MysqlDataSource();

>
> Can you upload your whole project somewhere?
>

Sorry, I don't have any server to upload my project. But my Activator class
only do this:

                  DataSource dataSource = new
com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
                  registration =
bc.registerService(DataSource.class.getName(), dataSource, null);

And when I start the bundle, Apache Felix show me exceptions write above.

Thanks a lot for yours answers :) !

>
> Christian
>
>
>
>
>
> Am 22.03.2012 11:50, schrieb elMateo:
>
>> This is an extract of my pom file:
>>
>>            <plugin>
>> <groupId>org.apache.felix</**groupId>
>> <artifactId>maven-bundle-**plugin</artifactId>
>> <extensions>true</extensions>
>> <configuration>
>> <manifestLocation>META-INF</**manifestLocation>
>> <instructions>
>> <_removeheaders>Ignore-**Package</_removeheaders>
>> <Bundle-SymbolicName>**bundleDBConnector</Bundle-**SymbolicName>
>> <Bundle-Name>**bundleDBConnector</Bundle-**Name>
>> <Bundle-Activator>com.**database.bundle.Activator</**Bundle-Activator>
>> <Export-Package>com.database.**bundle;-split-package:=merge-**
>> last</Export-Package>
>> <Import-Package>org.osgi.**framework</Import-Package>
>> <DynamicImport-Package>javax.**sql.*,com.mysql.jdbc.jdbc2.**
>> optional.*</DynamicImport-**Package>
>> </instructions>
>> </configuration>
>> </plugin>
>>
>> Is it correct?? Uff, still it appears the same exception :(
>>
>> On Thu, Mar 22, 2012 at 10:23 AM, elMateo<so...@gmail.com>  wrote:
>>
>>  Yes, I'm developing my application with Maven. I'm going to read this
>>> tutorial. Maybe I'm doing something wrong
>>>
>>>
>>> On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider<
>>> chris@die-schneider.net>  wrote:
>>>
>>>  In the bundle that creates the DataSource you have to have an
>>>> Import-Package statement in the Manifest for each package you use.
>>>> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is
>>>> quite
>>>> hard to do that by hand. If you use maven to build your bundle then
>>>> you should use the maven bundle plugin which analyzes your java code and
>>>> creates the necessary statements for the Manifest.
>>>>
>>>> In this tutorial I describe how to do it:
>>>> http://www.liquid-reality.de/****display/liquid/2011/02/15/**<http://www.liquid-reality.de/**display/liquid/2011/02/15/**>
>>>> Karaf+Tutorial+Part+1+-+****Installation+and+First+****application<
>>>> http://www.liquid-**reality.de/display/liquid/**
>>>> 2011/02/15/Karaf+Tutorial+**Part+1+-+Installation+and+**
>>>> First+application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>>>> >
>>>>
>>>>
>>>> The tutorial is about karaf but the bundle creation is the same if you
>>>> use pure felix. Only the deployment may be different.
>>>>
>>>> Christian
>>>>
>>>>
>>>> Am 21.03.2012 18:35, schrieb elMateo:
>>>>
>>>>  I'm struggling with this exception of
>>>>> Apache java.lang.****NoClassDefFoundError:
>>>>> com/mysql/jdbc/jdbc2/optional/****MysqlDataSource. Because I have the
>>>>>
>>>>> MySQL
>>>>> Connect/J bundle running in Apache Felix. Sometimes I have another
>>>>> exception, java.lang.****NoClassDefFoundError: javax/sql/DataSource,
>>>>> and I
>>>>>
>>>>> don't know why :(
>>>>>
>>>>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>>>>> chris@die-schneider.net>   wrote:
>>>>>
>>>>>  The solution described by Neil should allow that and should be easy to
>>>>>
>>>>>> implement. Using the service to connect your other bundles to the
>>>>>> datasource even makes them
>>>>>> very indepentent of the implementation of the "bridge bundle". A user
>>>>>> bundle could import the DataSource from my solution with blueprint or
>>>>>> the
>>>>>> handwritten java bridge bundle
>>>>>> and would not have to be changed in any way.
>>>>>>
>>>>>> What exactly are you struggling with?
>>>>>>
>>>>>> Christian
>>>>>>
>>>>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>>>>
>>>>>>  What I want it's only a bundle that work as a bridge to  whatever
>>>>>>
>>>>>>  database.
>>>>>>> The other bundles access to this one to retrieve any information from
>>>>>>> external databases. But I don't get this :(
>>>>>>>
>>>>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>>>>>  wrote:
>>>>>>>
>>>>>>>  What you probably want in the long run is a datasource-generating
>>>>>>>
>>>>>>>  service, to which you pass the database URI.  You can put things in
>>>>>>>> the properties of the service to indicate whether it's Oracle,
>>>>>>>> mySQL,
>>>>>>>> MSSQL, etc., so that you can have multiple generating services at
>>>>>>>> the
>>>>>>>> same time.
>>>>>>>>
>>>>>>>> Don
>>>>>>>>
>>>>>>>> ------------------------------******--------------------------**
>>>>>>>> --**--**
>>>>>>>> ---------
>>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.****apac**he.org<
>>>>>>>> http://apache.org**>
>>>>>>>> <users-unsubscribe@**felix.**apache.org <http://felix.apache.org><
>>>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>>>> >
>>>>>>>>
>>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>  --
>>>>>>>>
>>>>>>> Christian Schneider
>>>>>> http://www.liquid-reality.de
>>>>>>
>>>>>> Open Source Architect
>>>>>> Talend Application Integration Division http://www.talend.com
>>>>>>
>>>>>>
>>>>>> ------------------------------******--------------------------**--**
>>>>>> --**---------
>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.****apac**he.org<
>>>>>> http://apache.org**>
>>>>>> <users-unsubscribe@**felix.**apache.org <http://felix.apache.org><
>>>>>> users-unsubscribe@**felix.apache.org<us...@felix.apache.org>
>>>>>> >
>>>>>>
>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>
>>>>>>
>>>>>>
>>>>>>  --
>>>> Christian Schneider
>>>> http://www.liquid-reality.de
>>>>
>>>> Open Source Architect
>>>> Talend Application Integration Division http://www.talend.com
>>>>
>>>>
>>>> ------------------------------****----------------------------**
>>>> --**---------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>> <us...@felix.apache.org>
>>>> >
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
Some questions /remarks:

Why do you use <manifestLocation> and <_removeheaders>? I never used 
those till now.
I think you do not have to export any packages as you just provide a 
service.

Do you initialize the DataSource in java code? If yes then you probably 
do not need any dynamic imports and you typically do not need to define 
Import-Package as the plugin normally does the correct thing.

Can you upload your whole project somewhere?

Christian





Am 22.03.2012 11:50, schrieb elMateo:
> This is an extract of my pom file:
>
>             <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-bundle-plugin</artifactId>
> <extensions>true</extensions>
> <configuration>
> <manifestLocation>META-INF</manifestLocation>
> <instructions>
> <_removeheaders>Ignore-Package</_removeheaders>
> <Bundle-SymbolicName>bundleDBConnector</Bundle-SymbolicName>
> <Bundle-Name>bundleDBConnector</Bundle-Name>
> <Bundle-Activator>com.database.bundle.Activator</Bundle-Activator>
> <Export-Package>com.database.bundle;-split-package:=merge-last</Export-Package>
> <Import-Package>org.osgi.framework</Import-Package>
> <DynamicImport-Package>javax.sql.*,com.mysql.jdbc.jdbc2.optional.*</DynamicImport-Package>
> </instructions>
> </configuration>
> </plugin>
>
> Is it correct?? Uff, still it appears the same exception :(
>
> On Thu, Mar 22, 2012 at 10:23 AM, elMateo<so...@gmail.com>  wrote:
>
>> Yes, I'm developing my application with Maven. I'm going to read this
>> tutorial. Maybe I'm doing something wrong
>>
>>
>> On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider<
>> chris@die-schneider.net>  wrote:
>>
>>> In the bundle that creates the DataSource you have to have an
>>> Import-Package statement in the Manifest for each package you use.
>>> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is quite
>>> hard to do that by hand. If you use maven to build your bundle then
>>> you should use the maven bundle plugin which analyzes your java code and
>>> creates the necessary statements for the Manifest.
>>>
>>> In this tutorial I describe how to do it:
>>> http://www.liquid-reality.de/**display/liquid/2011/02/15/**
>>> Karaf+Tutorial+Part+1+-+**Installation+and+First+**application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>>>
>>> The tutorial is about karaf but the bundle creation is the same if you
>>> use pure felix. Only the deployment may be different.
>>>
>>> Christian
>>>
>>>
>>> Am 21.03.2012 18:35, schrieb elMateo:
>>>
>>>> I'm struggling with this exception of
>>>> Apache java.lang.**NoClassDefFoundError:
>>>> com/mysql/jdbc/jdbc2/optional/**MysqlDataSource. Because I have the
>>>> MySQL
>>>> Connect/J bundle running in Apache Felix. Sometimes I have another
>>>> exception, java.lang.**NoClassDefFoundError: javax/sql/DataSource, and I
>>>> don't know why :(
>>>>
>>>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>>>> chris@die-schneider.net>   wrote:
>>>>
>>>>   The solution described by Neil should allow that and should be easy to
>>>>> implement. Using the service to connect your other bundles to the
>>>>> datasource even makes them
>>>>> very indepentent of the implementation of the "bridge bundle". A user
>>>>> bundle could import the DataSource from my solution with blueprint or
>>>>> the
>>>>> handwritten java bridge bundle
>>>>> and would not have to be changed in any way.
>>>>>
>>>>> What exactly are you struggling with?
>>>>>
>>>>> Christian
>>>>>
>>>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>>>
>>>>>   What I want it's only a bundle that work as a bridge to  whatever
>>>>>
>>>>>> database.
>>>>>> The other bundles access to this one to retrieve any information from
>>>>>> external databases. But I don't get this :(
>>>>>>
>>>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>>>>   wrote:
>>>>>>
>>>>>>   What you probably want in the long run is a datasource-generating
>>>>>>
>>>>>>> service, to which you pass the database URI.  You can put things in
>>>>>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>>>>>> MSSQL, etc., so that you can have multiple generating services at the
>>>>>>> same time.
>>>>>>>
>>>>>>> Don
>>>>>>>
>>>>>>> ------------------------------****----------------------------**--**
>>>>>>> ---------
>>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>>>>> <us...@felix.apache.org>
>>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>   --
>>>>> Christian Schneider
>>>>> http://www.liquid-reality.de
>>>>>
>>>>> Open Source Architect
>>>>> Talend Application Integration Division http://www.talend.com
>>>>>
>>>>>
>>>>> ------------------------------****----------------------------**
>>>>> --**---------
>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>>> <us...@felix.apache.org>
>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>
>>>>>
>>>>>
>>> --
>>> Christian Schneider
>>> http://www.liquid-reality.de
>>>
>>> Open Source Architect
>>> Talend Application Integration Division http://www.talend.com
>>>
>>>
>>> ------------------------------**------------------------------**---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
This is an extract of my pom file:

           <plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<manifestLocation>META-INF</manifestLocation>
<instructions>
<_removeheaders>Ignore-Package</_removeheaders>
<Bundle-SymbolicName>bundleDBConnector</Bundle-SymbolicName>
<Bundle-Name>bundleDBConnector</Bundle-Name>
<Bundle-Activator>com.database.bundle.Activator</Bundle-Activator>
<Export-Package>com.database.bundle;-split-package:=merge-last</Export-Package>
<Import-Package>org.osgi.framework</Import-Package>
<DynamicImport-Package>javax.sql.*,com.mysql.jdbc.jdbc2.optional.*</DynamicImport-Package>
</instructions>
</configuration>
</plugin>

Is it correct?? Uff, still it appears the same exception :(

On Thu, Mar 22, 2012 at 10:23 AM, elMateo <so...@gmail.com> wrote:

> Yes, I'm developing my application with Maven. I'm going to read this
> tutorial. Maybe I'm doing something wrong
>
>
> On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider <
> chris@die-schneider.net> wrote:
>
>> In the bundle that creates the DataSource you have to have an
>> Import-Package statement in the Manifest for each package you use.
>> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is quite
>> hard to do that by hand. If you use maven to build your bundle then
>> you should use the maven bundle plugin which analyzes your java code and
>> creates the necessary statements for the Manifest.
>>
>> In this tutorial I describe how to do it:
>> http://www.liquid-reality.de/**display/liquid/2011/02/15/**
>> Karaf+Tutorial+Part+1+-+**Installation+and+First+**application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>>
>> The tutorial is about karaf but the bundle creation is the same if you
>> use pure felix. Only the deployment may be different.
>>
>> Christian
>>
>>
>> Am 21.03.2012 18:35, schrieb elMateo:
>>
>>> I'm struggling with this exception of
>>> Apache java.lang.**NoClassDefFoundError:
>>> com/mysql/jdbc/jdbc2/optional/**MysqlDataSource. Because I have the
>>> MySQL
>>> Connect/J bundle running in Apache Felix. Sometimes I have another
>>> exception, java.lang.**NoClassDefFoundError: javax/sql/DataSource, and I
>>> don't know why :(
>>>
>>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>>> chris@die-schneider.net>  wrote:
>>>
>>>  The solution described by Neil should allow that and should be easy to
>>>> implement. Using the service to connect your other bundles to the
>>>> datasource even makes them
>>>> very indepentent of the implementation of the "bridge bundle". A user
>>>> bundle could import the DataSource from my solution with blueprint or
>>>> the
>>>> handwritten java bridge bundle
>>>> and would not have to be changed in any way.
>>>>
>>>> What exactly are you struggling with?
>>>>
>>>> Christian
>>>>
>>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>>
>>>>  What I want it's only a bundle that work as a bridge to  whatever
>>>>
>>>>> database.
>>>>> The other bundles access to this one to retrieve any information from
>>>>> external databases. But I don't get this :(
>>>>>
>>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>>>  wrote:
>>>>>
>>>>>  What you probably want in the long run is a datasource-generating
>>>>>
>>>>>> service, to which you pass the database URI.  You can put things in
>>>>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>>>>> MSSQL, etc., so that you can have multiple generating services at the
>>>>>> same time.
>>>>>>
>>>>>> Don
>>>>>>
>>>>>> ------------------------------****----------------------------**--**
>>>>>> ---------
>>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>>>> <us...@felix.apache.org>
>>>>>> >
>>>>>>
>>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>>
>>>>>>
>>>>>>
>>>>>>  --
>>>>
>>>> Christian Schneider
>>>> http://www.liquid-reality.de
>>>>
>>>> Open Source Architect
>>>> Talend Application Integration Division http://www.talend.com
>>>>
>>>>
>>>> ------------------------------****----------------------------**
>>>> --**---------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>> <us...@felix.apache.org>
>>>> >
>>>>
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>>
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>

Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
Yes, I'm developing my application with Maven. I'm going to read this
tutorial. Maybe I'm doing something wrong

On Wed, Mar 21, 2012 at 7:43 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> In the bundle that creates the DataSource you have to have an
> Import-Package statement in the Manifest for each package you use.
> So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is quite
> hard to do that by hand. If you use maven to build your bundle then
> you should use the maven bundle plugin which analyzes your java code and
> creates the necessary statements for the Manifest.
>
> In this tutorial I describe how to do it:
> http://www.liquid-reality.de/**display/liquid/2011/02/15/**
> Karaf+Tutorial+Part+1+-+**Installation+and+First+**application<http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application>
>
> The tutorial is about karaf but the bundle creation is the same if you use
> pure felix. Only the deployment may be different.
>
> Christian
>
>
> Am 21.03.2012 18:35, schrieb elMateo:
>
>> I'm struggling with this exception of
>> Apache java.lang.**NoClassDefFoundError:
>> com/mysql/jdbc/jdbc2/optional/**MysqlDataSource. Because I have the MySQL
>> Connect/J bundle running in Apache Felix. Sometimes I have another
>> exception, java.lang.**NoClassDefFoundError: javax/sql/DataSource, and I
>> don't know why :(
>>
>> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
>> chris@die-schneider.net>  wrote:
>>
>>  The solution described by Neil should allow that and should be easy to
>>> implement. Using the service to connect your other bundles to the
>>> datasource even makes them
>>> very indepentent of the implementation of the "bridge bundle". A user
>>> bundle could import the DataSource from my solution with blueprint or the
>>> handwritten java bridge bundle
>>> and would not have to be changed in any way.
>>>
>>> What exactly are you struggling with?
>>>
>>> Christian
>>>
>>> Am 21.03.2012 15:43, schrieb elMateo:
>>>
>>>  What I want it's only a bundle that work as a bridge to  whatever
>>>
>>>> database.
>>>> The other bundles access to this one to retrieve any information from
>>>> external databases. But I don't get this :(
>>>>
>>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>>  wrote:
>>>>
>>>>  What you probably want in the long run is a datasource-generating
>>>>
>>>>> service, to which you pass the database URI.  You can put things in
>>>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>>>> MSSQL, etc., so that you can have multiple generating services at the
>>>>> same time.
>>>>>
>>>>> Don
>>>>>
>>>>> ------------------------------****----------------------------**--**
>>>>> ---------
>>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>>>> <us...@felix.apache.org>
>>>>> >
>>>>>
>>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>>
>>>>>
>>>>>
>>>>>  --
>>>
>>> Christian Schneider
>>> http://www.liquid-reality.de
>>>
>>> Open Source Architect
>>> Talend Application Integration Division http://www.talend.com
>>>
>>>
>>> ------------------------------****----------------------------**
>>> --**---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>> <us...@felix.apache.org>
>>> >
>>>
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
In the bundle that creates the DataSource you have to have an 
Import-Package statement in the Manifest for each package you use.
So at least com.mysql.jdbc.optional and javax.sql.DataSource. It is 
quite hard to do that by hand. If you use maven to build your bundle then
you should use the maven bundle plugin which analyzes your java code and 
creates the necessary statements for the Manifest.

In this tutorial I describe how to do it:
http://www.liquid-reality.de/display/liquid/2011/02/15/Karaf+Tutorial+Part+1+-+Installation+and+First+application

The tutorial is about karaf but the bundle creation is the same if you 
use pure felix. Only the deployment may be different.

Christian


Am 21.03.2012 18:35, schrieb elMateo:
> I'm struggling with this exception of
> Apache java.lang.NoClassDefFoundError:
> com/mysql/jdbc/jdbc2/optional/MysqlDataSource. Because I have the MySQL
> Connect/J bundle running in Apache Felix. Sometimes I have another
> exception, java.lang.NoClassDefFoundError: javax/sql/DataSource, and I
> don't know why :(
>
> On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider<
> chris@die-schneider.net>  wrote:
>
>> The solution described by Neil should allow that and should be easy to
>> implement. Using the service to connect your other bundles to the
>> datasource even makes them
>> very indepentent of the implementation of the "bridge bundle". A user
>> bundle could import the DataSource from my solution with blueprint or the
>> handwritten java bridge bundle
>> and would not have to be changed in any way.
>>
>> What exactly are you struggling with?
>>
>> Christian
>>
>> Am 21.03.2012 15:43, schrieb elMateo:
>>
>>   What I want it's only a bundle that work as a bridge to  whatever
>>> database.
>>> The other bundles access to this one to retrieve any information from
>>> external databases. But I don't get this :(
>>>
>>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>>   wrote:
>>>
>>>   What you probably want in the long run is a datasource-generating
>>>> service, to which you pass the database URI.  You can put things in
>>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>>> MSSQL, etc., so that you can have multiple generating services at the
>>>> same time.
>>>>
>>>> Don
>>>>
>>>> ------------------------------**------------------------------**
>>>> ---------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>> --
>>
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
I'm struggling with this exception of
Apache java.lang.NoClassDefFoundError:
com/mysql/jdbc/jdbc2/optional/MysqlDataSource. Because I have the MySQL
Connect/J bundle running in Apache Felix. Sometimes I have another
exception, java.lang.NoClassDefFoundError: javax/sql/DataSource, and I
don't know why :(

On Wed, Mar 21, 2012 at 5:20 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> The solution described by Neil should allow that and should be easy to
> implement. Using the service to connect your other bundles to the
> datasource even makes them
> very indepentent of the implementation of the "bridge bundle". A user
> bundle could import the DataSource from my solution with blueprint or the
> handwritten java bridge bundle
> and would not have to be changed in any way.
>
> What exactly are you struggling with?
>
> Christian
>
> Am 21.03.2012 15:43, schrieb elMateo:
>
>  What I want it's only a bundle that work as a bridge to  whatever
>> database.
>> The other bundles access to this one to retrieve any information from
>> external databases. But I don't get this :(
>>
>> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>
>>  wrote:
>>
>>  What you probably want in the long run is a datasource-generating
>>> service, to which you pass the database URI.  You can put things in
>>> the properties of the service to indicate whether it's Oracle, mySQL,
>>> MSSQL, etc., so that you can have multiple generating services at the
>>> same time.
>>>
>>> Don
>>>
>>> ------------------------------**------------------------------**
>>> ---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>
> --
>
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
The solution described by Neil should allow that and should be easy to 
implement. Using the service to connect your other bundles to the 
datasource even makes them
very indepentent of the implementation of the "bridge bundle". A user 
bundle could import the DataSource from my solution with blueprint or 
the handwritten java bridge bundle
and would not have to be changed in any way.

What exactly are you struggling with?

Christian

Am 21.03.2012 15:43, schrieb elMateo:
> What I want it's only a bundle that work as a bridge to  whatever database.
> The other bundles access to this one to retrieve any information from
> external databases. But I don't get this :(
>
> On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock<dw...@gmail.com>  wrote:
>
>> What you probably want in the long run is a datasource-generating
>> service, to which you pass the database URI.  You can put things in
>> the properties of the service to indicate whether it's Oracle, mySQL,
>> MSSQL, etc., so that you can have multiple generating services at the
>> same time.
>>
>> Don
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>


-- 

Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
What I want it's only a bundle that work as a bridge to  whatever database.
The other bundles access to this one to retrieve any information from
external databases. But I don't get this :(

On Wed, Mar 21, 2012 at 2:38 PM, Donald Whytock <dw...@gmail.com> wrote:

> What you probably want in the long run is a datasource-generating
> service, to which you pass the database URI.  You can put things in
> the properties of the service to indicate whether it's Oracle, mySQL,
> MSSQL, etc., so that you can have multiple generating services at the
> same time.
>
> Don
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Donald Whytock <dw...@gmail.com>.
What you probably want in the long run is a datasource-generating
service, to which you pass the database URI.  You can put things in
the properties of the service to indicate whether it's Oracle, mySQL,
MSSQL, etc., so that you can have multiple generating services at the
same time.

Don

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by Neil Bartlett <nj...@gmail.com>.
You DO need to import that package "javax.sql" (the rule is that you import ALL packages you use except java.*, e.g. you don't import java.lang, java.io, java.util etc). 

This specific package will be exported by the System Bundle, i.e. the OSGi framework itself. The same is true for all the javax.* packages that are normally offered by the JRE.

Regards,
Neil


On Wednesday, 21 March 2012 at 10:02, elMateo wrote:

> I'm working with the code of Neil and I have a problem: Felix show a
> java.lang.ClassNotFoundException: javax.sql.DataSource not found
> 
> I import the javax.sql package in my manifest file but I don't know if I
> need a bundle with this package in my Apache Felix. If I need a bundle with
> this, what is this bundle? I don't find a bundle called javax.sql and I
> have imported the MySQL connector/J bundle too.
> 
> On Tue, Mar 20, 2012 at 1:13 PM, Christian Schneider <
> chris@die-schneider.net (mailto:chris@die-schneider.net)> wrote:
> 
> > Absolutely. That is what I wanted to say in my last post. The only problem
> > with this aproach is that you hardcode the database type in your bridge
> > bundle.
> > ( At least if you dont want to mess with the classloader).
> > 
> > As you probably will not have that many database types in one use case
> > this is probably ok though.
> > 
> > Christian
> > 
> > 
> > Am 20.03.2012 11:27, schrieb Neil Bartlett:
> > 
> > You certainly don't need Glassfish... you don't need Karaf or Blueprint
> > > either.
> > > 
> > > There is a tendency on many mailing lists to respond to beginner
> > > questions with answers like "you can do this with product X" where X
> > > happens to be the product that the person is working on or selling. Those
> > > products may indeed add value but they are rarely the only or the simplest
> > > way to achieve what you want!
> > > 
> > > To register a DataSource service with pure OSGi, it's as simple as
> > > writing this code in a BundleActivator (using MySQL as an example):
> > > 
> > > DataSource ds = new com.mysql.jdbc.jdbc2.optional.**MysqlDataSource();
> > > context.registerService(**DataSource.class.getName(), ds, null);
> > > 
> > > This would be your "bridge" bundle that you deploy alongside the MySQL
> > > driver JAR. For other database types, write a different 2 lines of code and
> > > deploy that bundle instead.
> > > 
> > > Kind regards,
> > > Neil
> > > 
> > 
> > --
> > Christian Schneider
> > http://www.liquid-reality.de
> > 
> > Open Source Architect
> > Talend Application Integration Division http://www.talend.com
> > 
> > 
> > ------------------------------**------------------------------**---------
> > To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<users-unsubscribe@felix.apache.org (mailto:users-unsubscribe@felix.apache.org)>
> > For additional commands, e-mail: users-help@felix.apache.org (mailto:users-help@felix.apache.org)
> > 
> 
> 
> 



Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
I'm working with the code of Neil and I have a problem: Felix show a
java.lang.ClassNotFoundException: javax.sql.DataSource not found

I import the javax.sql package in my manifest file but I don't know if I
need a bundle with this package in my Apache Felix. If I need a bundle with
this, what is this bundle? I don't find a bundle called javax.sql and I
have imported the MySQL connector/J bundle too.

On Tue, Mar 20, 2012 at 1:13 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> Absolutely. That is what I wanted to say in my last post. The only problem
> with this aproach is that you hardcode the database type in your bridge
> bundle.
> ( At least if you dont want to mess with the classloader).
>
> As you probably will not have that many database types in one use case
> this is probably ok though.
>
> Christian
>
>
> Am 20.03.2012 11:27, schrieb Neil Bartlett:
>
>  You certainly don't need Glassfish... you don't need Karaf or Blueprint
>> either.
>>
>> There is a tendency on many mailing lists to respond to beginner
>> questions with answers like "you can do this with product X" where X
>> happens to be the product that the person is working on or selling. Those
>> products may indeed add value but they are rarely the only or the simplest
>> way to achieve what you want!
>>
>> To register a DataSource service with pure OSGi, it's as simple as
>> writing this code in a BundleActivator (using MySQL as an example):
>>
>> DataSource ds = new com.mysql.jdbc.jdbc2.optional.**MysqlDataSource();
>> context.registerService(**DataSource.class.getName(), ds, null);
>>
>> This would be your "bridge" bundle that you deploy alongside the MySQL
>> driver JAR. For other database types, write a different 2 lines of code and
>> deploy that bundle instead.
>>
>> Kind regards,
>> Neil
>>
>>
>>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
Absolutely. That is what I wanted to say in my last post. The only 
problem with this aproach is that you hardcode the database type in your 
bridge bundle.
( At least if you dont want to mess with the classloader).

As you probably will not have that many database types in one use case 
this is probably ok though.

Christian


Am 20.03.2012 11:27, schrieb Neil Bartlett:
> You certainly don't need Glassfish... you don't need Karaf or Blueprint either.
>
> There is a tendency on many mailing lists to respond to beginner questions with answers like "you can do this with product X" where X happens to be the product that the person is working on or selling. Those products may indeed add value but they are rarely the only or the simplest way to achieve what you want!
>
> To register a DataSource service with pure OSGi, it's as simple as writing this code in a BundleActivator (using MySQL as an example):
>
> DataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
> context.registerService(DataSource.class.getName(), ds, null);
>
> This would be your "bridge" bundle that you deploy alongside the MySQL driver JAR. For other database types, write a different 2 lines of code and deploy that bundle instead.
>
> Kind regards,
> Neil
>
>

-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
Thanks!!  I think that this is a very good tendency. All that it is
writting in this post is very interesting and I'm reading with a lot
of enthusiasm.
And it is a very good way to learn about other tools that I could use in
the future :)

I'm going to test this code

On Tue, Mar 20, 2012 at 11:27 AM, Neil Bartlett <nj...@gmail.com>wrote:

> You certainly don't need Glassfish... you don't need Karaf or Blueprint
> either.
>
> There is a tendency on many mailing lists to respond to beginner questions
> with answers like "you can do this with product X" where X happens to be
> the product that the person is working on or selling. Those products may
> indeed add value but they are rarely the only or the simplest way to
> achieve what you want!
>



> To register a DataSource service with pure OSGi, it's as simple as writing
> this code in a BundleActivator (using MySQL as an example):
>
> DataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
> context.registerService(DataSource.class.getName(), ds, null);
>
> This would be your "bridge" bundle that you deploy alongside the MySQL
> driver JAR. For other database types, write a different 2 lines of code and
> deploy that bundle instead.
>
> Kind regards,
> Neil
>
>
>
> On Tuesday, 20 March 2012 at 09:58, elMateo wrote:
>
> > Sorry for my ignorance, but why does it need a Glassfish server?
> >
> > If I only want to access to a database through a bundle, Is necessary a
> > server?
> >
> > On Sat, Mar 17, 2012 at 10:25 PM, Peter Penzov <peter.penzov@gmail.com(mailto:
> peter.penzov@gmail.com)>wrote:
> >
> > > I use Glassfish and Apache Felix. Can you tell me how to rewrite this
> > > bundle activator.
> > >
> > > package org.DX_57.osgi.SH_27.impl;
> > >
> > > import javax.sql.DataSource;
> > > import java.sql.SQLException;
> > >
> > > import java.util.Properties;
> > > import org.DX_57.osgi.SH_27.api.SessionHandle;
> > > import org.osgi.framework.BundleActivator;
> > > import org.osgi.framework.BundleContext;
> > > import org.osgi.framework.Constants;
> > > import org.osgi.framework.Filter;
> > > import org.osgi.framework.ServiceReference;
> > > import org.osgi.framework.ServiceRegistration;
> > > import org.osgi.util.tracker.ServiceTracker;
> > >
> > >
> > > public class SessionHandleApp implements BundleActivator {
> > >
> > > public static final String DSNAME = "jdbc/Oracle";
> > > public ServiceTracker st;
> > >
> > > @Override
> > > public void start(final BundleContext bc) throws Exception {
> > > debug("Activator started");
> > >
> > >
> > > Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
> > > + "=" + DataSource.class.getName() + ")" + "(jndi-name="
> > > + DSNAME + ")" + ")");
> > > st = new ServiceTracker(bc, filter, null) {
> > >
> > > @Override
> > > public Object addingService(ServiceReference reference) {
> > > DataSource ds = (DataSource) bc.getService(reference);
> > > try {
> > > debug(ds.getConnection().toString());
> > >
> > > SessionHandle sh = new SessionHandleImpl();
> > > sh.setDataSource(ds);
> > > ServiceRegistration registerService =
> > > bc.registerService(SessionHandle.class.getName(), sh, new
> Properties());
> > >
> > > } catch (SQLException e) {
> > > }
> > >
> > > return super.addingService(reference);
> > > }
> > >
> > > @Override
> > > public void removedService(ServiceReference reference,
> > > Object service) {
> > > super.removedService(reference, service);
> > > }
> > >
> > > };
> > > st.open();
> > > }
> > >
> > > public void stop(BundleContext bc) throws Exception {
> > > boolean ungetService =
> > > bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
> > > st.close();
> > > }
> > >
> > > private void debug(String msg) {
> > > System.out.println("JDBCBundleActivator: " + msg);
> > > }
> > >
> > > }
> > >
> > >
> > > On Sat, Mar 17, 2012 at 8:21 PM, Christian Schneider <
> > > chris@die-schneider.net (mailto:chris@die-schneider.net)> wrote:
> > >
> > > > You can do the same I do with blueprint with plain java.
> > > >
> > > > Write a bundle with an Activator that initializes the datasource and
> > > > offers it as a service. The problem here is only that you would have
> the
> > > >
> > >
> > > db
> > > > datasource class hardwired. You can get all other attributes from the
> > > > config admin service to make them configurable.
> > > >
> > > > The other solution is to include blueprint and the felix fileinstall
> > > > service in your osgi framework. In that case even the blueprint way
> could
> > > > work outside Apache Karaf.
> > > >
> > > > What kind of server are you using btw?
> > > >
> > > > Christian
> > > >
> > > >
> > > > Am 17.03.2012 12:06, schrieb Peter Penzov:
> > > >
> > > > > I agree that the legacy approach is not a good solution. Is there
> any
> > > > > other
> > > > > solution without using Apache Karaf for creating datasource
> service for
> > > > > OSGI bundle?
> > > > >
> > > > > Best wishes
> > > > > Peter
> > > > >
> > > > > On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider<
> > > > > chris@die-schneider.net (mailto:chris@die-schneider.net)> wrote:
> > > > >
> > > > > The best practice is to configure a DataSource outside of the
> bundle
> > > and
> > > > > > offer it as a OSGi service. So the user bundle should just
> reference
> > > > >
> > > >
> > >
> > > the
> > > > > > datasource and use it. The legacy aproach with using a database
> driver
> > > > > > classname to configure jdbc does not work well in OSGi.
> > > > > >
> > > > > > I have a tutorial that shows how to do this with Apache Karaf.
> This
> > > > > > should also be possible with pure felix just perhaps a little
> more
> > > > > > effort.
> > > > > > http://www.liquid-reality.de/****x/LYBk<
> > > > > >
> > > > >
> > > >
> > >
> > > http://www.liquid-reality.de/**x/LYBk>
> > > > > > <http://www.liquid-**reality.de/x/LYBk (http://reality.de/x/LYBk
> )<
> > > > >
> > > >
> > >
> > > http://www.liquid-reality.de/x/LYBk>
> > > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > Christian
> > > > > >
> > > > > >
> > > > > > Am 16.03.2012 10:10, schrieb elMateo:
> > > > > >
> > > > > > Hello all,
> > > > > >
> > > > > > > I need to connect a bundle to a MySQL data base and I don't
> know how I
> > > > > > > can
> > > > > > > do it.
> > > > > > >
> > > > > > > I know that I can't do the same that in a normal Java
> application,
> > > but I
> > > > > > > don't find the correct form to do this.
> > > > > > >
> > > > > > > Could anybody help me?
> > > > > > >
> > > > > > > Thanks in advance, Jesus
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > Christian Schneider
> > > > > > http://www.liquid-reality.de
> > > > > >
> > > > > > Open Source Architect
> > > > > > Talend Application Integration Division http://www.talend.com
> > > > > >
> > > > > >
> > > > > > ------------------------------****----------------------------**
> > > > > > --**---------
> > > > > > To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org (
> http://he.org)<
> > > > > >
> > > > >
> > > >
> > >
> > > http://apache.org>
> > > > > > <users-unsubscribe@**felix.apache.org (http://felix.apache.org)<
> > > > >
> > > >
> > >
> > > users-unsubscribe@felix.apache.org (mailto:
> users-unsubscribe@felix.apache.org)>
> > > > > > >
> > > > > >
> > > > > >
> > > > > > For additional commands, e-mail: users-help@felix.apache.org(mailto:
> users-help@felix.apache.org)
> > > >
> > > > --
> > > >
> > > > Christian Schneider
> > > > http://www.liquid-reality.de
> > > >
> > > > Open Source Architect
> > > > Talend Application Integration Division http://www.talend.com
> > > >
> > > >
> > > >
> ------------------------------**------------------------------**---------
> > > > To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org (
> http://apache.org)<
> > > >
> > >
> > > users-unsubscribe@felix.apache.org (mailto:
> users-unsubscribe@felix.apache.org)>
> > > > For additional commands, e-mail: users-help@felix.apache.org(mailto:
> users-help@felix.apache.org)
> > >
> > >
> >
> >
> >
>
>
>

Re: Connect a bundle to a data base

Posted by Neil Bartlett <nj...@gmail.com>.
You certainly don't need Glassfish... you don't need Karaf or Blueprint either.

There is a tendency on many mailing lists to respond to beginner questions with answers like "you can do this with product X" where X happens to be the product that the person is working on or selling. Those products may indeed add value but they are rarely the only or the simplest way to achieve what you want!

To register a DataSource service with pure OSGi, it's as simple as writing this code in a BundleActivator (using MySQL as an example):

DataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
context.registerService(DataSource.class.getName(), ds, null);

This would be your "bridge" bundle that you deploy alongside the MySQL driver JAR. For other database types, write a different 2 lines of code and deploy that bundle instead.

Kind regards,
Neil



On Tuesday, 20 March 2012 at 09:58, elMateo wrote:

> Sorry for my ignorance, but why does it need a Glassfish server?
> 
> If I only want to access to a database through a bundle, Is necessary a
> server?
> 
> On Sat, Mar 17, 2012 at 10:25 PM, Peter Penzov <peter.penzov@gmail.com (mailto:peter.penzov@gmail.com)>wrote:
> 
> > I use Glassfish and Apache Felix. Can you tell me how to rewrite this
> > bundle activator.
> > 
> > package org.DX_57.osgi.SH_27.impl;
> > 
> > import javax.sql.DataSource;
> > import java.sql.SQLException;
> > 
> > import java.util.Properties;
> > import org.DX_57.osgi.SH_27.api.SessionHandle;
> > import org.osgi.framework.BundleActivator;
> > import org.osgi.framework.BundleContext;
> > import org.osgi.framework.Constants;
> > import org.osgi.framework.Filter;
> > import org.osgi.framework.ServiceReference;
> > import org.osgi.framework.ServiceRegistration;
> > import org.osgi.util.tracker.ServiceTracker;
> > 
> > 
> > public class SessionHandleApp implements BundleActivator {
> > 
> > public static final String DSNAME = "jdbc/Oracle";
> > public ServiceTracker st;
> > 
> > @Override
> > public void start(final BundleContext bc) throws Exception {
> > debug("Activator started");
> > 
> > 
> > Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
> > + "=" + DataSource.class.getName() + ")" + "(jndi-name="
> > + DSNAME + ")" + ")");
> > st = new ServiceTracker(bc, filter, null) {
> > 
> > @Override
> > public Object addingService(ServiceReference reference) {
> > DataSource ds = (DataSource) bc.getService(reference);
> > try {
> > debug(ds.getConnection().toString());
> > 
> > SessionHandle sh = new SessionHandleImpl();
> > sh.setDataSource(ds);
> > ServiceRegistration registerService =
> > bc.registerService(SessionHandle.class.getName(), sh, new Properties());
> > 
> > } catch (SQLException e) {
> > }
> > 
> > return super.addingService(reference);
> > }
> > 
> > @Override
> > public void removedService(ServiceReference reference,
> > Object service) {
> > super.removedService(reference, service);
> > }
> > 
> > };
> > st.open();
> > }
> > 
> > public void stop(BundleContext bc) throws Exception {
> > boolean ungetService =
> > bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
> > st.close();
> > }
> > 
> > private void debug(String msg) {
> > System.out.println("JDBCBundleActivator: " + msg);
> > }
> > 
> > }
> > 
> > 
> > On Sat, Mar 17, 2012 at 8:21 PM, Christian Schneider <
> > chris@die-schneider.net (mailto:chris@die-schneider.net)> wrote:
> > 
> > > You can do the same I do with blueprint with plain java.
> > > 
> > > Write a bundle with an Activator that initializes the datasource and
> > > offers it as a service. The problem here is only that you would have the
> > > 
> > 
> > db
> > > datasource class hardwired. You can get all other attributes from the
> > > config admin service to make them configurable.
> > > 
> > > The other solution is to include blueprint and the felix fileinstall
> > > service in your osgi framework. In that case even the blueprint way could
> > > work outside Apache Karaf.
> > > 
> > > What kind of server are you using btw?
> > > 
> > > Christian
> > > 
> > > 
> > > Am 17.03.2012 12:06, schrieb Peter Penzov:
> > > 
> > > > I agree that the legacy approach is not a good solution. Is there any
> > > > other
> > > > solution without using Apache Karaf for creating datasource service for
> > > > OSGI bundle?
> > > > 
> > > > Best wishes
> > > > Peter
> > > > 
> > > > On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider<
> > > > chris@die-schneider.net (mailto:chris@die-schneider.net)> wrote:
> > > > 
> > > > The best practice is to configure a DataSource outside of the bundle
> > and
> > > > > offer it as a OSGi service. So the user bundle should just reference
> > > > 
> > > 
> > 
> > the
> > > > > datasource and use it. The legacy aproach with using a database driver
> > > > > classname to configure jdbc does not work well in OSGi.
> > > > > 
> > > > > I have a tutorial that shows how to do this with Apache Karaf. This
> > > > > should also be possible with pure felix just perhaps a little more
> > > > > effort.
> > > > > http://www.liquid-reality.de/****x/LYBk<
> > > > > 
> > > > 
> > > 
> > 
> > http://www.liquid-reality.de/**x/LYBk>
> > > > > <http://www.liquid-**reality.de/x/LYBk (http://reality.de/x/LYBk)<
> > > > 
> > > 
> > 
> > http://www.liquid-reality.de/x/LYBk>
> > > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > Christian
> > > > > 
> > > > > 
> > > > > Am 16.03.2012 10:10, schrieb elMateo:
> > > > > 
> > > > > Hello all,
> > > > > 
> > > > > > I need to connect a bundle to a MySQL data base and I don't know how I
> > > > > > can
> > > > > > do it.
> > > > > > 
> > > > > > I know that I can't do the same that in a normal Java application,
> > but I
> > > > > > don't find the correct form to do this.
> > > > > > 
> > > > > > Could anybody help me?
> > > > > > 
> > > > > > Thanks in advance, Jesus
> > > > > > 
> > > > > > 
> > > > > > --
> > > > > Christian Schneider
> > > > > http://www.liquid-reality.de
> > > > > 
> > > > > Open Source Architect
> > > > > Talend Application Integration Division http://www.talend.com
> > > > > 
> > > > > 
> > > > > ------------------------------****----------------------------**
> > > > > --**---------
> > > > > To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org (http://he.org)<
> > > > > 
> > > > 
> > > 
> > 
> > http://apache.org>
> > > > > <users-unsubscribe@**felix.apache.org (http://felix.apache.org)<
> > > > 
> > > 
> > 
> > users-unsubscribe@felix.apache.org (mailto:users-unsubscribe@felix.apache.org)>
> > > > > > 
> > > > > 
> > > > > 
> > > > > For additional commands, e-mail: users-help@felix.apache.org (mailto:users-help@felix.apache.org)
> > > 
> > > --
> > > 
> > > Christian Schneider
> > > http://www.liquid-reality.de
> > > 
> > > Open Source Architect
> > > Talend Application Integration Division http://www.talend.com
> > > 
> > > 
> > > ------------------------------**------------------------------**---------
> > > To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org (http://apache.org)<
> > > 
> > 
> > users-unsubscribe@felix.apache.org (mailto:users-unsubscribe@felix.apache.org)>
> > > For additional commands, e-mail: users-help@felix.apache.org (mailto:users-help@felix.apache.org)
> > 
> > 
> 
> 
> 



Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
Sorry for my ignorance, but why does it need a Glassfish server?

If I only want to access to a database through a bundle, Is necessary a
server?

On Sat, Mar 17, 2012 at 10:25 PM, Peter Penzov <pe...@gmail.com>wrote:

> I use Glassfish and Apache Felix. Can you tell me how to rewrite this
> bundle activator.
>
> package org.DX_57.osgi.SH_27.impl;
>
> import javax.sql.DataSource;
> import java.sql.SQLException;
>
> import java.util.Properties;
> import org.DX_57.osgi.SH_27.api.SessionHandle;
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.Constants;
> import org.osgi.framework.Filter;
> import org.osgi.framework.ServiceReference;
> import org.osgi.framework.ServiceRegistration;
> import org.osgi.util.tracker.ServiceTracker;
>
>
> public class SessionHandleApp implements BundleActivator {
>
>    public static final String DSNAME = "jdbc/Oracle";
>    public ServiceTracker st;
>
>    @Override
>    public void start(final BundleContext bc) throws Exception {
>        debug("Activator started");
>
>
>        Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
>                + "=" + DataSource.class.getName() + ")" + "(jndi-name="
>                + DSNAME + ")" + ")");
>        st = new ServiceTracker(bc, filter, null) {
>
>            @Override
>            public Object addingService(ServiceReference reference) {
>                DataSource ds = (DataSource) bc.getService(reference);
>                try {
>                    debug(ds.getConnection().toString());
>
>                SessionHandle sh = new SessionHandleImpl();
>                                sh.setDataSource(ds);
>                    ServiceRegistration registerService =
> bc.registerService(SessionHandle.class.getName(), sh, new Properties());
>
>                } catch (SQLException e) {
>                }
>
>                return super.addingService(reference);
>            }
>
>            @Override
>            public void removedService(ServiceReference reference,
>                    Object service) {
>                super.removedService(reference, service);
>            }
>
>        };
>        st.open();
>    }
>
>    public void stop(BundleContext bc) throws Exception {
>        boolean ungetService =
> bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
>        st.close();
>    }
>
>    private void debug(String msg) {
>        System.out.println("JDBCBundleActivator: " + msg);
>    }
>
> }
>
>
> On Sat, Mar 17, 2012 at 8:21 PM, Christian Schneider <
> chris@die-schneider.net> wrote:
>
> > You can do the same I do with blueprint with plain java.
> >
> > Write a bundle with an  Activator that initializes the datasource and
> > offers it as a service. The problem here is only that you would have the
> db
> > datasource class hardwired. You can get all other attributes from the
> > config admin service to make them configurable.
> >
> > The other solution is to include blueprint and the felix fileinstall
> > service in your osgi framework. In that case even the blueprint way could
> > work outside Apache Karaf.
> >
> > What kind of server are you using btw?
> >
> > Christian
> >
> >
> > Am 17.03.2012 12:06, schrieb Peter Penzov:
> >
> >> I agree that the legacy approach is not a good solution. Is there any
> >> other
> >> solution without using Apache Karaf for creating datasource service for
> >> OSGI bundle?
> >>
> >> Best wishes
> >> Peter
> >>
> >> On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider<
> >> chris@die-schneider.net>  wrote:
> >>
> >>  The best practice is to configure a DataSource outside of the bundle
> and
> >>> offer it as a OSGi service. So the user bundle should just reference
> the
> >>> datasource and use it. The legacy aproach with using a database driver
> >>> classname to configure jdbc does not work well in OSGi.
> >>>
> >>> I have a  tutorial that shows how to do this with Apache Karaf. This
> >>> should also be possible with pure felix just perhaps a little more
> >>> effort.
> >>> http://www.liquid-reality.de/****x/LYBk<
> http://www.liquid-reality.de/**x/LYBk>
> >>> <http://www.liquid-**reality.de/x/LYBk<
> http://www.liquid-reality.de/x/LYBk>
> >>> >
> >>>
> >>>
> >>> Christian
> >>>
> >>>
> >>> Am 16.03.2012 10:10, schrieb elMateo:
> >>>
> >>>  Hello all,
> >>>
> >>>> I need to connect a bundle to a MySQL data base and I don't know how I
> >>>> can
> >>>> do it.
> >>>>
> >>>> I know that I can't do the same that in a normal Java application,
> but I
> >>>> don't find the correct form to do this.
> >>>>
> >>>> Could anybody help me?
> >>>>
> >>>> Thanks in advance, Jesus
> >>>>
> >>>>
> >>>>  --
> >>> Christian Schneider
> >>> http://www.liquid-reality.de
> >>>
> >>> Open Source Architect
> >>> Talend Application Integration Division http://www.talend.com
> >>>
> >>>
> >>> ------------------------------****----------------------------**
> >>> --**---------
> >>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<
> http://apache.org>
> >>> <users-unsubscribe@**felix.apache.org<
> users-unsubscribe@felix.apache.org>
> >>> >
> >>>
> >>> For additional commands, e-mail: users-help@felix.apache.org
> >>>
> >>>
> >>>
> >
> > --
> >
> > Christian Schneider
> > http://www.liquid-reality.de
> >
> > Open Source Architect
> > Talend Application Integration Division http://www.talend.com
> >
> >
> > ------------------------------**------------------------------**---------
> > To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<
> users-unsubscribe@felix.apache.org>
> > For additional commands, e-mail: users-help@felix.apache.org
> >
> >
>

Re: Connect a bundle to a data base

Posted by Peter Penzov <pe...@gmail.com>.
I use Glassfish and Apache Felix. Can you tell me how to rewrite this
bundle activator.

package org.DX_57.osgi.SH_27.impl;

import javax.sql.DataSource;
import java.sql.SQLException;

import java.util.Properties;
import org.DX_57.osgi.SH_27.api.SessionHandle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;


public class SessionHandleApp implements BundleActivator {

    public static final String DSNAME = "jdbc/Oracle";
    public ServiceTracker st;

    @Override
    public void start(final BundleContext bc) throws Exception {
        debug("Activator started");


        Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
                + "=" + DataSource.class.getName() + ")" + "(jndi-name="
                + DSNAME + ")" + ")");
        st = new ServiceTracker(bc, filter, null) {

            @Override
            public Object addingService(ServiceReference reference) {
                DataSource ds = (DataSource) bc.getService(reference);
                try {
                    debug(ds.getConnection().toString());

                SessionHandle sh = new SessionHandleImpl();
                                sh.setDataSource(ds);
                    ServiceRegistration registerService =
bc.registerService(SessionHandle.class.getName(), sh, new Properties());

                } catch (SQLException e) {
                }

                return super.addingService(reference);
            }

            @Override
            public void removedService(ServiceReference reference,
                    Object service) {
                super.removedService(reference, service);
            }

        };
        st.open();
    }

    public void stop(BundleContext bc) throws Exception {
        boolean ungetService =
bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
        st.close();
    }

    private void debug(String msg) {
        System.out.println("JDBCBundleActivator: " + msg);
    }

}


On Sat, Mar 17, 2012 at 8:21 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> You can do the same I do with blueprint with plain java.
>
> Write a bundle with an  Activator that initializes the datasource and
> offers it as a service. The problem here is only that you would have the db
> datasource class hardwired. You can get all other attributes from the
> config admin service to make them configurable.
>
> The other solution is to include blueprint and the felix fileinstall
> service in your osgi framework. In that case even the blueprint way could
> work outside Apache Karaf.
>
> What kind of server are you using btw?
>
> Christian
>
>
> Am 17.03.2012 12:06, schrieb Peter Penzov:
>
>> I agree that the legacy approach is not a good solution. Is there any
>> other
>> solution without using Apache Karaf for creating datasource service for
>> OSGI bundle?
>>
>> Best wishes
>> Peter
>>
>> On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider<
>> chris@die-schneider.net>  wrote:
>>
>>  The best practice is to configure a DataSource outside of the bundle and
>>> offer it as a OSGi service. So the user bundle should just reference the
>>> datasource and use it. The legacy aproach with using a database driver
>>> classname to configure jdbc does not work well in OSGi.
>>>
>>> I have a  tutorial that shows how to do this with Apache Karaf. This
>>> should also be possible with pure felix just perhaps a little more
>>> effort.
>>> http://www.liquid-reality.de/****x/LYBk<http://www.liquid-reality.de/**x/LYBk>
>>> <http://www.liquid-**reality.de/x/LYBk<http://www.liquid-reality.de/x/LYBk>
>>> >
>>>
>>>
>>> Christian
>>>
>>>
>>> Am 16.03.2012 10:10, schrieb elMateo:
>>>
>>>  Hello all,
>>>
>>>> I need to connect a bundle to a MySQL data base and I don't know how I
>>>> can
>>>> do it.
>>>>
>>>> I know that I can't do the same that in a normal Java application, but I
>>>> don't find the correct form to do this.
>>>>
>>>> Could anybody help me?
>>>>
>>>> Thanks in advance, Jesus
>>>>
>>>>
>>>>  --
>>> Christian Schneider
>>> http://www.liquid-reality.de
>>>
>>> Open Source Architect
>>> Talend Application Integration Division http://www.talend.com
>>>
>>>
>>> ------------------------------****----------------------------**
>>> --**---------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.**apac**he.org<http://apache.org>
>>> <us...@felix.apache.org>
>>> >
>>>
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>
> --
>
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
You can do the same I do with blueprint with plain java.

Write a bundle with an  Activator that initializes the datasource and 
offers it as a service. The problem here is only that you would have the db
datasource class hardwired. You can get all other attributes from the 
config admin service to make them configurable.

The other solution is to include blueprint and the felix fileinstall 
service in your osgi framework. In that case even the blueprint way 
could work outside Apache Karaf.

What kind of server are you using btw?

Christian


Am 17.03.2012 12:06, schrieb Peter Penzov:
> I agree that the legacy approach is not a good solution. Is there any other
> solution without using Apache Karaf for creating datasource service for
> OSGI bundle?
>
> Best wishes
> Peter
>
> On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider<
> chris@die-schneider.net>  wrote:
>
>> The best practice is to configure a DataSource outside of the bundle and
>> offer it as a OSGi service. So the user bundle should just reference the
>> datasource and use it. The legacy aproach with using a database driver
>> classname to configure jdbc does not work well in OSGi.
>>
>> I have a  tutorial that shows how to do this with Apache Karaf. This
>> should also be possible with pure felix just perhaps a little more effort.
>> http://www.liquid-reality.de/**x/LYBk<http://www.liquid-reality.de/x/LYBk>
>>
>> Christian
>>
>>
>> Am 16.03.2012 10:10, schrieb elMateo:
>>
>>   Hello all,
>>> I need to connect a bundle to a MySQL data base and I don't know how I can
>>> do it.
>>>
>>> I know that I can't do the same that in a normal Java application, but I
>>> don't find the correct form to do this.
>>>
>>> Could anybody help me?
>>>
>>> Thanks in advance, Jesus
>>>
>>>
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>>
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>>
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>


-- 

Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by Peter Penzov <pe...@gmail.com>.
I agree that the legacy approach is not a good solution. Is there any other
solution without using Apache Karaf for creating datasource service for
OSGI bundle?

Best wishes
Peter

On Fri, Mar 16, 2012 at 12:44 PM, Christian Schneider <
chris@die-schneider.net> wrote:

> The best practice is to configure a DataSource outside of the bundle and
> offer it as a OSGi service. So the user bundle should just reference the
> datasource and use it. The legacy aproach with using a database driver
> classname to configure jdbc does not work well in OSGi.
>
> I have a  tutorial that shows how to do this with Apache Karaf. This
> should also be possible with pure felix just perhaps a little more effort.
> http://www.liquid-reality.de/**x/LYBk<http://www.liquid-reality.de/x/LYBk>
>
> Christian
>
>
> Am 16.03.2012 10:10, schrieb elMateo:
>
>  Hello all,
>>
>> I need to connect a bundle to a MySQL data base and I don't know how I can
>> do it.
>>
>> I know that I can't do the same that in a normal Java application, but I
>> don't find the correct form to do this.
>>
>> Could anybody help me?
>>
>> Thanks in advance, Jesus
>>
>>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@felix.**apache.org<us...@felix.apache.org>
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: Connect a bundle to a data base

Posted by Christian Schneider <ch...@die-schneider.net>.
The best practice is to configure a DataSource outside of the bundle and 
offer it as a OSGi service. So the user bundle should just reference the 
datasource and use it. The legacy aproach with using a database driver 
classname to configure jdbc does not work well in OSGi.

I have a  tutorial that shows how to do this with Apache Karaf. This 
should also be possible with pure felix just perhaps a little more effort.
http://www.liquid-reality.de/x/LYBk

Christian


Am 16.03.2012 10:10, schrieb elMateo:
> Hello all,
>
> I need to connect a bundle to a MySQL data base and I don't know how I can
> do it.
>
> I know that I can't do the same that in a normal Java application, but I
> don't find the correct form to do this.
>
> Could anybody help me?
>
> Thanks in advance, Jesus
>


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Connect a bundle to a data base

Posted by elMateo <so...@gmail.com>.
I use MySQL.

Thanks very much for the code. I'm going to edit!!

On Fri, Mar 16, 2012 at 10:30 AM, Peter Penzov <pe...@gmail.com>wrote:

> What Application do you use?
>
> This code I use for Glassfish and Oracle:
>
> Activator:
>
> import javax.sql.DataSource;
> import java.sql.SQLException;
>
> import java.util.Properties;
> import org.DX_57.osgi.SH_27.api.SessionHandle;
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.Constants;
> import org.osgi.framework.Filter;
> import org.osgi.framework.ServiceReference;
> import org.osgi.framework.ServiceRegistration;
> import org.osgi.util.tracker.ServiceTracker;
>
>
> public class SessionHandleApp implements BundleActivator {
>
>    public static final String DSNAME = "jdbc/Oracle";
>    public ServiceTracker st;
>
>    @Override
>    public void start(final BundleContext bc) throws Exception {
>        debug("Activator started");
>
>
>        Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
>                + "=" + DataSource.class.getName() + ")" + "(jndi-name="
>                + DSNAME + ")" + ")");
>        st = new ServiceTracker(bc, filter, null) {
>
>            @Override
>            public Object addingService(ServiceReference reference) {
>                DataSource ds = (DataSource) bc.getService(reference);
>                try {
>                    debug(ds.getConnection().toString());
>
>                SessionHandle sh = new SessionHandleImpl();
>                                sh.setDataSource(ds);
>                    ServiceRegistration registerService =
> bc.registerService(SessionHandle.class.getName(), sh, new Properties());
>
>                } catch (SQLException e) {
>                }
>
>                return super.addingService(reference);
>            }
>
>            @Override
>            public void removedService(ServiceReference reference,
>                    Object service) {
>                super.removedService(reference, service);
>            }
>
>        };
>        st.open();
>    }
>
>    public void stop(BundleContext bc) throws Exception {
>        boolean ungetService =
> bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
>        st.close();
>    }
>
>    private void debug(String msg) {
>        System.out.println("JDBCBundleActivator: " + msg);
>    }
>
> }
>
>
> Simple SQL query:
>
> public class SessionHandleImpl implements SessionHandle {
>
>       public DataSource ds;
>
>       /** method for checking password into the Oracle database */
>       @Override
>       public String CheckUserDB(String userToCheck) throws SQLException {
>            String storedPassword = null;
>            String SQL_Statement = null;
>
>            if (ds == null) throw new SQLException();
>       Connection conn = ds.getConnection();
>            if (conn == null) throw new SQLException();
>
>       try {
>            conn.setAutoCommit(false);
>            boolean committed = false;
>                try {
>                       SQL_Statement = "SELECT Passwd from USERS WHERE
> Username = ?";
>
>                       PreparedStatement passwordQuery =
> conn.prepareStatement(SQL_Statement);
>                       passwordQuery.setString(1, userToCheck);
>
>                       ResultSet result = passwordQuery.executeQuery();
>
>                       if(result.next()){
>                            storedPassword = result.getString("Passwd");
>                       }
>
>                       conn.commit();
>                       committed = true;
>                 } finally {
>                       if (!committed) conn.rollback();
>                       }
>            }
>                finally {
>                conn.close();
>
>                }
>
>       return storedPassword;
>       }
> }
>
> You have to edit the code - name of the datasource and the java classes.
>
> Best wishes
> Peter
>
>
> On Fri, Mar 16, 2012 at 11:10 AM, elMateo <so...@gmail.com> wrote:
>
> > Hello all,
> >
> > I need to connect a bundle to a MySQL data base and I don't know how I
> can
> > do it.
> >
> > I know that I can't do the same that in a normal Java application, but I
> > don't find the correct form to do this.
> >
> > Could anybody help me?
> >
> > Thanks in advance, Jesus
> >
>

Re: Connect a bundle to a data base

Posted by Peter Penzov <pe...@gmail.com>.
What Application do you use?

This code I use for Glassfish and Oracle:

Activator:

import javax.sql.DataSource;
import java.sql.SQLException;

import java.util.Properties;
import org.DX_57.osgi.SH_27.api.SessionHandle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;


public class SessionHandleApp implements BundleActivator {

    public static final String DSNAME = "jdbc/Oracle";
    public ServiceTracker st;

    @Override
    public void start(final BundleContext bc) throws Exception {
        debug("Activator started");


        Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
                + "=" + DataSource.class.getName() + ")" + "(jndi-name="
                + DSNAME + ")" + ")");
        st = new ServiceTracker(bc, filter, null) {

            @Override
            public Object addingService(ServiceReference reference) {
                DataSource ds = (DataSource) bc.getService(reference);
                try {
                    debug(ds.getConnection().toString());

                SessionHandle sh = new SessionHandleImpl();
                                sh.setDataSource(ds);
                    ServiceRegistration registerService =
bc.registerService(SessionHandle.class.getName(), sh, new Properties());

                } catch (SQLException e) {
                }

                return super.addingService(reference);
            }

            @Override
            public void removedService(ServiceReference reference,
                    Object service) {
                super.removedService(reference, service);
            }

        };
        st.open();
    }

    public void stop(BundleContext bc) throws Exception {
        boolean ungetService =
bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
        st.close();
    }

    private void debug(String msg) {
        System.out.println("JDBCBundleActivator: " + msg);
    }

}


Simple SQL query:

public class SessionHandleImpl implements SessionHandle {

       public DataSource ds;

       /** method for checking password into the Oracle database */
       @Override
       public String CheckUserDB(String userToCheck) throws SQLException {
            String storedPassword = null;
            String SQL_Statement = null;

            if (ds == null) throw new SQLException();
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException();

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       SQL_Statement = "SELECT Passwd from USERS WHERE
Username = ?";

                       PreparedStatement passwordQuery =
conn.prepareStatement(SQL_Statement);
                       passwordQuery.setString(1, userToCheck);

                       ResultSet result = passwordQuery.executeQuery();

                       if(result.next()){
                            storedPassword = result.getString("Passwd");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {
                conn.close();

                }

       return storedPassword;
       }
}

You have to edit the code - name of the datasource and the java classes.

Best wishes
Peter


On Fri, Mar 16, 2012 at 11:10 AM, elMateo <so...@gmail.com> wrote:

> Hello all,
>
> I need to connect a bundle to a MySQL data base and I don't know how I can
> do it.
>
> I know that I can't do the same that in a normal Java application, but I
> don't find the correct form to do this.
>
> Could anybody help me?
>
> Thanks in advance, Jesus
>