Skip to content

Commit

Permalink
Introduced DialogProvider annotation as an alternative for models tha…
Browse files Browse the repository at this point in the history
…t don't need the extra stuff (backwards compatible with using GeneratedDialog as a base class)
  • Loading branch information
badvision committed May 30, 2019
1 parent 73bb3c6 commit c7bf04d
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Adobe.
*
* Licensed 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 com.adobe.acs.commons.mcp.form;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Signifies a bean which gets an automatically-generated dialog
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DialogProvider {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.osgi.annotation.versioning.ProviderType;

/**
* Generates a dialog out of @FormField annotations
* Ideally your sling model should extend this class to inherit its features
* but you can also just use the @DialogProvider annotation
*/
@Model(
adaptables = {SlingHttpServletRequest.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@DialogProvider
public class GeneratedDialog {
@Inject
@JsonIgnore
Expand All @@ -58,7 +59,7 @@ public class GeneratedDialog {
private FormComponent form;

@JsonIgnore
Map<String, FieldComponent> fieldComponents;
protected Map<String, FieldComponent> fieldComponents;

@PostConstruct
public void init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019 Adobe.
*
* Licensed 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 com.adobe.acs.commons.mcp.form.impl;

import com.adobe.acs.commons.mcp.form.FieldComponent;
import com.adobe.acs.commons.mcp.form.GeneratedDialog;
import com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer;
import java.util.Map;

/**
* Provides a generated dialog for annotated model classes that do not already extend GeneratedDialog
*/
public class GeneratedDialogWrapper extends GeneratedDialog {
Class wrappedClass;

public GeneratedDialogWrapper(Class c) {
wrappedClass = c;
}

@Override
public Map<String, FieldComponent> getFieldComponents() {
if (fieldComponents == null) {
fieldComponents = AnnotatedFieldDeserializer.getFormFields(wrappedClass, getSlingHelper());
}
return fieldComponents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import com.adobe.acs.commons.mcp.DialogResourceProviderConfiguration;
import com.adobe.acs.commons.mcp.DialogResourceProviderFactory;
import com.adobe.acs.commons.mcp.form.GeneratedDialog;
import com.adobe.acs.commons.mcp.form.DialogProvider;
import java.util.*;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.ClassUtils;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.spi.resource.provider.ResourceProvider;
import org.osgi.framework.Bundle;
Expand All @@ -45,7 +47,7 @@ public class DialogResourceProviderFactoryImpl implements DialogResourceProvider
private final Map<String, ServiceRegistration<ResourceProvider>> resourceProviderRegistrations
= Collections.synchronizedMap(new HashMap<>());
private final Set<String> allKnownModels = Collections.synchronizedSet(new HashSet<>());
private String[] ignoredPackages = new String[]{
private final String[] ignoredPackages = new String[]{
"com.adobe.cq.",
"com.adobe.aemds.",
"com.adobe.fd.ccm.",
Expand All @@ -68,6 +70,7 @@ public class DialogResourceProviderFactoryImpl implements DialogResourceProvider
volatile BundleContext bundleContext;

private boolean enabled = false;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ public void registerClass(String className) {
@Override
public void registerClass(Class c) {
allKnownModels.add(c.getName());
if (isEnabled() && GeneratedDialog.class.isAssignableFrom(c)) {
if (isEnabled() && isDialogProvider(c)) {
unregisterClass(c);
DialogResourceProviderImpl provider = null;
try {
Expand Down Expand Up @@ -181,4 +184,12 @@ public void unregisterClass(String c) {
public Map<String, ServiceRegistration<ResourceProvider>> getActiveProviders() {
return Collections.unmodifiableMap(resourceProviderRegistrations);
}
}

private boolean isDialogProvider(Class c) {
return c.isAnnotationPresent(DialogProvider.class)
|| StreamSupport.stream(ClassUtils.hierarchy(c, ClassUtils.Interfaces.INCLUDE).spliterator(), false)
.filter(clazz -> clazz.isAnnotationPresent(DialogProvider.class))
.findFirst()
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.adobe.acs.commons.mcp.form.AbstractResourceImpl;
import com.adobe.acs.commons.mcp.form.GeneratedDialog;
import com.adobe.acs.commons.mcp.form.impl.GeneratedDialogWrapper;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -44,9 +45,15 @@ public class DialogResourceProviderImpl extends ResourceProvider {
private String root = "";
private GeneratedDialog dialog;
private AbstractResourceImpl resource;
private Class originalClass;

public DialogResourceProviderImpl(Class<? extends GeneratedDialog> c) throws InstantiationException, IllegalAccessException {
dialog = c.newInstance();
public DialogResourceProviderImpl(Class c) throws InstantiationException, IllegalAccessException {
originalClass = c;
if (GeneratedDialog.class.isAssignableFrom(c)) {
dialog = (GeneratedDialog) c.newInstance();
} else {
dialog = new GeneratedDialogWrapper(c);
}
setResourceTypeFromClass();
root = "/apps/" + resourceType + "/cq:dialog";
resource = (AbstractResourceImpl) dialog.getFormResource();
Expand All @@ -60,7 +67,7 @@ public DialogResourceProviderImpl(Class<? extends GeneratedDialog> c) throws Ins
}

private void setResourceTypeFromClass() throws IllegalAccessException, InstantiationException {
Model modelAnnotation = dialog.getClass().getAnnotation(Model.class);
Model modelAnnotation = (Model) originalClass.getAnnotation(Model.class);
// Use the model annotation for resource type if possible
if (modelAnnotation != null
&& modelAnnotation.resourceType() != null
Expand All @@ -69,17 +76,17 @@ private void setResourceTypeFromClass() throws IllegalAccessException, Instantia
} else {
// Last-ditch effort is hope that there's a java bean property for it
try {
Method getter = MethodUtils.getMatchingAccessibleMethod(dialog.getClass(), "getResourceType", new Class[]{});
Method getter = MethodUtils.getMatchingAccessibleMethod(originalClass, "getResourceType", new Class[]{});
if (getter != null) {
resourceType = String.valueOf(getter.invoke(dialog));
resourceType = String.valueOf(getter.invoke(originalClass.newInstance()));
} else {
Field field = FieldUtils.getField(dialog.getClass(), "resourceType", true);
Field field = FieldUtils.getField(originalClass, "resourceType", true);
if (field != null) {
resourceType = String.valueOf(field.get(dialog));
resourceType = String.valueOf(field.get(originalClass.newInstance()));
}
}
} catch (InvocationTargetException | IllegalAccessException ex) {
LOGGER.debug("Unable to determine sling resource type for model bean: " + dialog.getClass());
LOGGER.debug("Unable to determine sling resource type for model bean: " + originalClass);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.adobe.acs.commons.mcp.model.SimpleModelThree;
import com.adobe.acs.commons.mcp.model.SimpleModelTwo;
import java.io.IOException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.*;
Expand Down Expand Up @@ -84,7 +83,6 @@ public void testResourceResolution() {
}

private boolean resourceExists(String path) {
Resource res = slingContext.resourceResolver().getResource(path);
return res != null;
return slingContext.resourceResolver().getResource(path) != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

/**
* Simple sling model with a resource type declared in the model annotation directly.
* This extends GeneratedDialog, so should be detected as a dialog provider.
*/
@Model(adaptables = {Resource.class, SlingHttpServletRequest.class},
resourceType = "test/model1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.adobe.acs.commons.mcp.model;

import com.adobe.acs.commons.mcp.form.DialogProvider;
import com.adobe.acs.commons.mcp.form.FormField;
import com.adobe.acs.commons.mcp.form.GeneratedDialog;
import org.apache.sling.api.SlingHttpServletRequest;
Expand All @@ -27,7 +28,9 @@

/**
* Simple sling model with a resource type declared via an internal variable.
* Uses both dialog provider annotation and extends Generated Dialog.
*/
@DialogProvider
@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class SimpleModelThree extends GeneratedDialog {
public String resourceType = "test/model3";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
*/
package com.adobe.acs.commons.mcp.model;

import com.adobe.acs.commons.mcp.form.DialogProvider;
import com.adobe.acs.commons.mcp.form.FormField;
import com.adobe.acs.commons.mcp.form.GeneratedDialog;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

/**
* Simple sling model with a resource type declared via a getter function.
* This should be detected via the DialogProvider annotation.
*/
@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class SimpleModelTwo extends GeneratedDialog {
@DialogProvider
public class SimpleModelTwo {
@FormField(name = "Field 1")
private String field1;

Expand Down

0 comments on commit c7bf04d

Please sign in to comment.