Skip to content

Commit

Permalink
split the base model adapter factory and the custom implementation (#402
Browse files Browse the repository at this point in the history
)

So that the actual model classes can be separated from the base class. This means the model classes can now be used completely independently from the rest of the source code.
  • Loading branch information
Carlos Munoz authored Sep 8, 2020
1 parent e568787 commit d3dfb13
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.redhat.pantheon.model;

import com.redhat.pantheon.model.api.SlingModelAdapterFactory;
import org.apache.sling.api.adapter.AdapterFactory;
import org.osgi.service.component.annotations.Component;

import static org.apache.sling.api.adapter.AdapterFactory.ADAPTABLE_CLASSES;
import static org.apache.sling.api.adapter.AdapterFactory.ADAPTER_CLASSES;

/**
* Custom {@link SlingModelAdapterFactory} for the Pantheon system. It lists all the
* model classes which will be converted from the adaptTo invocations.
*/
@Component(
name = "Pantheon - SlingModel Adapter Factory",
service = AdapterFactory.class,
property = {
ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.Resource",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.Content",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.Product",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.ProductVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.api.SlingModel",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.api.FileResource",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.Assembly",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.Document",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.Module",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.workspace.Workspace"
}
)
public class CustomModelAdapterFactory extends SlingModelAdapterFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.sling.api.adapter.AdapterFactory.ADAPTABLE_CLASSES;
import static org.apache.sling.api.adapter.AdapterFactory.ADAPTER_CLASSES;

/**
* An adapter factory for model objects of type {@link SlingModel}. This component makes sure that
* calls to {@link Resource#adaptTo(Class)} with a sub-interface of {@link SlingModel} as a parameter
Expand All @@ -21,43 +17,12 @@
*
* @author Carlos Munoz
*/
@Component(
name = "Pantheon - SlingModel Adapter Factory",
service = AdapterFactory.class,
property = {
ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.Resource",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.Content",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.Product",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.ProductVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.api.SlingModel",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.api.FileResource",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.Assembly",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.assembly.AssemblyVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.Document",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.document.DocumentVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.Module",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleLocale",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleMetadata",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariant",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariants",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVersion",
ADAPTER_CLASSES + "=com.redhat.pantheon.model.workspace.Workspace"
}
)
public class SlingModelAdapterFactory implements AdapterFactory {
public abstract class SlingModelAdapterFactory implements AdapterFactory {

private static final Logger log = LoggerFactory.getLogger(SlingModelAdapterFactory.class);

@Override
public <AdapterType> @Nullable AdapterType getAdapter(@NotNull Object adaptable, @NotNull Class<AdapterType> type) {
public final <AdapterType> @Nullable AdapterType getAdapter(@NotNull Object adaptable, @NotNull Class<AdapterType> type) {
// ensure the adapter type is a subclass of SlingResource
if(!SlingModel.class.isAssignableFrom(type)) {
log.error("Error in " + this.getClass().getSimpleName() + ": adapter type not a SlingModel interface ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class SlingModelAdapterFactoryTest {

private final SlingContext slingContext = new SlingContext();

SlingModelAdapterFactory modelAdapterFactory = new SlingModelAdapterFactory();
SlingModelAdapterFactory modelAdapterFactory = new SlingModelAdapterFactory() {
};

@Test
void getAdapter() {
Expand Down

0 comments on commit d3dfb13

Please sign in to comment.