TimeZoneRegistryFactory instantiation not OSGI compliant
See original GitHub issueHello,
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:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Also, we’ll probably look for a replacement to ServiceLoader that supports all of these:
Probably some form of light-weight dependency injection (e.g. guice, etc.)
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.