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 f817ebe997085d2177f8ea7c6b7d04e85349561f Author: Konrad Windszus <[email protected]> AuthorDate: Mon Jun 1 16:24:13 2015 +0000 SLING-4161 move validation to separate bundle git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api@1682946 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 13 ----- .../InvalidModelException.java} | 19 +++++- .../models/factory/InvalidResourceException.java | 68 ---------------------- .../apache/sling/models/factory/ModelFactory.java | 10 ++-- ...odelException.java => ValidationException.java} | 12 ++-- .../apache/sling/models/spi/ModelValidation.java | 35 +++++++++++ .../org/apache/sling/models/spi/package-info.java | 2 +- 7 files changed, 64 insertions(+), 95 deletions(-) diff --git a/pom.xml b/pom.xml index 7f4e053..9ead9f8 100644 --- a/pom.xml +++ b/pom.xml @@ -68,19 +68,6 @@ <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> - <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.0</version> diff --git a/src/main/java/org/apache/sling/models/spi/package-info.java b/src/main/java/org/apache/sling/models/factory/InvalidModelException.java similarity index 61% copy from src/main/java/org/apache/sling/models/spi/package-info.java copy to src/main/java/org/apache/sling/models/factory/InvalidModelException.java index f97a279..fefaa2b 100644 --- a/src/main/java/org/apache/sling/models/spi/package-info.java +++ b/src/main/java/org/apache/sling/models/factory/InvalidModelException.java @@ -14,7 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@Version("1.0.3") -package org.apache.sling.models.spi; +package org.apache.sling.models.factory; -import aQute.bnd.annotation.Version; \ No newline at end of file +import org.apache.sling.models.spi.ModelValidation; + +/** + * Thrown in case the given model type could not be validated through the {@link ModelValidation}. + * The actual validation error message is encapsulated + * @see ModelFactory + */ +public class InvalidModelException extends RuntimeException { + private static final long serialVersionUID = 366657841414210438L; + + public InvalidModelException(String message) { + super(message); + } + +} diff --git a/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java b/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java deleted file mode 100644 index 78fbdc3..0000000 --- a/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 instantiated. - * @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/ModelFactory.java b/src/main/java/org/apache/sling/models/factory/ModelFactory.java index 7a88d81..462e7c9 100644 --- a/src/main/java/org/apache/sling/models/factory/ModelFactory.java +++ b/src/main/java/org/apache/sling/models/factory/ModelFactory.java @@ -20,6 +20,8 @@ package org.apache.sling.models.factory; import javax.annotation.Nonnull; +import org.apache.sling.models.spi.ModelValidation; + /** * The ModelFactory instantiates Sling Model classes similar to adaptTo but is allowed to throw an exception in case @@ -34,13 +36,13 @@ 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 ModelClassException in case the model could not be instanciated because model annotation was missing, reflection failed, no valid constructor was found or post-construct could not be called + * @throws ModelClassException in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found or post-construct could not be called * @throws PostConstructException in case the post-construct method has thrown an exception itself - * @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 + * @throws ValidationException in case validation could not be performed for some reason (e.g. no validation information available) + * @throws InvalidModelException in case the given model type could not be validated through the {@link ModelValidation} */ public @Nonnull <ModelType> ModelType createModel(@Nonnull Object adaptable, @Nonnull Class<ModelType> type) throws MissingElementsException, - InvalidAdaptableException, ModelClassException, PostConstructException, InvalidValidationModelException, InvalidResourceException; + InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException; /** * diff --git a/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java b/src/main/java/org/apache/sling/models/factory/ValidationException.java similarity index 69% rename from src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java rename to src/main/java/org/apache/sling/models/factory/ValidationException.java index a1620e8..58f056e 100644 --- a/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java +++ b/src/main/java/org/apache/sling/models/factory/ValidationException.java @@ -16,22 +16,22 @@ */ package org.apache.sling.models.factory; + /** - * Thrown in case an invalid Sling validation model was found or no validation model was found at all + * Thrown in case an validation could not be performed for the given model. * (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> + * Depends on the actual implementation under which exact cirumstances this is thrown. * @see ModelFactory */ -public class InvalidValidationModelException extends RuntimeException { +public class ValidationException extends RuntimeException { private static final long serialVersionUID = 1115037385798809055L; - public InvalidValidationModelException(String message) { + public ValidationException(String message) { super(message); } - public InvalidValidationModelException(Throwable cause) { + public ValidationException(Throwable cause) { super(cause); } diff --git a/src/main/java/org/apache/sling/models/spi/ModelValidation.java b/src/main/java/org/apache/sling/models/spi/ModelValidation.java new file mode 100644 index 0000000..673b0db --- /dev/null +++ b/src/main/java/org/apache/sling/models/spi/ModelValidation.java @@ -0,0 +1,35 @@ +/* + * 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.spi; + +import org.apache.sling.models.factory.InvalidModelException; +import org.apache.sling.models.factory.ValidationException; + +public interface ModelValidation { + + /** + * Triggers validation for the given model on the given adaptable + * @param adaptable the adaptable about to be used instantiate the Sling Model Class + * @param modelClass the class of the model which is about to be instantiated + * @param required if {@code true} validation fails even if validation model can't be found. + * @return {@code null} if validation was successful, otherwise either {@link ValidationException} + * in case validation could not be performed for some reason (e.g. no validation information available) + * or {@link InvalidModelException} in case the given model type could not be validated through the {@link ModelValidation}. + */ + public abstract <ModelType> RuntimeException validate(Object adaptable, Class<ModelType> modelClass, boolean required) throws ValidationException, InvalidModelException; + +} diff --git a/src/main/java/org/apache/sling/models/spi/package-info.java b/src/main/java/org/apache/sling/models/spi/package-info.java index f97a279..a16e5d0 100644 --- a/src/main/java/org/apache/sling/models/spi/package-info.java +++ b/src/main/java/org/apache/sling/models/spi/package-info.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@Version("1.0.3") +@Version("1.1.0") package org.apache.sling.models.spi; import aQute.bnd.annotation.Version; \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
