Skip to content

Commit

Permalink
Merge pull request #30222 from njr-11/27352-nls-messages-for-concurre…
Browse files Browse the repository at this point in the history
…ncy-resources-with-qualifiers

NLS messages for new Jakarta Concurrency function
  • Loading branch information
njr-11 authored Nov 21, 2024
2 parents fc70d27 + e0f3f61 commit 600fde7
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2022,2023 IBM Corporation and others.
# Copyright (c) 2022,2024 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 @@ -24,5 +24,34 @@
# which do NOT contain replacement variables are NOT processed by the MessageFormat class
# (single quote must be coded as one single quote ').

# All messages must use the range CWWCK1410 to CWWCK1499
# All messages must use the range CWWKC1410 to CWWKC1499

CWWKC1410.schedule.lacks.seconds=CWWKC1410E: The Schedule annotation, {0}, \
specifies an empty value for the seconds at which the task runs. \
Specify a nonempty value for the seconds field, or omit the seconds field \
from the Schedule annotation. Omitting the seconds field schedules the task \
to run at the start of the minute.
CWWKC1410.schedule.lacks.seconds.explanation=The empty array value indicates that \
the seconds field must be disregarded when scheduled times are computed. \
Omitting the seconds field schedules the task to run at the start of the minute.
CWWKC1410.schedule.lacks.seconds.useraction=Modify the seconds field of the \
Schedule annotation to specify a nonempty value, or omit the seconds field \
from the Schedule annotation.

CWWKC1411.qualified.res.err=CWWKC1411E: The server did not create a {0} bean \
for the {1} annotation or {2} deployment descriptor element. \
The annotation or deployment descriptor element has the \
{3} name and {4} qualifiers and is defined in the {5} application artifact. \
The error is {6}
CWWKC1411.qualified.res.err.explanation=The code did not create the resource \
or did not register the resource as a bean.
CWWKC1411.qualified.res.err.useraction=Ensure that the resource is configured \
correctly.

CWWKC1412.qualifier.conflict=CWWKC1412E: The {0} application artifact includes \
multiple {1} resource definitions that have the {2} qualifiers. The names of \
the conflicting {1} resource definitions are {3}.
CWWKC1412.qualifier.conflict.explanation=Each resource definition of the \
same type that has a qualifier must have a different qualifier.
CWWKC1412.qualifier.conflict.useraction=Ensure that each resource definition \
of the same type has a different qualifier or does not have any qualifiers.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package io.openliberty.concurrent.internal.cdi;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -24,18 +26,28 @@
import org.osgi.framework.ServiceReference;

import com.ibm.websphere.csi.J2EEName;
import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.websphere.ras.annotation.Trivial;
import com.ibm.ws.cdi.CDIServiceUtils;
import com.ibm.ws.runtime.metadata.ApplicationMetaData;
import com.ibm.ws.runtime.metadata.ComponentMetaData;
import com.ibm.ws.runtime.metadata.MetaData;
import com.ibm.ws.runtime.metadata.ModuleMetaData;
import com.ibm.ws.threadContext.ComponentMetaDataAccessorImpl;

import io.openliberty.concurrent.internal.cdi.interceptor.AsyncInterceptor;
import io.openliberty.concurrent.internal.qualified.QualifiedResourceFactories;
import io.openliberty.concurrent.internal.qualified.QualifiedResourceFactory;
import jakarta.enterprise.concurrent.Asynchronous;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.ContextServiceDefinition;
import jakarta.enterprise.concurrent.ManagedExecutorDefinition;
import jakarta.enterprise.concurrent.ManagedExecutorService;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorDefinition;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;
import jakarta.enterprise.concurrent.ManagedThreadFactory;
import jakarta.enterprise.concurrent.ManagedThreadFactoryDefinition;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
Expand All @@ -51,6 +63,8 @@
* CDI Extension for Jakarta Concurrency 3.1+ in Jakarta EE 11+, which corresponds to CDI 4.1+
*/
public class ConcurrencyExtension implements Extension {
private static final TraceComponent tc = Tr.register(ConcurrencyExtension.class);

private static final Annotation[] DEFAULT_QUALIFIER_ARRAY = new Annotation[] { Default.Literal.INSTANCE };

private static final Set<Annotation> DEFAULT_QUALIFIER_SET = Set.of(Default.Literal.INSTANCE);
Expand Down Expand Up @@ -149,11 +163,14 @@ private void addBeans(AfterBeanDiscovery event, List<Map<List<String>, Qualified
try {
event.addBean(new ContextServiceBean(factory));
} catch (Throwable x) {
// TODO NLS
System.out.println(" E Unable to create a bean for the " +
factory + " ContextServiceDefinition with the " + factory.getQualifiers() + " qualifiers" +
" due to the following error: ");
x.printStackTrace();
Tr.error(tc, "CWWKC1411.qualified.res.err",
ContextService.class.getSimpleName(),
ContextServiceDefinition.class.getSimpleName(),
"context-service",
factory.getName(),
factory.getQualifiers(),
toArtifactName(factory.getDeclaringMetadata()),
toStackTrace(x));
}
}

Expand All @@ -164,11 +181,14 @@ private void addBeans(AfterBeanDiscovery event, List<Map<List<String>, Qualified
try {
event.addBean(new ManagedExecutorBean(factory));
} catch (Throwable x) {
// TODO NLS
System.out.println(" E Unable to create a bean for the " +
factory + " ManagedExecutorDefinition with the " + factory.getQualifiers() + " qualifiers" +
" due to the following error: ");
x.printStackTrace();
Tr.error(tc, "CWWKC1411.qualified.res.err",
ManagedExecutorService.class.getSimpleName(),
ManagedExecutorDefinition.class.getSimpleName(),
"managed-executor",
factory.getName(),
factory.getQualifiers(),
toArtifactName(factory.getDeclaringMetadata()),
toStackTrace(x));
}
}

Expand All @@ -179,11 +199,14 @@ private void addBeans(AfterBeanDiscovery event, List<Map<List<String>, Qualified
try {
event.addBean(new ManagedScheduledExecutorBean(factory));
} catch (Throwable x) {
// TODO NLS
System.out.println(" E Unable to create a bean for the " +
factory + " ManagedScheduledExecutorDefinition with the " + factory.getQualifiers() + " qualifiers" +
" due to the following error: ");
x.printStackTrace();
Tr.error(tc, "CWWKC1411.qualified.res.err",
ManagedScheduledExecutorService.class.getSimpleName(),
ManagedScheduledExecutorDefinition.class.getSimpleName(),
"managed-scheduled-executor",
factory.getName(),
factory.getQualifiers(),
toArtifactName(factory.getDeclaringMetadata()),
toStackTrace(x));
}
}

Expand All @@ -194,11 +217,14 @@ private void addBeans(AfterBeanDiscovery event, List<Map<List<String>, Qualified
try {
event.addBean(new ManagedThreadFactoryBean(factory, extSvc));
} catch (Throwable x) {
// TODO NLS
System.out.println(" E Unable to create a bean for the " +
factory + " ManagedThreadFactoryDefinition with the " + factory.getQualifiers() + " qualifiers" +
" due to the following error: ");
x.printStackTrace();
Tr.error(tc, "CWWKC1411.qualified.res.err",
ManagedThreadFactory.class.getSimpleName(),
ManagedThreadFactoryDefinition.class.getSimpleName(),
"managed-thread-factory",
factory.getName(),
factory.getQualifiers(),
toArtifactName(factory.getDeclaringMetadata()),
toStackTrace(x));
}
}
}
Expand All @@ -223,4 +249,36 @@ public void afterDeploymentValidation(@Observes AfterDeploymentValidation event,
qualifierSetsPerMTF = null;
}
}

/**
* Utility method to obtain the JEE name for a MetaData instance if possible,
* and otherwise the name.
*
* @param metadata ComponentMetaData, ModuleMetaData, or ApplicationMetaData.
* @return the artifact name, preferably as a JEE name string.
*/
@Trivial
private static String toArtifactName(MetaData metadata) {
if (metadata instanceof ComponentMetaData)
return ((ComponentMetaData) metadata).getJ2EEName().toString();
else if (metadata instanceof ModuleMetaData)
return ((ModuleMetaData) metadata).getJ2EEName().toString();
else if (metadata instanceof ApplicationMetaData)
return ((ApplicationMetaData) metadata).getJ2EEName().toString();
else
return metadata.getName();
}

/**
* Utility method that converts an exception to a stack trace string.
*
* @param x exception.
* @return stack trace string.
*/
@Trivial
private static String toStackTrace(Throwable x) {
StringWriter s = new StringWriter();
x.printStackTrace(new PrintWriter(s));
return s.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.ws.cdi.extension.CDIExtensionMetadataInternal;
import com.ibm.ws.container.service.metadata.extended.DeferredMetaDataFactory;
import com.ibm.ws.javaee.version.JavaEEVersion;
Expand All @@ -45,6 +47,8 @@
@Component(configurationPolicy = ConfigurationPolicy.IGNORE,
service = { CDIExtensionMetadata.class, QualifiedResourceFactories.class })
public class ConcurrencyExtensionMetadata implements CDIExtensionMetadata, CDIExtensionMetadataInternal, QualifiedResourceFactories {
private static final TraceComponent tc = Tr.register(ConcurrencyExtensionMetadata.class);

private static final Set<Class<?>> beanClasses = Set.of(ContextService.class,
ManagedExecutorService.class,
ManagedScheduledExecutorService.class,
Expand Down Expand Up @@ -121,20 +125,37 @@ public class ConcurrencyExtensionMetadata implements CDIExtensionMetadata, CDIEx
* @param resourceFactory the resource factory
*/
@Override
public void add(String jeeName, QualifiedResourceFactory.Type resourceType,
List<String> qualifierNames, QualifiedResourceFactory resourceFactory) {
public void add(String jeeName,
QualifiedResourceFactory.Type resourceType,
List<String> qualifierNames,
QualifiedResourceFactory resourceFactory) {
List<Map<List<String>, QualifiedResourceFactory>> list = resourceFactories.get(jeeName);
if (list == null) {
list = List.of(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
resourceFactories.put(jeeName, list);
}

Map<List<String>, QualifiedResourceFactory> qualifiersToResourceFactory = list.get(resourceType.ordinal());
QualifiedResourceFactory conflict = qualifiersToResourceFactory.put(qualifierNames, resourceFactory);

if (conflict != null)
throw new IllegalStateException("The " + jeeName + " application artifact defines multiple " + //
resourceType + " resources with the " + qualifierNames + " qualifiers."); // TODO NLS and Tr.error
Map<List<String>, QualifiedResourceFactory> qualifiersToResourceFactory = //
list.get(resourceType.ordinal());
QualifiedResourceFactory conflict = //
qualifiersToResourceFactory.put(qualifierNames, resourceFactory);

if (conflict != null) {
Set<String> names = Set.of(resourceFactory.getName(), conflict.getName());

Tr.error(tc, "CWWKC1412.qualifier.conflict",
jeeName,
resourceType,
resourceFactory.getQualifiers(),
names);

throw new IllegalStateException(Tr //
.formatMessage(tc, "CWWKC1412.qualifier.conflict",
jeeName,
resourceType,
resourceFactory.getQualifiers(),
names));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;

import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.websphere.ras.annotation.Trivial;

import jakarta.enterprise.concurrent.CronTrigger;
Expand All @@ -27,6 +29,7 @@
* so that multiple triggers can compute from the same time.
*/
class ScheduleCronTrigger extends CronTrigger {
private static final TraceComponent tc = Tr.register(ScheduleCronTrigger.class);

private static final Month[] ALL_MONTHS = Month.values();

Expand Down Expand Up @@ -105,7 +108,12 @@ static ScheduleCronTrigger create(Schedule schedule) {

int[] seconds = schedule.seconds();
if (seconds.length == 0)
throw new IllegalArgumentException("seconds: {}"); // TODO NLS The seconds field cannot be ignored because no other units of Schedule are more granular.
// The seconds field cannot be ignored because no other
// units of Schedule are more granular.
throw new IllegalArgumentException(Tr //
.formatMessage(tc,
"CWWKC1410.schedule.lacks.seconds",
schedule));

trigger.months(months) //
.daysOfMonth(daysOfMonth) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ public MetaData getDeclaringMetadata() {
return declaringMetadata;
}

@Override
@Trivial
public String getName() {
return jndiName;
}

/**
* Returns text showing an example of a valid qualifier with the specified
* class name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ enum Type {
*/
MetaData getDeclaringMetadata();

/**
* Returns the name of the resource definition, which is typically a
* JNDI name in java:comp, java:module, java:app, or java:global
*
* @return the name.
*/
String getName();

/**
* Returns instances of the qualifier annotations for this resource factory.
*
Expand Down

0 comments on commit 600fde7

Please sign in to comment.