Skip to content

Commit f25f831

Browse files
authored
Merge pull request #72 from mwlynch/master
Improve CDI version compatibility.
2 parents af174de + 9786ddc commit f25f831

8 files changed

Lines changed: 191 additions & 14 deletions

File tree

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@ jdk:
66
- openjdk8
77
- openjdk7
88

9+
script:
10+
- ./mvnw test -B $MAVEN_PROFILE
11+
912
after_success:
1013
- chmod -R 777 ./travis/after_success.sh
1114
- "./travis/after_success.sh"
1215

16+
matrix:
17+
exclude:
18+
- jdk: openjdk7
19+
env: MAVEN_PROFILE="-Pcdi-2.0"
20+
1321
env:
22+
matrix:
23+
- MAVEN_PROFILE="-Pcdi-2.0"
24+
- MAVEN_PROFILE="-Pcdi-1.2"
25+
- MAVEN_PROFILE="-Pcdi-1.1"
26+
- MAVEN_PROFILE="-Pcdi-1.0"
1427
global:
1528
- secure: Yt0S6+G81okW0/575CBg9pP2CcNnilyar2KKDw+4bPnFxlMS0t4y1YyX08p/Zvj5/1q7n88NrPDFdnr/qwZ0gKjmKcYMm4WTCfy+UOPtYhyQSxX6aTYOhYDMpbAQ84jB3Gh70mG52ULXxF56Hc5bNAXT0lSNfIAOndrF/LIBJAU=
1629
- secure: QAeXjP+BabMYAHSivnbYaxrcr9pj6+FBEO/fpTlB4M0W5nALBoj8nVTOVYGv6zQHV1cxDv4DOLUxg/2sDcrWPyHV+8xbXbHvXK80T4wgeTc/SFEGOINMm/oXGdTGtY6lJ6i5VEDGwJDMRUt+M/W22HD0lqUZNDJ8xL5PAwKVr6g=

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ MyBatis-CDI extension takes care of the lifecycle of MyBatis mappers and SqlSess
1313
CDI beans ready to be used, there is no need to create or destroy them. It also provides local and JTA transaction support based on the
1414
@Transactional annotation.
1515

16-
Important
17-
---------
16+
Compatibility
17+
-------------
18+
19+
| CDI API VERSION | Oracle JDK 8 | OpenJDK 8 | OpenJDK 7 |
20+
| ------------------- | ------------ | --------- | --------- |
21+
| cdi-1.0 | Y | Y | Y |
22+
| cdi-1.1 | Y | Y | Y |
23+
| cdi-1.2 (preferred) | Y | Y | Y |
24+
| cdi-2.0 | Y | Y | N |
1825

19-
CDI 1.0 is not supported. This extension requieres minimum CDI 1.1, CDI 1.2 is prefered.
2026

2127
Essentials
2228
----------

pom.xml

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@
7171
<version>3.4.5</version>
7272
<scope>provided</scope>
7373
</dependency>
74-
<dependency>
75-
<groupId>org.jboss.weld.se</groupId>
76-
<artifactId>weld-se</artifactId>
77-
<version>2.4.4.Final</version>
78-
<scope>provided</scope>
79-
</dependency>
8074
<dependency>
8175
<groupId>org.jboss.spec.javax.transaction</groupId>
8276
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
@@ -161,4 +155,78 @@
161155
</plugins>
162156
</build>
163157

158+
<profiles>
159+
<profile>
160+
<id>cdi-2.0</id>
161+
<dependencies>
162+
<dependency>
163+
<groupId>javax.enterprise</groupId>
164+
<artifactId>cdi-api</artifactId>
165+
<version>2.0</version>
166+
<scope>provided</scope>
167+
</dependency>
168+
<dependency>
169+
<groupId>org.jboss.weld.se</groupId>
170+
<artifactId>weld-se-core</artifactId>
171+
<version>3.0.1.Final</version>
172+
<scope>test</scope>
173+
</dependency>
174+
</dependencies>
175+
</profile>
176+
<profile>
177+
<id>cdi-1.2</id>
178+
<activation>
179+
<activeByDefault>true</activeByDefault>
180+
</activation>
181+
<dependencies>
182+
<dependency>
183+
<groupId>javax.enterprise</groupId>
184+
<artifactId>cdi-api</artifactId>
185+
<version>1.2</version>
186+
<scope>provided</scope>
187+
</dependency>
188+
<dependency>
189+
<groupId>org.jboss.weld.se</groupId>
190+
<artifactId>weld-se</artifactId>
191+
<version>2.4.5.Final</version>
192+
<scope>test</scope>
193+
</dependency>
194+
</dependencies>
195+
</profile>
196+
<profile>
197+
<id>cdi-1.1</id>
198+
<dependencies>
199+
<dependency>
200+
<groupId>javax.enterprise</groupId>
201+
<artifactId>cdi-api</artifactId>
202+
<version>1.1</version>
203+
<scope>provided</scope>
204+
</dependency>
205+
<dependency>
206+
<groupId>org.jboss.weld.se</groupId>
207+
<artifactId>weld-se</artifactId>
208+
<version>2.1.2.Final</version>
209+
<scope>test</scope>
210+
</dependency>
211+
</dependencies>
212+
</profile>
213+
<profile>
214+
<id>cdi-1.0</id>
215+
<dependencies>
216+
<dependency>
217+
<groupId>javax.enterprise</groupId>
218+
<artifactId>cdi-api</artifactId>
219+
<version>1.0</version>
220+
<scope>provided</scope>
221+
</dependency>
222+
<dependency>
223+
<groupId>org.jboss.weld.se</groupId>
224+
<artifactId>weld-se</artifactId>
225+
<version>1.1.34.Final</version>
226+
<scope>test</scope>
227+
</dependency>
228+
</dependencies>
229+
</profile>
230+
</profiles>
231+
164232
</project>

src/main/java/org/mybatis/cdi/CDIUtils.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import javax.enterprise.inject.Default;
2424
import javax.enterprise.inject.spi.Bean;
2525
import javax.enterprise.inject.spi.BeanManager;
26-
import javax.enterprise.inject.spi.CDI;
2726
import javax.enterprise.util.AnnotationLiteral;
27+
import javax.naming.InitialContext;
28+
import javax.naming.NamingException;
29+
2830
import org.apache.ibatis.session.SqlSessionFactory;
2931

3032
/**
@@ -37,14 +39,27 @@ private CDIUtils() {
3739
// this class cannot be instantiated
3840
}
3941

42+
/**
43+
* Gets a CDI BeanManager instance
44+
*
45+
* @return BeanManager instance
46+
*/
47+
private static BeanManager getBeanManager() {
48+
try {
49+
return InitialContext.doLookup("java:comp/BeanManager");
50+
} catch (NamingException e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
4055
/**
4156
* Gets the registry.
4257
*
4358
* @param creationalContext the creational context
4459
* @return the registry
4560
*/
4661
public static SqlSessionManagerRegistry getRegistry(CreationalContext creationalContext) {
47-
final BeanManager beanManager = CDI.current().getBeanManager();
62+
final BeanManager beanManager = getBeanManager();
4863
Iterator<Bean<?>> beans = beanManager.getBeans(SqlSessionManagerRegistry.class).iterator();
4964
return (SqlSessionManagerRegistry) beanManager.getReference(beans.next(), SqlSessionManagerRegistry.class,
5065
creationalContext);
@@ -60,7 +75,7 @@ public static SqlSessionManagerRegistry getRegistry(CreationalContext creational
6075
*/
6176
public static SqlSessionFactory findSqlSessionFactory(String name, Set<Annotation> qualifiers,
6277
CreationalContext creationalContext) {
63-
final BeanManager beanManager = CDI.current().getBeanManager();
78+
final BeanManager beanManager = getBeanManager();
6479
Set<Bean<?>> beans;
6580
if (name != null) {
6681
beans = beanManager.getBeans(name);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2013-2017 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+
* http://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 java.util.Hashtable;
19+
20+
import javax.naming.Context;
21+
import javax.naming.InitialContext;
22+
import javax.naming.NamingException;
23+
import javax.naming.spi.InitialContextFactory;
24+
25+
import org.mockito.Mock;
26+
import org.mockito.MockitoAnnotations;
27+
28+
/**
29+
* Provides a means to mock InitialContext JNDI lookups.
30+
* Also see src/test/resources/jndi.properties
31+
*
32+
* @author Michael Lynch [mwlynch]
33+
*/
34+
public class MockInitialContextFactory implements InitialContextFactory {
35+
36+
@Mock
37+
public static InitialContext initialContext;
38+
39+
public MockInitialContextFactory() {
40+
if (initialContext == null) {
41+
synchronized (MockInitialContextFactory.class) {
42+
if (initialContext == null) {
43+
MockitoAnnotations.initMocks(this);
44+
}
45+
}
46+
}
47+
}
48+
49+
@Override
50+
public Context getInitialContext(Hashtable<?, ?> envmt) throws NamingException {
51+
return initialContext;
52+
}
53+
54+
}

src/test/java/org/mybatis/cdi/WeldJUnit4Runner.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package org.mybatis.cdi;
1717

18+
import javax.naming.InitialContext;
19+
1820
import org.jboss.weld.environment.se.Weld;
1921
import org.jboss.weld.environment.se.WeldContainer;
2022
import org.junit.runners.BlockJUnit4ClassRunner;
2123
import org.junit.runners.model.InitializationError;
24+
import org.mockito.Mockito;
2225

2326
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
2427

@@ -35,6 +38,7 @@ public WeldJUnit4Runner(final Class<Object> initializerClass) throws Initializat
3538

3639
@Override
3740
protected Object createTest() throws Exception {
41+
Mockito.when(new InitialContext().doLookup("java:comp/BeanManager")).thenReturn(container.getBeanManager());
3842
return this.container.instance().select(this.klass).get();
3943
}
4044

src/test/resources/jndi.properties

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2013-2017 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+
# http://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+
17+
java.naming.factory.initial=org.mybatis.cdi.MockInitialContextFactory

travis/after_success.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ echo "Current commit detected: ${commit_message}"
3333

3434
if [ $TRAVIS_REPO_SLUG == "mybatis/cdi" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ] && [[ "$commit_message" != *"[maven-release-plugin]"* ]]; then
3535

36-
if [ ${TRAVIS_JDK_VERSION} == "oraclejdk8" ]; then
36+
if [ ${TRAVIS_JDK_VERSION} == "oraclejdk8" ] && [ ${MAVEN_PROFILE} == "-Pcdi-1.2" ]; then
3737

3838
# Deploy to sonatype
3939
./mvnw clean deploy -q --settings ./travis/settings.xml
@@ -54,4 +54,4 @@ else
5454
echo "Travis Pull Request: $TRAVIS_PULL_REQUEST"
5555
echo "Travis Branch: $TRAVIS_BRANCH"
5656
echo "Travis build skipped"
57-
fi
57+
fi

0 commit comments

Comments
 (0)