felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Neil Bartlett <njbartl...@gmail.com>
Subject Re: Connect a bundle to a data base
Date Tue, 20 Mar 2012 10:27:00 GMT
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)
> > 
> > 
> 
> 
> 



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message