From 2feedb98ccaad01d9c75073604a9c90956a33afb Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Sep 2021 18:15:00 +0200 Subject: [PATCH 1/3] Remove lineSeparator LF requirement (accept LF/CR/CRLF by default) See gh-27481 --- src/checkstyle/checkstyle.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml index 6358bc47e75..2d8a043623c 100644 --- a/src/checkstyle/checkstyle.xml +++ b/src/checkstyle/checkstyle.xml @@ -13,9 +13,7 @@ - - - + From 86b010a6b2270d0fb3ff4017ca3e52eb100534d5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Sep 2021 18:15:22 +0200 Subject: [PATCH 2/3] Defensive reference to JNDI API for JDK 9+ (optional `java.naming` module) Closes gh-27483 --- .../CommonAnnotationBeanPostProcessor.java | 43 ++++++++++++++----- .../support/StandardServletEnvironment.java | 9 +++- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index d9acb722b08..8a3ca5a9f88 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -146,22 +146,27 @@ import org.springframework.util.StringValueResolver; public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable { + // Defensive reference to JNDI API for JDK 9+ (optional java.naming module) + private static final boolean jndiPresent = ClassUtils.isPresent( + "javax.naming.InitialContext", CommonAnnotationBeanPostProcessor.class.getClassLoader()); + + private static final Set> resourceAnnotationTypes = new LinkedHashSet<>(4); + @Nullable private static final Class webServiceRefClass; @Nullable private static final Class ejbClass; - private static final Set> resourceAnnotationTypes = new LinkedHashSet<>(4); - static { - webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef"); - ejbClass = loadAnnotationType("javax.ejb.EJB"); - resourceAnnotationTypes.add(Resource.class); + + webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef"); if (webServiceRefClass != null) { resourceAnnotationTypes.add(webServiceRefClass); } + + ejbClass = loadAnnotationType("javax.ejb.EJB"); if (ejbClass != null) { resourceAnnotationTypes.add(ejbClass); } @@ -174,7 +179,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean private boolean alwaysUseJndiLookup = false; - private transient BeanFactory jndiFactory = new SimpleJndiBeanFactory(); + @Nullable + private transient BeanFactory jndiFactory; @Nullable private transient BeanFactory resourceFactory; @@ -199,6 +205,11 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean setInitAnnotationType(PostConstruct.class); setDestroyAnnotationType(PreDestroy.class); ignoreResourceType("javax.xml.ws.WebServiceContext"); + + // java.naming module present on JDK 9+? + if (jndiPresent) { + this.jndiFactory = new SimpleJndiBeanFactory(); + } } @@ -464,6 +475,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean public void releaseTarget(Object target) { } }; + ProxyFactory pf = new ProxyFactory(); pf.setTargetSource(ts); if (element.lookupType.isInterface()) { @@ -484,12 +496,23 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean protected Object getResource(LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { + // JNDI lookup to perform? + String jndiName = null; if (StringUtils.hasLength(element.mappedName)) { - return this.jndiFactory.getBean(element.mappedName, element.lookupType); + jndiName = element.mappedName; + } + else if (this.alwaysUseJndiLookup) { + jndiName = element.name; } - if (this.alwaysUseJndiLookup) { - return this.jndiFactory.getBean(element.name, element.lookupType); + if (jndiName != null) { + if (this.jndiFactory == null) { + throw new NoSuchBeanDefinitionException(element.lookupType, + "No JNDI factory configured - specify the 'jndiFactory' property"); + } + return this.jndiFactory.getBean(jndiName, element.lookupType); } + + // Regular resource autowiring if (this.resourceFactory == null) { throw new NoSuchBeanDefinitionException(element.lookupType, "No resource factory configured - specify the 'resourceFactory' property"); diff --git a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java index 3ec9b92be64..4d49afb974a 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java @@ -27,6 +27,7 @@ import org.springframework.core.env.StandardEnvironment; import org.springframework.jndi.JndiLocatorDelegate; import org.springframework.jndi.JndiPropertySource; import org.springframework.lang.Nullable; +import org.springframework.util.ClassUtils; import org.springframework.web.context.ConfigurableWebEnvironment; /** @@ -39,6 +40,7 @@ import org.springframework.web.context.ConfigurableWebEnvironment; * documentation for details. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 * @see StandardEnvironment */ @@ -54,6 +56,11 @@ public class StandardServletEnvironment extends StandardEnvironment implements C public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties"; + // Defensive reference to JNDI API for JDK 9+ (optional java.naming module) + private static final boolean jndiPresent = ClassUtils.isPresent( + "javax.naming.InitialContext", StandardServletEnvironment.class.getClassLoader()); + + /** * Create a new {@code StandardServletEnvironment} instance. */ @@ -100,7 +107,7 @@ public class StandardServletEnvironment extends StandardEnvironment implements C protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); - if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { + if (jndiPresent && JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); } super.customizePropertySources(propertySources); From 040445612f157827e7ee199c2d6af55cbd2497bc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Sep 2021 18:15:56 +0200 Subject: [PATCH 3/3] Polishing --- .../org/springframework/core/env/AbstractEnvironment.java | 3 +++ .../org/springframework/core/env/StandardEnvironment.java | 7 +++++-- .../main/java/org/apache/commons/logging/LogAdapter.java | 8 ++++---- 3 files changed, 12 insertions(+), 6 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 d4027e16f36..85968d699a8 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 @@ -48,6 +48,7 @@ import org.springframework.util.StringUtils; * * @author Chris Beams * @author Juergen Hoeller + * @author Phillip Webb * @since 3.1 * @see ConfigurableEnvironment * @see StandardEnvironment @@ -129,6 +130,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { * {@link #customizePropertySources(MutablePropertySources)} during * construction to allow subclasses to contribute or manipulate * {@link PropertySource} instances as appropriate. + * @param propertySources property sources to use * @since 5.3.4 * @see #customizePropertySources(MutablePropertySources) */ @@ -138,6 +140,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { customizePropertySources(propertySources); } + /** * Factory method used to create the {@link ConfigurablePropertyResolver} * instance used by the Environment. diff --git a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java index b228de56bb2..9d2a2eb6b88 100644 --- a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java @@ -46,6 +46,7 @@ package org.springframework.core.env; * variable names. * * @author Chris Beams + * @author Phillip Webb * @since 3.1 * @see ConfigurableEnvironment * @see SystemEnvironmentPropertySource @@ -61,13 +62,15 @@ public class StandardEnvironment extends AbstractEnvironment { /** - * Create a new {@code StandardEnvironment} instance. + * Create a new {@code StandardEnvironment} instance with a default + * {@link MutablePropertySources} instance. */ public StandardEnvironment() { } /** - * Create a new {@code StandardEnvironment} instance with a specific {@link MutablePropertySources} instance. + * Create a new {@code StandardEnvironment} instance with a specific + * {@link MutablePropertySources} instance. * @param propertySources property sources to use * @since 5.3.4 */ diff --git a/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java b/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java index c918ee7c2df..cc5bed06ea9 100644 --- a/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java +++ b/spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -278,7 +278,7 @@ final class LogAdapter { protected final String name; - protected transient T logger; + protected final transient T logger; public Slf4jLog(T logger) { this.name = logger.getName(); @@ -500,9 +500,9 @@ final class LogAdapter { @SuppressWarnings("serial") private static class JavaUtilLog implements Log, Serializable { - private String name; + private final String name; - private transient java.util.logging.Logger logger; + private final transient java.util.logging.Logger logger; public JavaUtilLog(String name) { this.name = name;