Skip to content

fix(OSGI) Rest plugin fails when redeployed from OSGI Refs: #31348 #32274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

fabrizzio-dotCMS
Copy link
Contributor

@fabrizzio-dotCMS fabrizzio-dotCMS commented May 29, 2025

Fixed IllegalStateException: object is not an instance of declaring class that occurs when OSGi bundles are unloaded and reloaded in Jersey REST endpoints.
Root Cause: Jersey's default ResourceMethodInvocationHandlerFactory uses a static DEFAULT_HANDLER that captures method references from the initial ClassLoader. When OSGi bundles are reloaded:

Method objects retain references to the original ClassLoader (A)
Target instances are created with the new ClassLoader (B)
method.invoke(target, args) fails because target is not an instance of the method's declaring class from different ClassLoader

Solution
Implemented a proactive ClassLoader resolution strategy by replacing Jersey's default InvocationHandler:

Custom ResourceMethodInvocationHandlerProvider (DotResourceMethodInvocationHandlerProvider)

Registered with @priority(1) to override Jersey's default handler
Implements proactive ClassLoader conflict detection and resolution

Proactive ClassLoader Resolution

BEFORE method invocation, checks if target.getClass().getClassLoader() differs from method.getDeclaringClass().getClassLoader()
If different, resolves the correct method from target's ClassLoader using target.getClass().getMethod()
GUARANTEES method and target share the same ClassLoader before invocation

Enhanced Error Handling

Downgrades original Jersey exception logging from SEVERE to WARN level
Removes verbose stack traces to reduce log noise
Provides clear diagnostic messages for ClassLoader conflicts

Testing
Verified fix resolves the original IllegalStateException during OSGi bundle reload cycles without impacting normal REST endpoint functionality.

This PR fixes: #31348

@fabrizzio-dotCMS fabrizzio-dotCMS marked this pull request as ready for review May 30, 2025 15:27
@fabrizzio-dotCMS fabrizzio-dotCMS added this pull request to the merge queue May 30, 2025
@fabrizzio-dotCMS fabrizzio-dotCMS removed this pull request from the merge queue due to a manual request May 30, 2025
@fabrizzio-dotCMS fabrizzio-dotCMS added this pull request to the merge queue Jun 2, 2025
Merged via the queue into main with commit 2c479fa Jun 2, 2025
37 checks passed
@fabrizzio-dotCMS fabrizzio-dotCMS deleted the issue-31348-object-is-not-instance-declaring-class branch June 2, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OSGI Rest Resource fails upon Redeploy with message: IllegalArgumentException: object is not an instance of declaring class
3 participants