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

mybatis / cdi / #831

03 Nov 2023 11:37PM UTC coverage: 93.033%. Remained the same
#831

push

github

web-flow
Merge pull request #403 from hazendaz/master

Support new site distro and allow jdk 22

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

100.0
/src/main/java/org/mybatis/cdi/MyBatisBean.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.Dependent;
19
import jakarta.enterprise.context.spi.CreationalContext;
20
import jakarta.enterprise.inject.spi.Bean;
21
import jakarta.enterprise.inject.spi.InjectionPoint;
22
import jakarta.enterprise.inject.spi.PassivationCapable;
23

24
import java.io.Serializable;
25
import java.lang.annotation.Annotation;
26
import java.lang.reflect.Proxy;
27
import java.lang.reflect.Type;
28
import java.util.Collections;
29
import java.util.HashSet;
30
import java.util.Set;
31

32
import org.apache.ibatis.executor.ErrorContext;
33
import org.apache.ibatis.session.SqlSession;
34
import org.apache.ibatis.session.SqlSessionFactory;
35
import org.apache.ibatis.session.SqlSessionManager;
36

37
/**
38
 * Internal CDI metadata for a mapper bean.
39
 *
40
 * @author Frank D. Martinez [mnesarco]
41
 */
42
public class MyBatisBean implements Bean<Object>, Serializable, PassivationCapable {
43

44
  private static final long serialVersionUID = 1L;
45

46
  protected final Class<Type> type;
47

48
  protected final Set<Annotation> qualifiers;
49

50
  protected final String sqlSessionFactoryName;
51

52
  protected final String id;
53

54
  /**
55
   * Instantiates a new my batis bean.
56
   *
57
   * @param id
58
   *          the id
59
   * @param type
60
   *          the type
61
   * @param qualifiers
62
   *          the qualifiers
63
   * @param sqlSessionFactoryName
64
   *          the sql session factory name
65
   */
66
  public MyBatisBean(String id, Class<Type> type, Set<Annotation> qualifiers, String sqlSessionFactoryName) {
1✔
67
    this.id = id;
1✔
68
    this.type = type;
1✔
69
    this.sqlSessionFactoryName = sqlSessionFactoryName;
1✔
70
    if (qualifiers == null || qualifiers.isEmpty()) {
1✔
71
      this.qualifiers = new HashSet<>();
1✔
72
      this.qualifiers.add(new CDIUtils.SerializableDefaultAnnotationLiteral());
1✔
73
      this.qualifiers.add(new CDIUtils.SerializableAnyAnnotationLiteral());
1✔
74
    } else {
75
      this.qualifiers = qualifiers;
1✔
76
    }
77
  }
1✔
78

79
  @Override
80
  public Set<Type> getTypes() {
81
    Set<Type> types = new HashSet<>();
1✔
82
    types.add(this.type);
1✔
83
    return types;
1✔
84
  }
85

86
  @Override
87
  public Set<Annotation> getQualifiers() {
88
    return this.qualifiers;
1✔
89
  }
90

91
  @Override
92
  public Class<Dependent> getScope() {
93
    return Dependent.class;
1✔
94
  }
95

96
  @Override
97
  public String getName() {
98
    return null;
1✔
99
  }
100

101
  @Override
102
  public Set<Class<? extends Annotation>> getStereotypes() {
103
    return Collections.emptySet();
1✔
104
  }
105

106
  @Override
107
  public Class<Type> getBeanClass() {
108
    return this.type;
1✔
109
  }
110

111
  @Override
112
  public boolean isAlternative() {
113
    return false;
1✔
114
  }
115

116
  @Override
117
  public Set<InjectionPoint> getInjectionPoints() {
118
    return Collections.emptySet();
1✔
119
  }
120

121
  @Override
122
  public Object create(CreationalContext<Object> creationalContext) {
123
    if (SqlSession.class.equals(this.type)) {
1✔
124
      return findSqlSessionManager(creationalContext);
1✔
125
    }
126
    ErrorContext.instance().reset();
1✔
127
    return Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[] { this.type },
1✔
128
        new SerializableMapperProxy<>(this, creationalContext));
129
  }
130

131
  @Override
132
  public void destroy(Object instance, CreationalContext<Object> creationalContext) {
133
    creationalContext.release();
1✔
134
  }
1✔
135

136
  private <T> SqlSessionManager findSqlSessionManager(CreationalContext<T> creationalContext) {
137
    SqlSessionFactory factory = CDIUtils.findSqlSessionFactory(this.sqlSessionFactoryName, this.qualifiers,
1✔
138
        creationalContext);
139
    return CDIUtils.getRegistry(creationalContext).getManager(factory);
1✔
140
  }
141

142
  @Override
143
  public String getId() {
144
    return this.id;
1✔
145
  }
146

147
}
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