Skip to content

Commit

Permalink
PAYARA-2677 Reset thread-context ClassLoader after de-serialization (#…
Browse files Browse the repository at this point in the history
…2622)

* reset thread-context ClassLoader after de-serialization

* reset old class loader even if it is null, if applicable
  • Loading branch information
lprimak authored and smillidge committed Apr 17, 2018
1 parent 84814b4 commit 48f1914
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public void close() {
private final InvocationManager invMgr;
}

@RequiredArgsConstructor
public static class ClassLoaderContext implements JavaEEContextUtil.Context {
private final ClassLoader oldClassLoader;
private final boolean resetOldClassLoader;

@Override
public void close() {
if (resetOldClassLoader) {
Utility.setContextClassLoader(oldClassLoader);
}
}
}

@RequiredArgsConstructor
public static class RequestContext implements JavaEEContextUtil.Context {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Context pushRequestContext() {
* set context class loader by component ID
*/
@Override
public void setApplicationClassLoader() {
public Context setApplicationClassLoader() {
ClassLoader cl = null;
if (capturedInvocation != null && capturedInvocation.getJNDIEnvironment() != null) {
cl = getClassLoaderForEnvironment((JndiNameEnvironment)capturedInvocation.getJNDIEnvironment());
Expand All @@ -135,8 +135,9 @@ else if (instanceComponentId != null) {
cl = getClassLoaderForEnvironment(compEnvMgr.getJndiNameEnvironment(instanceComponentId));
}
if (cl != null) {
Utility.setContextClassLoader(cl);
return new ContextImpl.ClassLoaderContext(Utility.setContextClassLoader(cl), true);
}
return new ContextImpl.ClassLoaderContext(null, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public interface JavaEEContextUtil {

/**
* set context class loader by internal state of this instance
* @return context so class loader can be reset
*/
void setApplicationClassLoader();
Context setApplicationClassLoader();

/**
* Sets the state of this instance from current invocation context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2016-2017] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2016-2018] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -45,6 +45,7 @@
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.StreamSerializer;
import java.io.IOException;
import org.glassfish.internal.api.JavaEEContextUtil.Context;

/**
*
Expand All @@ -69,8 +70,9 @@ public void write(ObjectDataOutput out, Object object) throws IOException {
public Object read(ObjectDataInput in) throws IOException {
String componentId = (String)delegate.read(in);
ctxUtil.setInstanceComponentId(componentId);
ctxUtil.setApplicationClassLoader();
return delegate.read(in);
try (Context ctx = ctxUtil.setApplicationClassLoader()) {
return delegate.read(in);
}
}

@Override
Expand Down

0 comments on commit 48f1914

Please sign in to comment.