Skip to content

Commit

Permalink
Merge pull request #30875 from njr-11/30638-include-queryinfo-failure…
Browse files Browse the repository at this point in the history
…s-in-introspector-output

include QueryInfo init failures in introspector output
  • Loading branch information
njr-11 authored Feb 27, 2025
2 parents 6559bef + d866838 commit e1efd2b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -519,6 +520,7 @@ public void initialize(ComponentMetaData metadata) throws IllegalStateException
*/
@Override
public void introspect(PrintWriter writer) {
List<RepositoryImpl<?>> repositoryImpls = new ArrayList<>();
Set<QueryInfo> queryInfos = new LinkedHashSet<>();
Set<EntityManagerBuilder> builders = new LinkedHashSet<>();

Expand Down Expand Up @@ -555,7 +557,7 @@ public void introspect(PrintWriter writer) {
repositoryProducers.forEach((appName, producers) -> {
writer.println(" for application " + appName);
for (RepositoryProducer<?> producer : producers) {
queryInfos.addAll(producer.introspect(writer, " "));
queryInfos.addAll(producer.introspect(writer, " ", repositoryImpls));
writer.println();
}
});
Expand Down Expand Up @@ -604,7 +606,12 @@ else if (entityInfoFuture.isDone())
writer.println("Query Information:");
for (List<QueryInfo> queryInfoList : queryInfoPerEntity.values())
for (QueryInfo queryInfo : queryInfoList) {
queryInfo.introspect(writer, " ");
CompletableFuture<QueryInfo> future = null;
for (RepositoryImpl<?> r : repositoryImpls)
if ((future = r.getQueryFuture(queryInfo.method)) != null)
break;

queryInfo.introspect(writer, " ", future);
writer.println();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4648,9 +4648,13 @@ else if (Iterator.class.equals(multiType))
*
* @param writer writes to the introspection file.
* @param indent indentation for lines.
* @param future future for this QueryInfo.
*/
@FFDCIgnore(Throwable.class)
@Trivial
public void introspect(PrintWriter writer, String indent) {
public void introspect(PrintWriter writer,
String indent,
CompletableFuture<QueryInfo> future) {
writer.println(indent + "QueryInfo@" + Integer.toHexString(hashCode()));
indent = indent + " ";
writer.println(indent + "entity: " + entityInfo);
Expand Down Expand Up @@ -4722,6 +4726,22 @@ else if (sortPositions == NONE_STATIC_SORT_ONLY)

writer.println(indent + "validate method parameters? " + validateParams);
writer.println(indent + "validate method result? " + validateResult);

if (future != null) {
writer.print(indent + "state: ");
if (future.isCancelled())
writer.println("cancelled");
else if (future.isDone())
try {
future.join();
writer.println("completed");
} catch (Throwable x) {
writer.println("failed");
Util.printStackTrace(x, writer, indent + " ", null);
}
else
writer.println("not completed");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,18 @@ private static final String getName(Parameter param, int index) {
: ("(" + (index + 1) + ")");
}

/**
* Used during introspection to report errors that occurred when processing
* repository methods.
*
* @param method repository method.
* @return future for the QueryInfo.
*/
@Trivial
public final CompletableFuture<QueryInfo> getQueryFuture(Method method) {
return queries.get(method);
}

/**
* Request an instance of a resource of the specified type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
Expand Down Expand Up @@ -67,6 +68,8 @@ public class RepositoryProducer<R> implements Producer<R>, ProducerFactory<R>, B
private final Class<?> primaryEntityClass;
private final DataProvider provider;
private final Map<Class<?>, List<QueryInfo>> queriesPerEntityClass;
private final AtomicReference<RepositoryImpl<?>> repositoryImplRef = //
new AtomicReference<>(); // most recently created instance
private final Class<?> repositoryInterface;

RepositoryProducer(Class<?> repositoryInterface, BeanManager beanMgr, DataProvider provider, DataExtension extension,
Expand Down Expand Up @@ -96,6 +99,7 @@ public void dispose(R repository) {
repository = r;

RepositoryImpl<?> handler = (RepositoryImpl<?>) Proxy.getInvocationHandler(repository);
repositoryImplRef.compareAndSet(handler, null);
handler.beanDisposed();
}

Expand Down Expand Up @@ -139,12 +143,20 @@ public Set<Type> getTypes() {
* Write information about this instance to the introspection file for
* Jakarta Data.
*
* @param writer writes to the introspection file.
* @param indent indentation for lines.
* @param writer writes to the introspection file.
* @param indent indentation for lines.
* @param repositoryImpls list to populate with a RepositoryImpl that is produced
* by this producer.
* @return list of QueryInfo for the caller to log.
*/
@Trivial
public List<QueryInfo> introspect(PrintWriter writer, String indent) {
public List<QueryInfo> introspect(PrintWriter writer,
String indent,
List<RepositoryImpl<?>> repositoryImpls) {
RepositoryImpl<?> repositoryImpl = repositoryImplRef.get();
if (repositoryImpl != null)
repositoryImpls.add(repositoryImpl);

List<QueryInfo> queryInfos = new ArrayList<>();

writer.println(indent + "RepositoryProducer@" + Integer.toHexString(hashCode()));
Expand Down Expand Up @@ -232,6 +244,8 @@ public R produce(CreationalContext<R> cc) {
instance = r;
}

repositoryImplRef.set(handler);

if (trace && tc.isEntryEnabled())
Tr.exit(this, tc, "produce", instance.toString());
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ public void testOutputContainsPrimaryEntity() {
assertLineFound(" primary entity: test.jakarta.data.errpaths.web.Voter");
}

/**
* Verify that introspector output contains the a failure that occurred when
* attempting to initialize query information for a repository method.
*/
@Test
public void testOutputContainsQueryInfoInitFailure() {
assertLineContains("java.lang.UnsupportedOperationException: CWWKD1003E:");
}

/**
* Verify that introspector output contains method signatures from an
* application-supplied record entity.
Expand Down

0 comments on commit e1efd2b

Please sign in to comment.