1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.guice.provision;
17
18 import com.google.inject.Binder;
19 import com.google.inject.MembersInjector;
20 import com.google.inject.spi.ProvisionListener;
21
22 import jakarta.inject.Provider;
23
24 import org.mybatis.guice.configuration.ConfigurationSettingListener;
25 import org.mybatis.guice.configuration.settings.ConfigurationSetting;
26 import org.mybatis.guice.configuration.settings.MapperConfigurationSetting;
27
28 public final class ConfigurationProviderProvisionListener implements ProvisionListener {
29
30 private final ConfigurationProviderProvisionAction action;
31
32 ConfigurationProviderProvisionListener(ConfigurationProviderProvisionAction action) {
33 this.action = action;
34 }
35
36 @Override
37 public <T> void onProvision(ProvisionInvocation<T> provision) {
38 ConfigurationSettingListener configurationSettingListener = (ConfigurationSettingListener) provision.provision();
39 this.action.perform(configurationSettingListener);
40 }
41
42 public static <P extends Provider<? extends ConfigurationSetting>> ConfigurationProviderProvisionListener create(
43 final P configurationSettingProvider, final Binder binder) {
44 @SuppressWarnings("unchecked")
45 final MembersInjector<P> membersInjector = (MembersInjector<P>) binder
46 .getMembersInjector(configurationSettingProvider.getClass());
47 return new ConfigurationProviderProvisionListener(new ConfigurationProviderProvisionAction() {
48 @Override
49 public void perform(ConfigurationSettingListener configurationSettingListener) {
50 membersInjector.injectMembers(configurationSettingProvider);
51 configurationSettingListener.addConfigurationSetting(configurationSettingProvider.get());
52 }
53 });
54 }
55
56 public static ConfigurationProviderProvisionListener create(final ConfigurationSetting configurationSetting) {
57 return new ConfigurationProviderProvisionListener(new ConfigurationProviderProvisionAction() {
58 @Override
59 public void perform(ConfigurationSettingListener configurationSettingListener) {
60 configurationSettingListener.addConfigurationSetting(configurationSetting);
61 }
62 });
63 }
64
65 public static ConfigurationProviderProvisionListener create(
66 final MapperConfigurationSetting mapperConfigurationSetting) {
67 return new ConfigurationProviderProvisionListener(new ConfigurationProviderProvisionAction() {
68 @Override
69 public void perform(ConfigurationSettingListener configurationSettingListener) {
70 configurationSettingListener.addMapperConfigurationSetting(mapperConfigurationSetting);
71 }
72 });
73 }
74
75 private static interface ConfigurationProviderProvisionAction {
76 void perform(ConfigurationSettingListener configurationSettingListener);
77 }
78 }