Skip to content

Commit

Permalink
Refactor - move method handling registrations
Browse files Browse the repository at this point in the history
The WhiteboardServletContainer was responsible for adding the components which supplied Promise handling and SSE handling to the container. This is better located in the JerseyApplication so that all containers gain the benefit.

Signed-off-by: Tim Ward <timothyjward@apache.org>
  • Loading branch information
timothyjward authored and maho7791 committed Jul 13, 2023
1 parent 3a021a5 commit 56e5c73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.eclipse.osgitech.rest.annotations.RequireJerseyServlet;
import org.eclipse.osgitech.rest.binder.PromiseResponseHandlerBinder;
import org.eclipse.osgitech.rest.provider.jakartars.RuntimeDelegateService;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl;
import org.glassfish.jersey.servlet.init.FilterUrlMappingsProviderImpl;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -45,9 +38,6 @@
@RequireJerseyServlet
public class WhiteboardServletContainer extends ServletContainer {

/** SERVICE_LOCATOR_PREFIX */
private static final String SERVICE_LOCATOR_PREFIX = "JakartaRESTJerseyWhiteboard-";

/**
*
*/
Expand All @@ -58,16 +48,8 @@ public class WhiteboardServletContainer extends ServletContainer {
private final AtomicBoolean initialized = new AtomicBoolean();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

private final ServiceLocator locator;

public WhiteboardServletContainer(ResourceConfig config) {
initialConfig = config;

// Ensure that promise types can be returned by resource methods
locator = ServiceLocatorFactory.getInstance()
.create(SERVICE_LOCATOR_PREFIX + System.identityHashCode(this));
ServiceLocatorUtilities.bind(locator, new PromiseResponseHandlerBinder());
ServiceLocatorUtilities.addClasses(locator, AsyncContextDelegateProviderImpl.class, FilterUrlMappingsProviderImpl.class);
}

/* (non-Javadoc)
Expand All @@ -83,7 +65,6 @@ public void init() throws ServletException {
try {
Thread.currentThread().setContextClassLoader(RuntimeDelegateService.class.getClassLoader());

getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
super.init();
// we have to wait until the injection manager is available on first start
Future<?> future = Executors.newSingleThreadExecutor().submit(()->{
Expand Down Expand Up @@ -189,6 +170,5 @@ public ResourceConfig getConfiguration() {
}

public void dispose() {
locator.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@
*/
package org.eclipse.osgitech.rest.runtime.application;

import static java.util.stream.Collectors.toUnmodifiableSet;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.osgitech.rest.binder.PromiseResponseHandlerBinder;
import org.eclipse.osgitech.rest.binder.PrototypeServiceBinder;
import org.eclipse.osgitech.rest.factories.InjectableFactory;
import org.eclipse.osgitech.rest.factories.JerseyResourceInstanceFactory;
import org.eclipse.osgitech.rest.runtime.application.feature.WhiteboardFeature;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.server.spi.AbstractContainerLifecycleListener;
import org.glassfish.jersey.server.spi.Container;
import org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl;
import org.glassfish.jersey.servlet.init.FilterUrlMappingsProviderImpl;
import org.osgi.framework.ServiceObjects;
import org.osgi.service.jakartars.whiteboard.JakartarsWhiteboardConstants;

Expand Down Expand Up @@ -94,10 +102,10 @@ public Map<String, Object> getProperties() {
*/
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resutlClasses = new HashSet<>();
resutlClasses.addAll(classes.values());
resutlClasses.addAll(sourceApplication.getClasses());
return Collections.unmodifiableSet(resutlClasses);
return Stream.of(classes.values().stream(), sourceApplication.getClasses().stream(),
Stream.of(PromiseResponseHandlerBinder.class, AsyncContextDelegateProviderImpl.class, FilterUrlMappingsProviderImpl.class))
.flatMap(Function.identity())
.collect(toUnmodifiableSet());
}

/*
Expand Down

0 comments on commit 56e5c73

Please sign in to comment.