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.datasource.builtin;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  
22  import java.util.Properties;
23  
24  import javax.naming.Context;
25  import javax.sql.DataSource;
26  
27  import org.apache.ibatis.datasource.jndi.JndiDataSourceFactory;
28  
29  /**
30   * Provides the myBatis built-in JndiDataSourceFactory.
31   */
32  public final class JndiDataSourceProvider implements Provider<DataSource> {
33  
34    /** The properties. */
35    private final Properties properties = new Properties();
36  
37    /**
38     * Creates a new JndiDataSourceProvider with the specified JNDI data source.
39     *
40     * @param dataSource
41     *          the JNDI datasource name (fully qualified)
42     */
43    @Inject
44    public JndiDataSourceProvider(@Named("jndi.dataSource") final String dataSource) {
45      properties.setProperty(JndiDataSourceFactory.DATA_SOURCE, dataSource);
46    }
47  
48    /**
49     * Sets the initial context.
50     *
51     * @param initialContext
52     *          the new initial context
53     */
54    @com.google.inject.Inject(optional = true)
55    public void setInitialContext(@Named("jndi.initialContext") final String initialContext) {
56      properties.setProperty(JndiDataSourceFactory.INITIAL_CONTEXT, initialContext);
57    }
58  
59    /**
60     * Sets the env initial context factory.
61     *
62     * @param initialContextFactory
63     *          the new env initial context factory
64     */
65    @com.google.inject.Inject(optional = true)
66    public void setEnvInitialContextFactory(@Named(Context.INITIAL_CONTEXT_FACTORY) final String initialContextFactory) {
67      properties.setProperty(JndiDataSourceFactory.ENV_PREFIX + Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
68    }
69  
70    /**
71     * Sets the env provider URL.
72     *
73     * @param providerUrl
74     *          the new env provider URL
75     */
76    @com.google.inject.Inject(optional = true)
77    public void setEnvProviderURL(@Named(Context.PROVIDER_URL) final String providerUrl) {
78      properties.setProperty(JndiDataSourceFactory.ENV_PREFIX + Context.PROVIDER_URL, providerUrl);
79    }
80  
81    @Override
82    public DataSource get() {
83      JndiDataSourceFactory factory = new JndiDataSourceFactory();
84      factory.setProperties(properties);
85      return factory.getDataSource();
86    }
87  }