Author: justin Date: Mon Sep 18 13:18:04 2017 New Revision: 1808699 URL: http://svn.apache.org/viewvc?rev=1808699&view=rev Log: SLING-7124 - allow PostConstruct method to return a boolean to indicate a failure
Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/TruePostConstuctModel.java Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/Result.java sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java 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=1808699&r1=1808698&r2=1808699&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 Sep 18 13:18:04 2017 @@ -610,7 +610,7 @@ public class ModelAdapterFactory impleme final Map<ValuePreparer, Object> preparedValues = new HashMap<>(VALUE_PREPARERS_COUNT); - final ModelType object; + ModelType object; if (constructorToUse.getConstructor().getParameterTypes().length == 0) { // no parameters for constructor injection? instantiate it right away object = constructorToUse.getConstructor().newInstance(); @@ -656,7 +656,7 @@ public class ModelAdapterFactory impleme return new Result<>(missingElements); } try { - invokePostConstruct(object); + object = invokePostConstruct(object); } catch (InvocationTargetException e) { return new Result<>(new PostConstructException("Post-construct method has thrown an exception for model " + modelClass.getType(), e.getCause())); } catch (IllegalAccessException e) { @@ -830,7 +830,7 @@ public class ModelAdapterFactory impleme return true; } - private void invokePostConstruct(Object object) throws InvocationTargetException, IllegalAccessException { + private <ModelType> ModelType invokePostConstruct(ModelType object) throws InvocationTargetException, IllegalAccessException { Class<?> clazz = object.getClass(); List<Method> postConstructMethods = new ArrayList<>(); while (clazz != null) { @@ -849,13 +849,18 @@ public class ModelAdapterFactory impleme if (!accessible) { method.setAccessible(true); } - method.invoke(object); + Object result = method.invoke(object); + if (result instanceof Boolean && !((Boolean) result).booleanValue()) { + log.debug("PostConstruct method {}.{} returned false. Returning null model.", method.getDeclaringClass().getName(), method.getName()); + return null; + } } finally { if (!accessible) { method.setAccessible(false); } } } + return object; } private RuntimeException setField(InjectableField injectableField, Object createdObject, Object value) { Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/Result.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/Result.java?rev=1808699&r1=1808698&r2=1808699&view=diff ============================================================================== --- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/Result.java (original) +++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/Result.java Mon Sep 18 13:18:04 2017 @@ -19,7 +19,6 @@ package org.apache.sling.models.impl; import javax.annotation.Nonnull; - /** * This class encapsulates a value of a generic class in case of success or the * {@link RuntimeException}s in case of an error. It is used because the @@ -59,7 +58,7 @@ public class Result<SuccessObjectType> { */ public @Nonnull RuntimeException getThrowable() { if (t == null) { - throw new IllegalStateException("No throwable available"); + return new IllegalStateException("No throwable available"); } return t; } @@ -87,4 +86,5 @@ public class Result<SuccessObjectType> { return object != null; } + } 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=1808699&r1=1808698&r2=1808699&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 Sep 18 13:18:04 2017 @@ -18,6 +18,7 @@ package org.apache.sling.models.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; @@ -26,8 +27,10 @@ import java.util.Hashtable; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.factory.PostConstructException; import org.apache.sling.models.testmodels.classes.FailingPostConstuctModel; +import org.apache.sling.models.testmodels.classes.FalsePostConstuctModel; import org.apache.sling.models.testmodels.classes.SubClass; import org.apache.sling.models.testmodels.classes.SubClassOverriddenPostConstruct; +import org.apache.sling.models.testmodels.classes.TruePostConstuctModel; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -56,7 +59,7 @@ public class PostConstructTest { when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>()); factory.activate(componentCtx); // no injectors are necessary - factory.adapterImplementations.addClassesAsAdapterAndImplementation(SubClass.class, SubClassOverriddenPostConstruct.class, FailingPostConstuctModel.class); + factory.adapterImplementations.addClassesAsAdapterAndImplementation(SubClass.class, SubClassOverriddenPostConstruct.class, FailingPostConstuctModel.class, FalsePostConstuctModel.class, TruePostConstuctModel.class); } @Test @@ -80,6 +83,29 @@ public class PostConstructTest { } @Test + public void testPostConstructMethodWhichReturnsFalse() { + FalsePostConstuctModel model = factory.getAdapter(resource, FalsePostConstuctModel.class); + assertNull(model); + } + + @Test + public void testPostConstructMethodWhichReturnsTrue() { + TruePostConstuctModel model = factory.getAdapter(resource, TruePostConstuctModel.class); + assertNotNull(model); + } + + @Test(expected = IllegalStateException.class) + public void testPostConstructMethodWhichReturnsFalseCreateModel() { + FalsePostConstuctModel model = factory.createModel(resource, FalsePostConstuctModel.class); + } + + @Test + public void testPostConstructMethodWhichReturnsTrueCreateModel() { + TruePostConstuctModel model = factory.createModel(resource, TruePostConstuctModel.class); + assertNotNull(model); + } + + @Test public void testPostConstructMethodWhichThrowsExceptionThrowingException() { boolean thrown = false; try { Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java?rev=1808699&view=auto ============================================================================== --- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java (added) +++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java Mon Sep 18 13:18:04 2017 @@ -0,0 +1,34 @@ +/* + * 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.testmodels.classes; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.models.annotations.Model; + +import javax.annotation.PostConstruct; + +@Model(adaptables=Resource.class) +public class FalsePostConstuctModel { + + @PostConstruct + protected boolean pc() throws Exception { + return false; + } + +} Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/TruePostConstuctModel.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/TruePostConstuctModel.java?rev=1808699&view=auto ============================================================================== --- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/TruePostConstuctModel.java (added) +++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/TruePostConstuctModel.java Mon Sep 18 13:18:04 2017 @@ -0,0 +1,34 @@ +/* + * 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.testmodels.classes; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.models.annotations.Model; + +import javax.annotation.PostConstruct; + +@Model(adaptables=Resource.class) +public class TruePostConstuctModel { + + @PostConstruct + protected boolean pc() throws Exception { + return true; + } + +}