View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
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    *       https://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.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  }