diff options
author | Emmanuel Bourg <ebourg@apache.org> | 2018-10-16 19:01:59 +0200 |
---|---|---|
committer | Emmanuel Bourg <ebourg@apache.org> | 2018-10-16 19:01:59 +0200 |
commit | cb53abe54064010cd788530c7a882ecebb88afcd (patch) | |
tree | 2e6c1bd607cefae2f97919b16f8c6851e8389bad /spring-core | |
parent | 28270ba2bd2be3c3f66eeafbeca15d09ba63cc5f (diff) |
New upstream version 4.3.20
Diffstat (limited to 'spring-core')
7 files changed, 86 insertions, 85 deletions
diff --git a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java index 1ffb98bd..5c748552 100644 --- a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java @@ -379,6 +379,31 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @Override @SuppressWarnings({"unchecked", "rawtypes"}) + public Map<String, Object> getSystemProperties() { + try { + return (Map) System.getProperties(); + } + catch (AccessControlException ex) { + return (Map) new ReadOnlySystemAttributesMap() { + @Override + protected String getSystemAttribute(String attributeName) { + try { + return System.getProperty(attributeName); + } + catch (AccessControlException ex) { + if (logger.isInfoEnabled()) { + logger.info("Caught AccessControlException when accessing system property '" + + attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage()); + } + return null; + } + } + }; + } + } + + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) public Map<String, Object> getSystemEnvironment() { if (suppressGetenvAccess()) { return Collections.emptyMap(); @@ -421,31 +446,6 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { } @Override - @SuppressWarnings({"unchecked", "rawtypes"}) - public Map<String, Object> getSystemProperties() { - try { - return (Map) System.getProperties(); - } - catch (AccessControlException ex) { - return (Map) new ReadOnlySystemAttributesMap() { - @Override - protected String getSystemAttribute(String attributeName) { - try { - return System.getProperty(attributeName); - } - catch (AccessControlException ex) { - if (logger.isInfoEnabled()) { - logger.info("Caught AccessControlException when accessing system property '" + - attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage()); - } - return null; - } - } - }; - } - } - - @Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { diff --git a/spring-core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java index d2b20052..1d875f2c 100644 --- a/spring-core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -37,7 +37,7 @@ import java.util.Map; * <pre class="code"> * ConfigurableEnvironment environment = new StandardEnvironment(); * MutablePropertySources propertySources = environment.getPropertySources(); - * Map<String, String> myMap = new HashMap<String, String>(); + * Map<String, String> myMap = new HashMap<>(); * myMap.put("xyz", "myValue"); * propertySources.addFirst(new MapPropertySource("MY_MAP", myMap)); * </pre> @@ -78,26 +78,26 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper * <p>Any existing active profiles will be replaced with the given arguments; call * with zero arguments to clear the current set of active profiles. Use * {@link #addActiveProfile} to add a profile while preserving the existing set. + * @throws IllegalArgumentException if any profile is null, empty or whitespace-only * @see #addActiveProfile * @see #setDefaultProfiles * @see org.springframework.context.annotation.Profile * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME - * @throws IllegalArgumentException if any profile is null, empty or whitespace-only */ void setActiveProfiles(String... profiles); /** * Add a profile to the current set of active profiles. - * @see #setActiveProfiles * @throws IllegalArgumentException if the profile is null, empty or whitespace-only + * @see #setActiveProfiles */ void addActiveProfile(String profile); /** * Specify the set of profiles to be made active by default if no other profiles * are explicitly made active through {@link #setActiveProfiles}. - * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME * @throws IllegalArgumentException if any profile is null, empty or whitespace-only + * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME */ void setDefaultProfiles(String... profiles); @@ -119,34 +119,34 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper MutablePropertySources getPropertySources(); /** - * Return the value of {@link System#getenv()} if allowed by the current + * Return the value of {@link System#getProperties()} if allowed by the current * {@link SecurityManager}, otherwise return a map implementation that will attempt - * to access individual keys using calls to {@link System#getenv(String)}. - * <p>Note that most {@link Environment} implementations will include this system - * environment map as a default {@link PropertySource} to be searched. Therefore, it - * is recommended that this method not be used directly unless bypassing other - * property sources is expressly intended. + * to access individual keys using calls to {@link System#getProperty(String)}. + * <p>Note that most {@code Environment} implementations will include this system + * properties map as a default {@link PropertySource} to be searched. Therefore, it is + * recommended that this method not be used directly unless bypassing other property + * sources is expressly intended. * <p>Calls to {@link Map#get(Object)} on the Map returned will never throw * {@link IllegalAccessException}; in cases where the SecurityManager forbids access * to a property, {@code null} will be returned and an INFO-level log message will be * issued noting the exception. */ - Map<String, Object> getSystemEnvironment(); + Map<String, Object> getSystemProperties(); /** - * Return the value of {@link System#getProperties()} if allowed by the current + * Return the value of {@link System#getenv()} if allowed by the current * {@link SecurityManager}, otherwise return a map implementation that will attempt - * to access individual keys using calls to {@link System#getProperty(String)}. - * <p>Note that most {@code Environment} implementations will include this system - * properties map as a default {@link PropertySource} to be searched. Therefore, it is - * recommended that this method not be used directly unless bypassing other property - * sources is expressly intended. + * to access individual keys using calls to {@link System#getenv(String)}. + * <p>Note that most {@link Environment} implementations will include this system + * environment map as a default {@link PropertySource} to be searched. Therefore, it + * is recommended that this method not be used directly unless bypassing other + * property sources is expressly intended. * <p>Calls to {@link Map#get(Object)} on the Map returned will never throw * {@link IllegalAccessException}; in cases where the SecurityManager forbids access * to a property, {@code null} will be returned and an INFO-level log message will be * issued noting the exception. */ - Map<String, Object> getSystemProperties(); + Map<String, Object> getSystemEnvironment(); /** * Append the given parent environment's active profiles, default profiles and diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java index 7d1ef88e..d6251427 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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,7 +19,6 @@ package org.springframework.core.io; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; @@ -112,14 +111,13 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { return true; } if (httpCon != null) { - // no HTTP OK status, and no content-length header: give up + // No HTTP OK status, and no content-length header: give up httpCon.disconnect(); return false; } else { // Fall back to stream existence: can we open the stream? - InputStream is = getInputStream(); - is.close(); + getInputStream().close(); return true; } } @@ -180,7 +178,6 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { return con.getLastModified(); } - /** * Customize the given {@link URLConnection}, obtained in the course of an * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call. diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index 7d38cee9..eb60a49a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -55,8 +55,7 @@ public abstract class AbstractResource implements Resource { catch (IOException ex) { // Fall back to stream existence: can we open the stream? try { - InputStream is = getInputStream(); - is.close(); + getInputStream().close(); return true; } catch (Throwable isEx) { @@ -126,7 +125,7 @@ public abstract class AbstractResource implements Resource { Assert.state(is != null, "Resource InputStream must not be null"); try { long size = 0; - byte[] buf = new byte[255]; + byte[] buf = new byte[256]; int read; while ((read = is.read(buf)) != -1) { size += read; @@ -149,10 +148,11 @@ public abstract class AbstractResource implements Resource { */ @Override public long lastModified() throws IOException { - long lastModified = getFileForLastModifiedCheck().lastModified(); - if (lastModified == 0L) { + File fileToCheck = getFileForLastModifiedCheck(); + long lastModified = fileToCheck.lastModified(); + if (lastModified == 0L && !fileToCheck.exists()) { throw new FileNotFoundException(getDescription() + - " cannot be resolved in the file system for resolving its last-modified timestamp"); + " cannot be resolved in the file system for checking its last-modified timestamp"); } return lastModified; } diff --git a/spring-core/src/main/java/org/springframework/core/io/PathResource.java b/spring-core/src/main/java/org/springframework/core/io/PathResource.java index f0462751..c578c39b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/PathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/PathResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -40,6 +40,8 @@ import org.springframework.util.Assert; * @author Juergen Hoeller * @since 4.0 * @see java.nio.file.Path + * @see java.nio.file.Files + * @see FileSystemResource */ @UsesJava7 public class PathResource extends AbstractResource implements WritableResource { @@ -77,8 +79,8 @@ public class PathResource extends AbstractResource implements WritableResource { * <p>Note: Unlike {@link FileSystemResource}, when building relative resources * via {@link #createRelative}, the relative path will be built <i>underneath</i> * the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"! - * @see java.nio.file.Paths#get(URI) * @param uri a path URI + * @see java.nio.file.Paths#get(URI) */ public PathResource(URI uri) { Assert.notNull(uri, "URI must not be null"); @@ -95,7 +97,7 @@ public class PathResource extends AbstractResource implements WritableResource { /** * This implementation returns whether the underlying file exists. - * @see org.springframework.core.io.PathResource#exists() + * @see java.nio.file.Files#exists(Path, java.nio.file.LinkOption...) */ @Override public boolean exists() { @@ -186,7 +188,7 @@ public class PathResource extends AbstractResource implements WritableResource { } /** - * This implementation returns the underlying File's length. + * This implementation returns the underlying file's length. */ @Override public long contentLength() throws IOException { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java index 3b3a17dd..341172fb 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java @@ -82,7 +82,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito } @Override - public AnnotationVisitor visitAnnotation(final String desc, boolean visible) { + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { String className = Type.getType(desc).getClassName(); this.annotationSet.add(className); return new AnnotationAttributesReadingVisitor( diff --git a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java index e094e342..ea125edc 100644 --- a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 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. @@ -38,6 +38,7 @@ import static org.springframework.core.env.AbstractEnvironment.*; * @author Chris Beams * @author Juergen Hoeller */ +@SuppressWarnings("deprecation") public class StandardEnvironmentTests { private static final String ALLOWED_PROPERTY_NAME = "theanswer"; @@ -51,7 +52,8 @@ public class StandardEnvironmentTests { private static final Object NON_STRING_PROPERTY_NAME = new Object(); private static final Object NON_STRING_PROPERTY_VALUE = new Object(); - private ConfigurableEnvironment environment = new StandardEnvironment(); + private final ConfigurableEnvironment environment = new StandardEnvironment(); + @Test public void merge() { @@ -129,42 +131,42 @@ public class StandardEnvironmentTests { assertThat(activeProfiles.length, is(2)); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNullProfileArray() { - environment.setActiveProfiles((String[])null); + environment.setActiveProfiles((String[]) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNullProfile() { - environment.setActiveProfiles((String)null); + environment.setActiveProfiles((String) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withEmptyProfile() { environment.setActiveProfiles(""); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNotOperator() { environment.setActiveProfiles("p1", "!p2"); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNullProfileArray() { - environment.setDefaultProfiles((String[])null); + environment.setDefaultProfiles((String[]) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNullProfile() { - environment.setDefaultProfiles((String)null); + environment.setDefaultProfiles((String) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withEmptyProfile() { environment.setDefaultProfiles(""); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNotOperator() { environment.setDefaultProfiles("d1", "!d2"); } @@ -204,7 +206,7 @@ public class StandardEnvironmentTests { System.getProperties().remove(DEFAULT_PROFILES_PROPERTY_NAME); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void defaultProfileWithCircularPlaceholder() { System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}"); try { @@ -263,27 +265,26 @@ public class StandardEnvironmentTests { assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItems("pd2", "pd3")); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void acceptsProfiles_withEmptyArgumentList() { environment.acceptsProfiles(); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void acceptsProfiles_withNullArgumentList() { - environment.acceptsProfiles((String[])null); + environment.acceptsProfiles((String[]) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void acceptsProfiles_withNullArgument() { - environment.acceptsProfiles((String)null); + environment.acceptsProfiles((String) null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void acceptsProfiles_withEmptyArgument() { environment.acceptsProfiles(""); } - @Test public void acceptsProfiles_activeProfileSetProgrammatically() { assertThat(environment.acceptsProfiles("p1", "p2"), is(false)); @@ -321,7 +322,7 @@ public class StandardEnvironmentTests { assertThat(environment.acceptsProfiles("!p1"), is(false)); } - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void acceptsProfiles_withInvalidNotOperator() { environment.acceptsProfiles("p1", "!"); } @@ -488,6 +489,7 @@ public class StandardEnvironmentTests { getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME); } + @SuppressWarnings("unchecked") public static Map<String, String> getModifiableSystemEnvironment() { // for os x / linux |