From e19dff179e462d3726e0ccb1d4e7174ffd65d81e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 12 Jan 2017 21:18:01 +0100 Subject: [PATCH] Polishing --- .../cache/ehcache/EhCacheFactoryBean.java | 6 +- .../context/MessageSource.java | 18 ++--- .../context/MessageSourceResolvable.java | 17 ++++- .../context/support/MessageSourceSupport.java | 19 +++-- .../core/CollectionFactory.java | 4 +- .../incrementer/MySQLMaxValueIncrementer.java | 70 +++++++------------ ...tEntityManagerFactoryIntegrationTests.java | 10 +-- .../mock/web/MockHttpServletResponse.java | 3 +- .../org/springframework/http/MediaType.java | 4 ++ 9 files changed, 73 insertions(+), 78 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java index 5ae259297fc..429fefefebe 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -73,10 +73,6 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea private Set cacheEventListeners; - private boolean statisticsEnabled = false; - - private boolean sampledStatisticsEnabled = false; - private boolean disabled = false; private String beanName; diff --git a/spring-context/src/main/java/org/springframework/context/MessageSource.java b/spring-context/src/main/java/org/springframework/context/MessageSource.java index 13f1530b31f..1a4d0b40ef8 100644 --- a/spring-context/src/main/java/org/springframework/context/MessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/MessageSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-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. @@ -42,11 +42,11 @@ public interface MessageSource { * @param code the code to lookup up, such as 'calculator.noRateSet'. Users of * this class are encouraged to base message names on the relevant fully * qualified class name, thus avoiding conflict and ensuring maximum clarity. - * @param args array of arguments that will be filled in for params within + * @param args an array of arguments that will be filled in for params within * the message (params look like "{0}", "{1,date}", "{2,time}" within a message), * or {@code null} if none. - * @param defaultMessage String to return if the lookup fails - * @param locale the Locale in which to do the lookup + * @param defaultMessage a default message to return if the lookup fails + * @param locale the locale in which to do the lookup * @return the resolved message if the lookup was successful; * otherwise the default message passed as a parameter * @see java.text.MessageFormat @@ -56,10 +56,10 @@ public interface MessageSource { /** * Try to resolve the message. Treat as an error if the message can't be found. * @param code the code to lookup up, such as 'calculator.noRateSet' - * @param args Array of arguments that will be filled in for params within + * @param args an array of arguments that will be filled in for params within * the message (params look like "{0}", "{1,date}", "{2,time}" within a message), * or {@code null} if none. - * @param locale the Locale in which to do the lookup + * @param locale the locale in which to do the lookup * @return the resolved message * @throws NoSuchMessageException if the message wasn't found * @see java.text.MessageFormat @@ -71,9 +71,9 @@ public interface MessageSource { * {@code MessageSourceResolvable} argument that was passed in. *

NOTE: We must throw a {@code NoSuchMessageException} on this method * since at the time of calling this method we aren't able to determine if the - * {@code defaultMessage} property of the resolvable is null or not. - * @param resolvable value object storing attributes required to properly resolve a message - * @param locale the Locale in which to do the lookup + * {@code defaultMessage} property of the resolvable is {@code null} or not. + * @param resolvable the value object storing attributes required to resolve a message + * @param locale the locale in which to do the lookup * @return the resolved message * @throws NoSuchMessageException if the message wasn't found * @see java.text.MessageFormat diff --git a/spring-context/src/main/java/org/springframework/context/MessageSourceResolvable.java b/spring-context/src/main/java/org/springframework/context/MessageSourceResolvable.java index 965a44bbe77..3dca904f63d 100644 --- a/spring-context/src/main/java/org/springframework/context/MessageSourceResolvable.java +++ b/spring-context/src/main/java/org/springframework/context/MessageSourceResolvable.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-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. @@ -27,6 +27,7 @@ package org.springframework.context; * @see org.springframework.validation.ObjectError * @see org.springframework.validation.FieldError */ +@FunctionalInterface public interface MessageSourceResolvable { /** @@ -38,16 +39,26 @@ public interface MessageSourceResolvable { /** * Return the array of arguments to be used to resolve this message. + *

The default implementation simply returns {@code null}. * @return an array of objects to be used as parameters to replace * placeholders within the message text * @see java.text.MessageFormat */ - Object[] getArguments(); + default Object[] getArguments() { + return null; + } /** * Return the default message to be used to resolve this message. + *

The default implementation simply returns {@code null}. + * Note that the default message may be identical to the primary + * message code ({@link #getCodes()}), which effectively enforces + * {@link org.springframework.context.support.AbstractMessageSource#setUseCodeAsDefaultMessage} + * for this particular message. * @return the default message, or {@code null} if no default */ - String getDefaultMessage(); + default String getDefaultMessage() { + return null; + } } diff --git a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java index 2f21c7fa0ee..d7e175d8a87 100644 --- a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java +++ b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -52,13 +52,12 @@ public abstract class MessageSourceSupport { * Used for passed-in default messages. MessageFormats for resolved * codes are cached on a specific basis in subclasses. */ - private final Map> messageFormatsPerMessage = - new HashMap<>(); + private final Map> messageFormatsPerMessage = new HashMap<>(); /** - * Set whether to always apply the MessageFormat rules, parsing even - * messages without arguments. + * Set whether to always apply the {@code MessageFormat} rules, + * parsing even messages without arguments. *

Default is "false": Messages without arguments are by default * returned as-is, without parsing them through MessageFormat. * Set this to "true" to enforce MessageFormat for all messages, @@ -112,7 +111,7 @@ public abstract class MessageSourceSupport { * @return the formatted message (with resolved arguments) */ protected String formatMessage(String msg, Object[] args, Locale locale) { - if (msg == null || (!this.alwaysUseMessageFormat && ObjectUtils.isEmpty(args))) { + if (msg == null || (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args))) { return msg; } MessageFormat messageFormat = null; @@ -130,12 +129,12 @@ public abstract class MessageSourceSupport { messageFormat = createMessageFormat(msg, locale); } catch (IllegalArgumentException ex) { - // invalid message format - probably not intended for formatting, - // rather using a message structure with no arguments involved - if (this.alwaysUseMessageFormat) { + // Invalid message format - probably not intended for formatting, + // rather using a message structure with no arguments involved... + if (isAlwaysUseMessageFormat()) { throw ex; } - // silently proceed with raw message if format not enforced + // Silently proceed with raw message if format not enforced... messageFormat = INVALID_MESSAGE_FORMAT; } messageFormatsPerLocale.put(locale, messageFormat); diff --git a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java index db67f9c249d..b21641761b4 100644 --- a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java +++ b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java @@ -54,9 +54,9 @@ import org.springframework.util.ReflectionUtils; */ public abstract class CollectionFactory { - private static final Set> approximableCollectionTypes = new HashSet<>(11); + private static final Set> approximableCollectionTypes = new HashSet<>(); - private static final Set> approximableMapTypes = new HashSet<>(7); + private static final Set> approximableMapTypes = new HashSet<>(); static { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/MySQLMaxValueIncrementer.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/MySQLMaxValueIncrementer.java index ec17e1a5154..5b33cb9bc7b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/MySQLMaxValueIncrementer.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/MySQLMaxValueIncrementer.java @@ -67,12 +67,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer /** The max id to serve */ private long maxId = 0; - /** - * Whether or not to use a new connection for the incrementer. Defaults to true - * in order to support transactional storage engines. Set this to false if the storage engine - * for the incrementer table is non-transactional like MYISAM and you prefer to not acquire - * an additional database connection - */ + /** Whether or not to use a new connection for the incrementer */ private boolean useNewConnection = true; @@ -88,41 +83,30 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer /** * Convenience constructor. * @param dataSource the DataSource to use - * @param incrementerName the name of the sequence/table to use + * @param incrementerName the name of the sequence table to use * @param columnName the name of the column in the sequence table to use */ public MySQLMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) { super(dataSource, incrementerName, columnName); } - /** - * Convenience constructor for setting whether to use a new connection for the incrementer. - * @param dataSource the DataSource to use - * @param incrementerName the name of the sequence/table to use - * @param columnName the name of the column in the sequence table to use - * @param useNewConnection whether to use a new connection for the incrementer - */ - public MySQLMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName, - boolean useNewConnection) { - super(dataSource, incrementerName, columnName); - this.useNewConnection = useNewConnection; - } - - - /** - * Return whether to use a new connection for the incrementer. - */ - public boolean isUseNewConnection() { - return useNewConnection; - } /** * Set whether to use a new connection for the incrementer. + *

{@code true} is necessary to support transactional storage engines, + * using an isolated separate transaction for the increment operation. + * {@code false} is sufficient if the storage engine of the sequence table + * is non-transactional (like MYISAM), avoiding the effort of acquiring an + * extra {@code Connection} for the increment operation. + *

Default is {@code true} since Spring Framework 5.0. + * @since 4.3.6 + * @see DataSource#getConnection() */ public void setUseNewConnection(boolean useNewConnection) { this.useNewConnection = useNewConnection; } + @Override protected synchronized long getNextKey() throws DataAccessException { if (this.maxId == this.nextId) { @@ -138,7 +122,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer Statement stmt = null; boolean mustRestoreAutoCommit = false; try { - if (useNewConnection) { + if (this.useNewConnection) { con = getDataSource().getConnection(); if (con.getAutoCommit()) { mustRestoreAutoCommit = true; @@ -149,7 +133,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer con = DataSourceUtils.getConnection(getDataSource()); } stmt = con.createStatement(); - if (!useNewConnection) { + if (!this.useNewConnection) { DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); } // Increment the sequence column... @@ -180,23 +164,23 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer } finally { JdbcUtils.closeStatement(stmt); - if (useNewConnection) { - try { - con.commit(); - if (mustRestoreAutoCommit) { - con.setAutoCommit(true); + if (con != null) { + if (this.useNewConnection) { + try { + con.commit(); + if (mustRestoreAutoCommit) { + con.setAutoCommit(true); + } + } + catch (SQLException ignore) { + throw new DataAccessResourceFailureException( + "Unable to commit new sequence value changes for " + getIncrementerName()); } + JdbcUtils.closeConnection(con); } - catch (SQLException ignore) { - throw new DataAccessResourceFailureException( - "Unable to commit new sequence value changes for " + getIncrementerName()); + else { + DataSourceUtils.releaseConnection(con, getDataSource()); } - try { - con.close(); - } catch (SQLException ignore) {} - } - else { - DataSourceUtils.releaseConnection(con, getDataSource()); } } } diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryIntegrationTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryIntegrationTests.java index c4ddeb4daa0..7848387adfe 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryIntegrationTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -124,8 +124,8 @@ public abstract class AbstractEntityManagerFactoryIntegrationTests { public static void closeContext() { if (applicationContext != null) { applicationContext.close(); + applicationContext = null; } - applicationContext = null; } @@ -164,9 +164,9 @@ public abstract class AbstractEntityManagerFactoryIntegrationTests { this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition); } - protected void deleteFromTables(String... names) { - for (String name : names) { - this.jdbcTemplate.update("DELETE FROM " + name); + protected void deleteFromTables(String... tableNames) { + for (String tableName : tableNames) { + this.jdbcTemplate.update("DELETE FROM " + tableName); } this.zappedTables = true; } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 1d112875261..8b0967c5e6b 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-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. @@ -215,6 +215,7 @@ public class MockHttpServletResponse implements HttpServletResponse { return (int) this.contentLength; } + @Override public void setContentLengthLong(long contentLength) { this.contentLength = contentLength; doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true); diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index bd309e679f9..35f291e6fbd 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -126,11 +126,13 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code application/rss+xml}. + * @since 4.3.6 */ public final static MediaType APPLICATION_RSS_XML; /** * A String equivalent of {@link MediaType#APPLICATION_RSS_XML}. + * @since 4.3.6 */ public final static String APPLICATION_RSS_XML_VALUE = "application/rss+xml"; @@ -196,12 +198,14 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code text/event-stream}. + * @since 4.3.6 * @see Server-Sent Events W3C recommendation */ public final static MediaType TEXT_EVENT_STREAM; /** * A String equivalent of {@link MediaType#TEXT_EVENT_STREAM}. + * @since 4.3.6 */ public final static String TEXT_EVENT_STREAM_VALUE = "text/event-stream";