Re: Classloading issue with Berkley DB

2024-03-18 Thread Paul Spencer
Will,
This is the intend reply.

I use both Ingres and PostgreSQL databases in my applications and use the JDBC 
api to interact with the database.  To do this, I created in 
IngresDataSourceFactory that implements 
org.osgi.service.jdbc.DataSourceFactory.DataSourceFactory bundle so my 
application bundles just @Reference Datasource.  I also loaded the Ingres JDBC 
driver as a wrapped jar, `bundle:install wrap:mvn:` Karaf/OSGi and 
configuration take care of the rest after the datasource is in Karaf is 
created.  

See the following.
  
https://karaf.apache.org/manual/latest/#_creating_bundles_for_non_osgi_third_party_dependencies
  https://karaf.apache.org/manual/latest/#_datasources_jdbc

Paul Spencer



Classloading issue with Berkley DB

2024-03-18 Thread Paul Spencer
We're using BerkelyDB within our application.

I did not look too hard for an OSGI module for it, it wasn't readily apparent, 
so I just hamfisted one by taking their jar and using bnd to create a bundle 
from it.

But there's a curious issue.

We have code like this:

final DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(false);
dbConfig.setAllowCreate(allowCreate);
dbConfig.setReadOnly(readOnly);
dbConfig.setDeferredWrite(true);
dbConfig.setBtreeComparator(new KeyComparator());

But when I call this:

this.queueDatabase = dbEnv.openDatabase(null, queueName, dbConfig);

I get a Class Not Found exception. It can not find the KeyComparator class 
(which is a local class to this class that's starting the DB).

Well, it seems from a glance at the stack trace, that the BDB is serializing 
the comparator, and trying to read it back.

Caused by: com.sleepycat.je.EnvironmentFailureException: (JE 18.3.12) Exception 
while trying to load BtreeComparator UNEXPECTED_EXCEPTION: Unexpected internal 
Exception, may have side effects.
at 
com.sleepycat.je.EnvironmentFailureException.unexpectedException(EnvironmentFailureException.java:384)
at com.sleepycat.je.dbi.DatabaseImpl.bytesToObject(DatabaseImpl.java:1957)
at 
com.sleepycat.je.dbi.DatabaseImpl$ComparatorReader.(DatabaseImpl.java:2002)
at com.sleepycat.je.dbi.DatabaseImpl.initWithEnvironment(DatabaseImpl.java:390)
at com.sleepycat.je.dbi.DatabaseImpl.(DatabaseImpl.java:247)
at com.sleepycat.je.dbi.DbTree.doCreateDb(DbTree.java:593)
at com.sleepycat.je.dbi.DbTree.createDb(DbTree.java:486)
at com.sleepycat.je.Database.initNew(Database.java:174)
at com.sleepycat.je.Environment.setupDatabase(Environment.java:864)
at com.sleepycat.je.Environment.openDatabase(Environment.java:668)
at com.qpoint.caterwaul.connect.queue.BDBQueue.(BDBQueue.java:88)
at com.qpoint.caterwaul.connect.queue.BDBQueue.(BDBQueue.java:52)
at com.qpoint.caterwaul.connect.queue.RichBDBQueue.(RichBDBQueue.java:49)
at 
com.qpoint.caterwaul.connect.queue.TickEventOrchestrator.start(TickEventOrchestrator.java:87)
at com.qpoint.caterwaul.connect.core.CWCore.initialize(CWCore.java:168)
... 9 more
Caused by: java.lang.ClassNotFoundException: 
com.qpoint.caterwaul.connect.queue.KeyComparator
at 
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at 
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:488)
at java.base/java.lang.Class.forName(Class.java:467)
at com.sleepycat.util.ClassResolver$Stream.resolveClass(ClassResolver.java:74)
at 
java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2058)
at 
java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1922)
at 
java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2248)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1757)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:538)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:496)
at com.sleepycat.je.dbi.DatabaseImpl.bytesToObject(DatabaseImpl.java:1954)
... 22 more

We see that it's calling the ObjectInputStream readObject. Which means it 
serialized the KeyComparator instance somewhere.

Now, I can't fathom why this is happening, seems an odd decision, but it's 
clear to me that it's trying to do this from within the BDB bundle, and ITS 
classloader, rather than the bundle classloader where it's being initialized. 
That would explain why it can't find the class.

So, do I need to somehow tweak the BDB bundle to import the KeyComparator 
class? Won't this be a circular dependency (my module depends on BDB which 
depends on my module), unless, of course, I break out my KeyComparator class 
into -- I don't know what I would break it into, seems a bit much to make it 
its own bundle.

While the solution would be appreciated, how are problems like this approached? 
I imagine there's rules of thumb or a top 5 "do this kind of thing" that is 
used to approach classpath issues with OSGI and Karaf.

Thanks so much.

Regards,

Will Hartung



Re: Programmatically restart bundle and/or component?

2024-03-18 Thread Steinar Bang
> Jean-Baptiste Onofré :

> You can create a controller for instance by leveraging event admin (to
> send an event triggering the reload, and the bundle decides what he
> wants to reload). Pros, you control exactly what you want to reload.
> Cons, you need to implement your Event handler.
> Another option would be to define a dependency between components (via
> @Reference). That's probably the cleaner option, but it will do a full
> refresh.

Thanks for the pointer, JB!



Re: Can a DS component expose multiple services?

2024-03-18 Thread Steinar Bang
> Jean-Baptiste Onofré :

> Yes, it' possible to have several services by component (imho, it
> should be avoided when possible to avoid unexpected cascading
> refresh).

> A component can implement multiple interfaces, and the @Component
> annotation accepts the list of exposed interfaces.

Yep, I found out. :-)

Working example code further down the thread.



Classloading issue with Berkley DB

2024-03-18 Thread Will Hartung
We're using BerkelyDB within our application.

I did not look too hard for an OSGI module for it, it wasn't readily
apparent, so I just hamfisted one by taking their jar and using bnd to
create a bundle from it.

But there's a curious issue.

We have code like this:

final DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(false);
dbConfig.setAllowCreate(allowCreate);
dbConfig.setReadOnly(readOnly);
dbConfig.setDeferredWrite(true);
dbConfig.setBtreeComparator(new KeyComparator());

But when I call this:

this.queueDatabase = dbEnv.openDatabase(null, queueName, dbConfig);

I get a Class Not Found exception. It can not find the KeyComparator class
(which is a local class to this class that's starting the DB).

Well, it seems from a glance at the stack trace, that the BDB is
serializing the comparator, and trying to read it back.

Caused by: com.sleepycat.je.EnvironmentFailureException: (JE 18.3.12)
Exception while trying to load BtreeComparator UNEXPECTED_EXCEPTION:
Unexpected internal Exception, may have side effects.
at
com.sleepycat.je.EnvironmentFailureException.unexpectedException(EnvironmentFailureException.java:384)
at com.sleepycat.je.dbi.DatabaseImpl.bytesToObject(DatabaseImpl.java:1957)
at
com.sleepycat.je.dbi.DatabaseImpl$ComparatorReader.(DatabaseImpl.java:2002)
at
com.sleepycat.je.dbi.DatabaseImpl.initWithEnvironment(DatabaseImpl.java:390)
at com.sleepycat.je.dbi.DatabaseImpl.(DatabaseImpl.java:247)
at com.sleepycat.je.dbi.DbTree.doCreateDb(DbTree.java:593)
at com.sleepycat.je.dbi.DbTree.createDb(DbTree.java:486)
at com.sleepycat.je.Database.initNew(Database.java:174)
at com.sleepycat.je.Environment.setupDatabase(Environment.java:864)
at com.sleepycat.je.Environment.openDatabase(Environment.java:668)
at com.qpoint.caterwaul.connect.queue.BDBQueue.(BDBQueue.java:88)
at com.qpoint.caterwaul.connect.queue.BDBQueue.(BDBQueue.java:52)
at
com.qpoint.caterwaul.connect.queue.RichBDBQueue.(RichBDBQueue.java:49)
at
com.qpoint.caterwaul.connect.queue.TickEventOrchestrator.start(TickEventOrchestrator.java:87)
at com.qpoint.caterwaul.connect.core.CWCore.initialize(CWCore.java:168)
... 9 more
Caused by: java.lang.ClassNotFoundException:
com.qpoint.caterwaul.connect.queue.KeyComparator
at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:488)
at java.base/java.lang.Class.forName(Class.java:467)
at
com.sleepycat.util.ClassResolver$Stream.resolveClass(ClassResolver.java:74)
at
java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2058)
at
java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1922)
at
java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2248)
at
java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1757)
at
java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:538)
at
java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:496)
at com.sleepycat.je.dbi.DatabaseImpl.bytesToObject(DatabaseImpl.java:1954)
... 22 more

We see that it's calling the ObjectInputStream readObject. Which means it
serialized the KeyComparator instance somewhere.

Now, I can't fathom why this is happening, seems an odd decision, but it's
clear to me that it's trying to do this from within the BDB bundle, and ITS
classloader, rather than the bundle classloader where it's being
initialized. That would explain why it can't find the class.

So, do I need to somehow tweak the BDB bundle to import the KeyComparator
class? Won't this be a circular dependency (my module depends on BDB which
depends on my module), unless, of course, I break out my KeyComparator
class into -- I don't know what I would break it into, seems a bit much to
make it its own bundle.

While the solution would be appreciated, how are problems like this
approached? I imagine there's rules of thumb or a top 5 "do this kind of
thing" that is used to approach classpath issues with OSGI and Karaf.

Thanks so much.

Regards,

Will Hartung


Re: Can a DS component expose multiple services?

2024-03-18 Thread Jean-Baptiste Onofré
Hi,

Yes, it' possible to have several services by component (imho, it
should be avoided when possible to avoid unexpected cascading
refresh).

A component can implement multiple interfaces, and the @Component
annotation accepts the list of exposed interfaces.

Regards
JB

On Sat, Mar 16, 2024 at 7:44 AM Steinar Bang  wrote:
>
> Can a Java class implementing a DS component expose more than one
> OSGi service?
>
> My usecase is that I have a DS component implementing a servlet Filter,
> plugging into the web whiteboard.
>  
> https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35
>
> And then I would like a way to retrigger configuration of the filter and
> the simplest way would be if the filter could expose a service with a
> method that could be used to trigger reconfiguration.
>
> What if I subtype the Filter interface with a new interface and expose
> that interface from the component, would still be able to "find" the web
> whiteboard? (I think not, because if I remember correctly if a class
> does not directly implement an interface, the interface for the service
> must be specified in @Component annotation...?)
>
> Hm... looks like the service parameter of @Component can be an array...?
>  
> https://docs.osgi.org/specification/osgi.cmpn/8.0.0/service.component.html#org.osgi.service.component.annotations.Component.service--
>
> I'll try.
>
> Hm... looks like the Filter interface has an init() method...?
>  
> https://docs.oracle.com/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/Filter.html#init(javax.servlet.FilterConfig)
>
> Could that be used, I wonder?
>
> No, implemented by shiro AbstractFilter, probably best not to mess with
> that...?
>
> So: I will try a new interface with a single callback method and inject
> that service into a REST endpoint and see if I can make the filter
> reconfigure itself.
>


Re: Programmatically restart bundle and/or component?

2024-03-18 Thread Jean-Baptiste Onofré
Hi Steinar,

You can create a controller for instance by leveraging event admin (to
send an event triggering the reload, and the bundle decides what he
wants to reload). Pros, you control exactly what you want to reload.
Cons, you need to implement your Event handler.
Another option would be to define a dependency between components (via
@Reference). That's probably the cleaner option, but it will do a full
refresh.

Regards
JB

On Fri, Mar 15, 2024 at 5:37 PM Steinar Bang  wrote:
>
> I have this bundle:
>  https://github.com/steinarb/oldalbum/tree/master/oldalbum.web.security
>
> which contains two DS/SCR components:
>
>  1. A component implementing ServletContextHelper used to define the
> servlet context
>  
> https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumServletContextHelper.java
>
>  2. A component implementing a Shiro servlet Filter
>  
> https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35
>
> I would like to reload the shiro servlet filter programmatically when
> its configuration changes.
>
> What's the best way of doing that?
>
> Reload the entire bundle?
>
> Reload just the filter component?
>
> Make the filter reinitialize itself without reloading?
>
> Has anyone done anything similar?
>
> Suggestions? Ideas? All are welcome!
>
> Thanks!
>
>
> - Steinar
>


Re: java.lang.NoClassDefFoundError: javax/net/ssl/TrustManager

2024-03-18 Thread Achim Nierbeck
Hi Michael,

it's quite old (tbh. 5 years )
but maybe you can find something helpful
https://github.com/ANierbeck/Karaf-Vertx/blob/master/Vertx-Http/src/main/java/de/nierbeck/example/vertx/http/VertxHttpServer.java
I was experimenting with vert.x and Karaf at the time.

as it's 5 years ago, a lot could have changed by now.

maybe it helps, maybe not 

regards, Achim


Am Mo., 18. März 2024 um 11:41 Uhr schrieb Jean-Baptiste Onofré <
j...@nanthrax.net>:

> Hi Michael
>
> It looks like a ClassLoading issue (the import is there but the class
> comes from another classloader).
>
> Did you try to set the TCCL just before creating the vertx HttpServer ?
>
> Regards
> JB
>
> On Sun, Mar 17, 2024 at 5:54 PM Michael Elbaz 
> wrote:
> >
> > Hello i use vertx on Karaf everything is ok but when i want to run an
> vertx http server (just http)
> >
> > vertx.createHttpServer()
> > .requestHandler(router)
> > .listen(8080)
> > .onSuccess(LOGGER::error)
> > .onFailure(LOGGER::error);
> >
> >
> > i get this error java.lang.NoClassDefFoundError:
> javax/net/ssl/TrustManager (i also get this one in karaf log:  Using the
> default address resolver as the dns resolver could not be loaded)
>


-- 

Apache Member
Apache Karaf  Committer & PMC
OPS4J Pax Web  Committer &
Project Lead
blog 
Co-Author of Apache Karaf Cookbook 


Re: java.lang.NoClassDefFoundError: javax/net/ssl/TrustManager

2024-03-18 Thread Jean-Baptiste Onofré
Hi Michael

It looks like a ClassLoading issue (the import is there but the class
comes from another classloader).

Did you try to set the TCCL just before creating the vertx HttpServer ?

Regards
JB

On Sun, Mar 17, 2024 at 5:54 PM Michael Elbaz  wrote:
>
> Hello i use vertx on Karaf everything is ok but when i want to run an vertx 
> http server (just http)
>
> vertx.createHttpServer()
> .requestHandler(router)
> .listen(8080)
> .onSuccess(LOGGER::error)
> .onFailure(LOGGER::error);
>
>
> i get this error java.lang.NoClassDefFoundError: javax/net/ssl/TrustManager 
> (i also get this one in karaf log:  Using the default address resolver as the 
> dns resolver could not be loaded)