Skip to content

Commit

Permalink
Value object for context types and more clear usage
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Oct 15, 2022
1 parent 67d980f commit 80181bf
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.deployment.annotation.handlers.ContextualResourceDefinition;
import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.ConcurrencyContextType;
import com.sun.enterprise.transaction.api.JavaEETransactionManager;

import jakarta.inject.Inject;
Expand All @@ -35,8 +35,6 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.naming.InitialContext;
import javax.naming.NamingException;

Expand All @@ -62,8 +60,8 @@
import org.glassfish.resourcebase.resources.naming.ResourceNamingService;
import org.jvnet.hk2.annotations.Service;

import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.Classloader;
import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.WorkArea;
import static com.sun.enterprise.deployment.types.StandardContextType.*;
import static com.sun.enterprise.deployment.types.StandardContextType.WorkArea;
import static java.util.Collections.emptySet;

/**
Expand Down Expand Up @@ -245,10 +243,7 @@ public ContextServiceImpl findOrCreateContextService(ContextualResourceDefinitio
if (lookup2 != null) {
return lookup2;
}
// Create default
Set<String> provided = Set.of(StandardContextType.Classloader, StandardContextType.JNDI,
StandardContextType.Security, StandardContextType.WorkArea).stream().map(Enum::name)
.collect(Collectors.toSet());
Set<ConcurrencyContextType> provided = Set.of(Classloader, JNDI, Security, WorkArea);
ConcurrentServiceCfg config = new ConcurrentServiceCfg(jndiName, provided, null);
return contextServiceMap.computeIfAbsent(jndiName, n -> createContextService(jndiName, config, true));
}
Expand All @@ -262,8 +257,8 @@ public synchronized ContextServiceImpl getContextService(ContextServiceCfg confi

public synchronized ContextServiceImpl createContextService(ContextServiceCfg config) {
LOG.log(Level.FINE, "createContextService(config={0})", config);
boolean keepTxUnchanged = config.getUnchangedContexts().contains(WorkArea.name());
boolean clearTx = config.getClearedContexts().contains(WorkArea.name());
boolean keepTxUnchanged = config.getUnchangedContexts().contains(WorkArea);
boolean clearTx = config.getClearedContexts().contains(WorkArea);
TransactionSetupProvider txSetupProvider = createTxSetupProvider(keepTxUnchanged, clearTx);
ContextSetupProvider ctxSetupProvider = new ContextSetupProviderImpl(config.getPropagatedContexts(),
config.getClearedContexts(), config.getUnchangedContexts());
Expand Down Expand Up @@ -333,12 +328,12 @@ private ContextServiceImpl createContextService(String contextServiceJndiName, C
LOG.log(Level.FINE, "createContextService(contextServiceJndiName={0}, config={1}, cleanupTransaction={2})",
new Object[] {contextServiceJndiName, config, cleanupTransaction});
// if the context service is not known, create it
Set<String> propagated = config.getContextInfo();
final Set<String> cleared;
final Set<ConcurrencyContextType> propagated = config.getContextInfo();
final Set<ConcurrencyContextType> cleared;
final boolean clearTx;
if (cleanupTransaction && !propagated.contains(WorkArea.name())) {
if (cleanupTransaction && !propagated.contains(WorkArea)) {
// pass the cleanup transaction in list of cleared handlers
cleared = Set.of(WorkArea.name());
cleared = Set.of(WorkArea);
clearTx = true;
} else {
cleared = emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.glassfish.concurrent.runtime;


import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.ConcurrencyContextType;
import com.sun.enterprise.deployment.types.CustomContextType;
import com.sun.enterprise.deployment.types.StandardContextType;

import jakarta.enterprise.concurrent.spi.ThreadContextProvider;
import jakarta.enterprise.concurrent.spi.ThreadContextSnapshot;
Expand All @@ -27,13 +29,13 @@
import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;

import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.standardize;
import static java.util.ServiceLoader.load;

/**
Expand All @@ -43,16 +45,16 @@ public class ContextSetup implements Serializable {
private static final long serialVersionUID = 7817957604183520917L;
private static final Logger LOG = System.getLogger(ContextSetup.class.getName());

private final Set<String> contextPropagate;
private final Set<String> contextClear;
private final Set<String> contextUnchanged;
private transient Map<String, ThreadContextProvider> allThreadContextProviders;
private final Set<ConcurrencyContextType> contextPropagate;
private final Set<ConcurrencyContextType> contextClear;
private final Set<ConcurrencyContextType> contextUnchanged;
private transient Map<CustomContextType, ThreadContextProvider> allThreadContextProviders;


public ContextSetup(Set<String> propagated, Set<String> cleared, Set<String> unchanged) {
this.contextPropagate = standardize(propagated);
this.contextClear = standardize(cleared);
this.contextUnchanged = standardize(unchanged);
public ContextSetup(Set<ConcurrencyContextType> propagated, Set<ConcurrencyContextType> cleared, Set<ConcurrencyContextType> unchanged) {
this.contextPropagate = new HashSet<>(propagated);
this.contextClear = new HashSet<>(cleared);
this.contextUnchanged = new HashSet<>(unchanged);
}


Expand All @@ -64,17 +66,17 @@ public void reloadProviders(final ClassLoader loader) {


public boolean isPropagated(StandardContextType contextType) {
return contextPropagate.contains(contextType.name());
return contextPropagate.contains(contextType);
}


public boolean isClear(StandardContextType contextType) {
return contextClear.contains(contextType.name());
return contextClear.contains(contextType);
}


public boolean isUnchanged(StandardContextType contextType) {
return contextUnchanged.contains(contextType.name());
return contextUnchanged.contains(contextType);
}


Expand All @@ -98,48 +100,49 @@ public String toString() {
}


private static Map<String, ThreadContextProvider> loadAllProviders(ClassLoader loader) {
private static Map<CustomContextType, ThreadContextProvider> loadAllProviders(ClassLoader loader) {
LOG.log(Level.TRACE, "Using classloader: {0}", loader);
ServiceLoader<ThreadContextProvider> services = load(ThreadContextProvider.class, loader);
Map<String, ThreadContextProvider> providers = new HashMap<>();
Map<CustomContextType, ThreadContextProvider> providers = new HashMap<>();
for (ThreadContextProvider service : services) {
String serviceName = service.getThreadContextType();
providers.put(serviceName, service);
CustomContextType ctxType = new CustomContextType(service.getThreadContextType());
providers.put(ctxType, service);
}
LOG.log(Level.DEBUG, "Detected ThreadContextProvider implementations: {0}", providers);
return providers;
}


private static void addRemaining(Set<String> propagated, Set<String> clear, Set<String> unchanged,
Map<String, ThreadContextProvider> allThreadContextProviders) {
Set<String> remaining = chooseSet(propagated, clear, unchanged);
private static void addRemaining(Set<ConcurrencyContextType> propagated, Set<ConcurrencyContextType> clear,
Set<ConcurrencyContextType> unchanged, Map<CustomContextType, ThreadContextProvider> allThreadContextProviders) {
Set<ConcurrencyContextType> remaining = chooseSet(propagated, clear, unchanged);
for (StandardContextType contextType : StandardContextType.values()) {
if (contextType == StandardContextType.Remaining) {
continue;
}
final String name = contextType.name();
addIfNotInAnotherSet(name, remaining, propagated, clear, unchanged);
addIfNotInAnotherSet(contextType, remaining, propagated, clear, unchanged);
}
for (String name : allThreadContextProviders.keySet()) {
for (CustomContextType name : allThreadContextProviders.keySet()) {
addIfNotInAnotherSet(name, remaining, propagated, clear, unchanged);
}
}


private static Set<String> chooseSet(Set<String> propagated, Set<String> clear, Set<String> unchanged) {
if (clear.contains(StandardContextType.Remaining.name())) {
private static Set<ConcurrencyContextType> chooseSet(Set<ConcurrencyContextType> propagated,
Set<ConcurrencyContextType> clear, Set<ConcurrencyContextType> unchanged) {
if (clear.contains(StandardContextType.Remaining)) {
return clear;
} else if (unchanged.contains(StandardContextType.Remaining.name())) {
} else if (unchanged.contains(StandardContextType.Remaining)) {
return unchanged;
} else {
return propagated;
}
}


private static void addIfNotInAnotherSet(String name, Set<String> remaining, Set<String> propagated,
Set<String> clear, Set<String> unchanged) {
private static void addIfNotInAnotherSet(ConcurrencyContextType name, Set<ConcurrencyContextType> remaining,
Set<ConcurrencyContextType> propagated, Set<ConcurrencyContextType> clear,
Set<ConcurrencyContextType> unchanged) {
if (propagated.contains(name) || clear.contains(name) || unchanged.contains(name)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.ConcurrencyContextType;
import com.sun.enterprise.deployment.types.StandardContextType;
import com.sun.enterprise.security.SecurityContext;
import com.sun.enterprise.transaction.api.JavaEETransactionManager;
import com.sun.enterprise.util.Utility;
Expand All @@ -44,10 +45,10 @@
import org.glassfish.enterprise.concurrent.spi.ContextSetupProvider;
import org.glassfish.internal.deployment.Deployment;

import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.Classloader;
import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.JNDI;
import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.Security;
import static com.sun.enterprise.deployment.annotation.handlers.StandardContextType.WorkArea;
import static com.sun.enterprise.deployment.types.StandardContextType.Classloader;
import static com.sun.enterprise.deployment.types.StandardContextType.JNDI;
import static com.sun.enterprise.deployment.types.StandardContextType.Security;
import static com.sun.enterprise.deployment.types.StandardContextType.WorkArea;
import static jakarta.enterprise.concurrent.ManagedTask.SUSPEND;
import static jakarta.enterprise.concurrent.ManagedTask.TRANSACTION;
import static jakarta.enterprise.concurrent.ManagedTask.USE_TRANSACTION_OF_EXECUTION_THREAD;
Expand Down Expand Up @@ -75,7 +76,8 @@ public class ContextSetupProviderImpl implements ContextSetupProvider {

private final ContextSetup setup;

public ContextSetupProviderImpl(Set<String> propagated, Set<String> cleared, Set<String> unchanged) {
public ContextSetupProviderImpl(Set<ConcurrencyContextType> propagated, Set<ConcurrencyContextType> cleared,
Set<ConcurrencyContextType> unchanged) {
this.setup = new ContextSetup(propagated, cleared, unchanged);
ConcurrentRuntime runtime = ConcurrentRuntime.getRuntime();
this.invocationManager = runtime.getInvocationManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.sun.enterprise.deployment.ContextServiceDefinitionDescriptor;
import com.sun.enterprise.deployment.ResourceDescriptor;
import com.sun.enterprise.deployment.annotation.handlers.ContextServiceDefinitionData;
import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.StandardContextType;

import jakarta.enterprise.concurrent.ContextService;
import jakarta.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

package org.glassfish.concurrent.runtime.deployer.cfg;

import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.ConcurrencyContextType;
import com.sun.enterprise.deployment.types.CustomContextType;
import com.sun.enterprise.deployment.types.StandardContextType;

import jakarta.enterprise.concurrent.ContextServiceDefinition;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -31,21 +37,40 @@ private CfgParser() {
}


static Set<String> parseContextInfo(String contextInfo, String contextInfoEnabled) {
Set<String> contextTypeArray = new HashSet<>();
static Set<ConcurrencyContextType> parseContextInfo(String contextInfo, String contextInfoEnabled) {
if (contextInfo == null || !Boolean.TRUE.toString().equalsIgnoreCase(contextInfoEnabled)) {
return StandardContextType.names();
return Set.of(StandardContextType.values());
}
String[] strings = contextInfo.split(",");
for (String string : strings) {
String contextType = string.trim();
if (contextType.isEmpty()) {
List<String> strings = Arrays.asList(contextInfo.split(","));
return standardize(strings);
}


/**
* Converts strings from annotations and xml to enums.
* If the provided context name is not supported, it is returned without changes.
*
* @param contexts
* @return set of enum names.
*/
public static Set<ConcurrencyContextType> standardize(Iterable<String> contexts) {
Set<ConcurrencyContextType> result = new HashSet<>();
for (String input : contexts) {
final String context = input.trim();
if (context.isEmpty()) {
continue;
}
StandardContextType standardContextType = StandardContextType.parse(contextType);
contextTypeArray.add(standardContextType == null ? contextType : standardContextType.name());
if (ContextServiceDefinition.TRANSACTION.equalsIgnoreCase(context)) {
result.add(StandardContextType.WorkArea);
}
if (ContextServiceDefinition.APPLICATION.equals(context)) {
result.add(StandardContextType.Classloader);
result.add(StandardContextType.JNDI);
}
StandardContextType contextType = StandardContextType.parse(context);
result.add(contextType == null ? new CustomContextType(context) : contextType);
}
return contextTypeArray;
return result;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package org.glassfish.concurrent.runtime.deployer.cfg;

import com.sun.enterprise.deployment.annotation.handlers.StandardContextType;
import com.sun.enterprise.deployment.types.ConcurrencyContextType;
import com.sun.enterprise.deployment.types.StandardContextType;

import java.io.Serializable;
import java.util.Collections;
Expand All @@ -27,22 +28,22 @@ public final class ConcurrentServiceCfg implements Serializable {
private static final long serialVersionUID = -9039607497553448223L;

private final String jndiName;
private final Set<String> contextInfo;
private final Set<ConcurrencyContextType> contextInfo;
private final String context;

public ConcurrentServiceCfg(String jndiName) {
this(jndiName, Collections.emptySet(), null);
}


public ConcurrentServiceCfg(String jndiName, Set<String> contextInfo) {
public ConcurrentServiceCfg(String jndiName, Set<ConcurrencyContextType> contextInfo) {
this.jndiName = jndiName;
this.contextInfo = contextInfo;
this.context = null;
}


public ConcurrentServiceCfg(String jndiName, Set<String> contextInfo, String context) {
public ConcurrentServiceCfg(String jndiName, Set<ConcurrencyContextType> contextInfo, String context) {
this.jndiName = jndiName;
this.contextInfo = contextInfo;
this.context = context;
Expand All @@ -51,7 +52,7 @@ public ConcurrentServiceCfg(String jndiName, Set<String> contextInfo, String con

public ConcurrentServiceCfg(String jndiName, StandardContextType contextInfo, String context) {
this.jndiName = jndiName;
this.contextInfo = Set.of(contextInfo.name());
this.contextInfo = Set.of(contextInfo);
this.context = context;
}

Expand All @@ -61,7 +62,7 @@ public String getJndiName() {
}


public Set<String> getContextInfo() {
public Set<ConcurrencyContextType> getContextInfo() {
return contextInfo;
}

Expand Down

0 comments on commit 80181bf

Please sign in to comment.