DynamicMediaHelper.java 2.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.dm.delivery.api.ImageDelivery
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.osgi.framework.Bundle
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.BundleReference
 *  org.osgi.framework.ServiceReference
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.commons.util;

import com.adobe.cq.dam.dm.delivery.api.ImageDelivery;
import java.util.Collections;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleReference;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DynamicMediaHelper {
    private static final String DYNAMIC_MEDIA_CONFIG_PATH = "/etc/dam/dynamicmediaconfig";
    private static final String PN_DYNAMIC_MEDIA_ENABLED = "dynamicMediaEnabled";
    private static final Logger LOG = LoggerFactory.getLogger(DynamicMediaHelper.class);

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Deprecated
    public static boolean isDynamicMediaEnabled(ResourceResolver resolver) {
        BundleContext ctx = BundleReference.class.cast(ImageDelivery.class.getClassLoader()).getBundle().getBundleContext();
        ServiceReference serviceReference = ctx.getServiceReference(ImageDelivery.class.getName());
        ImageDelivery service = ImageDelivery.class.cast(ctx.getService(serviceReference));
        try {
            boolean bl = service != null && service.isEnabled();
            return bl;
        }
        finally {
            ctx.ungetService(serviceReference);
        }
    }

    private static ValueMap getDynamicMediaConfigurationProperties(ResourceResolver resolver) {
        ValueMap configProps = null;
        try {
            Resource dmConfigResource = resolver.getResource("/etc/dam/dynamicmediaconfig");
            if (dmConfigResource != null) {
                configProps = (ValueMap)dmConfigResource.adaptTo(ValueMap.class);
            }
        }
        catch (Exception e) {
            LOG.error("Could not read the Dynamic Media configuration from node /etc/dam/dynamicmediaconfig", (Throwable)e);
        }
        if (configProps == null) {
            configProps = new ValueMapDecorator(Collections.emptyMap());
        }
        return configProps;
    }
}