Search in sources :

Example 1 with AdapterFactory

use of org.apache.sling.api.adapter.AdapterFactory in project sling by apache.

the class SlingContextImpl method registerAdapter.

/**
     * Create a Sling AdapterFactory on the fly which can adapt from <code>adaptableClass</code>
     * to <code>adapterClass</code> and delegates the adapter mapping to the given <code>adaptHandler</code> function.
     * @param adaptableClass Class to adapt from
     * @param adapterClass Class to adapt to
     * @param adaptHandler Function to handle the adaption
     * @param <T1> Adaptable type
     * @param <T2> Adapter type
     */
public final <T1, T2> void registerAdapter(final Class<T1> adaptableClass, final Class<T2> adapterClass, final Function<T1, T2> adaptHandler) {
    AdapterFactory adapterFactory = new AdapterFactory() {

        @SuppressWarnings("unchecked")
        @Override
        public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
            return (AdapterType) adaptHandler.apply((T1) adaptable);
        }
    };
    registerService(AdapterFactory.class, adapterFactory, ImmutableMap.<String, Object>builder().put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { adaptableClass.getName() }).put(AdapterFactory.ADAPTER_CLASSES, new String[] { adapterClass.getName() }).put(Constants.SERVICE_RANKING, Integer.MAX_VALUE).build());
}
Also used : AdapterFactory(org.apache.sling.api.adapter.AdapterFactory) ModelAdapterFactory(org.apache.sling.models.impl.ModelAdapterFactory)

Example 2 with AdapterFactory

use of org.apache.sling.api.adapter.AdapterFactory in project sling by apache.

the class MockAdapterManagerImpl method getAdapter.

// DISABLED IN THIS COPY OF CLASS
/*
    @Reference
    private PackageAdmin packageAdmin;
    */
// ---------- AdapterManager interface -------------------------------------
/**
     * Returns the adapted <code>adaptable</code> or <code>null</code> if
     * the object cannot be adapted.
     *
     * @see org.apache.sling.api.adapter.AdapterManager#getAdapter(java.lang.Object, java.lang.Class)
     */
public <AdapterType> AdapterType getAdapter(final Object adaptable, final Class<AdapterType> type) {
    // get the adapter factories for the type of adaptable object
    final Map<String, List<AdapterFactoryDescriptor>> factories = getAdapterFactories(adaptable.getClass());
    // get the factory for the target type
    final List<AdapterFactoryDescriptor> descList = factories.get(type.getName());
    if (descList != null && descList.size() > 0) {
        for (AdapterFactoryDescriptor desc : descList) {
            final AdapterFactory factory = desc == null ? null : desc.getFactory();
            // have the factory adapt the adaptable if the factory exists
            if (factory != null) {
                log.debug("Trying adapter factory {} to map {} to {}", new Object[] { factory, adaptable, type });
                AdapterType adaptedObject = factory.getAdapter(adaptable, type);
                if (adaptedObject != null) {
                    log.debug("Using adapter factory {} to map {} to {}", new Object[] { factory, adaptable, type });
                    return adaptedObject;
                }
            }
        }
    }
    // no factory has been found, so we cannot adapt
    log.debug("No adapter factory found to map {} to {}", adaptable, type);
    return null;
}
Also used : AdapterFactoryDescriptor(org.apache.sling.adapter.internal.AdapterFactoryDescriptor) AdapterFactory(org.apache.sling.api.adapter.AdapterFactory) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with AdapterFactory

use of org.apache.sling.api.adapter.AdapterFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SocialMediaHelperImplTest method setUp.

@BeforeEach
void setUp() {
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
    context.registerService(AdapterFactory.class, new AdapterFactory() {

        @Override
        @SuppressWarnings("unchecked")
        public <AdapterType> AdapterType getAdapter(@NotNull Object o, @NotNull Class<AdapterType> clazz) {
            Object result = null;
            switch(clazz.getName()) {
                case CLASS_PRODUCT:
                    if (o instanceof Resource) {
                        result = MockCommerceFactory.getProduct((Resource) o);
                    }
                    break;
                case CLASS_COMMERCE_SERVICE:
                    if (o instanceof Resource) {
                        result = MockCommerceFactory.getCommerceService((Resource) o);
                    }
                    break;
                case CLASS_XF_SOCIAL_VARIATION:
                    if (o instanceof Page) {
                        result = MockXFFactory.getExperienceFragmentSocialVariation((Page) o);
                    }
            }
            return (AdapterType) result;
        }
    }, new HashMap<String, Object>() {

        {
            put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { CLASS_RESOURCE, CLASS_PAGE });
            put(AdapterFactory.ADAPTER_CLASSES, new String[] { CLASS_PRODUCT, CLASS_COMMERCE_SERVICE, CLASS_XF_SOCIAL_VARIATION });
        }
    });
}
Also used : AdapterFactory(org.apache.sling.api.adapter.AdapterFactory) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with AdapterFactory

use of org.apache.sling.api.adapter.AdapterFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SocialMediaHelperImplTest method setUp.

@BeforeClass
public static void setUp() {
    CONTEXT.registerService(Externalizer.class, MockExternalizerFactory.getExternalizerService());
    CONTEXT.registerService(AdapterFactory.class, new AdapterFactory() {

        @Override
        public <AdapterType> AdapterType getAdapter(@Nonnull Object o, @Nonnull Class<AdapterType> clazz) {
            Object result = null;
            switch(clazz.getName()) {
                case CLASS_PRODUCT:
                    if (o instanceof Resource) {
                        result = MockCommerceFactory.getProduct((Resource) o);
                    }
                    break;
                case CLASS_COMMERCE_SERVICE:
                    if (o instanceof Resource) {
                        result = MockCommerceFactory.getCommerceService((Resource) o);
                    }
                    break;
                case CLASS_XF_SOCIAL_VARIATION:
                    if (o instanceof Page) {
                        result = MockXFFactory.getExperienceFragmentSocialVariation((Page) o);
                    }
            }
            return (AdapterType) result;
        }
    }, new HashMap<String, Object>() {

        {
            put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { CLASS_RESOURCE, CLASS_PAGE });
            put(AdapterFactory.ADAPTER_CLASSES, new String[] { CLASS_PRODUCT, CLASS_COMMERCE_SERVICE, CLASS_XF_SOCIAL_VARIATION });
        }
    });
}
Also used : AdapterFactory(org.apache.sling.api.adapter.AdapterFactory) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) BeforeClass(org.junit.BeforeClass)

Example 5 with AdapterFactory

use of org.apache.sling.api.adapter.AdapterFactory in project sling by apache.

the class AdapterManagerImpl method getAdapter.

// ---------- AdapterManager interface -------------------------------------
/**
     * Returns the adapted <code>adaptable</code> or <code>null</code> if
     * the object cannot be adapted.
     *
     * @see org.apache.sling.api.adapter.AdapterManager#getAdapter(java.lang.Object, java.lang.Class)
     */
@Override
public <AdapterType> AdapterType getAdapter(final Object adaptable, final Class<AdapterType> type) {
    // get the adapter factories for the type of adaptable object
    final Map<String, List<AdapterFactoryDescriptor>> factories = getAdapterFactories(adaptable.getClass());
    // get the factory for the target type
    final List<AdapterFactoryDescriptor> descList = factories.get(type.getName());
    if (descList != null && descList.size() > 0) {
        for (AdapterFactoryDescriptor desc : descList) {
            final AdapterFactory factory = desc == null ? null : desc.getFactory();
            // have the factory adapt the adaptable if the factory exists
            if (factory != null) {
                log.debug("Trying adapter factory {} to map {} to {}", new Object[] { factory, adaptable, type });
                AdapterType adaptedObject = factory.getAdapter(adaptable, type);
                if (adaptedObject != null) {
                    log.debug("Using adapter factory {} to map {} to {}", new Object[] { factory, adaptable, type });
                    return adaptedObject;
                }
            }
        }
    }
    // no factory has been found, so we cannot adapt
    log.debug("No adapter factory found to map {} to {}", adaptable, type);
    return null;
}
Also used : AdapterFactory(org.apache.sling.api.adapter.AdapterFactory) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

AdapterFactory (org.apache.sling.api.adapter.AdapterFactory)6 Page (com.day.cq.wcm.api.Page)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Resource (org.apache.sling.api.resource.Resource)2 AdapterFactoryDescriptor (org.apache.sling.adapter.internal.AdapterFactoryDescriptor)1 ModelAdapterFactory (org.apache.sling.models.impl.ModelAdapterFactory)1 BeforeClass (org.junit.BeforeClass)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1