Skip to content

Commit

Permalink
FISH-790 Merge pull request payara#5010 from flowlogix/Refactor-CTX-Util
Browse files Browse the repository at this point in the history
FISH-790 Refactor, fix bugs in  JavaEE Context Utility
  • Loading branch information
MattGill98 authored and Pandrex247 committed Sep 27, 2021
1 parent f92fe87 commit e58cbd6
Show file tree
Hide file tree
Showing 21 changed files with 894 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.deployment.util;

Expand Down Expand Up @@ -142,8 +142,7 @@ public void event(Event<?> event) {
parseResources(deploymentContext, application, appResources);

// Ensure we have a valid component invocation before triggering lookups
contextUtil.setEmptyInvocation();
try (Context ctx = contextUtil.pushContext()) {
try (Context ctx = contextUtil.empty().pushContext()) {
validateResources(deploymentContext, application, appResources);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2020] [Payara Foundation and/or its affiliates]

package com.sun.ejb;

Expand All @@ -56,14 +57,17 @@ public EjbInvocationFactory(String compEnvId, Container container) {
}

public EjbInvocation create() {
return new EjbInvocation(compEnvId, container);
EjbInvocation ejbInv = new EjbInvocation(compEnvId, container);
ejbInv.jndiEnvironment = container.getEjbDescriptor();
return ejbInv;
}

public <C extends ComponentContext> EjbInvocation create(Object ejb, C ctx) {
EjbInvocation ejbInv = new EjbInvocation(compEnvId, container);
ejbInv.ejb = ejb;
ejbInv.instance = ejb;
ejbInv.context = ctx;
ejbInv.jndiEnvironment = container.getEjbDescriptor();

return ejbInv;
}
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-2019 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2020 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 @@ -46,8 +46,10 @@
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Optional;
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.api.JavaEEContextUtil;
import org.glassfish.internal.api.JavaEEContextUtil.Context;

/**
* Packages up an object into a Serializable value
Expand All @@ -66,7 +68,7 @@ public PayaraValueHolder() {

public PayaraValueHolder(T value) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(Globals.getDefaultHabitat().getService(JavaEEContextUtil.class).getInstanceComponentId());
oos.writeObject(Globals.getDefaultHabitat().getService(JavaEEContextUtil.class).getInvocationComponentId());
oos.writeObject(value);
data = baos.toByteArray();
}
Expand All @@ -77,17 +79,21 @@ public T getValue() throws IOException, ClassNotFoundException {
String componentId = null;
try (ByteArrayInputStream bais = new ByteArrayInputStream(data); PayaraTCCLObjectInputStream ois = new PayaraTCCLObjectInputStream(bais)) {
componentId = (String)ois.readObject();
Object result = ois.readObject();
return (T)result;
JavaEEContextUtil ctxUtil = Globals.getDefaultHabitat().getService(JavaEEContextUtil.class);
JavaEEContextUtil.Instance inst = Optional.ofNullable(componentId)
.map(ctxUtil::fromComponentId).orElse(ctxUtil.empty());
try (Context ctx = inst.setApplicationClassLoader()) {
return (T) ois.readObject();
}
}
catch (ClassNotFoundException ex) {
String invocationComponentId = Globals.getDefaultHabitat().getService(JavaEEContextUtil.class).getInstanceComponentId();
String invocationComponentId = Globals.getDefaultHabitat().getService(JavaEEContextUtil.class).getInvocationComponentId();
if (componentId == null){
componentId = "";
}
if (componentId.equals(invocationComponentId)) {
throw new ClassNotFoundException(String.format("Wrong application: expected %s bug got %s", componentId, invocationComponentId),
new IllegalStateException("Wrong Application"));
if (!componentId.equals(invocationComponentId)) {
throw new ClassNotFoundException(String.format("Wrong application: expected %s but got %s", componentId, invocationComponentId),
new IllegalStateException("Wrong Application", ex));
} else {
throw ex;
}
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-2018 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2020 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 @@ -39,8 +39,6 @@
*/
package fish.payara.micro.cdi.extension;

import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.util.DOLUtils;
import com.sun.enterprise.util.Utility;
import fish.payara.micro.cdi.Outbound;
import fish.payara.micro.cdi.Inbound;
Expand All @@ -58,7 +56,6 @@
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
Expand All @@ -71,8 +68,6 @@
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.api.JavaEEContextUtil;
import org.glassfish.internal.api.JavaEEContextUtil.Context;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.internal.deployment.ExtendedDeploymentContext;

/**
*
Expand All @@ -89,15 +84,15 @@ public class ClusteredCDIEventBusImpl implements CDIEventListener, ClusteredCDIE

private ManagedExecutorService managedExecutorService;

private JavaEEContextUtil ctxUtil;
private JavaEEContextUtil.Instance ctxUtil;

private final static String INSTANCE_PROPERTY = "InstanceName";

private final static String EVENT_PROPERTY = "EventName";

@PostConstruct
void postConstruct() {
ctxUtil = Globals.getDefaultHabitat().getService(JavaEEContextUtil.class);
ctxUtil = Globals.getDefaultHabitat().getService(JavaEEContextUtil.class).currentInvocation();
try {
InitialContext ctx = new InitialContext();
managedExecutorService = (ManagedExecutorService) ctx.lookup("java:comp/DefaultManagedExecutorService");
Expand Down Expand Up @@ -144,50 +139,39 @@ public void eventReceived(final PayaraClusteredCDIEvent event) {
}

try(Context ctx = ctxUtil.pushContext()) {
managedExecutorService.submit(new Runnable() {
@Override
public void run() {
ClassLoader oldCL = Utility.getClassLoader();
try {
ClassLoader invocationClassLoader = ctxUtil.getInvocationClassLoader();
if (invocationClassLoader != null) { // null in case of an event from server such as CDI notifier
Utility.setContextClassLoader(invocationClassLoader);
managedExecutorService.submit(() -> {
try (final Context ctx1 = ctxUtil.setApplicationClassLoader()) {
// create the set of qualifiers for the event
// first add Inbound qualifier with the correct properties
Set<Annotation> qualifiers = new HashSet<>();
Serializable eventPayload = event.getPayload();
Inbound inbound = new Inbound() {
@Override
public String eventName() {
return event.getProperty(EVENT_PROPERTY);
}

// create the set of qualifiers for the event
// first add Inbound qualifier with the correct properties
Set<Annotation> qualifiers = new HashSet<>();
Serializable eventPayload = event.getPayload();
Inbound inbound = new Inbound() {
@Override
public String eventName() {
return event.getProperty(EVENT_PROPERTY);
}

@Override
public Class<? extends Annotation> annotationType() {
return Inbound.class;
}
};
qualifiers.add(inbound);

// Now create Qualifiers for the sent event qualifiers
Set<Annotation> receivedQualifiers = event.getQualifiers();
for (Annotation receivedQualifier : receivedQualifiers) {
// strip out OutBound as we don't want it even though it was sent over
if (!(receivedQualifier instanceof Outbound)) {
qualifiers.add(receivedQualifier);
}
@Override
public Class<? extends Annotation> annotationType() {
return Inbound.class;
}
};
qualifiers.add(inbound);

// Now create Qualifiers for the sent event qualifiers
Set<Annotation> receivedQualifiers = event.getQualifiers();
for (Annotation receivedQualifier : receivedQualifiers) {
// strip out OutBound as we don't want it even though it was sent over
if (!(receivedQualifier instanceof Outbound)) {
qualifiers.add(receivedQualifier);
}
Annotation annotations[] = qualifiers.toArray(new Annotation[0]);
bm.fireEvent(eventPayload,annotations);
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(ClusteredCDIEventBusImpl.class.getName())
.log(ex.getCause() instanceof IllegalStateException? Level.FINE : Level.INFO,
"Received Event but could not process it", ex);
} finally {
Utility.setContextClassLoader(oldCL);
}
Annotation annotations[] = qualifiers.toArray(new Annotation[0]);
bm.fireEvent(eventPayload,annotations);
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(ClusteredCDIEventBusImpl.class.getName())
.log(ex.getCause() instanceof IllegalStateException? Level.FINE : Level.INFO,
"Received Event but could not process it", ex);
}
});
}
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-2019] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2016-2020] 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 @@ -39,6 +39,7 @@
*/
package fish.payara.appserver.context;

import com.sun.enterprise.container.common.spi.util.ComponentEnvManager;
import com.sun.enterprise.util.Utility;
import java.util.Map;
import org.glassfish.api.invocation.ComponentInvocation;
Expand All @@ -52,7 +53,6 @@
* @author lprimak
*/
class ContextImpl {

public static class Context implements JavaEEContextUtil.Context {
@Override
public void close() {
Expand All @@ -62,13 +62,23 @@ public void close() {
}
}

@Override
public boolean isValid() {
return invocation != null && !JavaEEContextUtilImpl.isLeaked(compEnvMgr,
invocation, invocation.getComponentId());
}


private final ComponentInvocation invocation;
private final InvocationManager invMgr;
private final ComponentEnvManager compEnvMgr;
private final ClassLoader oldClassLoader;

public Context(ComponentInvocation invocation, InvocationManager invMgr, ClassLoader oldClassLoader) {
public Context(ComponentInvocation invocation, InvocationManager invMgr, ComponentEnvManager compEnvMgr,
ClassLoader oldClassLoader) {
this.invocation = invocation;
this.invMgr = invMgr;
this.compEnvMgr = compEnvMgr;
this.oldClassLoader = oldClassLoader;
}
}
Expand Down Expand Up @@ -111,5 +121,10 @@ public RequestContext(org.glassfish.internal.api.JavaEEContextUtil.Context rootC
this.ctx = ctx;
this.storage = storage;
}

@Override
public boolean isValid() {
return rootCtx.isValid();
}
}
}
Loading

0 comments on commit e58cbd6

Please sign in to comment.