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:54:57 UTC
[sling-org-apache-sling-models-api] 17/34: SLING-4161 support Sling
Validation by a new field on the Model annotation
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.models.api-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-api.git
commit 0513619b441f4c14afaceba60addafd5c96e33bf
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Dec 9 14:46:28 2014 +0000
SLING-4161 support Sling Validation by a new field on the Model annotation
The dependency to Sling Validation is optional (i.e. Sling Models still work even if Sling Validation is not deployed)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api@1644069 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 14 +++++
.../org/apache/sling/models/annotations/Model.java | 9 +++
.../{package-info.java => ValidationStrategy.java} | 8 ++-
.../sling/models/annotations/package-info.java | 2 +-
.../models/factory/InvalidResourceException.java | 68 ++++++++++++++++++++++
.../factory/InvalidValidationModelException.java | 38 ++++++++++++
...odelException.java => ModelClassException.java} | 10 ++--
.../apache/sling/models/factory/ModelFactory.java | 11 ++--
8 files changed, 148 insertions(+), 12 deletions(-)
diff --git a/pom.xml b/pom.xml
index 6c7aceb..0d18236 100644
--- a/pom.xml
+++ b/pom.xml
@@ -49,6 +49,7 @@
<configuration>
<instructions>
<Export-Package>org.apache.sling.models.*,javax.inject;version=0</Export-Package>
+ <DynamicImport-Package>org.apache.sling.validation.api.*</DynamicImport-Package>
</instructions>
</configuration>
</plugin>
@@ -61,5 +62,18 @@
<version>1</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.validation.api</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.5</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/main/java/org/apache/sling/models/annotations/Model.java b/src/main/java/org/apache/sling/models/annotations/Model.java
index b532dc9..42b4406 100644
--- a/src/main/java/org/apache/sling/models/annotations/Model.java
+++ b/src/main/java/org/apache/sling/models/annotations/Model.java
@@ -50,5 +50,14 @@ public @interface Model {
* @return Condition that is displayed in the felix console adapter plugin
*/
public String condition() default "";
+
+ /**
+ *
+ * @return {@link ValidationStrategy.DISABLED} in case the model should not be validated through Sling Validation (default),
+ * {@link ValidationStrategy.REQUIRED} in case the model should be validated and if no appropriate Sling Validation Model exists it is considered invalid or
+ * {@link ValidationStrategy.OPTIONAL} in case the model should be validated only in case an appropriate Sling Validation Model is found.
+ * @see <a href="http://sling.apache.org/documentation/bundles/validation.html">Sling Validation</a>
+ */
+ public ValidationStrategy validation() default ValidationStrategy.DISABLED;
}
diff --git a/src/main/java/org/apache/sling/models/annotations/package-info.java b/src/main/java/org/apache/sling/models/annotations/ValidationStrategy.java
similarity index 87%
copy from src/main/java/org/apache/sling/models/annotations/package-info.java
copy to src/main/java/org/apache/sling/models/annotations/ValidationStrategy.java
index be6612e..6db5bbb 100644
--- a/src/main/java/org/apache/sling/models/annotations/package-info.java
+++ b/src/main/java/org/apache/sling/models/annotations/ValidationStrategy.java
@@ -14,7 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-@Version("1.1.3")
package org.apache.sling.models.annotations;
-import aQute.bnd.annotation.Version;
\ No newline at end of file
+/**
+ * Used by the {@link Model} annotation.
+ */
+public enum ValidationStrategy {
+ DISABLED, REQUIRED, OPTIONAL
+}
diff --git a/src/main/java/org/apache/sling/models/annotations/package-info.java b/src/main/java/org/apache/sling/models/annotations/package-info.java
index be6612e..b262f8a 100644
--- a/src/main/java/org/apache/sling/models/annotations/package-info.java
+++ b/src/main/java/org/apache/sling/models/annotations/package-info.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-@Version("1.1.3")
+@Version("1.2.0")
package org.apache.sling.models.annotations;
import aQute.bnd.annotation.Version;
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java b/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java
new file mode 100644
index 0000000..c9efc86
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java
@@ -0,0 +1,68 @@
+/*
+ * 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.models.factory;
+
+import java.util.List;
+import java.util.Map.Entry;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.sling.validation.api.ValidationResult;
+
+/**
+ * Thrown in case Sling Validation detected an invalid resource upon which the model should be instanciated.
+ * @see <a href="http://sling.apache.org/documentation/bundles/validation.html">Sling Validation</a>
+ * @see ModelFactory
+ */
+public class InvalidResourceException extends RuntimeException {
+ private static final long serialVersionUID = 366657841414210438L;
+ private final ValidationResult result;
+ private final String path;
+
+ public InvalidResourceException(ValidationResult result, String path) {
+ if (result.isValid()) {
+ throw new IllegalArgumentException("Could not create a validator exception from a valid validation result!");
+ }
+ this.path = path;
+ this.result = result;
+ }
+
+ /**
+ *
+ * @return the underlying {@link ValidationResult}
+ */
+ public ValidationResult getResult() {
+ return result;
+ }
+
+ /**
+ *
+ * @return the path of the resource which was considered invalid
+ */
+ public String getPath() {
+ return path;
+ }
+
+ @Override
+ public String getMessage() {
+ StringBuilder builder = new StringBuilder("Validation errors for ");
+ builder.append("'" + path +"':");
+ for (Entry<String, List<String>> entry : result.getFailureMessages().entrySet()) {
+ builder.append("\n" + entry.getKey() + ":" + StringUtils.join(entry.getValue(), "\n\t"));
+ }
+ return builder.toString();
+ }
+}
diff --git a/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java b/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java
new file mode 100644
index 0000000..a1620e8
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.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.models.factory;
+
+/**
+ * Thrown in case an invalid Sling validation model was found or no validation model was found at all
+ * (although it would be required through {@link org.apache.sling.models.annotations.ValidationStrategy.REQUIRED}).
+ * Usually just wraps a {@link org.apache.sling.validation.api.exceptions.SlingValidationException}
+ * @see <a href="http://sling.apache.org/documentation/bundles/validation.html">Sling Validation</a>
+ * @see ModelFactory
+ */
+public class InvalidValidationModelException extends RuntimeException {
+
+ private static final long serialVersionUID = 1115037385798809055L;
+
+ public InvalidValidationModelException(String message) {
+ super(message);
+ }
+
+ public InvalidValidationModelException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/models/factory/InvalidModelException.java b/src/main/java/org/apache/sling/models/factory/ModelClassException.java
similarity index 73%
rename from src/main/java/org/apache/sling/models/factory/InvalidModelException.java
rename to src/main/java/org/apache/sling/models/factory/ModelClassException.java
index 744f03d..7718dd6 100644
--- a/src/main/java/org/apache/sling/models/factory/InvalidModelException.java
+++ b/src/main/java/org/apache/sling/models/factory/ModelClassException.java
@@ -19,17 +19,17 @@
package org.apache.sling.models.factory;
/**
- * Exception which is triggered when the Model could not be instantiated due to
- * model class is not having a model annotation, some reflection error, invalid constructors or
- * some exception within the post construct method was triggered.
+ * Exception which is triggered when the Model class could not be instantiated due to
+ * not having a model annotation, some reflection error, invalid constructors or
+ * because some exception within the post construct method was triggered.
*
* @see ModelFactory
*/
-public final class InvalidModelException extends RuntimeException {
+public final class ModelClassException extends RuntimeException {
private static final long serialVersionUID = 4323592065808565135L;
- public InvalidModelException(String message) {
+ public ModelClassException(String message) {
super(message);
}
}
diff --git a/src/main/java/org/apache/sling/models/factory/ModelFactory.java b/src/main/java/org/apache/sling/models/factory/ModelFactory.java
index 3d34e8f..427b7d1 100644
--- a/src/main/java/org/apache/sling/models/factory/ModelFactory.java
+++ b/src/main/java/org/apache/sling/models/factory/ModelFactory.java
@@ -18,6 +18,7 @@
*/
package org.apache.sling.models.factory;
+
/**
* The ModelFactory instantiates Sling Model classes similar to adaptTo but is allowed to throw an exception in case
* instantiation fails for some reason.
@@ -31,19 +32,21 @@ public interface ModelFactory {
* @return a new instance for the required model (never null)
* @throws MissingElementsException in case no injector was able to inject some required values with the given types
* @throws InvalidAdaptableException in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
- * @throws InvalidModelException in case the model could not be instanciated because model annotation was missing, reflection failed, no valid constructor was found or post-construct has thrown an error
+ * @throws ModelClassException in case the model could not be instanciated because model annotation was missing, reflection failed, no valid constructor was found or post-construct has thrown an error
+ * @throws InvalidValidationModelException in case an invalid validation model was found
+ * @throws InvalidResourceException in case the resource (for the Sling Model) could not be validated through Sling Validation
*/
public <ModelType> ModelType createModel(Object adaptable, Class<ModelType> type) throws MissingElementsException,
- InvalidAdaptableException, InvalidModelException;
+ InvalidAdaptableException, ModelClassException, InvalidValidationModelException, InvalidResourceException;
/**
*
* @param adaptable the adaptable to check
* @param type the class to check
* @return false in case the given class can not be created from the given adaptable
- * @throws InvalidModelException in case no class with the Model annotation adapts to the requested type
+ * @throws ModelClassException in case no class with the Model annotation adapts to the requested type
*/
- public boolean canCreateFromAdaptable(Object adaptable, Class<?> type) throws InvalidModelException;
+ public boolean canCreateFromAdaptable(Object adaptable, Class<?> type) throws ModelClassException;
/**
*
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.