Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ jdk:
- openjdk8
- openjdk7

script:
- ./mvnw test -B $MAVEN_PROFILE

after_success:
- chmod -R 777 ./travis/after_success.sh
- "./travis/after_success.sh"

matrix:
exclude:
- jdk: openjdk7
env: MAVEN_PROFILE="-Pcdi-2.0"

env:
matrix:
- MAVEN_PROFILE="-Pcdi-2.0"
- MAVEN_PROFILE="-Pcdi-1.2"
- MAVEN_PROFILE="-Pcdi-1.1"
- MAVEN_PROFILE="-Pcdi-1.0"
global:
- secure: Yt0S6+G81okW0/575CBg9pP2CcNnilyar2KKDw+4bPnFxlMS0t4y1YyX08p/Zvj5/1q7n88NrPDFdnr/qwZ0gKjmKcYMm4WTCfy+UOPtYhyQSxX6aTYOhYDMpbAQ84jB3Gh70mG52ULXxF56Hc5bNAXT0lSNfIAOndrF/LIBJAU=
- secure: QAeXjP+BabMYAHSivnbYaxrcr9pj6+FBEO/fpTlB4M0W5nALBoj8nVTOVYGv6zQHV1cxDv4DOLUxg/2sDcrWPyHV+8xbXbHvXK80T4wgeTc/SFEGOINMm/oXGdTGtY6lJ6i5VEDGwJDMRUt+M/W22HD0lqUZNDJ8xL5PAwKVr6g=
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ MyBatis-CDI extension takes care of the lifecycle of MyBatis mappers and SqlSess
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
@Transactional annotation.

Important
---------
Compatibility
-------------

| CDI API VERSION | Oracle JDK 8 | OpenJDK 8 | OpenJDK 7 |
| ------------------- | ------------ | --------- | --------- |
| cdi-1.0 | Y | Y | Y |
| cdi-1.1 | Y | Y | Y |
| cdi-1.2 (preferred) | Y | Y | Y |
| cdi-2.0 | Y | Y | N |

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

Essentials
----------
Expand Down
80 changes: 74 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@
<version>3.4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>2.4.4.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
Expand Down Expand Up @@ -161,4 +155,78 @@
</plugins>
</build>

<profiles>
<profile>
<id>cdi-2.0</id>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.1.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>cdi-1.2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>2.4.5.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>cdi-1.1</id>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>2.1.2.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>cdi-1.0</id>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>1.1.34.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
21 changes: 18 additions & 3 deletions src/main/java/org/mybatis/cdi/CDIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.util.AnnotationLiteral;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.ibatis.session.SqlSessionFactory;

/**
Expand All @@ -37,14 +39,27 @@ private CDIUtils() {
// this class cannot be instantiated
}

/**
* Gets a CDI BeanManager instance
*
* @return BeanManager instance
*/
private static BeanManager getBeanManager() {
try {
return InitialContext.doLookup("java:comp/BeanManager");
} catch (NamingException e) {
throw new RuntimeException(e);
}
}

/**
* Gets the registry.
*
* @param creationalContext the creational context
* @return the registry
*/
public static SqlSessionManagerRegistry getRegistry(CreationalContext creationalContext) {
final BeanManager beanManager = CDI.current().getBeanManager();
final BeanManager beanManager = getBeanManager();
Iterator<Bean<?>> beans = beanManager.getBeans(SqlSessionManagerRegistry.class).iterator();
return (SqlSessionManagerRegistry) beanManager.getReference(beans.next(), SqlSessionManagerRegistry.class,
creationalContext);
Expand All @@ -60,7 +75,7 @@ public static SqlSessionManagerRegistry getRegistry(CreationalContext creational
*/
public static SqlSessionFactory findSqlSessionFactory(String name, Set<Annotation> qualifiers,
CreationalContext creationalContext) {
final BeanManager beanManager = CDI.current().getBeanManager();
final BeanManager beanManager = getBeanManager();
Set<Bean<?>> beans;
if (name != null) {
beans = beanManager.getBeans(name);
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/mybatis/cdi/MockInitialContextFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.cdi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
* Provides a means to mock InitialContext JNDI lookups.
* Also see src/test/resources/jndi.properties
*
* @author Michael Lynch [mwlynch]
*/
public class MockInitialContextFactory implements InitialContextFactory {

@Mock
public static InitialContext initialContext;

public MockInitialContextFactory() {
if (initialContext == null) {
synchronized (MockInitialContextFactory.class) {
if (initialContext == null) {
MockitoAnnotations.initMocks(this);
}
}
}
}

@Override
public Context getInitialContext(Hashtable<?, ?> envmt) throws NamingException {
return initialContext;
}

}
4 changes: 4 additions & 0 deletions src/test/java/org/mybatis/cdi/WeldJUnit4Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.mybatis.cdi;

import javax.naming.InitialContext;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.mockito.Mockito;

public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {

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

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

Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/jndi.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2013-2017 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

java.naming.factory.initial=org.mybatis.cdi.MockInitialContextFactory
4 changes: 2 additions & 2 deletions travis/after_success.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ echo "Current commit detected: ${commit_message}"

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

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

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