• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / cdi / #1015

28 May 2024 08:39PM CUT coverage: 93.033%. Remained the same
#1015

Pull #469

github

web-flow
Update dependency org.mybatis:mybatis-parent to v44
Pull Request #469: Update dependency org.mybatis:mybatis-parent to v44

79 of 100 branches covered (79.0%)

227 of 244 relevant lines covered (93.03%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/main/java/org/mybatis/cdi/CDIUtils.java
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();
1✔
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();
1✔
60
    Iterator<Bean<? extends Object>> beans = beanManager.getBeans(SqlSessionManagerRegistry.class).iterator();
1✔
61
    return (SqlSessionManagerRegistry) beanManager.getReference(beans.next(), SqlSessionManagerRegistry.class,
1✔
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();
1✔
80
    Set<Bean<? extends Object>> beans;
81
    if (name != null) {
1✔
82
      beans = beanManager.getBeans(name);
1✔
83
    } else {
84
      beans = beanManager.getBeans(SqlSessionFactory.class, qualifiers.toArray(new Annotation[] {}));
1✔
85
    }
86
    Bean<? extends Object> bean = beanManager.resolve(beans);
1✔
87
    if (bean == null) {
1!
88
      throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured.");
×
89
    }
90
    return (SqlSessionFactory) beanManager.getReference(bean, SqlSessionFactory.class, creationalContext);
1✔
91
  }
92

93
  public static class SerializableDefaultAnnotationLiteral extends AnnotationLiteral<Default> {
1✔
94
    private static final long serialVersionUID = 1L;
95
  }
96

97
  public static class SerializableAnyAnnotationLiteral extends AnnotationLiteral<Any> {
1✔
98
    private static final long serialVersionUID = 1L;
99
  }
100

101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc