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

Add 'InstantiatorService' to allow customization of object construction #3187

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.ehcache.core.config.store.StoreEventSourceConfiguration;
import org.ehcache.core.spi.store.Store;
import org.ehcache.core.store.StoreConfigurationImpl;
import org.ehcache.impl.internal.classes.DefaultInstantiatorService;
import org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider;
import org.ehcache.spi.persistence.PersistableResourceService;
import org.ehcache.spi.persistence.StateRepository;
Expand Down Expand Up @@ -537,7 +538,7 @@ public void testGetServerStoreProxySharedAutoCreate() throws Exception {
service.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -598,7 +599,7 @@ public void testGetServerStoreProxySharedNoAutoCreateNonExistent() throws Except
accessService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -644,7 +645,7 @@ public void testGetServerStoreProxySharedNoAutoCreateExists() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> creationStoreConfig =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -725,7 +726,7 @@ public void testGetServerStoreProxySharedAutoCreateTwice() throws Exception {
firstService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> firstSharedStoreConfig =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -787,7 +788,7 @@ public void testReleaseServerStoreProxyShared() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -838,7 +839,7 @@ public void testGetServerStoreProxyDedicatedAutoCreate() throws Exception {
service.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -901,7 +902,7 @@ public void testGetServerStoreProxyDedicatedNoAutoCreateNonExistent() throws Exc
accessService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -949,7 +950,7 @@ public void testGetServerStoreProxyDedicatedNoAutoCreateExists() throws Exceptio
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> creationStoreConfig =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1032,7 +1033,7 @@ public void testGetServerStoreProxyDedicatedAutoCreateTwice() throws Exception {
firstService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> firstSharedStoreConfig =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1099,7 +1100,7 @@ public void testReleaseServerStoreProxyDedicated() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1151,7 +1152,7 @@ public void testGetServerStoreProxySharedDestroy() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1204,7 +1205,7 @@ public void testGetServerStoreProxyDedicatedDestroy() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> storeConfiguration =
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1316,7 +1317,7 @@ public void testDestroyAllWithStores() throws Exception {
createService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> sharedStoreConfiguration =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1414,7 +1415,7 @@ public void testStoreValidation_autoCreateConfigGood_autoCreateConfigBad() throw
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> createStoreConfig =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1476,7 +1477,7 @@ public void testStoreValidation_autoCreateConfigGood_autoCreateConfigGood() thro
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> storeConfig =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1528,7 +1529,7 @@ public void testStoreValidation_autoCreateConfigBad() throws Exception {
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> storeConfig =
getSharedStoreConfig("dedicatedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1575,7 +1576,7 @@ public void testStoreValidation_autoCreateConfigGood_noAutoCreateConfigBad() thr
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> creationStoreConfig =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1644,7 +1645,7 @@ public void testStoreValidation_autoCreateConfigGood_noAutoCreateConfigGood() th
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));

Store.Configuration<Long, String> storeConfig =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
Expand Down Expand Up @@ -1705,7 +1706,7 @@ public void testStoreValidation_MismatchedPoolTypes_ConfiguredDedicatedValidateS
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> createStoreConfig =
getDedicatedStoreConfig("serverResource1", serializationProvider, Long.class, String.class);

Expand Down Expand Up @@ -1773,7 +1774,7 @@ public void testStoreValidation_MismatchedPoolTypes_ConfiguredSharedValidateDedi
creationService.start(null);

DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
serializationProvider.start(providerContaining());
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
Store.Configuration<Long, String> createStoreConfig =
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static ServiceProvider<Service> providerContaining(final Service... servi

for (Service s : services) {
servicesMap.put(s.getClass(), s);
for (Class<?> service : s.getClass().getInterfaces()) {
if (!service.equals(Service.class) && Service.class.isAssignableFrom(service)) {
servicesMap.put(service.asSubclass(Service.class), s);
}
}
}

return new ServiceProvider<Service>() {
Expand Down
1 change: 1 addition & 0 deletions ehcache-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

plugins {
id 'org.ehcache.build.internal-module'
id 'java-test-fixtures'
}

publishing.publications.withType(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -52,6 +53,7 @@
import static java.util.Collections.newSetFromMap;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
Expand Down Expand Up @@ -262,11 +264,10 @@ public <T extends Service> DependencySet with(ServiceCreationConfiguration<T, ?>
.filter(f -> serviceType.isAssignableFrom(f.getServiceType())).map(f -> (ServiceFactory<T>) f)
.collect(toList());

OptionalInt highestRank = typedServiceFactories.stream().mapToInt(ServiceFactory::rank).max();
Optional<ServiceFactory<T>> highestRank = typedServiceFactories.stream().max(comparing(ServiceFactory::rank));

if (highestRank.isPresent()) {
typedServiceFactories.stream().filter(f -> highestRank.getAsInt() == f.rank()).forEach(f -> with(f.create(config)));
return this;
return with(highestRank.get().create(config));
} else {
throw new IllegalStateException("No factories exist for " + serviceType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Terracotta, Inc.
*
* 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.ehcache.core.spi.service;

import org.ehcache.spi.service.Service;

public interface InstantiatorService extends Service {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the interface of interest.


<T> T instantiate(Class<T> clazz, Object ... arguments) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Terracotta, Inc.
*
* 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.ehcache.core.spi;

import org.ehcache.spi.service.Service;

import java.util.function.UnaryOperator;

public interface ServiceLocatorUtils {

static <T extends Service> void withServiceLocator(T service, ServiceTask<T> task) throws Exception {
withServiceLocator(service, UnaryOperator.identity(), task);
}

static <T extends Service> void withServiceLocator(T service, UnaryOperator<ServiceLocator.DependencySet> dependencies, ServiceTask<T> task) throws Exception {
ServiceLocator serviceLocator = dependencies.apply(ServiceLocator.dependencySet().with(service)).build();
serviceLocator.startAllServices();
try {
task.execute(service);
} finally {
serviceLocator.stopAllServices();
}
}

@FunctionalInterface
interface ServiceTask<T extends Service> {

void execute(T service) throws Exception;
}

}
1 change: 1 addition & 0 deletions ehcache-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation group: 'org.terracotta', name: 'terracotta-utilities-tools', version: parent.terracottaUtilitiesVersion
compileOnly 'org.osgi:org.osgi.service.component.annotations:1.3.0'
testImplementation project(':core-spi-test')
testImplementation testFixtures(project(':ehcache-core'))
testImplementation 'org.ow2.asm:asm:6.2'
testImplementation 'org.ow2.asm:asm-commons:6.2'
testImplementation ("org.terracotta:statistics:$parent.statisticVersion")
Expand Down
Loading