Skip to content

Commit

Permalink
Next iteration towards supporting HK2 & Weld injection
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Jan 22, 2024
2 parents 3b1b53e + 21e3c34 commit 6f16858
Show file tree
Hide file tree
Showing 98 changed files with 3,777 additions and 519 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.grizzly2.httpserver;

import org.glassfish.jersey.innate.BootstrapPreinitialization;
import org.glassfish.jersey.internal.inject.InjectionManager;

import jakarta.ws.rs.RuntimeType;

public final class GrizzlyBootstrapPreinitialization implements BootstrapPreinitialization {
@Override
public void preregister(RuntimeType runtimeType, InjectionManager injectionManager) {
injectionManager.register(new GrizzlyHttpContainer.GrizzlyBinder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.grizzly2.httpserver.GrizzlyBootstrapPreinitialization
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -82,24 +82,26 @@ public PropertiesDelegate get() {
@Override
protected void configure() {
install(new MessagingBinders.MessageBodyProviders(clientRuntimeProperties, RuntimeType.CLIENT),
new MessagingBinders.HeaderDelegateProviders());
new MessagingBinders.HeaderDelegateProviders(RuntimeType.CLIENT));

bindFactory(ReferencingFactory.referenceFactory()).to(new GenericType<Ref<ClientConfig>>() {
}).in(RequestScoped.class);
}).in(RequestScoped.class).id(2101);

bindFactory(RequestContextInjectionFactory.class)
.to(ClientRequest.class)
.in(RequestScoped.class);
.in(RequestScoped.class)
.id(2102);

bindFactory(RequestContextInjectionFactory.class).to(HttpHeaders.class)
.proxy(true).proxyForSameScope(false).in(RequestScoped.class);
.proxy(true).proxyForSameScope(false).in(RequestScoped.class).id(2103);

bindFactory(ReferencingFactory.referenceFactory()).to(new GenericType<Ref<ClientRequest>>() {
}).in(RequestScoped.class);
}).in(RequestScoped.class).id(2104);

bindFactory(PropertiesDelegateFactory.class, Singleton.class).to(PropertiesDelegate.class).in(RequestScoped.class);
bindFactory(PropertiesDelegateFactory.class, Singleton.class).to(PropertiesDelegate.class).in(RequestScoped.class)
.forClient(true).id(1001);

// ChunkedInput entity support
bind(ChunkedInputReader.class).to(MessageBodyReader.class).in(Singleton.class);
bind(ChunkedInputReader.class).to(MessageBodyReader.class).in(Singleton.class).id(2105);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.client;

import org.glassfish.jersey.innate.BootstrapPreinitialization;
import org.glassfish.jersey.internal.inject.InjectionManager;

import jakarta.ws.rs.RuntimeType;

public class ClientBootstrapPreinitialization implements BootstrapPreinitialization {
@Override
public void preregister(RuntimeType runtimeType, InjectionManager injectionManager) {
if (runtimeType == RuntimeType.CLIENT) {
new ClientConfig.PreInitialization(injectionManager);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -88,7 +88,7 @@ private RuntimeConfigConfigurator(State runtimeConfig) {
@Override
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
bootstrapBag.setConfiguration(runtimeConfig);
injectionManager.register(Bindings.service(runtimeConfig).to(Configuration.class));
injectionManager.register(Bindings.service(runtimeConfig).to(Configuration.class).forClient(true).id(1000));
}
}

Expand Down Expand Up @@ -417,69 +417,27 @@ private ClientRuntime initRuntime() {
runtimeCfgState.markAsShared();

final InjectionManager injectionManager = findInjectionManager();
injectionManager.register(new ClientBinder(runtimeCfgState.getProperties()));

final ClientBootstrapBag bootstrapBag = new ClientBootstrapBag();
bootstrapBag.setManagedObjectsFinalizer(new ManagedObjectsFinalizer(injectionManager));

final ClientMessageBodyFactory.MessageBodyWorkersConfigurator messageBodyWorkersConfigurator =
new ClientMessageBodyFactory.MessageBodyWorkersConfigurator();

List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(
new RequestScope.RequestScopeConfigurator(),
new ParamConverterConfigurator(),
new ParameterUpdaterConfigurator(),
new RuntimeConfigConfigurator(runtimeCfgState),
new ContextResolverFactory.ContextResolversConfigurator(),
messageBodyWorkersConfigurator,
new ExceptionMapperFactory.ExceptionMappersConfigurator(),
new JaxrsProviders.ProvidersConfigurator(),
new AutoDiscoverableConfigurator(RuntimeType.CLIENT),
new ClientComponentConfigurator(),
new FeatureConfigurator(RuntimeType.CLIENT));
bootstrapConfigurators.forEach(configurator -> configurator.init(injectionManager, bootstrapBag));

// AutoDiscoverable.
if (!CommonProperties.getValue(runtimeCfgState.getProperties(), RuntimeType.CLIENT,
CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, Boolean.FALSE, Boolean.class)) {
runtimeCfgState.configureAutoDiscoverableProviders(injectionManager, bootstrapBag.getAutoDiscoverables());
} else {
runtimeCfgState.configureForcedAutoDiscoverableProviders(injectionManager);
}

// Configure binders and features.
runtimeCfgState.configureMetaProviders(injectionManager, bootstrapBag.getManagedObjectsFinalizer());

// Bind providers.
final Collection<ComponentProvider> componentProviders = bootstrapBag.getComponentProviders().get();
ProviderBinder.bindProviders(
runtimeCfgState.getComponentBag(), RuntimeType.CLIENT, null, injectionManager, componentProviders
);

ClientExecutorProvidersConfigurator executorProvidersConfigurator =
new ClientExecutorProvidersConfigurator(runtimeCfgState.getComponentBag(),
runtimeCfgState.client,
this.executorService,
this.scheduledExecutorService);
executorProvidersConfigurator.init(injectionManager, bootstrapBag);
final PreInitialization preInit =
new PreInitialization(runtimeCfgState, injectionManager, this.executorService, this.scheduledExecutorService);
List<BootstrapConfigurator> bootstrapConfigurators = preInit.bootstrapConfigurators;

injectionManager.completeRegistration();

bootstrapConfigurators.forEach(configurator -> configurator.postInit(injectionManager, bootstrapBag));
bootstrapConfigurators.forEach(configurator -> configurator.postInit(injectionManager, preInit.bootstrapBag));

final ClientConfig configuration = new ClientConfig(runtimeCfgState);
final Connector connector = connectorProvider.getConnector(client, configuration);
final ClientRuntime crt = new ClientRuntime(configuration, connector, injectionManager, bootstrapBag);
final ClientRuntime crt = new ClientRuntime(configuration, connector, injectionManager, preInit.bootstrapBag);

client.registerShutdownHook(crt);
messageBodyWorkersConfigurator.setClientRuntime(crt);
preInit.messageBodyWorkersConfigurator.setClientRuntime(crt);

return crt;
}

private final InjectionManager findInjectionManager() {
private InjectionManager findInjectionManager() {
try {
return Injections.createInjectionManager(RuntimeType.CLIENT);
return Injections.createInjectionManager(commonConfig);
} catch (IllegalStateException ise) {
return new NonInjectionManager(true);
}
Expand Down Expand Up @@ -515,6 +473,68 @@ public int hashCode() {
}
}

/* package */ static class PreInitialization {
private final ClientBootstrapBag bootstrapBag;
private final List<BootstrapConfigurator> bootstrapConfigurators;
private final ClientMessageBodyFactory.MessageBodyWorkersConfigurator messageBodyWorkersConfigurator;

/* package */ PreInitialization(InjectionManager injectionManager) {
this(new State(new JerseyClient()), injectionManager, null, null);
}


/* package */ PreInitialization(
State runtimeCfgState,
InjectionManager injectionManager,
ExecutorService executorService,
ScheduledExecutorService scheduledExecutorService) {
injectionManager.register(new ClientBinder(runtimeCfgState.getProperties()));

bootstrapBag = new ClientBootstrapBag();
bootstrapBag.setManagedObjectsFinalizer(new ManagedObjectsFinalizer(injectionManager));

messageBodyWorkersConfigurator = new ClientMessageBodyFactory.MessageBodyWorkersConfigurator(); // 2020

bootstrapConfigurators = Arrays.asList(
new RequestScope.RequestScopeConfigurator(),
new ParamConverterConfigurator(), // 2010
new ParameterUpdaterConfigurator(), // 2011
new RuntimeConfigConfigurator(runtimeCfgState), // 2012
new ContextResolverFactory.ContextResolversConfigurator(), // 2014
messageBodyWorkersConfigurator,
new ExceptionMapperFactory.ExceptionMappersConfigurator(), // 2015
new JaxrsProviders.ProvidersConfigurator(), // 2016
new AutoDiscoverableConfigurator(RuntimeType.CLIENT),
new ClientComponentConfigurator(),
new FeatureConfigurator(RuntimeType.CLIENT));
bootstrapConfigurators.forEach(configurator -> configurator.init(injectionManager, bootstrapBag));

// AutoDiscoverable.
if (!CommonProperties.getValue(runtimeCfgState.getProperties(), RuntimeType.CLIENT,
CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, Boolean.FALSE, Boolean.class)) {
runtimeCfgState.configureAutoDiscoverableProviders(injectionManager, bootstrapBag.getAutoDiscoverables());
} else {
runtimeCfgState.configureForcedAutoDiscoverableProviders(injectionManager);
}

// Configure binders and features.
runtimeCfgState.configureMetaProviders(injectionManager, bootstrapBag.getManagedObjectsFinalizer());

// Bind providers.
final Collection<ComponentProvider> componentProviders = bootstrapBag.getComponentProviders().get();
ProviderBinder.bindProviders(
runtimeCfgState.getComponentBag(), RuntimeType.CLIENT, null, injectionManager, componentProviders
);

ClientExecutorProvidersConfigurator executorProvidersConfigurator =
new ClientExecutorProvidersConfigurator(runtimeCfgState.getComponentBag(),
runtimeCfgState.client,
executorService,
scheduledExecutorService);
executorProvidersConfigurator.init(injectionManager, bootstrapBag);
}
}

/**
* Construct a new Jersey configuration instance with the default features
* and property values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -37,6 +37,8 @@
import org.glassfish.jersey.spi.ExecutorServiceProvider;
import org.glassfish.jersey.spi.ScheduledExecutorServiceProvider;

import jakarta.ws.rs.RuntimeType;

/**
* Configurator which initializes and register {@link ExecutorServiceProvider} and
* {@link ScheduledExecutorServiceProvider}.
Expand Down Expand Up @@ -106,7 +108,9 @@ public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {

InstanceBinding<ExecutorServiceProvider> executorBinding = Bindings
.service(defaultAsyncExecutorProvider)
.to(ExecutorServiceProvider.class);
.to(ExecutorServiceProvider.class)
.forClient(true)
.id(2020);

injectionManager.register(executorBinding);

Expand All @@ -130,11 +134,13 @@ public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {

InstanceBinding<ScheduledExecutorServiceProvider> schedulerBinding = Bindings
.service(defaultScheduledExecutorProvider)
.to(ScheduledExecutorServiceProvider.class);
.to(ScheduledExecutorServiceProvider.class)
.forClient(true)
.id(2021);
injectionManager.register(schedulerBinding);

registerExecutors(injectionManager, componentBag, defaultAsyncExecutorProvider,
defaultScheduledExecutorProvider, bootstrapBag.getManagedObjectsFinalizer());
defaultScheduledExecutorProvider, bootstrapBag.getManagedObjectsFinalizer(), RuntimeType.CLIENT);
}

private static ExecutorService lookupManagedExecutorService() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -65,8 +65,7 @@ static class MessageBodyWorkersConfigurator implements BootstrapConfigurator {
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
messageBodyFactory = new ClientMessageBodyFactory(bootstrapBag.getConfiguration(), () -> clientRuntime);
InstanceBinding<ClientMessageBodyFactory> binding =
Bindings.service(messageBodyFactory)
.to(MessageBodyWorkers.class);
Bindings.service(messageBodyFactory).to(MessageBodyWorkers.class).id(2020);
injectionManager.register(binding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
ParameterUpdaterFactory parameterUpdaterFactory = new ParameterUpdaterFactory(lazyParamConverterFactory);
clientBag.setParameterUpdaterProvider(parameterUpdaterFactory);
injectionManager.register(Bindings.service(parameterUpdaterFactory)
.to(ParameterUpdaterProvider.class));
.to(ParameterUpdaterProvider.class).id(2011));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.client.ClientBootstrapPreinitialization
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.innate;

import org.glassfish.jersey.Beta;
import org.glassfish.jersey.internal.inject.InjectionManager;

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.core.FeatureContext;

/**
* <p>
* The entry point for pre-initialize Jersey during bootstrap. Register the beans that are not recognized by the injection
* framework to be injected in runtime. Register beans for the specific runtime type into the {@link InjectionManager}.
* </p>
*/
@Beta
public interface BootstrapPreinitialization {
/**
* Manually register beans that are not automatically recognised by the injection framework.
* @param runtimeType
* @param injectionManager
*/
void preregister(RuntimeType runtimeType, InjectionManager injectionManager);

static FeatureContext featureContextInstance() {
return new PreinitializationFeatureContext();
}
}
Loading

0 comments on commit 6f16858

Please sign in to comment.