SLING-4161 move validation to separate bundle
authorKonrad Windszus <kwin@apache.org>
Mon, 1 Jun 2015 16:24:13 +0000 (16:24 +0000)
committerKonrad Windszus <kwin@apache.org>
Mon, 1 Jun 2015 16:24:13 +0000 (16:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1682946 13f79535-47bb-0310-9956-ffa450edef68

pom.xml
src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
src/main/java/org/apache/sling/models/impl/validation/ModelValidation.java [deleted file]
src/main/java/org/apache/sling/models/impl/validation/ModelValidationImpl.java [deleted file]

diff --git a/pom.xml b/pom.xml
index 981e5d8..eb77ebc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -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>
             <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>
index 0c0c6f0..b161cca 100644 (file)
@@ -54,34 +54,32 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
 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 implements AdapterFactory, Runnable, ModelFacto
             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 implements AdapterFactory, Runnable, ModelFacto
 
     @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 implements AdapterFactory, Runnable, ModelFacto
                 }
             }
             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 implements AdapterFactory, Runnable, ModelFacto
         }
     }
     
-    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;
     }
diff --git a/src/main/java/org/apache/sling/models/impl/validation/ModelValidation.java b/src/main/java/org/apache/sling/models/impl/validation/ModelValidation.java
deleted file mode 100644 (file)
index 163bf81..0000000
+++ /dev/null
@@ -1,31 +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.impl.validation;
-
-import org.apache.sling.api.resource.Resource;
-
-public interface ModelValidation {
-
-    /**
-     * Calls the Sling Validation for the given resource.
-     * @param resource
-     * @param required if {@code true} validation fails even if no appropriate validation model could be found.
-     * @return {@code null} if validation was successful, otherwise an exception
-     */
-    public abstract RuntimeException validate(Resource resource, boolean required);
-
-}
diff --git a/src/main/java/org/apache/sling/models/impl/validation/ModelValidationImpl.java b/src/main/java/org/apache/sling/models/impl/validation/ModelValidationImpl.java
deleted file mode 100644 (file)
index 2255cba..0000000
+++ /dev/null
@@ -1,70 +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.impl.validation;
-
-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.resource.Resource;
-import org.apache.sling.models.factory.InvalidResourceException;
-import org.apache.sling.models.factory.InvalidValidationModelException;
-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;
-
-@Service
-@Component
-public class ModelValidationImpl implements ModelValidation {
-
-    @Reference
-    private ValidationService validation;
-    
-    private static final Logger log = LoggerFactory.getLogger(ModelValidationImpl.class);
-    
-    @Override
-    public 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 InvalidValidationModelException(error);
-                } else {
-                    log.warn(error);
-                }
-            } else {
-                try {
-                    ValidationResult validationResult = validation.validate(resource, validationModel);
-                    if (!validationResult.isValid()) {
-                        return new InvalidResourceException(validationResult, resource.getPath());
-                    } 
-                } catch (SlingValidationException e) {
-                    return new InvalidValidationModelException(e);
-                }
-            }
-        } catch (IllegalStateException e) {
-            return new InvalidValidationModelException(e);
-        }
-        
-        return null;
-    }
-
-    
-}