Skip to content

Commit fae8534

Browse files
committed
Add Customizer and Initializer for MicroStream
1 parent eaaa334 commit fae8534

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2021-2022 Rudy De Busscher (https://www.atbash.be)
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 be.atbash.runtime.data.microstream.config;
17+
18+
19+
import one.microstream.storage.embedded.types.EmbeddedStorageFoundation;
20+
21+
public interface EmbeddedStorageFoundationCustomizer {
22+
23+
void customize(EmbeddedStorageFoundation<?> embeddedStorageFoundation);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2021-2022 Rudy De Busscher (https://www.atbash.be)
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 be.atbash.runtime.data.microstream.config;
17+
18+
19+
import one.microstream.storage.types.StorageManager;
20+
21+
public interface StorageManagerInitializer {
22+
23+
void initialize(StorageManager storageManager);
24+
}

microstream-module/src/main/java/be/atbash/runtime/data/microstream/MicroStreamModule.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package be.atbash.runtime.data.microstream;
1717

1818
import be.atbash.runtime.config.mp.module.MPConfigModule;
19+
import be.atbash.runtime.core.data.RuntimeConfiguration;
1920
import be.atbash.runtime.core.data.Specification;
2021
import be.atbash.runtime.core.data.deployment.ArchiveDeployment;
2122
import be.atbash.runtime.core.data.module.Module;
@@ -29,16 +30,22 @@
2930
import java.util.Collections;
3031
import java.util.List;
3132

33+
import static be.atbash.runtime.config.mp.MPConfigModuleConstant.ENABLED_FORCED;
34+
import static be.atbash.runtime.config.mp.module.MPConfigModule.MP_CONFIG_MODULE_NAME;
3235

33-
public class MicroStreamModule implements Module<Void> {
36+
37+
public class MicroStreamModule implements Module<RuntimeConfiguration> {
3438

3539
public static final String MICROSTREAM_MODULE_NAME = "microstream";
3640

41+
private RuntimeConfiguration configuration;
42+
3743
@Override
3844
public String name() {
3945
return MICROSTREAM_MODULE_NAME;
4046
}
4147

48+
4249
@Override
4350
public String[] dependencies() {
4451
return new String[]{MPConfigModule.MP_CONFIG_MODULE_NAME, JerseyModule.JERSEY_MODULE_NAME};
@@ -77,9 +84,21 @@ private void addJAXRSProviders(Object payload) {
7784
ExtraPackagesUtil.addPackages(deployment, "be.atbash.runtime.data.microstream.jaxrs");
7885
}
7986

87+
@Override
88+
public Class<RuntimeConfiguration> getModuleConfigClass() {
89+
return RuntimeConfiguration.class;
90+
}
91+
92+
@Override
93+
public void setConfig(RuntimeConfiguration configuration) {
94+
this.configuration = configuration;
95+
}
8096

8197
@Override
8298
public void run() {
99+
// We force that MPConfig module is always active .
100+
configuration.getConfig().getModules().writeConfigValue(MP_CONFIG_MODULE_NAME, ENABLED_FORCED, "true");
101+
83102
}
84103

85104
@Override

microstream-module/src/main/java/be/atbash/runtime/data/microstream/cdi/StorageBean.java

+13
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818

1919
import be.atbash.runtime.data.microstream.cdi.spi.AbstractBean;
20+
import be.atbash.runtime.data.microstream.config.StorageManagerInitializer;
2021
import be.atbash.runtime.data.microstream.exception.StorageTypeException;
2122
import jakarta.enterprise.context.spi.CreationalContext;
2223
import jakarta.enterprise.inject.Any;
2324
import jakarta.enterprise.inject.Default;
2425
import jakarta.enterprise.inject.spi.AnnotatedType;
26+
import jakarta.enterprise.inject.spi.Bean;
2527
import jakarta.enterprise.inject.spi.BeanManager;
2628
import jakarta.enterprise.inject.spi.InjectionPoint;
2729
import one.microstream.reflect.XReflect;
@@ -88,6 +90,17 @@ public T create(CreationalContext<T> context) {
8890
}
8991
}
9092
injectDependencies(result);
93+
94+
Set<Bean<?>> initializerBeans = beanManager.getBeans(StorageManagerInitializer.class);
95+
for (Bean<?> initializerBean : initializerBeans)
96+
{
97+
StorageManagerInitializer initializer = (StorageManagerInitializer) beanManager.getReference(initializerBean
98+
, initializerBean.getBeanClass()
99+
, beanManager.createCreationalContext(initializerBean));
100+
101+
initializer.initialize(manager);
102+
}
103+
91104
return result;
92105
}
93106

microstream-module/src/main/java/be/atbash/runtime/data/microstream/cdi/spi/MicroStreamExtension.java

+6
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ void addAnnotatedType(BeforeBeanDiscovery event, BeanManager beanManager, Class<
126126
event.addAnnotatedType(beanManager.createAnnotatedType(type), id);
127127
}
128128

129+
public boolean hasStorageRoot()
130+
{
131+
return !storageRoot.isEmpty();
132+
}
133+
134+
129135
}

microstream-module/src/main/java/be/atbash/runtime/data/microstream/cdi/spi/StorageManagerProducer.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
package be.atbash.runtime.data.microstream.cdi.spi;
1818

1919

20+
import be.atbash.runtime.data.microstream.config.EmbeddedStorageFoundationCustomizer;
21+
import be.atbash.runtime.data.microstream.config.StorageManagerInitializer;
2022
import jakarta.enterprise.context.ApplicationScoped;
2123
import jakarta.enterprise.inject.Disposes;
24+
import jakarta.enterprise.inject.Instance;
2225
import jakarta.enterprise.inject.Produces;
2326
import jakarta.inject.Inject;
2427
import one.microstream.reflect.ClassLoaderProvider;
2528
import one.microstream.storage.embedded.configuration.types.EmbeddedStorageConfigurationBuilder;
2629
import one.microstream.storage.embedded.types.EmbeddedStorageFoundation;
30+
import one.microstream.storage.embedded.types.EmbeddedStorageManager;
2731
import one.microstream.storage.types.StorageManager;
2832
import org.eclipse.microprofile.config.Config;
2933

@@ -41,6 +45,15 @@ class StorageManagerProducer {
4145
@Inject
4246
private Config config;
4347

48+
@Inject
49+
private MicroStreamExtension storageExtension;
50+
51+
@Inject
52+
private Instance<EmbeddedStorageFoundationCustomizer> customizers;
53+
54+
@Inject
55+
private Instance<StorageManagerInitializer> initializers;
56+
4457
@Produces
4558
@ApplicationScoped
4659
public StorageManager getStoreManager() {
@@ -63,10 +76,23 @@ public StorageManager getStoreManager() {
6376
embeddedStorageFoundation.onConnectionFoundation(cf -> cf.setClassLoaderProvider(ClassLoaderProvider.New(
6477
Thread.currentThread().getContextClassLoader())));
6578

66-
return embeddedStorageFoundation.start();
79+
customizers.stream()
80+
.forEach(customizer -> customizer.customize(embeddedStorageFoundation));
81+
82+
EmbeddedStorageManager storageManager = embeddedStorageFoundation.start();
83+
84+
if (!storageExtension.hasStorageRoot())
85+
{
86+
// Only execute at this point when no storage root bean has defined with @Storage
87+
// Initializers are called from StorageBean.create if user has defined @Storage and root is read.
88+
initializers.stream()
89+
.forEach(initializer -> initializer.initialize(storageManager));
90+
}
91+
92+
return storageManager;
6793
}
6894

69-
public void dispose(@Disposes final StorageManager manager) {
95+
public void dispose(@Disposes StorageManager manager) {
7096
LOGGER.info("Closing the default StorageManager");
7197
manager.close();
7298
}

0 commit comments

Comments
 (0)