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 09:21:28 UTC
svn commit: r1682826 - in /sling/trunk/bundles/extensions/models:
api/src/main/java/org/apache/sling/models/factory/
impl/src/main/java/org/apache/sling/models/impl/
impl/src/test/java/org/apache/sling/models/impl/
Author: kwin
Date: Mon Jun 1 07:21:27 2015
New Revision: 1682826
URL: http://svn.apache.org/r1682826
Log:
SLING-4767
Added:
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/PostConstructException.java
Modified:
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelClassException.java
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelFactory.java
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
Modified: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelClassException.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelClassException.java?rev=1682826&r1=1682825&r2=1682826&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelClassException.java (original)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/ModelClassException.java Mon Jun 1 07:21:27 2015
@@ -25,7 +25,7 @@ package org.apache.sling.models.factory;
*
* @see ModelFactory
*/
-public final class ModelClassException extends RuntimeException {
+public class ModelClassException extends RuntimeException {
private static final long serialVersionUID = 4323592065808565135L;
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=1682826&r1=1682825&r2=1682826&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 07:21:27 2015
@@ -34,12 +34,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 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 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
*/
public @Nonnull <ModelType> ModelType createModel(@Nonnull Object adaptable, @Nonnull Class<ModelType> type) throws MissingElementsException,
- InvalidAdaptableException, ModelClassException, InvalidValidationModelException, InvalidResourceException;
+ InvalidAdaptableException, ModelClassException, PostConstructException, InvalidValidationModelException, InvalidResourceException;
/**
*
Added: sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/PostConstructException.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/PostConstructException.java?rev=1682826&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/PostConstructException.java (added)
+++ sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/factory/PostConstructException.java Mon Jun 1 07:21:27 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.factory;
+
+/**
+ * Exception which is triggered, whenever the post-construct method has thrown an exception.
+ */
+public class PostConstructException extends ModelClassException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -2527043835215727726L;
+
+ public PostConstructException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
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=1682826&r1=1682825&r2=1682826&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 07:21:27 2015
@@ -69,6 +69,7 @@ import org.apache.sling.models.factory.M
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.impl.model.ConstructorParameter;
import org.apache.sling.models.impl.model.InjectableElement;
import org.apache.sling.models.impl.model.InjectableField;
@@ -183,7 +184,12 @@ public class ModelAdapterFactory impleme
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
Result<AdapterType> result = internalCreateModel(adaptable, type);
if (!result.wasSuccessfull()) {
- log.error("Could not adapt to model", result.getThrowable());
+ // treat post-construct exception differently, because they are sometimes used for flow-control or validation purposes
+ if (result.getThrowable() instanceof PostConstructException) {
+ log.debug("Could not adapt to model", result.getThrowable());
+ } else {
+ log.error("Could not adapt to model", result.getThrowable());
+ }
return null;
} else {
return result.getValue();
@@ -435,7 +441,12 @@ public class ModelAdapterFactory impleme
if (defaultInjectionResult.wasSuccessfull()) {
// log previous injection error, if there was any
if (lastInjectionException != null) {
- log.warn("Although falling back to default value worked, injection into {} failed because of: " + lastInjectionException.getMessage(), element.getAnnotatedElement(), lastInjectionException);
+ // treat post-construct exception differently, because they are sometimes used for flow-control or validation purposes
+ if (lastInjectionException instanceof PostConstructException) {
+ log.debug("Although falling back to default value worked, injection into {} failed because of: " + lastInjectionException.getMessage(), element.getAnnotatedElement(), lastInjectionException);
+ } else {
+ log.warn("Although falling back to default value worked, injection into {} failed because of: " + lastInjectionException.getMessage(), element.getAnnotatedElement(), lastInjectionException);
+ }
}
wasInjectionSuccessful = defaultInjectionResult.getValue();
} else {
@@ -547,7 +558,7 @@ public class ModelAdapterFactory impleme
try {
invokePostConstruct(object);
} catch (InvocationTargetException e) {
- return new Result<ModelType>(new ModelClassException("Post-construct method has thrown an exception for model " + modelClass.getType(), e));
+ return new Result<ModelType>(new PostConstructException("Post-construct method has thrown an exception for model " + modelClass.getType(), e.getCause()));
} catch (IllegalAccessException e) {
new Result<ModelType>(new ModelClassException("Could not call post-construct method for model " + modelClass.getType(), e));
}
Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java?rev=1682826&r1=1682825&r2=1682826&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java Mon Jun 1 07:21:27 2015
@@ -23,6 +23,7 @@ import java.util.Hashtable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelClassException;
+import org.apache.sling.models.factory.PostConstructException;
import org.apache.sling.models.testmodels.classes.FailingPostConstuctModel;
import org.apache.sling.models.testmodels.classes.SubClass;
import org.apache.sling.models.testmodels.classes.SubClassOverriddenPostConstruct;
@@ -81,9 +82,9 @@ public class PostConstructTest {
boolean thrown = false;
try {
factory.createModel(resource, FailingPostConstuctModel.class);
- } catch (ModelClassException e) {
+ } catch (PostConstructException e) {
assertTrue(e.getMessage().contains("Post-construct"));
- assertEquals("FAIL", e.getCause().getCause().getMessage());
+ assertEquals("FAIL", e.getCause().getMessage());
thrown = true;
}
assertTrue(thrown);