Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/injector extensions - part 2 - last #1670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0abdf75
Utility class for injectors
Dec 27, 2018
173a932
I18n Provider : Added a service to provide I18n values in a convenien…
Dec 27, 2018
95f7cc6
Added a json file to mock the aem context for injectors
Dec 27, 2018
f5b7699
Added aem context for injectors
Dec 27, 2018
90df7b3
I18n injector : Injects I18n values into your sling model.
Dec 27, 2018
0522fa3
Added a JSON injector:
Dec 27, 2018
c6445bb
Hierarchical page property injector: traverses upwards in the page st…
Dec 27, 2018
096017c
Cleaned up the AEMObject injector to remove code duplication.
Dec 27, 2018
a1a955d
Cleaned up the shared value map injector to remove redundant code.
Dec 27, 2018
992d746
Added copyright scripts
Dec 27, 2018
301a31d
Code Climate fixes
Dec 27, 2018
45c8e60
Updated changelog
Dec 27, 2018
d833711
Remove unneeded mockito junit runner
Dec 27, 2018
6c02296
Added guava cache (count based) for the i18nprovider with JMX interface
Dec 27, 2018
61429dc
Put back reverted code
Dec 27, 2018
637ba65
Merge branch 'feature/injector-utils' into feature/shared-valuemap-in…
Dec 27, 2018
7a33ac8
Merge branch 'master' into feature/shared-valuemap-injector-cleanup
Jan 8, 2019
a1ddd4b
Removed JsonInjector - this was renamed to JsonValueMapInjector. Faul…
Jan 8, 2019
85fbc3d
Removed unused I18n reference from AemObjectInjector. Moved to I18nIn…
Jan 8, 2019
f31520d
Added javadoc for public SOURCE property.
Jan 8, 2019
8cf7175
Remove unused I18nProvider from AemObjectInjector
Jan 8, 2019
85dc641
Deleted wrong reflection util reference.
Jan 8, 2019
986f046
Removed jsonannotationprocessorfactory.
Jan 8, 2019
c389f21
Removed unused reflectionutiltest
Jan 8, 2019
68acad6
Updated changelog.md
Jan 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
- #1637 - Add support for bounce address setting in EmailService
- #1654 - Added I18nProvider service to support injectors
- #1648 - Add Smart Tags to XMP Metadata Node Workflow Process
- #1670 - Added @JsonValueMapValue, @I18N, @HierarchicalPageProperty, and improved @AemObject and @SharedValueMapValue.

### Fixed
- #1667 - Refactored the activate methods of all http cache services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@
@Source(AemObject.SOURCE)
public @interface AemObject {

/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "define-objects";
/**
* if set to REQUIRED injection is mandatory, if set to OPTIONAL injection is optional, in case of DEFAULT
* the standard annotations ({@link org.apache.sling.models.annotations.Optional}, {@link org.apache.sling.models.annotations.Required}) are used.
* If even those are not available the default injection strategy defined on the {@link org.apache.sling.models.annotations.Model} applies.
* Default value = DEFAULT.
*/
public InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;
InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
@Source(HierarchicalPageProperty.SOURCE)
public @interface HierarchicalPageProperty {

/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "hierarchical-page-property";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
@Source(I18N.SOURCE)
public @interface I18N {

/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "i18n";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
@Source(JsonValueMapValue.SOURCE)
public @interface JsonValueMapValue {

/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "json-valuemap-value";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.adobe.acs.commons.models.injectors.annotation;

import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
Expand All @@ -38,11 +39,18 @@
@Target({ METHOD, FIELD, PARAMETER })
@Retention(RUNTIME)
@InjectAnnotation
@Source("shared-component-properties-valuemap")
@Source(SharedValueMapValue.SOURCE)
public @interface SharedValueMapValue {
String name() default "";

public InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;
/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "shared-component-properties-valuemap";

public SharedComponentProperties.ValueTypes type() default SharedComponentProperties.ValueTypes.MERGED;
String name() default StringUtils.EMPTY;

InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;

SharedComponentProperties.ValueTypes type() default SharedComponentProperties.ValueTypes.MERGED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@
*/
public final class AemObjectInjector implements Injector {

@Reference
private I18nProvider i18nProvider;

@Reference
private XSSAPI genericXxsApi;

Expand Down Expand Up @@ -175,7 +172,6 @@ private enum ObjectType {
RESOURCE_DESIGN,
CURRENT_STYLE,
SESSION,
I18N,
XSS_API;

private static final String RESOURCE_PAGE_STRING = "resourcePage";
Expand All @@ -201,8 +197,6 @@ public static ObjectType fromClassAndName(Class<?> classOrGenericParam, String n
return ObjectType.CURRENT_STYLE;
} else if (classOrGenericParam.isAssignableFrom(Session.class)) {
return ObjectType.SESSION;
} else if (classOrGenericParam.isAssignableFrom(I18n.class)) {
return ObjectType.I18N;
} else if (classOrGenericParam.isAssignableFrom(XSSAPI.class)) {
return ObjectType.XSS_API;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
*/
package com.adobe.acs.commons.models.injectors.impl;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.sling.api.SlingHttpServletRequest;
import com.adobe.acs.commons.models.injectors.annotation.SharedValueMapValue;
import com.adobe.acs.commons.util.impl.ReflectionUtil;
import com.adobe.acs.commons.wcm.PageRootProvider;
import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
Expand All @@ -39,11 +34,12 @@
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.adobe.acs.commons.models.injectors.annotation.SharedValueMapValue;
import com.adobe.acs.commons.wcm.PageRootProvider;
import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.Page;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getResource;

@Component(service=Injector.class, property= {Constants.SERVICE_RANKING +":Integer=4500"})
public class SharedValueMapValueInjector implements Injector {
Expand All @@ -53,7 +49,7 @@ public class SharedValueMapValueInjector implements Injector {

@Override
public String getName() {
return "shared-component-properties-valuemap";
return SharedValueMapValue.SOURCE;
}

@Override
Expand All @@ -63,17 +59,7 @@ public Object getValue(Object adaptable, String name, Type declaredType, Annotat
return null;
}

// sanity check
if (!(adaptable instanceof Resource || adaptable instanceof SlingHttpServletRequest)) {
return null;
}

Resource resource = null;
if (adaptable instanceof SlingHttpServletRequest) {
resource = ((SlingHttpServletRequest) adaptable).getResource();
} else if (adaptable instanceof Resource) {
resource = (Resource) adaptable;
}
Resource resource = getResource(adaptable);

if (resource != null) {
Page pageRoot = pageRootProvider.getRootPage(resource);
Expand All @@ -93,7 +79,7 @@ public Object getValue(Object adaptable, String name, Type declaredType, Annotat
break;
}
if (valueMap != null) {
return getValueMapValue(valueMap, name, declaredType);
return ReflectionUtil.convertValueMapValue(valueMap, name, declaredType);
}
}
}
Expand Down Expand Up @@ -132,28 +118,4 @@ protected ValueMap getMergedProperties(Page pageRoot, Resource resource) {
return new ValueMapDecorator(mergedProperties);
}

/**
* Get the property value from the value map.
*
* This function has special logic to handle parameterized types
* such as List<?> which can be addapted to from array properties.
*/
protected Object getValueMapValue(ValueMap valueMap, String name, Type type) {
if (type instanceof Class) {
return valueMap.get(name, (Class) type);
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getActualTypeArguments().length == 1) {
Class collectionType = (Class) parameterizedType.getRawType();
if (collectionType.equals(Collection.class) || collectionType.equals(List.class)) {
Class itemType = (Class) parameterizedType.getActualTypeArguments()[0];
Object valuesArray = valueMap.get(name, Array.newInstance(itemType, 0).getClass());
if (valuesArray != null) {
return Arrays.asList((Object[]) valuesArray);
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.adobe.acs.commons.models.injectors.impl;

import com.adobe.acs.commons.models.injectors.annotation.impl.SharedValueMapValueAnnotationProcessorFactory;
import com.adobe.acs.commons.models.injectors.impl.model.impl.SharedValueMapValueTestModel;
import com.adobe.acs.commons.wcm.impl.PageRootProviderConfig;
import com.adobe.acs.commons.wcm.impl.PageRootProviderMultiImpl;
import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties;
Expand All @@ -38,7 +39,6 @@
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import java.util.Arrays;
import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* limitations under the License.
* #L%
*/
package com.adobe.acs.commons.models.injectors.impl;
package com.adobe.acs.commons.models.injectors.impl.model.impl;

import com.adobe.acs.commons.models.injectors.annotation.SharedValueMapValue;
import com.adobe.acs.commons.models.injectors.impl.SharedValueMapValueInjectorTest;
import com.adobe.acs.commons.wcm.PageRootProvider;
import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties;
import org.apache.sling.api.SlingHttpServletRequest;
Expand Down Expand Up @@ -181,4 +182,4 @@ public List<Long> getLongListPropFromNonArray() {
return longListPropFromNonArray;
}

}
}