Skip to content

Commit

Permalink
Issue OpenLiberty#30638 introspector info that can be provided before…
Browse files Browse the repository at this point in the history
… app start
  • Loading branch information
njr-11 committed Jan 28, 2025
1 parent 14c50f6 commit 6aa6384
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;

import javax.sql.DataSource;
Expand Down Expand Up @@ -70,6 +71,7 @@
import io.openliberty.checkpoint.spi.CheckpointPhase;
import io.openliberty.data.internal.persistence.cdi.DataExtension;
import io.openliberty.data.internal.persistence.cdi.FutureEMBuilder;
import io.openliberty.data.internal.persistence.cdi.RepositoryProducer;
import io.openliberty.data.internal.persistence.metadata.DataComponentMetaData;
import io.openliberty.data.internal.persistence.metadata.DataModuleMetaData;
import io.openliberty.data.internal.version.DataVersionCompatibility;
Expand Down Expand Up @@ -208,6 +210,13 @@ public class DataProvider implements //
private final ConcurrentHashMap<String, DataComponentMetaData> metadatas = //
new ConcurrentHashMap<>();

/**
* Map of application name to list of producers of repository beans.
* Entries are removed when the application stops.
*/
final Map<String, Queue<RepositoryProducer<?>>> repositoryProducers = //
new ConcurrentHashMap<>();

/**
* For creating resource references.
*/
Expand Down Expand Up @@ -309,6 +318,8 @@ public void applicationStopped(ApplicationInfo appInfo) {
// Try to order removals based on dependencies, so that we remove first
// what might depend on the others.

repositoryProducers.remove(appName);

Queue<ServiceRegistration<DDLGenerationParticipant>> ddlgenRegistrations = //
ddlgeneratorsAllApps.remove(appName);
if (ddlgenRegistrations != null)
Expand Down Expand Up @@ -386,6 +397,8 @@ protected void deactivate(ComponentContext cc) {
// Try to order removals based on dependencies, so that we remove first
// what might depend on the others.

repositoryProducers.clear();

// Remove and unregister ddl generation services that our extension generated.
for (Iterator<Queue<ServiceRegistration<DDLGenerationParticipant>>> it = //
ddlgeneratorsAllApps.values().iterator(); it.hasNext();) {
Expand Down Expand Up @@ -496,12 +509,16 @@ public void initialize(ComponentMetaData metadata) throws IllegalStateException

/**
* Write to the introspection file for Jakarta Data.
*
* @param writer writes to the introspection file.
*/
@Override
public void introspect(PrintWriter writer) throws Exception {
public void introspect(PrintWriter writer) {
writer.println("compatibility: " + compat.getClass().getSimpleName());
writer.println("createTables? " + createTables);
writer.println("dropTables? " + dropTables);
writer.println("logValues for " + logValues);

writer.println();
writer.println("databaseStore config:");
dbStoreConfigAllApps.forEach((appName, dbStoreToConfig) -> {
Expand All @@ -514,7 +531,23 @@ public void introspect(PrintWriter writer) throws Exception {
});
});

// TODO more information
writer.println();
writer.println("EntityManager builders for unstarted applications:");
futureEMBuilders.forEach((appName, futureEMBuilders) -> {
writer.println(" for application " + appName);
for (FutureEMBuilder futureEMBuilder : futureEMBuilders)
futureEMBuilder.introspect(writer, " ");
});

writer.println();
writer.println("Repository Producers:");
repositoryProducers.forEach((appName, producers) -> {
writer.println(" for application " + appName);
for (RepositoryProducer<?> producer : producers)
producer.introspect(writer, " ");
});

writer.println();
}

/**
Expand Down Expand Up @@ -720,6 +753,25 @@ public void onAppStarted(String appName, Collection<FutureEMBuilder> builders) {
previous.addAll(builders);
}

/**
* Receives notification that a RepositoryProducer was created.
* DataProvider keeps track of RepositoryProducer instances in order to log
* information to the introspector output.
*
* @param appName application name.
* @param producer RepositoryProducer instance.
*/
@Trivial
public void producerCreated(String appName, RepositoryProducer<?> producer) {
Queue<RepositoryProducer<?>> producers = repositoryProducers.get(appName);
if (producers == null) {
Queue<RepositoryProducer<?>> empty = new ConcurrentLinkedQueue<>();
if ((producers = repositoryProducers.putIfAbsent(appName, empty)) == null)
producers = empty;
}
producers.add(producer);
}

@Reference(service = ModuleMetaDataListener.class,
target = "(service.pid=com.ibm.ws.beanvalidation.OSGiBeanValidationImpl)",
cardinality = ReferenceCardinality.OPTIONAL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024,2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -15,6 +15,7 @@
import static io.openliberty.data.internal.persistence.EntityManagerBuilder.getClassNames;
import static io.openliberty.data.internal.persistence.cdi.DataExtension.exc;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class FutureEMBuilder extends CompletableFuture<EntityManagerBuilder> imp
* Module name in which the repository interface is defined.
* If not defined in a module, only the application name part is included.
*/
private final J2EEName moduleName;
final J2EEName moduleName;

/**
* Namespace prefix (such as java:module) of the Repository dataStore.
Expand Down Expand Up @@ -662,6 +663,53 @@ public int hashCode() {
(module == null ? 0 : module.hashCode());
}

/**
* Write information about this instance to the introspection file for
* Jakarta Data.
*
* @param writer writes to the introspection file.
* @param indent indentation for lines.
*/
@FFDCIgnore(Throwable.class)
@Trivial
public void introspect(PrintWriter writer, String indent) {
writer.println(indent + "FutureEMBuilder@" + Integer.toHexString(hashCode()));
writer.println(indent + " dataStore: " + dataStore);
writer.println(indent + " namespace: " + namespace);
writer.println(indent + " application: " + application);
writer.println(indent + " module: " + module);
writer.println(indent + " defining artifact: " + moduleName);
writer.println(indent + " repository class loader: " + repositoryClassLoader);

repositoryInterfaces.forEach(r -> {
writer.println(indent + " repository: " + (r == null ? null : r.getName()));
});

entityTypes.forEach(e -> {
writer.println(indent + " entity: " + (e == null ? null : e.getName()));
});

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

if (builder != null) {
writer.println(indent + " builder: " + builder);
// TODO more information from builder
}
}

@Override
@Trivial
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022,2024 IBM Corporation and others.
* Copyright (c) 2022,2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,6 +12,7 @@
*******************************************************************************/
package io.openliberty.data.internal.persistence.cdi;

import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class RepositoryProducer<R> implements Producer<R>, ProducerFactory<R>, B
this.provider = provider;
this.queriesPerEntityClass = queriesPerEntityClass;
this.repositoryInterface = repositoryInterface;
provider.producerCreated(futureEMBuilder.moduleName.getApplication(), this);
}

@Override
Expand Down Expand Up @@ -130,6 +132,18 @@ public Set<Type> getTypes() {
return beanTypes;
}

/**
* Write information about this instance to the introspection file for
* Jakarta Data.
*
* @param writer writes to the introspection file.
* @param indent indentation for lines.
*/
@Trivial
public void introspect(PrintWriter writer, String indent) {
writer.println(indent + toString()); // TODO more information
}

@Override
@Trivial
public boolean isAlternative() {
Expand Down

0 comments on commit 6aa6384

Please sign in to comment.