Skip to content

Commit

Permalink
Merge pull request #645 from kenil121200/master
Browse files Browse the repository at this point in the history
Refactoring: Improve Readability and Simplify Logic in JdbcUrlAntFormatter, TxTransactionalMethodInterceptor, MyBatisModule
  • Loading branch information
hazendaz authored Dec 2, 2023
2 parents d055a75 + 5eee4de commit 6b5f63f
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 80 deletions.
33 changes: 32 additions & 1 deletion src/main/java/org/mybatis/guice/AbstractMyBatisModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 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.
Expand All @@ -20,14 +20,17 @@
import static com.google.inject.matcher.Matchers.not;
import static com.google.inject.name.Names.named;
import static com.google.inject.util.Providers.guicify;
import static org.mybatis.guice.Preconditions.checkArgument;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Scopes;
import com.google.inject.matcher.AbstractMatcher;

import java.lang.reflect.Method;
import java.util.Set;

import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionManager;
import org.mybatis.guice.mappers.MapperProvider;
Expand Down Expand Up @@ -55,6 +58,34 @@ public boolean matches(Method method) {

private ClassLoader driverClassLoader = getDefaultClassLoader();

/**
* Return a set of all classes contained in the given package.
*
* @param packageName
* the package has to be analyzed.
*
* @return a set of all classes contained in the given package.
*/
protected static Set<Class<?>> getClasses(String packageName) {
return AbstractMyBatisModule.getClasses(new ResolverUtil.IsA(Object.class), packageName);
}

/**
* Return a set of all classes contained in the given package that match with the given test requirement.
*
* @param test
* the class filter on the given package.
* @param packageName
* the package has to be analyzed.
*
* @return a set of all classes contained in the given package.
*/
protected static Set<Class<?>> getClasses(ResolverUtil.Test test, String packageName) {
checkArgument(test != null, "Parameter 'test' must not be null");
checkArgument(packageName != null, "Parameter 'packageName' must not be null");
return new ResolverUtil<Object>().find(test, packageName).getClasses();
}

@Override
protected final void configure() {
try {
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/org/mybatis/guice/MyBatisModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,32 +689,4 @@ protected final void addMapperClasses(final String packageName) {
protected final void addMapperClasses(final String packageName, final ResolverUtil.Test test) {
addMapperClasses(getClasses(test, packageName));
}

/**
* Return a set of all classes contained in the given package.
*
* @param packageName
* the package has to be analyzed.
*
* @return a set of all classes contained in the given package.
*/
private static Set<Class<?>> getClasses(String packageName) {
return getClasses(new ResolverUtil.IsA(Object.class), packageName);
}

/**
* Return a set of all classes contained in the given package that match with the given test requirement.
*
* @param test
* the class filter on the given package.
* @param packageName
* the package has to be analyzed.
*
* @return a set of all classes contained in the given package.
*/
private static Set<Class<?>> getClasses(ResolverUtil.Test test, String packageName) {
checkArgument(test != null, "Parameter 'test' must not be null");
checkArgument(packageName != null, "Parameter 'packageName' must not be null");
return new ResolverUtil<Object>().find(test, packageName).getClasses();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,87 @@ public final class JdbcUrlAntFormatter implements Provider<String> {
private final List<KeyResolver> resolvers = new ArrayList<KeyResolver>();

/**
* Instantiates a new jdbc url ant formatter.
* Constructs a new JdbcUrlAntFormatter based on the provided pattern.
*
* @param pattern
* the pattern
* the pattern for URL formatting
*/
public JdbcUrlAntFormatter(final String pattern) {
int prev = 0;
int pos;
while ((pos = pattern.indexOf(VAR_BEGIN, prev)) >= 0) {
if (pos > 0) {
appenders.add(Providers.of(pattern.substring(prev, pos)));
}
if (pos == pattern.length() - 1) {
appenders.add(Providers.of(VAR_BEGIN));
prev = pos + 1;
} else if (pattern.charAt(pos + 1) != '{') {
if (pattern.charAt(pos + 1) == '$') {
appenders.add(Providers.of(VAR_BEGIN));
prev = pos + 2;
} else {
appenders.add(Providers.of(pattern.substring(pos, pos + 2)));
prev = pos + 2;
}
} else {
int endName = pattern.indexOf('}', pos);
if (endName < 0) {
throw new IllegalArgumentException("Syntax error in property: " + pattern);
}
StringTokenizer keyTokenizer = new StringTokenizer(pattern.substring(pos + 2, endName), PIPE_SEPARATOR);
String key = keyTokenizer.nextToken();
String defaultValue = null;
if (keyTokenizer.hasMoreTokens()) {
defaultValue = keyTokenizer.nextToken();
}
KeyResolver resolver = new KeyResolver(key, defaultValue);
appenders.add(resolver);
resolvers.add(resolver);
prev = endName + 1;
}
initializeAppender(pattern);
}

/**
* Initializes the appenders based on the given pattern.
*
* @param pattern
* the pattern for URL formatting
*/
private void initializeAppender(String pattern) {
int previousIndex = 0;
int currentIndex;
while ((currentIndex = pattern.indexOf(VAR_BEGIN, previousIndex)) >= 0) {
processPatternSubstring(pattern, previousIndex, currentIndex);
previousIndex = updatePrevPosition(pattern, currentIndex);
}
appendRemainingPatternSubstring(pattern, previousIndex);
}

// Processes the substring of the pattern based on the currentIndex and previousIndex
private void processPatternSubstring(String pattern, int previousIndex, int currentIndex) {
if (currentIndex > 0) {
appenders.add(Providers.of(pattern.substring(previousIndex, currentIndex)));
}

if (currentIndex == pattern.length() - 1) {
appenders.add(Providers.of(VAR_BEGIN));
} else if (pattern.charAt(currentIndex + 1) != '{') {
handleNonCurlyBracePattern(pattern, currentIndex);
} else {
handleCurlyBracePattern(pattern, currentIndex);
}
}

// Handles patterns without curly braces
private void handleNonCurlyBracePattern(String pattern, int currentIndex) {
if (pattern.charAt(currentIndex + 1) == '$') {
appenders.add(Providers.of(VAR_BEGIN));
} else {
appenders.add(Providers.of(pattern.substring(currentIndex, currentIndex + 2)));
}
}

// Handles patterns with curly braces
private void handleCurlyBracePattern(String pattern, int currentIndex) {
int endName = pattern.indexOf('}', currentIndex);
if (endName < 0) {
throw new IllegalArgumentException("Syntax error in property: " + pattern);
}
processKeyResolver(pattern, currentIndex, endName);
}

// Method to append KeyResolver based on the variable
private void processKeyResolver(String pattern, int startPos, int endPos) {
StringTokenizer keyTokenizer = new StringTokenizer(pattern.substring(startPos + 2, endPos), PIPE_SEPARATOR);
String key = keyTokenizer.nextToken();
String defaultValue = keyTokenizer.hasMoreTokens() ? keyTokenizer.nextToken() : null;
KeyResolver resolver = new KeyResolver(key, defaultValue);
appenders.add(resolver);
resolvers.add(resolver);
}

// Updates the previous position based on the current index and pattern
private int updatePrevPosition(String pattern, int currentIndex) {
if (pattern.charAt(currentIndex + 1) == '{') {
return pattern.indexOf('}', currentIndex) + 1;
} else {
return currentIndex + (pattern.charAt(currentIndex + 1) == '$' ? 2 : 1);
}
if (prev < pattern.length()) {
appenders.add(Providers.of(pattern.substring(prev)));
}

// Appends the remaining substring of the pattern
private void appendRemainingPatternSubstring(String pattern, int previousIndex) {
if (previousIndex < pattern.length()) {
appenders.add(Providers.of(pattern.substring(previousIndex)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public class MandatoryTransactionAttributeStrategy implements TransactionAttributeStrategy {
@Override
public TransactionAttribute getTransactionAttribute() {
return TransactionAttribute.MANDATORY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public class NeverTransactionAttributeStrategy implements TransactionAttributeStrategy {
@Override
public TransactionAttribute getTransactionAttribute() {
return TransactionAttribute.NEVER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public class RequiredTransactionAttributeStrategy implements TransactionAttributeStrategy {
@Override
public TransactionAttribute getTransactionAttribute() {
return TransactionAttribute.REQUIRED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public class RequiresNewTransactionAttributeStrategy implements TransactionAttributeStrategy {
@Override
public TransactionAttribute getTransactionAttribute() {
return TransactionAttribute.REQUIRESNEW;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public class SupportsTransactionAttributeStrategy implements TransactionAttributeStrategy {
@Override
public TransactionAttribute getTransactionAttribute() {
return TransactionAttribute.SUPPORTS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2009-2023 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
*
* https://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.guice.transactional;

public interface TransactionAttributeStrategy {
TransactionAttribute getTransactionAttribute();
}
Loading

0 comments on commit 6b5f63f

Please sign in to comment.