question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TimeZoneRegistryFactory instantiation not OSGI compliant

See original GitHub issue

Hello,

In a OSGI application, when I try to create a TimeZoneRegistryFactory with:

private static final TimeZoneRegistry DEFAULT_REGISTRY = DefaultTimeZoneRegistryFactory.getInstance().createRegistry();

the CalendarValidatorFactory class cannot be instantiated.

The root cause is that AbstractCalendarValidatorFactory instance initialization uses java.util.ServiceLoader#load(java.lang.Class<S>) that uses the Thread.currentThread().getContextClassLoader() classloader to instantiate the class. That does not work in an OSGI application.

We found a workaround:

        ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(AbstractCalendarValidatorFactory.class.getClassLoader());
            timeZone = DEFAULT_REGISTRY.getTimeZone(timeZoneId);
        } finally {
            Thread.currentThread().setContextClassLoader(currentCl);
        }

But a more OSGI compliant code should be better.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
benfortunacommented, Nov 27, 2016

Also, we’ll probably look for a replacement to ServiceLoader that supports all of these:

  • OSGi compatible
  • Android compatible
  • Doesn’t require a container (e.g. OSGi) to work

Probably some form of light-weight dependency injection (e.g. guice, etc.)

0reactions
benfortunacommented, Jan 3, 2019

Starting with release 3.0.5 I’m trying to phase out use of ServiceLoader in main ical4j library.

The plan is that ical4j-extensions library will provide mechanism for dynamically loading extensions using alternatives like ServiceLoader, OSGi, etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ServiceLoader osgi problems · Issue #143 - GitHub
There is a problem with class AbstractCalendarValidatorFactory similar to #125, running code in an osgi system. java.lang.
Read more >
How instantiate several OSGi services? - java - Stack Overflow
Have a look at the OSGi ServiceFactory; this allows you to instantiate services for different requesting bundles. You can read more about it...
Read more >
Unraveling Java and OSGi class loader problems
A NoClassDefFoundError is a result of the JVM failing to load a class that was present during compile time. That means the class...
Read more >
112 Declarative Services Specification - OSGi Compendium 7
A delayed component specifies a service, is not specified to be a factory component and does not have the immediate attribute of the...
Read more >
Introduction to OSGi | Baeldung
To execute an application in an OSGi environment, we have to pack it as an OSGi bundle and define the application entry point,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found