Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Application subclasses available via our context during Feature executions #4745

Merged
merged 3 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -58,6 +58,8 @@

import io.helidon.common.Prioritized;
import io.helidon.common.configurable.ServerThreadPoolSupplier;
import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;
import io.helidon.common.http.Http;
import io.helidon.config.Config;
import io.helidon.microprofile.cdi.BuildTimeStart;
Expand Down Expand Up @@ -256,8 +258,16 @@ private void registerJaxRsApplications(BeanManager beanManager) {
.forEach(s -> shared.register(Bindings.service(s)));
}

// Add all applications
jaxRsApplications.forEach(it -> addApplication(jaxRs, it, shared));
// Add all applications making the Application subclass (if accessible via
// CDI) available in our context to be used by JAX-RS features
jaxRsApplications.forEach(it -> it.applicationClass()
.flatMap(appClass -> CDI.current().select(appClass).stream().findFirst())
.ifPresentOrElse(app -> {
Context parent = Contexts.context().orElse(null);
Context startupContext = Context.create(parent);
startupContext.register(app);
Contexts.runInContext(startupContext, () -> addApplication(jaxRs, it, shared));
}, () -> addApplication(jaxRs, it, shared)));
}
STARTUP_LOGGER.finest("Registered jersey application(s)");
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2022 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 @@
import javax.ws.rs.core.FeatureContext;

@ConstrainedTo(RuntimeType.SERVER)
public class MyFeature implements DynamicFeature {
public class Feature2 implements DynamicFeature {

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 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 Down Expand Up @@ -30,6 +30,6 @@ public class GreetApplication1 extends Application {

@Override
public Set<Class<?>> getClasses() {
return Set.of(GreetResource1.class, Filter1.class, SharedFilter.class);
return Set.of(GreetResource1.class, Filter1.class, SharedFilter.class, SharedFeature.class);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 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 Down Expand Up @@ -32,7 +32,7 @@ public class GreetApplication2 extends Application {

@Override
public Set<Class<?>> getClasses() {
return Set.of(GreetResource2.class, SharedFilter.class, MyFeature.class);
return Set.of(GreetResource2.class, SharedFilter.class, Feature2.class, SharedFeature.class);
}

@Override
Expand Down
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 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.tests.functional.multipleapps;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.FeatureContext;
import java.util.HashSet;
import java.util.Set;

import io.helidon.common.context.Contexts;

@ConstrainedTo(RuntimeType.SERVER)
@ApplicationScoped
public class SharedFeature implements DynamicFeature {

private static final Set<Class<? extends Application>> applications = new HashSet<>();

static Set<Class<? extends Application>> applications() {
return applications;
}

@Override
@SuppressWarnings("unchecked")
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
// Collect all application subclasses started
Application app = Contexts.context().flatMap(c -> c.get(Application.class)).orElse(null);
assert app != null;
applications.add(!app.getClass().isSynthetic() ? app.getClass()
: (Class<? extends Application>) app.getClass().getSuperclass());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 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 @@ -24,6 +24,8 @@
import io.helidon.microprofile.tests.junit5.HelidonTest;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -66,4 +68,10 @@ void testHelloWorld2() {
assertEquals("Hello World 2!", jsonObject.getString("message"),
"default message");
}

@Test
void testContextApps() {
assertThat(SharedFeature.applications(), hasItem(GreetApplication1.class));
assertThat(SharedFeature.applications(), hasItem(GreetApplication2.class));
}
}