Skip to content

Commit 7a7f83c

Browse files
authored
#3457 allow disabling the ContentPolicyValueInjector (#3549)
* #3457 allow disabling the COntentPolicyValueInjector
1 parent c6b64e2 commit 7a7f83c

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

bundle/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,12 @@
560560
<artifactId>mockito-core</artifactId>
561561
<scope>test</scope>
562562
</dependency>
563+
<dependency>
564+
<groupId>org.mockito</groupId>
565+
<artifactId>mockito-inline</artifactId>
566+
<scope>test</scope>
567+
<version>5.2.0</version>
568+
</dependency>
563569
<dependency>
564570
<groupId>org.mockito</groupId>
565571
<artifactId>mockito-junit-jupiter</artifactId>

bundle/src/main/java/com/adobe/acs/commons/models/injectors/impl/ContentPolicyValueInjector.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
import org.jetbrains.annotations.NotNull;
3535
import org.jetbrains.annotations.Nullable;
3636
import org.osgi.framework.Constants;
37+
import org.osgi.service.component.annotations.Activate;
3738
import org.osgi.service.component.annotations.Component;
39+
import org.osgi.service.metatype.annotations.AttributeDefinition;
40+
import org.osgi.service.metatype.annotations.Designate;
41+
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
3842
import org.slf4j.Logger;
3943
import org.slf4j.LoggerFactory;
4044

@@ -49,9 +53,21 @@
4953
Constants.SERVICE_RANKING + ":Integer=5500"
5054
},
5155
service = Injector.class
52-
)
56+
)
57+
@Designate(ocd=ContentPolicyValueInjector.Configuration.class)
5358
public class ContentPolicyValueInjector implements Injector {
5459

60+
private static final Logger LOG = LoggerFactory.getLogger(ContentPolicyValueInjector.class);
61+
62+
Configuration config;
63+
64+
@Activate
65+
public void activate(Configuration c) {
66+
this.config = c;
67+
LOG.info("ContentPolicyValueInjector {}", config.enabled() ? "enabled": "disabled");
68+
}
69+
70+
5571
@NotNull
5672
@Override
5773
public String getName() {
@@ -61,15 +77,25 @@ public String getName() {
6177
@Nullable
6278
@Override
6379
public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element,
64-
DisposalCallbackRegistry callbackRegistry) {
65-
ContentPolicy policy = getContentPolicy(adaptable);
80+
DisposalCallbackRegistry callbackRegistry) {
81+
if (!config.enabled()) {
82+
return null;
83+
}
6684

85+
ContentPolicy policy = getContentPolicy(adaptable);
6786
if(policy != null){
6887
return convertValueMapValue(policy.getProperties(), name, declaredType);
6988
}
70-
7189
return null;
7290
}
7391

7492

93+
@ObjectClassDefinition(name="ACS AEM Commons ContentPolicyValueInjector")
94+
public @interface Configuration {
95+
96+
@AttributeDefinition(name="enabled")
97+
public boolean enabled() default true;
98+
99+
}
100+
75101
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*-
2+
* #%L
3+
* ACS AEM Commons Bundle
4+
* %%
5+
* Copyright (C) 2013 - 2025 Adobe
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.adobe.acs.commons.models.injectors.impl;
21+
22+
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.Mockito.never;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
29+
import org.junit.Rule;
30+
import org.junit.jupiter.api.Test;
31+
import org.mockito.MockedStatic;
32+
import org.mockito.Mockito;
33+
34+
public class ContentPolicyValueInjectorTest {
35+
36+
@Rule
37+
private OsgiContext context = new OsgiContext();
38+
39+
40+
@Test
41+
public void testDisabledInjector() {
42+
ContentPolicyValueInjector injector = new ContentPolicyValueInjector();
43+
Map<String,String> props = new HashMap<>();
44+
props.put("enabled", "false");
45+
context.registerInjectActivateService(injector, props);
46+
47+
try (MockedStatic<InjectorUtils> injectorUtils = Mockito.mockStatic(InjectorUtils.class)) {
48+
injectorUtils.when(() -> InjectorUtils.getContentPolicy(any()))
49+
.thenReturn(null);
50+
injectorUtils.verify(() -> InjectorUtils.getContentPolicy(any()),never());
51+
}
52+
}
53+
54+
}

0 commit comments

Comments
 (0)