Skip to content

Commit 9d9fc1a

Browse files
committed
Add InstantiatorService to abstract user instance creation
1 parent 51ae2e4 commit 9d9fc1a

File tree

33 files changed

+742
-602
lines changed

33 files changed

+742
-602
lines changed

clustered/ehcache-client/src/test/java/org/ehcache/clustered/client/internal/service/DefaultClusteringServiceTest.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.ehcache.core.config.store.StoreEventSourceConfiguration;
5050
import org.ehcache.core.spi.store.Store;
5151
import org.ehcache.core.store.StoreConfigurationImpl;
52+
import org.ehcache.impl.internal.classes.DefaultInstantiatorService;
5253
import org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider;
5354
import org.ehcache.spi.persistence.PersistableResourceService;
5455
import org.ehcache.spi.persistence.StateRepository;
@@ -537,7 +538,7 @@ public void testGetServerStoreProxySharedAutoCreate() throws Exception {
537538
service.start(null);
538539

539540
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
540-
serializationProvider.start(providerContaining());
541+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
541542
Store.Configuration<Long, String> storeConfiguration =
542543
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
543544

@@ -598,7 +599,7 @@ public void testGetServerStoreProxySharedNoAutoCreateNonExistent() throws Except
598599
accessService.start(null);
599600

600601
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
601-
serializationProvider.start(providerContaining());
602+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
602603
Store.Configuration<Long, String> storeConfiguration =
603604
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
604605

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

646647
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
647-
serializationProvider.start(providerContaining());
648+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
648649
Store.Configuration<Long, String> creationStoreConfig =
649650
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
650651

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

727728
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
728-
serializationProvider.start(providerContaining());
729+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
729730
Store.Configuration<Long, String> firstSharedStoreConfig =
730731
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
731732

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

789790
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
790-
serializationProvider.start(providerContaining());
791+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
791792
Store.Configuration<Long, String> storeConfiguration =
792793
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
793794

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

840841
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
841-
serializationProvider.start(providerContaining());
842+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
842843
Store.Configuration<Long, String> storeConfiguration =
843844
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
844845

@@ -901,7 +902,7 @@ public void testGetServerStoreProxyDedicatedNoAutoCreateNonExistent() throws Exc
901902
accessService.start(null);
902903

903904
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
904-
serializationProvider.start(providerContaining());
905+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
905906
Store.Configuration<Long, String> storeConfiguration =
906907
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
907908

@@ -949,7 +950,7 @@ public void testGetServerStoreProxyDedicatedNoAutoCreateExists() throws Exceptio
949950
creationService.start(null);
950951

951952
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
952-
serializationProvider.start(providerContaining());
953+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
953954
Store.Configuration<Long, String> creationStoreConfig =
954955
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
955956

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

10341035
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1035-
serializationProvider.start(providerContaining());
1036+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
10361037
Store.Configuration<Long, String> firstSharedStoreConfig =
10371038
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
10381039

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

11011102
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1102-
serializationProvider.start(providerContaining());
1103+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
11031104
Store.Configuration<Long, String> storeConfiguration =
11041105
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
11051106

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

11531154
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1154-
serializationProvider.start(providerContaining());
1155+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
11551156
Store.Configuration<Long, String> storeConfiguration =
11561157
getSharedStoreConfig(targetPool, serializationProvider, Long.class, String.class);
11571158

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

12061207
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1207-
serializationProvider.start(providerContaining());
1208+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
12081209
Store.Configuration<Long, String> storeConfiguration =
12091210
getDedicatedStoreConfig(targetResource, serializationProvider, Long.class, String.class);
12101211

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

13181319
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1319-
serializationProvider.start(providerContaining());
1320+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
13201321

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

14161417
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1417-
serializationProvider.start(providerContaining());
1418+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
14181419

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

14781479
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1479-
serializationProvider.start(providerContaining());
1480+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
14801481

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

15301531
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1531-
serializationProvider.start(providerContaining());
1532+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
15321533

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

15771578
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1578-
serializationProvider.start(providerContaining());
1579+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
15791580

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

16461647
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1647-
serializationProvider.start(providerContaining());
1648+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
16481649

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

17071708
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1708-
serializationProvider.start(providerContaining());
1709+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
17091710
Store.Configuration<Long, String> createStoreConfig =
17101711
getDedicatedStoreConfig("serverResource1", serializationProvider, Long.class, String.class);
17111712

@@ -1773,7 +1774,7 @@ public void testStoreValidation_MismatchedPoolTypes_ConfiguredSharedValidateDedi
17731774
creationService.start(null);
17741775

17751776
DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(null);
1776-
serializationProvider.start(providerContaining());
1777+
serializationProvider.start(providerContaining(new DefaultInstantiatorService()));
17771778
Store.Configuration<Long, String> createStoreConfig =
17781779
getSharedStoreConfig("sharedPrimary", serializationProvider, Long.class, String.class);
17791780

clustered/ehcache-client/src/test/java/org/ehcache/clustered/client/internal/service/TestServiceProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public static ServiceProvider<Service> providerContaining(final Service... servi
3333

3434
for (Service s : services) {
3535
servicesMap.put(s.getClass(), s);
36+
for (Class<?> service : s.getClass().getInterfaces()) {
37+
if (!service.equals(Service.class) && Service.class.isAssignableFrom(service)) {
38+
servicesMap.put(service.asSubclass(Service.class), s);
39+
}
40+
}
3641
}
3742

3843
return new ServiceProvider<Service>() {

ehcache-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
plugins {
1818
id 'org.ehcache.build.internal-module'
19+
id 'java-test-fixtures'
1920
}
2021

2122
publishing.publications.withType(MavenPublication) {

ehcache-core/src/main/java/org/ehcache/core/spi/ServiceLocator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.LinkedList;
4141
import java.util.List;
4242
import java.util.Map;
43+
import java.util.Optional;
4344
import java.util.OptionalInt;
4445
import java.util.Set;
4546
import java.util.concurrent.atomic.AtomicBoolean;
@@ -52,6 +53,7 @@
5253
import static java.util.Collections.newSetFromMap;
5354
import static java.util.Collections.singleton;
5455
import static java.util.Collections.unmodifiableSet;
56+
import static java.util.Comparator.comparing;
5557
import static java.util.stream.Collectors.groupingBy;
5658
import static java.util.stream.Collectors.toList;
5759
import static java.util.stream.StreamSupport.stream;
@@ -262,11 +264,10 @@ public <T extends Service> DependencySet with(ServiceCreationConfiguration<T, ?>
262264
.filter(f -> serviceType.isAssignableFrom(f.getServiceType())).map(f -> (ServiceFactory<T>) f)
263265
.collect(toList());
264266

265-
OptionalInt highestRank = typedServiceFactories.stream().mapToInt(ServiceFactory::rank).max();
267+
Optional<ServiceFactory<T>> highestRank = typedServiceFactories.stream().max(comparing(ServiceFactory::rank));
266268

267269
if (highestRank.isPresent()) {
268-
typedServiceFactories.stream().filter(f -> highestRank.getAsInt() == f.rank()).forEach(f -> with(f.create(config)));
269-
return this;
270+
return with(highestRank.get().create(config));
270271
} else {
271272
throw new IllegalStateException("No factories exist for " + serviceType);
272273
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Terracotta, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ehcache.core.spi.service;
17+
18+
import org.ehcache.spi.service.Service;
19+
20+
public interface InstantiatorService extends Service {
21+
22+
<T> T instantiate(Class<T> clazz, Object ... arguments) throws IllegalArgumentException;
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Terracotta, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ehcache.core.spi;
17+
18+
import org.ehcache.spi.service.Service;
19+
20+
import java.util.function.UnaryOperator;
21+
22+
public interface ServiceLocatorUtils {
23+
24+
static <T extends Service> void withServiceLocator(T service, ServiceTask<T> task) throws Exception {
25+
withServiceLocator(service, UnaryOperator.identity(), task);
26+
}
27+
28+
static <T extends Service> void withServiceLocator(T service, UnaryOperator<ServiceLocator.DependencySet> dependencies, ServiceTask<T> task) throws Exception {
29+
ServiceLocator serviceLocator = dependencies.apply(ServiceLocator.dependencySet().with(service)).build();
30+
serviceLocator.startAllServices();
31+
try {
32+
task.execute(service);
33+
} finally {
34+
serviceLocator.stopAllServices();
35+
}
36+
}
37+
38+
@FunctionalInterface
39+
interface ServiceTask<T extends Service> {
40+
41+
void execute(T service) throws Exception;
42+
}
43+
44+
}

ehcache-impl/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
implementation group: 'org.terracotta', name: 'terracotta-utilities-tools', version: parent.terracottaUtilitiesVersion
5656
compileOnly 'org.osgi:org.osgi.service.component.annotations:1.3.0'
5757
testImplementation project(':core-spi-test')
58+
testImplementation testFixtures(project(':ehcache-core'))
5859
testImplementation 'org.ow2.asm:asm:6.2'
5960
testImplementation 'org.ow2.asm:asm-commons:6.2'
6061
testImplementation ("org.terracotta:statistics:$parent.statisticVersion")

0 commit comments

Comments
 (0)