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());
}
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;
}
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 });
}
});
}
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 });
}
});
}
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;
}
Aggregations