Skip to content

Commit

Permalink
[RESTEASY-3247] Use a singleton ConfigurationFactory to avoid too man…
Browse files Browse the repository at this point in the history
…y lookups of the factory.

Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Dec 1, 2022
1 parent a55ac72 commit dfb1061
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 103 deletions.
Expand Up @@ -19,10 +19,6 @@

package org.jboss.resteasy.spi.config;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ServiceLoader;

import org.jboss.resteasy.spi.ResteasyConfiguration;

/**
Expand All @@ -33,37 +29,15 @@
public interface ConfigurationFactory {

/**
* Returns a new factory. The factory with the lowest {@linkplain #priority() priority} will be selected.
* Returns the factory for the environment. The factory with the lowest {@linkplain #priority() priority} will be
* selected.
*
* @return the new factory
* @return the factory for the current environment
*
* @throws RuntimeException if the service loader could not find a factory
*/
static ConfigurationFactory getInstance() {
if (System.getSecurityManager() == null) {
final ServiceLoader<ConfigurationFactory> loader = ServiceLoader.load(ConfigurationFactory.class);
ConfigurationFactory current = null;
for (ConfigurationFactory factory : loader) {
if (current == null) {
current = factory;
} else if (factory.priority() < current.priority()) {
current = factory;
}
}
return current == null ? () -> Integer.MAX_VALUE : current;
}
return AccessController.doPrivileged((PrivilegedAction<ConfigurationFactory>) () -> {
final ServiceLoader<ConfigurationFactory> loader = ServiceLoader.load(ConfigurationFactory.class);
ConfigurationFactory current = null;
for (ConfigurationFactory factory : loader) {
if (current == null) {
current = factory;
} else if (factory.priority() < current.priority()) {
current = factory;
}
}
return current == null ? () -> Integer.MAX_VALUE : current;
});
return SingletonConfigurationFactory.getInstance();
}

/**
Expand All @@ -72,7 +46,7 @@ static ConfigurationFactory getInstance() {
* @return the configuration
*/
default Configuration getConfiguration() {
return getConfiguration(null);
return new DefaultConfiguration();
}

/**
Expand Down
Expand Up @@ -26,6 +26,7 @@

import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.spi.ResteasyConfiguration;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.config.security.ConfigPropertyPermission;

/**
Expand All @@ -47,7 +48,7 @@ public class DefaultConfiguration implements Configuration {
* Creates a new configuration .
*/
public DefaultConfiguration() {
this(null);
this(resolveConfiguration());
}

/**
Expand All @@ -56,7 +57,13 @@ public DefaultConfiguration() {
* @param config the resolver
*/
public DefaultConfiguration(final ResteasyConfiguration config) {
this.resolver = config == null ? DEFAULT_RESOLVER : new Resolver(config);
final ResteasyConfiguration delegate;
if (config == null) {
delegate = resolveConfiguration();
} else {
delegate = config;
}
this.resolver = delegate == null ? DEFAULT_RESOLVER : new Resolver(delegate);
}

@Override
Expand Down Expand Up @@ -115,6 +122,11 @@ public <T> T getValue(final String name, final Class<T> type) {
return getOptionalValue(name, type).orElseThrow(() -> Messages.MESSAGES.propertyNotFound(name));
}

private static ResteasyConfiguration resolveConfiguration() {
final ResteasyProviderFactory factory = ResteasyProviderFactory.peekInstance();
return factory == null ? null : factory.getContextData(ResteasyConfiguration.class);
}

private static class Resolver implements Function<String, String> {
private final ResteasyConfiguration config;

Expand Down
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2022 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.resteasy.spi.config;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ServiceLoader;

/**
* Loads the {@link ConfigurationFactory} lazily during instantiation.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class SingletonConfigurationFactory {

/**
* Returns the lazily created {@link ConfigurationFactory}.
*
* @return the configuration factory found
*/
static ConfigurationFactory getInstance() {
return Holder.INSTANCE;
}

private static class Holder {
static final ConfigurationFactory INSTANCE;

static {
final PrivilegedAction<ConfigurationFactory> action = () -> {
// We must use this class loader for environments where the TCCL might pick up an instance from a source
// that is not meant to be shared.
final ServiceLoader<ConfigurationFactory> loader = ServiceLoader.load(ConfigurationFactory.class, Holder.class.getClassLoader());
ConfigurationFactory current = null;
for (ConfigurationFactory factory : loader) {
if (current == null) {
current = factory;
} else if (factory.priority() < current.priority()) {
current = factory;
}
}
return current == null ? () -> Integer.MAX_VALUE : current;
};
if (System.getSecurityManager() == null) {
INSTANCE = action.run();
} else {
INSTANCE = AccessController.doPrivileged(action);
}
}
}
}

This file was deleted.

This file was deleted.

Expand Up @@ -40,6 +40,10 @@
<module name="jakarta.ws.rs.api"/>
<module name="org.jboss.logging"/>
<module name="org.reactivestreams"/>
<!-- If present, this loads a different ConfigurationFactory to use the MP Config API for configuration
properties.
-->
<module name="org.jboss.resteasy.microprofile.config" services="import" optional="true"/>
<!-- If a deployment wants to use this it needs to be exported. However, the way it's loaded in the RESTEasyTracingLogger
it also needs to be available for this module. This is the reason we export it here.
-->
Expand Down
Expand Up @@ -48,10 +48,5 @@
<module name="org.jboss.resteasy.resteasy-core-spi"/>
<module name="org.jboss.resteasy.resteasy-validator-provider" optional="true" services="export" export="true"/>
<module name="org.reactivestreams"/>
<!-- Optional dependencies if we have the MicroProfile Config available. Note we need to export the
org.jboss.resteasy.microprofile.config as it will be loaded from another module loader.
-->
<module name="org.eclipse.microprofile.config.api" optional="true"/>
<module name="org.jboss.resteasy.microprofile.config" export="true" services="export" optional="true"/>
</dependencies>
</module>

0 comments on commit dfb1061

Please sign in to comment.