You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2015/06/01 18:24:14 UTC

svn commit: r1682946 - in /sling/trunk: ./ bundles/extensions/models/api/ bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ bundles/extensions/models/im...

Author: kwin
Date: Mon Jun  1 16:24:13 2015
New Revision: 1682946

URL: http://svn.apache.org/r1682946
Log:
SLING-4161 move validation to separate bundle

Added:
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidModelException.java
      - copied, changed from r1682822, sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ValidationException.java
      - copied, changed from r1682822, sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ModelValidation.java
    sling/trunk/bundles/extensions/models/validation-impl/   (with props)
    sling/trunk/bundles/extensions/models/validation-impl/pom.xml
    sling/trunk/bundles/extensions/models/validation-impl/src/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/
    sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java
Removed:
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/validation/
Modified:
    sling/trunk/bundles/extensions/models/api/pom.xml
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java
    sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java
    sling/trunk/bundles/extensions/models/impl/pom.xml
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
    sling/trunk/pom.xml

Modified: sling/trunk/bundles/extensions/models/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/pom.xml?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/pom.xml (original)
+++ sling/trunk/bundles/extensions/models/api/pom.xml Mon Jun  1 16:24:13 2015
@@ -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>

Copied: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidModelException.java (from r1682822, sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java)
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidModelException.java?p2=sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidModelException.java&p1=sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java&r1=1682822&r2=1682946&rev=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidResourceException.java (original)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidModelException.java Mon Jun  1 16:24:13 2015
@@ -16,53 +16,18 @@
  */
 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;
+import org.apache.sling.models.spi.ModelValidation;
 
 /**
- * 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>
+ * 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 InvalidResourceException extends RuntimeException {
+public class InvalidModelException 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;
+    public InvalidModelException(String message) {
+        super(message);
     }
     
-    /**
-     * 
-     * @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();
-    }
 }

Modified: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java (original)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java Mon Jun  1 16:24:13 2015
@@ -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;
 
     /**
      * 

Copied: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ValidationException.java (from r1682822, sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java)
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ValidationException.java?p2=sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ValidationException.java&p1=sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java&r1=1682822&r2=1682946&rev=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/InvalidValidationModelException.java (original)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ValidationException.java Mon Jun  1 16:24:13 2015
@@ -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);
     }
 

Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ModelValidation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ModelValidation.java?rev=1682946&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ModelValidation.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ModelValidation.java Mon Jun  1 16:24:13 2015
@@ -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;
+
+}

Modified: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java (original)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/package-info.java Mon Jun  1 16:24:13 2015
@@ -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

Modified: sling/trunk/bundles/extensions/models/impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/pom.xml?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/pom.xml (original)
+++ sling/trunk/bundles/extensions/models/impl/pom.xml Mon Jun  1 16:24:13 2015
@@ -54,7 +54,6 @@
                         <Embed-Dependency>
                             *;scope=compile,
                             org.osgi.compendium;inline="org/osgi/util/tracker/*"</Embed-Dependency>
-                        <DynamicImport-Package>org.apache.sling.validation.api.*</DynamicImport-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -140,13 +139,6 @@
             <scope>compile</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>com.google.code.findbugs</groupId>
             <artifactId>jsr305</artifactId>
             <version>3.0.0</version>

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java Mon Jun  1 16:24:13 2015
@@ -54,34 +54,32 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.ReferencePolicyOption;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.adapter.Adaptable;
 import org.apache.sling.api.adapter.AdapterFactory;
-import org.apache.sling.api.resource.Resource;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.commons.osgi.ServiceUtil;
 import org.apache.sling.models.annotations.Model;
 import org.apache.sling.models.annotations.ValidationStrategy;
 import org.apache.sling.models.factory.InvalidAdaptableException;
-import org.apache.sling.models.factory.InvalidResourceException;
-import org.apache.sling.models.factory.InvalidValidationModelException;
+import org.apache.sling.models.factory.InvalidModelException;
 import org.apache.sling.models.factory.MissingElementException;
 import org.apache.sling.models.factory.MissingElementsException;
 import org.apache.sling.models.factory.ModelClassException;
 import org.apache.sling.models.factory.ModelFactory;
 import org.apache.sling.models.factory.PostConstructException;
+import org.apache.sling.models.factory.ValidationException;
 import org.apache.sling.models.impl.model.ConstructorParameter;
 import org.apache.sling.models.impl.model.InjectableElement;
 import org.apache.sling.models.impl.model.InjectableField;
 import org.apache.sling.models.impl.model.InjectableMethod;
 import org.apache.sling.models.impl.model.ModelClass;
 import org.apache.sling.models.impl.model.ModelClassConstructor;
-import org.apache.sling.models.impl.validation.ModelValidation;
 import org.apache.sling.models.spi.AcceptsNullName;
 import org.apache.sling.models.spi.DisposalCallback;
 import org.apache.sling.models.spi.DisposalCallbackRegistry;
 import org.apache.sling.models.spi.ImplementationPicker;
 import org.apache.sling.models.spi.Injector;
+import org.apache.sling.models.spi.ModelValidation;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessor;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2;
@@ -167,6 +165,7 @@ public class ModelAdapterFactory impleme
             cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
     private final @Nonnull Map<Object, ImplementationPicker> implementationPickers = new TreeMap<Object, ImplementationPicker>();
     
+    // bind the service with the highest priority (if a new one comes in this service gets restarted)
     @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policyOption=ReferencePolicyOption.GREEDY)
     private ModelValidation modelValidation;
 
@@ -198,7 +197,7 @@ public class ModelAdapterFactory impleme
 
     @Override
     public @Nonnull <ModelType> ModelType createModel(@Nonnull Object adaptable, @Nonnull Class<ModelType> type) throws MissingElementsException,
-            InvalidAdaptableException, InvalidValidationModelException, InvalidResourceException {
+            InvalidAdaptableException, ValidationException, InvalidModelException {
         if (adaptable == null) {
             throw new IllegalArgumentException("Given adaptable is null!");
         }
@@ -288,10 +287,10 @@ public class ModelAdapterFactory impleme
                 }
             }
             if (!isAdaptable) {
-            	String msg = String.format("Adaptables (%s) are not acceptable for the model class: %s", StringUtils.join(declaredAdaptable), modelClass.getType());
-            	return new Result<ModelType>(new InvalidAdaptableException(msg)); 
+                String msg = String.format("Adaptables (%s) are not acceptable for the model class: %s", StringUtils.join(declaredAdaptable), modelClass.getType());
+                return new Result<ModelType>(new InvalidAdaptableException(msg)); 
             } else {
-                RuntimeException t = validateModel(modelAnnotation, adaptable);
+                RuntimeException t = validateModel(adaptable, modelClass.getType(), modelAnnotation);
                 if (t != null) {
                     return new Result<ModelType>(t);
                 }
@@ -318,22 +317,12 @@ public class ModelAdapterFactory impleme
         }
     }
     
-    private RuntimeException validateModel(Model modelAnnotation, Object adaptable) {
+    private <ModelType> RuntimeException validateModel(Object adaptable, Class<ModelType> modelType, Model modelAnnotation) {
         if (modelAnnotation.validation() != ValidationStrategy.DISABLED) {
             if (modelValidation == null) {
-                return new ModelClassException("Sling Validation Bundle is not there, therefore no validation can be performed.");
-            }
-            Resource resource = null;
-            if (adaptable instanceof SlingHttpServletRequest) {
-                resource = ((SlingHttpServletRequest)adaptable).getResource();
-            } else if (adaptable instanceof Resource) {
-                resource = (Resource)adaptable;
-            }
-            if (resource != null) {
-                return modelValidation.validate(resource, modelAnnotation.validation() == ValidationStrategy.REQUIRED);
-            } else {
-                return new ModelClassException("Sling Validation can only be performed if model is adaptable from either SlingHttpServletRequest or Resource.");
+                return new ValidationException("No active service for ModelValidation found, therefore no validation can be performed.");
             }
+            return modelValidation.validate(adaptable, modelType, modelAnnotation.validation() == ValidationStrategy.REQUIRED);
         }
         return null;
     }

Propchange: sling/trunk/bundles/extensions/models/validation-impl/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Jun  1 16:24:13 2015
@@ -0,0 +1 @@
+target

Added: sling/trunk/bundles/extensions/models/validation-impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/pom.xml?rev=1682946&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/pom.xml (added)
+++ sling/trunk/bundles/extensions/models/validation-impl/pom.xml Mon Jun  1 16:24:13 2015
@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>22</version>
+        <relativePath/>
+    </parent>
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.models.validation-impl</artifactId>
+    <packaging>bundle</packaging>
+    <version>1.0.0-SNAPSHOT</version>
+    <name>Apache Sling Models Validation Implementation</name>
+    <description>Validation Implementation which leverages Sling Validation</description>
+    <properties>
+        <sling.java.version>6</sling.java.version>
+    </properties>
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/validation-impl</connection>
+        <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/validation-impl</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl</url>
+    </scm>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.models.api</artifactId>
+            <version>1.1.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-lang</groupId>
+            <artifactId>commons-lang</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <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>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.findbugs</groupId>
+            <artifactId>jsr305</artifactId>
+            <version>3.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

Added: sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java?rev=1682946&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java (added)
+++ sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java Mon Jun  1 16:24:13 2015
@@ -0,0 +1,69 @@
+/*
+ * 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.validation;
+
+import java.util.List;
+import java.util.Map.Entry;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.sling.models.factory.InvalidModelException;
+import org.apache.sling.validation.api.ValidationResult;
+
+/**
+ * Exception embedding a ValidationResult from Sling Validation.
+ *
+ */
+public class InvalidResourceException extends InvalidModelException {
+
+    /**
+     * 
+     */
+    private static final long serialVersionUID = -5134512515466089688L;
+    private final ValidationResult result;
+    private final String path;
+    
+    public InvalidResourceException(String message, ValidationResult result, String path) {
+        super(message);
+        this.result = result;
+        this.path = path;
+    }
+    /**
+     * 
+     * @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();
+    }
+}

Added: sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java?rev=1682946&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java (added)
+++ sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java Mon Jun  1 16:24:13 2015
@@ -0,0 +1,99 @@
+/*
+ * 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.validation.impl;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.factory.InvalidModelException;
+import org.apache.sling.models.factory.ValidationException;
+import org.apache.sling.models.spi.ModelValidation;
+import org.apache.sling.models.validation.InvalidResourceException;
+import org.apache.sling.validation.api.ValidationModel;
+import org.apache.sling.validation.api.ValidationResult;
+import org.apache.sling.validation.api.ValidationService;
+import org.apache.sling.validation.api.exceptions.SlingValidationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides a validation service for Sling Models based on Sling Validation.
+ * It enforces a validation of the resource which is adapted to the model.
+ * @see <a href="http://sling.apache.org/documentation/bundles/validation.html">Sling Validation</a>
+ */
+@Service
+@Component
+public class ModelValidationImpl implements ModelValidation {
+
+    @Reference
+    private ValidationService validation;
+    
+    private static final Logger log = LoggerFactory.getLogger(ModelValidationImpl.class);
+    
+    /**
+     * Triggers validation for the given model on the given adaptable. Instead of the generic 
+     * @param adaptable {@inheritDoc}
+     * @param modelClass {@inheritDoc}
+     * @param required {@inheritDoc}
+     * @return a {@link ValidationException} in case validation could not be performed if validation model could not be performed but 
+     * required=true or if the adaptable is neither a Resource nor a SlingHttpServletRequest.
+     * Or a {@link InvalidResourceException} in case the given resource (in the adaptable) could not be validated through the {@link ModelValidation}.
+     */
+    public <ModelType> RuntimeException validate(Object adaptable, Class<ModelType> modelClass, boolean required) throws ValidationException, InvalidModelException {
+        Resource resource = null;
+        if (adaptable instanceof SlingHttpServletRequest) {
+            resource = ((SlingHttpServletRequest)adaptable).getResource();
+        } else if (adaptable instanceof Resource) {
+            resource = (Resource)adaptable;
+        }
+        if (resource != null) {
+            return validate(resource, required);
+        } else {
+            return new ValidationException("Sling Validation can only be performed if model is adapted from either SlingHttpServletRequest or Resource.");
+        }
+    }
+    
+    private RuntimeException validate(Resource resource, boolean required) {
+        try {
+            ValidationModel validationModel = validation.getValidationModel(resource);
+            if (validationModel == null) {
+                String error = String.format("Could not find validation model for resource '%s' with type '%s'", resource.getPath(), resource.getResourceType());
+                if (required) {
+                    return new ValidationException(error);
+                } else {
+                    log.warn(error);
+                }
+            } else {
+                try {
+                    ValidationResult validationResult = validation.validate(resource, validationModel);
+                    if (!validationResult.isValid()) {
+                        return new InvalidResourceException("Model is invalid", validationResult, resource.getPath());
+                    }
+                } catch (SlingValidationException e) {
+                    return new ValidationException(e);
+                }
+            }
+        } catch (IllegalStateException e) {
+            return new ValidationException(e);
+        }
+        
+        return null;
+    }
+
+}

Modified: sling/trunk/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/pom.xml?rev=1682946&r1=1682945&r2=1682946&view=diff
==============================================================================
--- sling/trunk/pom.xml (original)
+++ sling/trunk/pom.xml Mon Jun  1 16:24:13 2015
@@ -192,6 +192,7 @@
         <module>bundles/extensions/validation</module>
         <module>bundles/extensions/models/api</module>
         <module>bundles/extensions/models/impl</module>
+        <module>bundles/extensions/models/validation-impl</module>
         <module>bundles/extensions/models/integration-tests</module>
         <module>bundles/extensions/i18n</module>
         <module>bundles/extensions/xss</module>