Skip to content

Commit

Permalink
Stop Narayana getting initialized in static init
Browse files Browse the repository at this point in the history
This stops it being runtime configurable, and also loads the XML parsers
in JVM mode.
  • Loading branch information
stuartwdouglas committed Jul 26, 2021
1 parent 131d86f commit cbf9a7e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.quarkus.narayana.interceptor;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.RollbackException;
Expand All @@ -16,15 +20,20 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.bootstrap.classloading.ClassLoaderLimiter;
import io.quarkus.test.QuarkusUnitTest;

public class TransactionalTest {

static final Set<String> loadedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>());

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TransactionalTest.TransactionalBean.class, TestXAResource.class,
TxAssertionData.class, TestException.class));
TxAssertionData.class, TestException.class))
.addClassLoaderEventListener(ClassLoaderLimiter.builder()
.neverLoadedRuntimeClassName("javax.xml.stream.XMLInputFactory").build());

@Inject
private TransactionManager tm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

@Dependent
public class NarayanaJtaProducers {
private static final javax.transaction.UserTransaction USER_TRANSACTION = UserTransaction.userTransaction();

@Produces
@ApplicationScoped
Expand All @@ -29,7 +28,7 @@ public UserTransactionRegistry userTransactionRegistry() {
@Produces
@ApplicationScoped
public javax.transaction.UserTransaction userTransaction() {
return USER_TRANSACTION;
return UserTransaction.userTransaction();
}

@Produces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.enterprise.context.ContextNotActiveException;
Expand All @@ -24,6 +25,7 @@
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InjectableContext;
import io.quarkus.arc.impl.ContextInstanceHandleImpl;
import io.quarkus.arc.impl.LazyValue;

/**
* {@link javax.enterprise.context.spi.Context} class which defines the {@link TransactionScoped} context.
Expand All @@ -32,16 +34,27 @@ public class TransactionContext implements InjectableContext {
// marker object to be put as a key for SynchronizationRegistry to gather all beans created in the scope
private static final Object TRANSACTION_CONTEXT_MARKER = new Object();

private final TransactionSynchronizationRegistry transactionSynchronizationRegistry = new TransactionSynchronizationRegistryImple();
private final TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
private final LazyValue<TransactionSynchronizationRegistry> transactionSynchronizationRegistry = new LazyValue<>(
new Supplier<TransactionSynchronizationRegistry>() {
@Override
public TransactionSynchronizationRegistry get() {
return new TransactionSynchronizationRegistryImple();
}
});
private final LazyValue<TransactionManager> transactionManager = new LazyValue<>(new Supplier<TransactionManager>() {
@Override
public TransactionManager get() {
return com.arjuna.ats.jta.TransactionManager.transactionManager();
}
});

@Override
public void destroy() {
if (!isActive()) {
return;
}

TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
return;
Expand All @@ -54,7 +67,7 @@ public void destroy(Contextual<?> contextual) {
if (!isActive()) {
return;
}
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
return;
Expand All @@ -69,7 +82,7 @@ public ContextState getState() {
}

ContextState result;
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry
TransactionContextState contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);
if (contextState == null) {
result = new TransactionContextState(getCurrentTransaction());
Expand All @@ -95,12 +108,12 @@ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContex
}

TransactionContextState contextState;
contextState = (TransactionContextState) transactionSynchronizationRegistry
contextState = (TransactionContextState) transactionSynchronizationRegistry.get()
.getResource(TRANSACTION_CONTEXT_MARKER);

if (contextState == null) {
contextState = new TransactionContextState(getCurrentTransaction());
transactionSynchronizationRegistry.putResource(TRANSACTION_CONTEXT_MARKER, contextState);
transactionSynchronizationRegistry.get().putResource(TRANSACTION_CONTEXT_MARKER, contextState);
}

ContextInstanceHandle<T> instanceHandle = contextState.get(contextual);
Expand Down Expand Up @@ -150,7 +163,7 @@ public boolean isActive() {

private Transaction getCurrentTransaction() {
try {
return transactionManager.getTransaction();
return transactionManager.get().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Error getting the current transaction", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.bootstrap.classloading;

import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -14,13 +15,15 @@ public final class ClassLoaderLimiter implements ClassLoaderEventListener {

private final Set<String> vetoedResources;
private final Set<String> vetoedClasses;
private final Set<String> vetoedRuntimeClasses;
private final Set<String> atMostOnceResources;
private final Set<String> onHitPrintStacktrace;
private final boolean traceAllResourceLoad;

private ClassLoaderLimiter(Builder builder) {
vetoedClasses = builder.vetoedClasses;
vetoedResources = builder.vetoedResources;
vetoedRuntimeClasses = builder.vetoedRuntimeClasses;
atMostOnceResources = builder.atMostOnceResources;
onHitPrintStacktrace = builder.onHitPrintStacktrace;
traceAllResourceLoad = builder.traceAllResourceLoad;
Expand Down Expand Up @@ -67,6 +70,9 @@ public void loadClass(String className, String classLoaderName) {
if (vetoedClasses.contains(className)) {
throw new IllegalStateException(
"Attempted to load vetoed class '" + className + "' from classloader " + classLoaderName);
} else if (vetoedRuntimeClasses.contains(className) && classLoaderName.toLowerCase(Locale.ROOT).contains("runtime")) {
throw new IllegalStateException(
"Attempted to load vetoed class '" + className + "' from classloader " + classLoaderName);
}
}

Expand All @@ -77,6 +83,7 @@ public static Builder builder() {
public static class Builder {
private final Set<String> vetoedResources = new TreeSet<>();
private final Set<String> vetoedClasses = new TreeSet<>();
private final Set<String> vetoedRuntimeClasses = new TreeSet<>();
private final Set<String> atMostOnceResources = new TreeSet<>();
private final Set<String> onHitPrintStacktrace = new TreeSet<>();
private boolean traceAllResourceLoad = false;
Expand Down Expand Up @@ -135,6 +142,30 @@ public Builder neverLoadedClassName(String vetoedClassName) {
return this;
}

/**
* List a fully qualified class name as one that you don't expect to be loaded at runtime.
* If there is an attempt of loading the matched class, a runtime exception will be thrown instead:
* useful for running integration tests to verify your assumptions.
*
* DO NOT list the name by doing using <code>literal.class.getName()</code> as this will implicitly get you
* to load the class during the test, and produce a failure.
*
* Limitations: if the class is being loaded using the bootstrap classloader we
* can't check it. Most Quarkus extensions and frameworks will not use the bootstrap classloader,
* but some code could make use of it explicitly.
*
* @param vetoedClassName the fully qualified class name
* @return this, for method chaining.
*/
public Builder neverLoadedRuntimeClassName(String vetoedClassName) {

This comment has been minimized.

Copy link
@Sanne

Sanne Jul 26, 2021

Member

Nice!

Objects.requireNonNull(vetoedClassName);
final boolean add = vetoedRuntimeClasses.add(vetoedClassName);
if (!add)
throw new ClassLoaderLimiterConsistencyException(
"never loaded class listed multiple times: " + vetoedClassName);
return this;
}

/**
* Useful to check that a resource is being loaded only once, or never.
* If there is an attempt of loading the matched resource more than once, a runtime exception will be thrown instead:
Expand Down

0 comments on commit cbf9a7e

Please sign in to comment.