Use LogAccessor from SF
* Change main classes to use a `LogAccessor` API to simplify code flow * Fix tests according `LogAccessor` property * Fix some Sonar smells
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -18,8 +18,6 @@ package org.springframework.integration.jpa.config.xml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@@ -27,6 +25,7 @@ import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.jpa.core.JpaExecutor;
|
||||
import org.springframework.integration.jpa.support.JpaParameter;
|
||||
@@ -35,28 +34,23 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
|
||||
/**
|
||||
* Contains various utility methods for parsing JPA Adapter specific namesspace
|
||||
* Contains various utility methods for parsing JPA Adapter specific namespace
|
||||
* elements and generation the respective {@link BeanDefinition}s.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public final class JpaParserUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(JpaParserUtils.class);
|
||||
|
||||
/** Prevent instantiation. */
|
||||
private JpaParserUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
private static final LogAccessor LOGGER = new LogAccessor(JpaParserUtils.class);
|
||||
|
||||
/**
|
||||
* Create a new {@link BeanDefinitionBuilder} for the class {@link JpaExecutor}.
|
||||
* Initialize the wrapped {@link JpaExecutor} with common properties.
|
||||
*
|
||||
* @param element Must not be null
|
||||
* @param parserContext Must not be null
|
||||
* @return The BeanDefinitionBuilder for the JpaExecutor
|
||||
@@ -109,7 +103,7 @@ public final class JpaParserUtils {
|
||||
"'entity-manager-factory' or 'jpa-operations' must be be set.", source);
|
||||
}
|
||||
|
||||
final ManagedList<BeanDefinition> jpaParameterList = JpaParserUtils.getJpaParameterBeanDefinitions(element, parserContext);
|
||||
final ManagedList<BeanDefinition> jpaParameterList = getJpaParameterBeanDefinitions(element, parserContext);
|
||||
|
||||
if (!jpaParameterList.isEmpty()) {
|
||||
jpaExecutorBuilder.addPropertyValue("jpaParameters", jpaParameterList);
|
||||
@@ -127,22 +121,21 @@ public final class JpaParserUtils {
|
||||
/**
|
||||
* Create a new {@link BeanDefinitionBuilder} for the class {@link JpaExecutor}
|
||||
* that is specific for JPA Outbound Gateways.
|
||||
*
|
||||
* Initializes the wrapped {@link JpaExecutor} with common properties.
|
||||
* Delegates to {@link JpaParserUtils#getJpaExecutorBuilder(Element, ParserContext)}
|
||||
*
|
||||
* @param gatewayElement Must not be null
|
||||
* @param parserContext Must not be null
|
||||
*
|
||||
* @return The BeanDefinitionBuilder for the JpaExecutor
|
||||
*/
|
||||
public static BeanDefinitionBuilder getOutboundGatewayJpaExecutorBuilder(final Element gatewayElement,
|
||||
final ParserContext parserContext) {
|
||||
|
||||
final BeanDefinitionBuilder jpaExecutorBuilder = JpaParserUtils.getJpaExecutorBuilder(gatewayElement, parserContext);
|
||||
final BeanDefinitionBuilder jpaExecutorBuilder = getJpaExecutorBuilder(gatewayElement, parserContext);
|
||||
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(jpaExecutorBuilder, gatewayElement, "parameter-source-factory");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(jpaExecutorBuilder, gatewayElement, "use-payload-as-parameter-source");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(jpaExecutorBuilder, gatewayElement,
|
||||
"parameter-source-factory");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(jpaExecutorBuilder, gatewayElement,
|
||||
"use-payload-as-parameter-source");
|
||||
|
||||
return jpaExecutorBuilder;
|
||||
|
||||
@@ -151,10 +144,8 @@ public final class JpaParserUtils {
|
||||
/**
|
||||
* Create a {@link ManagedList} of {@link BeanDefinition}s containing parsed
|
||||
* JPA Parameters.
|
||||
*
|
||||
* @param jpaComponent Must not be null
|
||||
* @param parserContext Must not be null
|
||||
*
|
||||
* @return {@link ManagedList} of {@link BeanDefinition}s
|
||||
*/
|
||||
public static ManagedList<BeanDefinition> getJpaParameterBeanDefinitions(
|
||||
@@ -163,14 +154,13 @@ public final class JpaParserUtils {
|
||||
Assert.notNull(jpaComponent, "The provided element must not be null.");
|
||||
Assert.notNull(parserContext, "The provided parserContext must not be null.");
|
||||
|
||||
final ManagedList<BeanDefinition> parameterList = new ManagedList<BeanDefinition>();
|
||||
final ManagedList<BeanDefinition> parameterList = new ManagedList<>();
|
||||
|
||||
final List<Element> parameterChildElements = DomUtils
|
||||
.getChildElementsByTagName(jpaComponent, "parameter");
|
||||
|
||||
for (Element childElement : parameterChildElements) {
|
||||
|
||||
final BeanDefinitionBuilder parameterBuilder = BeanDefinitionBuilder.genericBeanDefinition(JpaParameter.class);
|
||||
BeanDefinitionBuilder parameterBuilder = BeanDefinitionBuilder.genericBeanDefinition(JpaParameter.class);
|
||||
|
||||
String name = childElement.getAttribute("name");
|
||||
String expression = childElement.getAttribute("expression");
|
||||
@@ -186,24 +176,14 @@ public final class JpaParserUtils {
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(value)) {
|
||||
|
||||
if (!StringUtils.hasText(type)) {
|
||||
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(String
|
||||
.format("Type attribute not set for parameter '%s'. Defaulting to "
|
||||
+ "'java.lang.String'.", value));
|
||||
}
|
||||
|
||||
parameterBuilder.addPropertyValue("value",
|
||||
new TypedStringValue(value, String.class));
|
||||
|
||||
LOGGER.info(() -> String.format("Type attribute not set for parameter '%s'. Defaulting to "
|
||||
+ "'java.lang.String'.", value));
|
||||
parameterBuilder.addPropertyValue("value", new TypedStringValue(value, String.class));
|
||||
}
|
||||
else {
|
||||
parameterBuilder.addPropertyValue("value",
|
||||
new TypedStringValue(value, type));
|
||||
parameterBuilder.addPropertyValue("value", new TypedStringValue(value, type));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parameterList.add(parameterBuilder.getBeanDefinition());
|
||||
@@ -213,4 +193,7 @@ public final class JpaParserUtils {
|
||||
|
||||
}
|
||||
|
||||
private JpaParserUtils() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,14 +20,13 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Parameter;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.integration.jpa.support.JpaUtils;
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
|
||||
import org.springframework.integration.jpa.support.parametersource.PositionSupportingParameterSource;
|
||||
@@ -37,7 +36,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Class similar to JPA template limited to the operations required for the JPA adapters/gateway
|
||||
* not using JpaTemplate as the class is deprecated since Spring 3.1
|
||||
* not using JpaTemplate as the class is deprecated since Spring 3.1.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Gunnar Hillert
|
||||
@@ -47,7 +46,7 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultJpaOperations.class);
|
||||
private static final LogAccessor LOGGER = new LogAccessor(DefaultJpaOperations.class);
|
||||
|
||||
@Override
|
||||
public void delete(Object entity) {
|
||||
@@ -57,11 +56,8 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
|
||||
@Override
|
||||
public void deleteInBatch(Iterable<?> entities) {
|
||||
|
||||
Assert.notNull(entities, "entities must not be null.");
|
||||
|
||||
Iterator<?> iterator = entities.iterator();
|
||||
|
||||
if (!iterator.hasNext()) {
|
||||
return;
|
||||
}
|
||||
@@ -80,7 +76,7 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
}
|
||||
|
||||
EntityManager entityManager = getEntityManager();
|
||||
final String entityName = JpaUtils.getEntityName(entityManager, entityClass);
|
||||
final String entityName = JpaUtils.getEntityName(entityManager, entityClass);
|
||||
final String queryString = JpaUtils.getQueryString(JpaUtils.DELETE_ALL_QUERY_STRING, entityName);
|
||||
|
||||
JpaUtils.applyAndBind(queryString, entities, entityManager)
|
||||
@@ -89,7 +85,7 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate(String updateQuery, ParameterSource source) {
|
||||
public int executeUpdate(String updateQuery, ParameterSource source) {
|
||||
Query query = getEntityManager().createQuery(updateQuery);
|
||||
setParametersIfRequired(updateQuery, source, query);
|
||||
return query.executeUpdate();
|
||||
@@ -156,7 +152,7 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<?> getResultListForNativeQuery(String selectQuery, Class<?> entityClass,
|
||||
public List<?> getResultListForNativeQuery(String selectQuery, @Nullable Class<?> entityClass,
|
||||
ParameterSource parameterSource, int firstResult, int maxNumberOfResults) {
|
||||
|
||||
final Query query;
|
||||
@@ -182,7 +178,7 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
|
||||
@Override
|
||||
public List<?> getResultListForQuery(String query, ParameterSource source) {
|
||||
return getResultListForQuery(query, source, 0, 0);
|
||||
return getResultListForQuery(query, source, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,15 +261,15 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
@SuppressWarnings("unchecked")
|
||||
Iterable<Object> entities = (Iterable<Object>) entity;
|
||||
|
||||
int savedEntities = 0;
|
||||
int nullEntities = 0;
|
||||
AtomicInteger savedEntities = new AtomicInteger();
|
||||
AtomicInteger nullEntities = new AtomicInteger();
|
||||
|
||||
List<Object> mergedEntities = new ArrayList<Object>();
|
||||
List<Object> mergedEntities = new ArrayList<>();
|
||||
|
||||
EntityManager entityManager = getEntityManager();
|
||||
for (Object iteratedEntity : entities) {
|
||||
if (iteratedEntity == null) {
|
||||
nullEntities++;
|
||||
nullEntities.incrementAndGet();
|
||||
}
|
||||
else {
|
||||
if (isMerge) {
|
||||
@@ -282,8 +278,8 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
else {
|
||||
entityManager.persist(iteratedEntity);
|
||||
}
|
||||
savedEntities++;
|
||||
if (flushSize > 0 && savedEntities % flushSize == 0) {
|
||||
savedEntities.incrementAndGet();
|
||||
if (flushSize > 0 && savedEntities.get() % flushSize == 0) {
|
||||
entityManager.flush();
|
||||
if (clearOnFlush) {
|
||||
entityManager.clear();
|
||||
@@ -292,10 +288,8 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("%s %s entities. %s NULL entities were ignored.",
|
||||
isMerge ? "Merged" : "Persisted", savedEntities, nullEntities));
|
||||
}
|
||||
LOGGER.debug(() -> String.format("%s %s entities. %s NULL entities were ignored.",
|
||||
isMerge ? "Merged" : "Persisted", savedEntities.get(), nullEntities));
|
||||
|
||||
if (isMerge) {
|
||||
result = mergedEntities;
|
||||
@@ -308,13 +302,13 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
* use the {@link ParameterSource} to find their values and set them.
|
||||
*
|
||||
*/
|
||||
private void setParametersIfRequired(String queryString, ParameterSource source, Query query) {
|
||||
private void setParametersIfRequired(String queryString, @Nullable ParameterSource source, Query query) {
|
||||
Set<Parameter<?>> parameters = query.getParameters();
|
||||
|
||||
if (parameters != null && !parameters.isEmpty()) {
|
||||
if (source != null) {
|
||||
for (Parameter<?> param:parameters) {
|
||||
String paramName = param.getName();
|
||||
for (Parameter<?> param : parameters) {
|
||||
String paramName = param.getName();
|
||||
Integer position = param.getPosition();
|
||||
|
||||
final Object paramValue;
|
||||
@@ -340,7 +334,7 @@ public class DefaultJpaOperations extends AbstractJpaOperations {
|
||||
else {
|
||||
throw new JpaOperationFailedException(
|
||||
"This parameter does not contain a parameter name. " +
|
||||
"Additionally it is not a positional parameter, neither.", queryString);
|
||||
"Additionally it is not a positional parameter, neither.", queryString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.integration.jpa.core;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* The Interface containing all the JpaOperations those will be executed by
|
||||
@@ -27,6 +28,8 @@ import org.springframework.integration.jpa.support.parametersource.ParameterSour
|
||||
* @author Amol Nayak
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
@@ -47,7 +50,6 @@ public interface JpaOperations {
|
||||
/**
|
||||
* Executes the given update statement and uses the given parameter source to
|
||||
* set the required query parameters.
|
||||
*
|
||||
* @param updateQuery Must Not be empty.
|
||||
* @param source Must Not be null.
|
||||
* @return The number of entities updated
|
||||
@@ -74,7 +76,6 @@ public interface JpaOperations {
|
||||
|
||||
/**
|
||||
* Find an Entity of given type with the given primary key type.
|
||||
*
|
||||
* @param <T> The type to return.
|
||||
* @param entityType The type.
|
||||
* @param id The object identifier.
|
||||
@@ -109,13 +110,14 @@ public interface JpaOperations {
|
||||
*
|
||||
* @param selectQuery The select query.
|
||||
* @param entityClass The entity class.
|
||||
* @param jpaQLParameterSource The paramter source.
|
||||
* @param jpaQLParameterSource The parameter source.
|
||||
* @param firstResult The index of the first result to return.
|
||||
* @param maxNumberOfResults The number of objects to return.
|
||||
* @return The list of found entities.
|
||||
*/
|
||||
List<?> getResultListForNativeQuery(String selectQuery,
|
||||
Class<?> entityClass, ParameterSource jpaQLParameterSource,
|
||||
@Nullable Class<?> entityClass,
|
||||
ParameterSource jpaQLParameterSource,
|
||||
int firstResult,
|
||||
int maxNumberOfResults);
|
||||
|
||||
@@ -129,7 +131,6 @@ public interface JpaOperations {
|
||||
|
||||
/**
|
||||
* Executes the provided query to return a list of results.
|
||||
*
|
||||
* @param query Must not be null or empty
|
||||
* @param firstResult The first result.
|
||||
* @param maxNumberOfResults Must be a non-negative value, any negative or zero will be ignored.
|
||||
@@ -140,7 +141,6 @@ public interface JpaOperations {
|
||||
|
||||
/**
|
||||
* Executes the provided query to return a single element
|
||||
*
|
||||
* @param query Must not be empty
|
||||
* @param source the Parameter source for this query to be executed, if none then set as null
|
||||
* @return Will always return a result. If no object was found in the database an exception is raised.
|
||||
@@ -154,7 +154,6 @@ public interface JpaOperations {
|
||||
* is treated as an entity and merged with the
|
||||
* {@link javax.persistence.EntityManager}. {@code Null}
|
||||
* values returned while iterating over the {@link Iterable} are ignored.
|
||||
*
|
||||
* @param entity Must not be null.
|
||||
* @return The merged managed instance of the entity.
|
||||
*/
|
||||
@@ -171,7 +170,6 @@ public interface JpaOperations {
|
||||
* provided object is {@link Iterable}.
|
||||
* {@code clearOnFlush}parameter specifies, if the {@link javax.persistence.EntityManager#clear()}
|
||||
* should be called after each {@link javax.persistence.EntityManager#flush()}.
|
||||
*
|
||||
* @param entity The entity.
|
||||
* @param flushSize The flush size.
|
||||
* @param clearOnFlush true to clear after flushing.
|
||||
@@ -186,9 +184,7 @@ public interface JpaOperations {
|
||||
* and persisted with the {@link javax.persistence.EntityManager}.
|
||||
* {@code Null} values returned
|
||||
* while iterating over the {@link Iterable} are ignored.
|
||||
*
|
||||
* @param entity Must not be null
|
||||
*
|
||||
*/
|
||||
void persist(Object entity);
|
||||
|
||||
@@ -203,7 +199,6 @@ public interface JpaOperations {
|
||||
* provided object is {@link Iterable}.
|
||||
* {@code clearOnFlush}parameter specifies, if the {@link javax.persistence.EntityManager#clear()}
|
||||
* should be called after each {@link javax.persistence.EntityManager#flush()}.
|
||||
*
|
||||
* @param entity The entity.
|
||||
* @param flushSize The flush size.
|
||||
* @param clearOnFlush true to clear after flushing.
|
||||
|
||||
Reference in New Issue
Block a user