Skip to content

Commit

Permalink
Adds support for Provider-specializing injection points in OciExtensi…
Browse files Browse the repository at this point in the history
…on (helidon-io#8028)

Signed-off-by: Laird Nelson <[email protected]>
  • Loading branch information
ljnelson authored Nov 17, 2023
1 parent 7579869 commit 99d1815
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 40 deletions.
4 changes: 2 additions & 2 deletions integrations/oci/sdk/cdi/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Oracle and/or its affiliates.
Copyright (c) 2022, 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
<Match>
<Class name="io.helidon.integrations.oci.sdk.cdi.OciExtension" />
<Bug pattern="DMI_BLOCKING_METHODS_ON_URL, REDOS" />
<Bug pattern="DMI_BLOCKING_METHODS_ON_URL, REDOS, UPM_UNCALLED_PRIVATE_METHOD" />
</Match>
<Match>
<Class name="io.helidon.integrations.oci.sdk.cdi.OciExtension$EndpointAdjuster" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.enterprise.util.TypeLiteral;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.ws.rs.Priorities;
import javax.ws.rs.client.Client;
Expand Down Expand Up @@ -630,37 +631,16 @@ private void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event) {

private void processInjectionPoint(@Observes ProcessInjectionPoint<?, ?> event) {
InjectionPoint ip = event.getInjectionPoint();
Type baseType = ip.getAnnotated().getBaseType();
if (!(baseType instanceof Class)) {
// Optimization: all OCI constructs we're interested in
// are non-generic classes (and not therefore
// ParameterizedTypes or GenericArrayTypes).
return;
}
Class<?> baseClass = (Class<?>) baseType;
String baseClassName = baseClass.getName();
if (!baseClassName.startsWith(OCI_PACKAGE_PREFIX)) {
// Optimization: the set of classes we're interested in is
// a subset of general OCI-related classes.
return;
}
Set<Annotation> qualifiers = ip.getQualifiers();
if (AbstractAuthenticationDetailsProvider.class.isAssignableFrom(baseClass)
|| AdpSelectionStrategy.builderClasses().contains(baseClass)) {
// Use an "empty" ServiceTaqs as an indicator of demand
// for some kind of AbstractAuthenticationDetailsProvider
// (or a relevant builder).
this.serviceTaqs.add(new ServiceTaqs(qualifiers.toArray(EMPTY_ANNOTATION_ARRAY)));
return;
}
Matcher m = SERVICE_CLIENT_CLASS_NAME_SUBSTRING_PATTERN.matcher(baseClassName.substring(OCI_PACKAGE_PREFIX.length()));
if (!m.matches() || this.isVetoed(baseClass)) {
return;
}
this.processServiceClientInjectionPoint(event::addDefinitionError,
baseClass,
qualifiers,
OCI_PACKAGE_PREFIX + m.group(1));
processInjectionPoint(ip.getAnnotated().getBaseType(),
ip.getQualifiers(),
event::addDefinitionError);
}

private void processProviderInjectionPoint(@Observes ProcessInjectionPoint<?, ? extends Provider<?>> event) {
InjectionPoint ip = event.getInjectionPoint();
processInjectionPoint(((ParameterizedType) ip.getAnnotated().getBaseType()).getActualTypeArguments()[0],
ip.getQualifiers(),
event::addDefinitionError);
}

private void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager bm) {
Expand Down Expand Up @@ -731,8 +711,39 @@ private boolean isVetoed(Class<?> c) {
return false;
}

private void processInjectionPoint(Type type,
Set<Annotation> qualifiers,
Consumer<? super ClassNotFoundException> errorHandler) {
if (!(type instanceof Class)) {
// Optimization: all OCI constructs we're interested in are non-generic classes (and not therefore
// ParameterizedTypes or GenericArrayTypes).
return;
}
Class<?> c = (Class<?>) type;
String className = c.getName();
if (!className.startsWith(OCI_PACKAGE_PREFIX)) {
// Optimization: the set of classes we're interested in is a subset of general OCI-related classes.
return;
}
if (AbstractAuthenticationDetailsProvider.class.isAssignableFrom(c)
|| AdpSelectionStrategy.builderClasses().contains(c)) {
// Register an "empty" ServiceTaqs as an indicator of demand for some kind of
// AbstractAuthenticationDetailsProvider (or a relevant builder).
this.serviceTaqs.add(new ServiceTaqs(qualifiers.toArray(EMPTY_ANNOTATION_ARRAY)));
return;
}
Matcher m = SERVICE_CLIENT_CLASS_NAME_SUBSTRING_PATTERN.matcher(className.substring(OCI_PACKAGE_PREFIX.length()));
if (!m.matches() || this.isVetoed(c)) {
return;
}
this.processServiceClientInjectionPoint(errorHandler,
c,
qualifiers,
OCI_PACKAGE_PREFIX + m.group(1));
}

private void processServiceClientInjectionPoint(Consumer<? super ClassNotFoundException> errorHandler,
Class<?> baseClass,
Class<?> c,
Set<Annotation> qualifiers,
String serviceInterfaceName) {
Annotation[] qualifiersArray = qualifiers.toArray(EMPTY_ANNOTATION_ARRAY);
Expand All @@ -742,12 +753,12 @@ private void processServiceClientInjectionPoint(Consumer<? super ClassNotFoundEx
// ....example.Example
// ....example.ExampleClient
// ....example.ExampleClient$Builder
Class<?> serviceInterfaceClass = toClassUnresolved(errorHandler, baseClass, serviceInterfaceName, lenient);
Class<?> serviceInterfaceClass = toClassUnresolved(errorHandler, c, serviceInterfaceName, lenient);
if (serviceInterfaceClass != null && serviceInterfaceClass.isInterface()) {
String serviceClient = serviceInterfaceName + "Client";
Class<?> serviceClientClass = toClassUnresolved(errorHandler, baseClass, serviceClient, lenient);
Class<?> serviceClientClass = toClassUnresolved(errorHandler, c, serviceClient, lenient);
if (serviceClientClass != null && serviceInterfaceClass.isAssignableFrom(serviceClientClass)) {
Class<?> serviceClientBuilderClass = toClassUnresolved(errorHandler, baseClass, serviceClient + "$Builder", true);
Class<?> serviceClientBuilderClass = toClassUnresolved(errorHandler, c, serviceClient + "$Builder", true);
if (serviceClientBuilderClass == null) {
serviceClientBuilderClass = toClassUnresolved(errorHandler, serviceClient + "Builder", lenient);
}
Expand All @@ -771,14 +782,14 @@ private void processServiceClientInjectionPoint(Consumer<? super ClassNotFoundEx
// ....example.ExampleAsyncClient
// ....example.ExampleAsyncClient$Builder
String serviceAsyncInterface = serviceInterfaceName + "Async";
Class<?> serviceAsyncInterfaceClass = toClassUnresolved(errorHandler, baseClass, serviceAsyncInterface, lenient);
Class<?> serviceAsyncInterfaceClass = toClassUnresolved(errorHandler, c, serviceAsyncInterface, lenient);
if (serviceAsyncInterfaceClass != null && serviceAsyncInterfaceClass.isInterface()) {
String serviceAsyncClient = serviceAsyncInterface + "Client";
Class<?> serviceAsyncClientClass = toClassUnresolved(errorHandler, baseClass, serviceAsyncClient, lenient);
Class<?> serviceAsyncClientClass = toClassUnresolved(errorHandler, c, serviceAsyncClient, lenient);
if (serviceAsyncClientClass != null
&& serviceAsyncInterfaceClass.isAssignableFrom(serviceAsyncClientClass)) {
Class<?> serviceAsyncClientBuilderClass =
toClassUnresolved(errorHandler, baseClass, serviceAsyncClient + "$Builder", true);
toClassUnresolved(errorHandler, c, serviceAsyncClient + "$Builder", true);
if (serviceAsyncClientBuilderClass == null) {
serviceAsyncClientBuilderClass = toClassUnresolved(errorHandler, serviceAsyncClient + "Builder", lenient);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.oci.sdk.cdi;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Provider;

import io.helidon.microprofile.config.ConfigCdiExtension;
import io.helidon.microprofile.tests.junit5.AddBean;
import io.helidon.microprofile.tests.junit5.AddExtension;
import io.helidon.microprofile.tests.junit5.DisableDiscovery;
import io.helidon.microprofile.tests.junit5.HelidonTest;

import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.ailanguage.AIServiceLanguage;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@AddBean(TestProcessProviderInjectionPoint.ExampleBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddExtension(OciExtension.class)
@DisableDiscovery
@HelidonTest
class TestProcessProviderInjectionPoint {


/*
* Instance fields.
*/


@Inject
private Provider<ExampleBean> exampleBeanProvider;


/*
* Test methods.
*/


@Test
void testProcessProviderInjectionPoint() throws IOException {
// Don't run this test if there's NO ADP anywhere; it will show up as "skipped" in the test run.
assumeTrue(imdsAvailable() || configFileExists());

assertThat(this.exampleBeanProvider.get(), is(not(nullValue())));
}


/*
* Static methods.
*/


private static final boolean configFileExists() throws IOException {
try {
return
ConfigFileReader.parse(System.getProperty("oci.config.file", "~/.oci/config"),
System.getProperty("oci.auth.profile")) != null;
} catch (final FileNotFoundException ignored) {
return false;
}
}

private static final boolean imdsAvailable() {
try {
return InetAddress.getByName(System.getProperty("oci.imds.hostname", "169.254.169.254"))
.isReachable(Integer.getInteger("oci.imds.timeout", 100).intValue());
} catch (final IOException ignored) {
return false;
}
}


/*
* Inner and nested classes.
*/


@Dependent
static class ExampleBean {

// Required by the CDI specification.
@Deprecated // For CDI use only.
ExampleBean() {
super();
}

@Inject
private ExampleBean(Provider<AIServiceLanguage> p) {
super();
assertThat(p, is(not(nullValue())));
assertThat(p.get(), is(not(nullValue())));
}

}

}

0 comments on commit 99d1815

Please sign in to comment.