You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:19:05 UTC
[sling-org-apache-sling-caconfig-api] 19/24: SLING-6023 Summary:
Context-Aware Config: Add pluggable context paths strategies SLING-6026
Summary: Context-Aware Config: Pluggable configuration persistence
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.caconfig.api-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-caconfig-api.git
commit 743a808d63d81136c2707fd77cf73d0156dc16d4
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Sep 19 15:39:17 2016 +0000
SLING-6023 Summary: Context-Aware Config: Add pluggable context paths strategies
SLING-6026 Summary: Context-Aware Config: Pluggable configuration persistence
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/contextaware-config/api@1761468 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 20 +++++-
.../contextaware/config/ConfigurationBuilder.java | 9 ++-
.../ConfigurationResourceResolvingStrategy.java | 84 ++++++++++++++++++++++
.../config/resource/spi/ContextPathStrategy.java | 46 ++++++++++++
.../config/resource/spi/package-info.java | 23 ++++++
.../spi/ConfigurationPersistenceException.java | 38 ++++++++++
.../spi/ConfigurationPersistenceStrategy.java | 67 +++++++++++++++++
.../config/spi/metadata/AbstractMetadata.java | 3 -
.../config/spi/metadata/ConfigurationMetadata.java | 9 ++-
.../config/spi/metadata/PropertyMetadata.java | 34 ++++++++-
.../config/spi/metadata/PropertyMetadataTest.java | 11 +++
11 files changed, 329 insertions(+), 15 deletions(-)
diff --git a/pom.xml b/pom.xml
index 5bed74c..2d10043 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,9 +67,27 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.3.2</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+ <version>2.0.5-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock</artifactId>
- <version>1.7.0</version>
+ <version>1.7.1-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/contextaware/config/ConfigurationBuilder.java b/src/main/java/org/apache/sling/contextaware/config/ConfigurationBuilder.java
index 1243e84..28d2a2a 100644
--- a/src/main/java/org/apache/sling/contextaware/config/ConfigurationBuilder.java
+++ b/src/main/java/org/apache/sling/contextaware/config/ConfigurationBuilder.java
@@ -44,6 +44,7 @@ public interface ConfigurationBuilder {
* Configuration name is optional - if not given via {@link #name(String)} method it is derived
* from the annotation interface class name.
* @param clazz Annotation interface class
+ * @param <T> Annotation class type
* @return Configuration object. Contains only the default values if content resource or configuration cannot be found.
*/
@Nonnull <T> T as(@Nonnull Class<T> clazz);
@@ -53,6 +54,7 @@ public interface ConfigurationBuilder {
* Configuration name is optional - if not given via {@link #name(String)} method it is derived
* from the annotation interface class name.
* @param clazz Annotation interface class
+ * @param <T> Annotation class type
* @return Collection of configuration objects. Is empty if content resource or configuration cannot be found.
*/
@Nonnull <T> Collection<T> asCollection(@Nonnull Class<T> clazz);
@@ -65,21 +67,22 @@ public interface ConfigurationBuilder {
/**
* Get collection of configuration resources with their properties mapped to the given annotation class.
- * @param clazz Annotation interface class
* @return Collection of value map. Is empty if content resource or configuration cannot be found.
*/
@Nonnull Collection<ValueMap> asValueMapCollection();
/**
* Get configuration as singleton configuration resource and adapt it to the given class.
- * @param clazz Class that can be adapted from a {@link Resource}
+ * @param clazz Class that can be adapted from a {@link org.apache.sling.api.resource.Resource}
+ * @param <T> Annotation class type
* @return Object instance or null if content resource or configuration cannot be found or if the adaption was not possible.
*/
<T> T asAdaptable(@Nonnull Class<T> clazz);
/**
* Get collection of configuration resources and adapt them to the given class.
- * @param clazz Class that can be adapted from a {@link Resource}
+ * @param clazz Class that can be adapted from a {@link org.apache.sling.api.resource.Resource}
+ * @param <T> Annotation class type
* @return Collection of object instances. Is empty if content resource or configuration cannot be found or if the adaption was not possible.
*/
@Nonnull <T> Collection<T> asAdaptableCollection(@Nonnull Class<T> clazz);
diff --git a/src/main/java/org/apache/sling/contextaware/config/resource/spi/ConfigurationResourceResolvingStrategy.java b/src/main/java/org/apache/sling/contextaware/config/resource/spi/ConfigurationResourceResolvingStrategy.java
new file mode 100644
index 0000000..89ad4b8
--- /dev/null
+++ b/src/main/java/org/apache/sling/contextaware/config/resource/spi/ConfigurationResourceResolvingStrategy.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sling.contextaware.config.resource.spi;
+
+import java.util.Collection;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.sling.api.resource.Resource;
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * Defines how and where the configuration resources are looked up.
+ * This SPI allows application to define their own configuration storage and inheritance strategies.
+ */
+@ConsumerType
+public interface ConfigurationResourceResolvingStrategy {
+
+ /**
+ * Get a context-aware singleton configuration resource defined by the given configuration name.
+ * @param resource Context resource to fetch configuration for
+ * @param bucketName Configuration "bucket" name. Each high-level configuration resolver should store
+ * it's configuration data grouped in a child resource of the configuration resource. This is what
+ * we call a "bucket", and the resource name is specified with this parameter.
+ * @param configName Configuration name or relative path.
+ * @return Configuration resource or {@code null}.
+ */
+ @CheckForNull Resource getResource(@Nonnull Resource resource, @Nonnull String bucketName, @Nonnull String configName);
+
+ /**
+ * Get a collection of context-aware configuration resources defined by the given configuration name.
+ * @param resource Context resource to fetch configuration for
+ * @param bucketName Configuration "bucket" name. Each high-level configuration resolver should store
+ * it's configuration data grouped in a child resource of the configuration resource. This is what
+ * we call a "bucket", and the resource name is specified with this parameter.
+ * @param configName Configuration name or relative path.
+ * @return Collection of configuration resources, the collection might be empty.
+ */
+ @Nonnull Collection<Resource> getResourceCollection(@Nonnull Resource resource, @Nonnull String bucketName, @Nonnull String configName);
+
+ /**
+ * Get the configuration resource path for storing configuration data for the given context resource and configuration name.
+ * This path is used when no configuration resource exists yet, but new configuration data should be stored.
+ * So usually the returned path does not yet exist (and perhaps not even it's parents).
+ * @param resource Context resource to fetch configuration for
+ * @param bucketName Configuration "bucket" name. Each high-level configuration resolver should store
+ * it's configuration data grouped in a child resource of the configuration resource. This is what
+ * we call a "bucket", and the resource name is specified with this parameter.
+ * @param configName Configuration name or relative path.
+ * @return Resource path, or null if no matching configuration resource path can be determined
+ */
+ @CheckForNull String getResourcePath(@Nonnull Resource resource, @Nonnull String bucketName, @Nonnull String configName);
+
+ /**
+ * Get the configuration resource collection parent path for storing configuration data for the given context resource and configuration name.
+ * This path is used when no configuration resource collection exists yet, but new configuration data should be stored.
+ * So usually the returned path does not yet exist (and perhaps not even it's parents).
+ * @param resource Context resource to fetch configuration for
+ * @param bucketName Configuration "bucket" name. Each high-level configuration resolver should store
+ * it's configuration data grouped in a child resource of the configuration resource. This is what
+ * we call a "bucket", and the resource name is specified with this parameter.
+ * @param configName Configuration name or relative path.
+ * @return Resource path, or null if no matching configuration resource path can be determined
+ */
+ @CheckForNull String getResourceCollectionParentPath(@Nonnull Resource resource, @Nonnull String bucketName, @Nonnull String configName);
+
+}
diff --git a/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java b/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java
new file mode 100644
index 0000000..4b93957
--- /dev/null
+++ b/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sling.contextaware.config.resource.spi;
+
+import java.util.Iterator;
+
+import javax.annotation.Nonnull;
+
+import org.apache.sling.api.resource.Resource;
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * Allows application to define a strategy to find context paths for content paths.
+ * A context paths is the root path of a "configuration context", which is a subtree in the resource hierarchy.
+ * Each context may have it's own context-aware configuration attached to.0
+ * If multiple context path strategy implementations are defined the results of them are merged.
+ */
+@ConsumerType
+public interface ContextPathStrategy {
+
+ /**
+ * Finds context paths for the given resource.
+ * @param resource Context resource
+ * @return Root resource for each context found (in order of closest matching first).
+ * Only one of the parent resources or the resource itself may be included in the result.
+ * If none are found an empty list is returned.
+ */
+ @Nonnull Iterator<Resource> findContextResources(@Nonnull Resource resource);
+
+}
diff --git a/src/main/java/org/apache/sling/contextaware/config/resource/spi/package-info.java b/src/main/java/org/apache/sling/contextaware/config/resource/spi/package-info.java
new file mode 100644
index 0000000..a7722f2
--- /dev/null
+++ b/src/main/java/org/apache/sling/contextaware/config/resource/spi/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+/**
+ * SPI for applications hooking into the configuration resource infrastructure for parameterizing and customizing.
+ */
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.apache.sling.contextaware.config.resource.spi;
diff --git a/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceException.java b/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceException.java
new file mode 100644
index 0000000..618357d
--- /dev/null
+++ b/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceException.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sling.contextaware.config.spi;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * Is thrown when configuration cannot be persisted.
+ */
+@ProviderType
+public final class ConfigurationPersistenceException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ public ConfigurationPersistenceException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ConfigurationPersistenceException(String message) {
+ super(message);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceStrategy.java b/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceStrategy.java
new file mode 100644
index 0000000..9dfe945
--- /dev/null
+++ b/src/main/java/org/apache/sling/contextaware/config/spi/ConfigurationPersistenceStrategy.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sling.contextaware.config.spi;
+
+import java.util.Collection;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.osgi.annotation.versioning.ConsumerType;
+
+/**
+ * Defines how configuration data is stored in the configuration resource.
+ * This SPI allows application to define their own content structure and node types to be used for configuration data storage.
+ */
+@ConsumerType
+public interface ConfigurationPersistenceStrategy {
+
+ /**
+ * Allows the strategy to transform the given configuration resource according to it's persistent strategies,
+ * e.g. fetching the data from a child resource instead of the given resource.
+ * @param resource Configuration resource
+ * @return Transformed configuration resource. If null is returned this strategy does not support the given configuration resource.
+ */
+ Resource getResource(@Nonnull Resource resource);
+
+ /**
+ * Stores configuration data for a singleton configuration resource.
+ * The changes are written using the given resource resolver. They are not committed, this is left to the caller.
+ * @param resourceResolver Resource resolver
+ * @param configResourcePath Path to store configuration data to. The resource (and it's parents) may not exist and may have to be created.
+ * @param properties Configuration properties
+ * @return true if the data was persisted. false if persisting the data was not accepted by this persistence strategy (but in case of error throw an exception).
+ */
+ boolean persist(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull String configResourcePath, @Nonnull Map<String,Object> properties);
+
+ /**
+ * Stores configuration data for a configuration resource collection.
+ * The changes are written using the given resource resolver. They are not committed, this is left to the caller.
+ * @param resourceResolver Resource resolver
+ * @param configResourceCollectionParentPath Parent path to store configuration collection data to. The resource (and it's parents) may not exist and may have to be created.
+ * @param propertiesCollection Configuration properties
+ * @return true if the data was persisted. false if persisting the data was not accepted by this persistence strategy (but in case of error throw an exception).
+ */
+ boolean persistCollection(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull String configResourceCollectionParentPath, @Nonnull Collection<Map<String,Object>> propertiesCollection);
+
+}
diff --git a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/AbstractMetadata.java b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/AbstractMetadata.java
index bd1f70b..2cd0d17 100644
--- a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/AbstractMetadata.java
+++ b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/AbstractMetadata.java
@@ -22,12 +22,9 @@ import java.util.Map;
import javax.annotation.Nonnull;
-import org.osgi.annotation.versioning.ProviderType;
-
/**
* Common properties for configuration and properties.
*/
-@ProviderType
abstract class AbstractMetadata {
private final String name;
diff --git a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/ConfigurationMetadata.java b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/ConfigurationMetadata.java
index a838f56..7730157 100644
--- a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/ConfigurationMetadata.java
+++ b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/ConfigurationMetadata.java
@@ -18,8 +18,7 @@
*/
package org.apache.sling.contextaware.config.spi.metadata;
-import java.util.Collection;
-import java.util.List;
+import java.util.Map;
import javax.annotation.Nonnull;
@@ -32,7 +31,7 @@ import org.osgi.annotation.versioning.ProviderType;
public final class ConfigurationMetadata extends AbstractMetadata {
private boolean isList;
- private List<PropertyMetadata<?>> propertyMetadata;
+ private Map<String,PropertyMetadata<?>> propertyMetadata;
/**
* @param name Configuration name
@@ -65,14 +64,14 @@ public final class ConfigurationMetadata extends AbstractMetadata {
/**
* @return Configuration properties
*/
- public Collection<PropertyMetadata<?>> getPropertyMetadata() {
+ public Map<String,PropertyMetadata<?>> getPropertyMetadata() {
return this.propertyMetadata;
}
/**
* @param propertyMetadata Configuration properties
*/
- public void setPropertyMetadata(List<PropertyMetadata<?>> propertyMetadata) {
+ public void setPropertyMetadata(Map<String,PropertyMetadata<?>> propertyMetadata) {
this.propertyMetadata = propertyMetadata;
}
diff --git a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadata.java b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadata.java
index fa6735a..5e0da26 100644
--- a/src/main/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadata.java
+++ b/src/main/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadata.java
@@ -25,6 +25,7 @@ import java.util.Set;
import javax.annotation.Nonnull;
+import org.apache.commons.lang3.ClassUtils;
import org.osgi.annotation.versioning.ProviderType;
/**
@@ -34,6 +35,7 @@ import org.osgi.annotation.versioning.ProviderType;
@ProviderType
public final class PropertyMetadata<T> extends AbstractMetadata {
+ // these are all types supported for fields of annotation classes (plus class which indicates nested configurations)
private static final Class<?>[] SUPPORTED_TYPES_ARRAY = {
String.class,
int.class,
@@ -51,14 +53,40 @@ public final class PropertyMetadata<T> extends AbstractMetadata {
private final Class<T> type;
private T defaultValue;
+ /**
+ * @param name Property name
+ * @param type Property type
+ */
+ @SuppressWarnings("unchecked")
public PropertyMetadata(@Nonnull String name, @Nonnull Class<T> type) {
super(name);
- if (type == null || !isSupportedType(type)) {
- throw new IllegalArgumentException("Parameter '" + name + "': Invalid type " + type);
+ Class<T> convertedType = (Class<T>)typeToPrimitive(type);
+ if (!isSupportedType(convertedType)) {
+ throw new IllegalArgumentException("Invalid type for property '" + name + "': " + type);
}
- this.type = type;
+ this.type = convertedType;
+ }
+
+ /**
+ * @param name Property name
+ * @param defaultValue Default value (also defines property type)
+ */
+ @SuppressWarnings("unchecked")
+ public PropertyMetadata(@Nonnull String name, @Nonnull T defaultValue) {
+ this(name, (Class<T>)defaultValue.getClass());
+ this.defaultValue = defaultValue;
}
+ private static Class<?> typeToPrimitive(Class<?> clazz) {
+ if (clazz != String.class && !clazz.isPrimitive()) {
+ Class<?> type = ClassUtils.wrapperToPrimitive(clazz);
+ if (type != null) {
+ return type;
+ }
+ }
+ return clazz;
+ }
+
private static boolean isSupportedType(Class<?> paramType) {
if (paramType.isArray()) {
return isSupportedType(paramType.getComponentType());
diff --git a/src/test/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadataTest.java b/src/test/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadataTest.java
index fb9a7b0..1427192 100644
--- a/src/test/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadataTest.java
+++ b/src/test/java/org/apache/sling/contextaware/config/spi/metadata/PropertyMetadataTest.java
@@ -76,4 +76,15 @@ public class PropertyMetadataTest {
new PropertyMetadata<>(null, Object.class);
}
+ @Test
+ public void testDefaultValueConstructor() {
+ PropertyMetadata<String> stringProp = new PropertyMetadata<>("name1", "defValue");
+ assertEquals("defValue", stringProp.getDefaultValue());
+ assertEquals(String.class, stringProp.getType());
+
+ PropertyMetadata<Integer> intProp = new PropertyMetadata<>("name1", 5);
+ assertEquals((Integer)5, intProp.getDefaultValue());
+ assertEquals(int.class, intProp.getType());
+ }
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.