View Javadoc
1   /*
2    *    Copyright 2013-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.cdi;
17  
18  import jakarta.enterprise.context.spi.CreationalContext;
19  import jakarta.enterprise.inject.Any;
20  import jakarta.enterprise.inject.Default;
21  import jakarta.enterprise.inject.spi.Bean;
22  import jakarta.enterprise.inject.spi.BeanManager;
23  import jakarta.enterprise.inject.spi.CDI;
24  import jakarta.enterprise.util.AnnotationLiteral;
25  
26  import java.lang.annotation.Annotation;
27  import java.util.Iterator;
28  import java.util.Set;
29  
30  import org.apache.ibatis.session.SqlSessionFactory;
31  
32  /**
33   * @author Frank D. Martinez [mnesarco]
34   */
35  public final class CDIUtils {
36  
37    private CDIUtils() {
38      // this class cannot be instantiated
39    }
40  
41    /**
42     * Gets a CDI BeanManager instance
43     *
44     * @return BeanManager instance
45     */
46    private static BeanManager getBeanManager() {
47      return CDI.current().getBeanManager();
48    }
49  
50    /**
51     * Gets the registry.
52     *
53     * @param creationalContext
54     *          the creational context
55     *
56     * @return the registry
57     */
58    public static <T> SqlSessionManagerRegistry getRegistry(CreationalContext<T> creationalContext) {
59      final BeanManager beanManager = getBeanManager();
60      Iterator<Bean<? extends Object>> beans = beanManager.getBeans(SqlSessionManagerRegistry.class).iterator();
61      return (SqlSessionManagerRegistry) beanManager.getReference(beans.next(), SqlSessionManagerRegistry.class,
62          creationalContext);
63    }
64  
65    /**
66     * Find sql session factory.
67     *
68     * @param name
69     *          the name
70     * @param qualifiers
71     *          the qualifiers
72     * @param creationalContext
73     *          the creational context
74     *
75     * @return the sql session factory
76     */
77    public static <T> SqlSessionFactory findSqlSessionFactory(String name, Set<Annotation> qualifiers,
78        CreationalContext<T> creationalContext) {
79      final BeanManager beanManager = getBeanManager();
80      Set<Bean<? extends Object>> beans;
81      if (name != null) {
82        beans = beanManager.getBeans(name);
83      } else {
84        beans = beanManager.getBeans(SqlSessionFactory.class, qualifiers.toArray(new Annotation[] {}));
85      }
86      Bean<? extends Object> bean = beanManager.resolve(beans);
87      if (bean == null) {
88        throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured.");
89      }
90      return (SqlSessionFactory) beanManager.getReference(bean, SqlSessionFactory.class, creationalContext);
91    }
92  
93    public static class SerializableDefaultAnnotationLiteral extends AnnotationLiteral<Default> {
94      private static final long serialVersionUID = 1L;
95    }
96  
97    public static class SerializableAnyAnnotationLiteral extends AnnotationLiteral<Any> {
98      private static final long serialVersionUID = 1L;
99    }
100 
101 }