[spring-extensions] branch master updated: Remove old Duration converter. Extend Spring context builder with more default features.
Scott Cantor
cantor.2 at osu.edu
Tue Mar 19 20:17:51 EDT 2019
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch master
in repository spring-extensions.
View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=4f81ff8e29b250b10b35c7d7db04d3bdef4f8c8a
The following commit(s) were added to refs/heads/master by this push:
new 4f81ff8 Remove old Duration converter. Extend Spring context builder with more default features.
4f81ff8 is described below
commit 4f81ff8e29b250b10b35c7d7db04d3bdef4f8c8a
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Mar 19 20:13:07 2019 -0400
Remove old Duration converter.
Extend Spring context builder with more default features.
---
.../ext/spring/config/DurationToLongConverter.java | 47 ------------
.../ext/spring/util/ApplicationContextBuilder.java | 55 +++++++++-----
.../shibboleth/ext/spring/util/SpringSupport.java | 86 ----------------------
3 files changed, 37 insertions(+), 151 deletions(-)
diff --git a/src/main/java/net/shibboleth/ext/spring/config/DurationToLongConverter.java b/src/main/java/net/shibboleth/ext/spring/config/DurationToLongConverter.java
deleted file mode 100644
index 84e48fa..0000000
--- a/src/main/java/net/shibboleth/ext/spring/config/DurationToLongConverter.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements. See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID licenses this file to You 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
- *
- * http://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 net.shibboleth.ext.spring.config;
-
-import net.shibboleth.utilities.java.support.annotation.Duration;
-import net.shibboleth.utilities.java.support.xml.DOMTypeSupport;
-
-import org.springframework.core.convert.TypeDescriptor;
-import org.springframework.core.convert.converter.ConditionalConverter;
-import org.springframework.core.convert.converter.Converter;
-
-/**
- * Allows setting of Duration-valued properties using lexical string form.
- */
-public class DurationToLongConverter implements Converter<String,Long>, ConditionalConverter {
-
- /** {@inheritDoc} */
- public Long convert(final String source) {
- if (source.startsWith("P") || source.startsWith("-P")) {
- return DOMTypeSupport.durationToLong(source.trim());
- } else {
- // Treat as a milliseconds.
- return Long.valueOf(source);
- }
- }
-
- /** {@inheritDoc} */
- public boolean matches(final TypeDescriptor sourceType, final TypeDescriptor targetType) {
- return targetType.hasAnnotation(Duration.class);
- }
-
-}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/ApplicationContextBuilder.java b/src/main/java/net/shibboleth/ext/spring/util/ApplicationContextBuilder.java
index 514e45c..dbcbe60 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/ApplicationContextBuilder.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/ApplicationContextBuilder.java
@@ -28,10 +28,10 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.shibboleth.ext.spring.config.BooleanToPredicateConverter;
-import net.shibboleth.ext.spring.config.DurationToLongConverter;
import net.shibboleth.ext.spring.config.FunctionToFunctionConverter;
import net.shibboleth.ext.spring.config.PredicateToPredicateConverter;
import net.shibboleth.ext.spring.config.StringBooleanToPredicateConverter;
+import net.shibboleth.ext.spring.config.StringToDurationConverter;
import net.shibboleth.ext.spring.config.StringToIPRangeConverter;
import net.shibboleth.ext.spring.config.StringToResourceConverter;
import net.shibboleth.ext.spring.context.FilesystemGenericApplicationContext;
@@ -50,6 +50,7 @@ import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.convert.ConversionService;
+import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import com.google.common.base.Predicates;
@@ -57,7 +58,7 @@ import com.google.common.collect.Collections2;
/**
* Fluent builder for a {@link FilesystemGenericApplicationContext} equipped with various standard features,
- * behavior, converters, etc.
+ * behavior, converters, etc. that are applicable to the Shibboleth software components.
*
* @since 5.4.0
*/
@@ -69,8 +70,8 @@ public class ApplicationContextBuilder {
/** Context name. */
@Nullable @NotEmpty private String contextName;
- /** List of configuration resources for this service. */
- @Nullable @NonnullElements private List<Resource> configurationResources;
+ /** Configuration resources for this service. */
+ @Nullable @NonnullElements private Collection<Resource> configurationResources;
/** Conversion service to use. */
@Nullable private ConversionService conversionService;
@@ -85,6 +86,9 @@ public class ApplicationContextBuilder {
/** List of bean post processors for this service's content. */
@Nullable @NonnullElements private List<BeanPostProcessor> postProcessors;
+ /** List of property sources to add. */
+ @Nullable @NonnullElements private List<PropertySource> propertySources;
+
/** Bean profiles to enable. */
@Nullable @NonnullElements private Collection<String> beanProfiles;
@@ -131,18 +135,33 @@ public class ApplicationContextBuilder {
}
/**
- * Set the list of configurations for this context.
+ * Set the configurations for this context.
*
- * @param configs list of configurations for this context
+ * @param configs configurations for this context
*
* @return this builder
*/
@Nonnull public ApplicationContextBuilder setServiceConfigurations(
- @Nonnull @NonnullElements final List<Resource> configs) {
+ @Nonnull @NonnullElements final Collection<Resource> configs) {
configurationResources = new ArrayList<>(Collections2.filter(configs, Predicates.notNull()));
return this;
}
+
+ /**
+ * Set additional property sources for this context.
+ *
+ * @param sources property sources to add
+ *
+ * @return this builder
+ */
+ @Nonnull public ApplicationContextBuilder setPropertySources(
+ @Nonnull @NonnullElements final List<PropertySource> sources) {
+ propertySources = new ArrayList<>(Collections2.filter(sources, Predicates.notNull()));
+
+ return this;
+ }
+
/**
* Set a single context initializer for this context.
@@ -272,7 +291,7 @@ public class ApplicationContextBuilder {
*/
@Nonnull public GenericApplicationContext build() {
- final GenericApplicationContext context = new FilesystemGenericApplicationContext(parentContext);
+ final FilesystemGenericApplicationContext context = new FilesystemGenericApplicationContext(parentContext);
context.setDisplayName("ApplicationContext:" + contextName != null ? contextName : "anonymous");
context.setResourceLoader(new PreferFileSystemResourceLoader());
@@ -282,11 +301,11 @@ public class ApplicationContextBuilder {
} else {
final ConversionServiceFactoryBean service = new ConversionServiceFactoryBean();
service.setConverters(new HashSet<>(Arrays.asList(
- new DurationToLongConverter(),
new StringToIPRangeConverter(),
new BooleanToPredicateConverter(),
new StringBooleanToPredicateConverter(),
new StringToResourceConverter(),
+ new StringToDurationConverter(),
new PredicateToPredicateConverter(),
new FunctionToFunctionConverter())));
service.afterPropertiesSet();
@@ -294,20 +313,22 @@ public class ApplicationContextBuilder {
}
if (factoryPostProcessors != null) {
- for (final BeanFactoryPostProcessor bfpp : factoryPostProcessors) {
- context.addBeanFactoryPostProcessor(bfpp);
- }
+ factoryPostProcessors.forEach(bfpp -> context.addBeanFactoryPostProcessor(bfpp));
}
if (postProcessors != null) {
- for (final BeanPostProcessor bpp : postProcessors) {
- context.getBeanFactory().addBeanPostProcessor(bpp);
- }
+ postProcessors.forEach(bpp -> context.getBeanFactory().addBeanPostProcessor(bpp));
}
if (beanProfiles != null) {
context.getEnvironment().setActiveProfiles(beanProfiles.toArray(new String[0]));
}
+
+ if (propertySources != null) {
+ propertySources.forEach(p -> context.getEnvironment().getPropertySources().addLast(p));
+ context.getEnvironment().setPlaceholderPrefix("%{");
+ context.getEnvironment().setPlaceholderSuffix("}");
+ }
final SchemaTypeAwareXMLBeanDefinitionReader beanDefinitionReader =
new SchemaTypeAwareXMLBeanDefinitionReader(context);
@@ -317,9 +338,7 @@ public class ApplicationContextBuilder {
}
if (contextInitializers != null) {
- for (final ApplicationContextInitializer initializer : contextInitializers) {
- initializer.initialize(context);
- }
+ contextInitializers.forEach(i -> i.initialize(context));
}
context.refresh();
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringSupport.java b/src/main/java/net/shibboleth/ext/spring/util/SpringSupport.java
index 60be724..d320314 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/SpringSupport.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringSupport.java
@@ -19,10 +19,7 @@ package net.shibboleth.ext.spring.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -33,19 +30,13 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
-import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
-import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextInitializer;
-import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.GenericApplicationContext;
-import org.springframework.core.io.Resource;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -53,14 +44,8 @@ import org.xml.sax.InputSource;
import com.google.common.base.Strings;
-import net.shibboleth.ext.spring.config.DurationToLongConverter;
-import net.shibboleth.ext.spring.config.StringBooleanToPredicateConverter;
-import net.shibboleth.ext.spring.config.StringToIPRangeConverter;
import net.shibboleth.ext.spring.context.FilesystemGenericApplicationContext;
-import net.shibboleth.ext.spring.resource.PreferFileSystemResourceLoader;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
-import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
-import net.shibboleth.utilities.java.support.logic.Constraint;
import net.shibboleth.utilities.java.support.primitive.StringSupport;
import net.shibboleth.utilities.java.support.xml.SerializeSupport;
import net.shibboleth.utilities.java.support.xml.XMLConstants;
@@ -82,77 +67,6 @@ public final class SpringSupport {
}
-// Checkstyle: ParameterNumber OFF
- /**
- * Creates a new, started, application context from the given configuration resources.
- *
- * @param name name of the application context
- * @param configurationResources configuration resources
- * @param factoryPostProcessors post processors to inject
- * @param postProcessors post processors to inject
- * @param initializers application context initializers
- * @param parentContext parent context, or null if there is no parent
- *
- * @return the created context
- *
- * TODO: The signature here needs to constrain the ApplicationContextInitializers supplied to
- * be safe for use with a FilesystemGenericApplicationContext. The raw types are masking the bug.
- *
- * @deprecated - use {@link net.shibboleth.ext.spring.util.ApplicationContextBuilder}
- */
- @Deprecated
- @Nonnull public static GenericApplicationContext newContext(@Nonnull @NotEmpty final String name,
- @Nonnull @NonnullElements final List<Resource> configurationResources,
- @Nonnull @NonnullElements final List<BeanFactoryPostProcessor> factoryPostProcessors,
- @Nonnull @NonnullElements final List<BeanPostProcessor> postProcessors,
- @Nonnull @NonnullElements final List<ApplicationContextInitializer> initializers,
- @Nullable final ApplicationContext parentContext) {
-
- Constraint.isNotNull(configurationResources, "configurationResources cannot be null");
- Constraint.noNullItems(configurationResources, "configurationResources must have no null items");
- Constraint.isNotNull(factoryPostProcessors, "factoryPostProcessors cannot be null");
- Constraint.noNullItems(factoryPostProcessors, "factoryPostProcessors must have no null items");
- Constraint.isNotNull(postProcessors, "postProcessors cannot be null");
- Constraint.noNullItems(postProcessors, "postProcessors must have no null items");
- Constraint.isNotNull(initializers, "initializers cannot be null");
- Constraint.noNullItems(initializers, "initializers must have no null items");
-
- final GenericApplicationContext context = new FilesystemGenericApplicationContext(parentContext);
- context.setDisplayName("ApplicationContext:" + name);
-
- context.setResourceLoader(new PreferFileSystemResourceLoader());
-
- final ConversionServiceFactoryBean service = new ConversionServiceFactoryBean();
- service.setConverters(new HashSet<>(Arrays.asList(new DurationToLongConverter(), new StringToIPRangeConverter(),
- new StringBooleanToPredicateConverter())));
- service.afterPropertiesSet();
-
- for (final BeanFactoryPostProcessor bfpp : factoryPostProcessors) {
- context.addBeanFactoryPostProcessor(bfpp);
- }
-
- context.getBeanFactory().setConversionService(service.getObject());
- for (final BeanPostProcessor bpp : postProcessors) {
- context.getBeanFactory().addBeanPostProcessor(bpp);
- }
-
- final SchemaTypeAwareXMLBeanDefinitionReader beanDefinitionReader =
- new SchemaTypeAwareXMLBeanDefinitionReader(context);
-
- beanDefinitionReader.loadBeanDefinitions(configurationResources.toArray(new Resource[] {}));
-
- if (initializers != null) {
- for (final ApplicationContextInitializer initializer : initializers) {
- initializer.initialize(context);
- }
- }
-
- context.refresh();
- return context;
- }
-// Checkstyle: ParameterNumber ON
-
-
/**
* Parse list of elements into bean definitions.
*
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list