BVPManager.java 8.57 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.component.Deactivate
 *  com.adobe.granite.xss.XSSAPI
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Modified
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.scripting.api.BindingsValuesProvider
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.osgi.framework.ServiceRegistration
 *  org.osgi.service.cm.Configuration
 *  org.osgi.service.cm.ConfigurationAdmin
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.util.tracker.ServiceTracker
 *  org.osgi.util.tracker.ServiceTrackerCustomizer
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.scripting.impl;

import aQute.bnd.annotation.component.Deactivate;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.wcm.scripting.impl.WCMBindingsValuesProvider;
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.scripting.api.BindingsValuesProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(immediate=1, metatype=1, label="Adobe CQ WCM Bindings Manager", description="The Adobe CQ WCM Bindings Manager dynamically registers BindingsValuesProvider services for scripting support.")
@Properties(value={@Property(name="com.day.cq.wcm.scripting.bvp.script.engines", value={"jsp", "sightly"}, label="Scripting engines", description="The scripting engines for which the WCMBindingsValuesProvider should be active.", unbounded=PropertyUnbounded.ARRAY)})
public class BVPManager {
    private static final Logger LOG = LoggerFactory.getLogger(BVPManager.class);
    public static final String SCRIPT_ENGINES = "com.day.cq.wcm.scripting.bvp.script.engines";
    public static final String[] SCRIPT_ENGINES_DEFAULT = new String[]{"jsp", "sightly"};
    private volatile boolean active = false;
    private ServiceTracker serviceTracker;
    private ServiceRegistration bvpServiceRegistration;
    private static final String WCM_CORE_BVP_PID = "com.day.cq.wcm.core.impl.WCMBindingsValuesProvider";
    @Reference
    private XSSAPI xssapi = null;
    @Reference
    private ConfigurationAdmin configurationAdmin = null;

    @Activate
    protected void activate(final ComponentContext componentContext) {
        block3 : {
            String[] scriptEngines = null;
            try {
                Configuration[] configurations = this.configurationAdmin.listConfigurations("(service.pid=" + BVPManager.class.getName() + ")");
                if (configurations != null || (configurations = this.configurationAdmin.listConfigurations("(service.pid=com.day.cq.wcm.core.impl.WCMBindingsValuesProvider)")) == null) break block3;
                for (Configuration c : configurations) {
                    scriptEngines = PropertiesUtil.toStringArray(c.getProperties().get("javax.script.name"), (String[])null);
                    if (scriptEngines == null) continue;
                    Hashtable<String, String[]> bvpManagerProperties = new Hashtable<String, String[]>();
                    bvpManagerProperties.put("com.day.cq.wcm.scripting.bvp.script.engines", scriptEngines);
                    this.updateBVPManagerConfiguration(bvpManagerProperties);
                    break;
                }
            }
            catch (Exception e) {
                LOG.error("Unable to determine if " + BVPManager.class.getName() + " was configured or not.", (Throwable)e);
            }
        }
        this.modified(componentContext);
        final BundleContext bundleContext = componentContext.getBundleContext();
        this.serviceTracker = new ServiceTracker(bundleContext, BindingsValuesProvider.class.getName(), null){

            public Object addingService(ServiceReference reference) {
                if ("com.day.cq.wcm.core.impl.WCMBindingsValuesProvider".equals(reference.getProperty("component.name"))) {
                    LOG.warn("Detected a previous version of a WCM BVP: {}. Disabling the newer BVP ({}) to maintain backwards compatibility.", (Object)"com.day.cq.wcm.core.impl.WCMBindingsValuesProvider", (Object)WCMBindingsValuesProvider.class.getName());
                    BVPManager.this.unregisterBVP();
                }
                return super.addingService(reference);
            }

            public void removedService(ServiceReference reference, Object obj) {
                if ("com.day.cq.wcm.core.impl.WCMBindingsValuesProvider".equals(reference.getProperty("component.name")) && BVPManager.this.active) {
                    LOG.info("{} was removed. Activating a newer version of that BVP: {}.", (Object)"com.day.cq.wcm.core.impl.WCMBindingsValuesProvider", (Object)WCMBindingsValuesProvider.class.getName());
                    BVPManager.this.registerBVP(bundleContext, BVPManager.this.getBVPProperties(componentContext));
                }
                super.removedService(reference, obj);
            }
        };
        this.serviceTracker.open();
        this.active = true;
    }

    @Modified
    protected void modified(ComponentContext componentContext) {
        this.unregisterBVP();
        this.registerBVP(componentContext.getBundleContext(), this.getBVPProperties(componentContext));
    }

    @Deactivate
    protected void deactivate(ComponentContext componentContext) {
        this.active = false;
        if (this.serviceTracker != null) {
            this.serviceTracker.close();
            this.serviceTracker = null;
        }
        this.unregisterBVP();
    }

    private Dictionary<String, Object> getBVPProperties(ComponentContext componentContext) {
        Hashtable<String, Object> properties = new Hashtable<String, Object>();
        properties.put("service.pid", WCMBindingsValuesProvider.class.getName());
        properties.put("service.vendor", "Adobe Systems");
        properties.put("service.ranking", 1000);
        properties.put("javax.script.name", PropertiesUtil.toStringArray(componentContext.getProperties().get("com.day.cq.wcm.scripting.bvp.script.engines"), (String[])SCRIPT_ENGINES_DEFAULT));
        return properties;
    }

    private void updateBVPManagerConfiguration(Dictionary properties) {
        try {
            Configuration bvpManagerConfiguration = this.configurationAdmin.getConfiguration(BVPManager.class.getName());
            bvpManagerConfiguration.update(properties);
        }
        catch (Exception e) {
            LOG.error("Unable to update configuration for " + BVPManager.class.getName(), (Throwable)e);
        }
    }

    private void registerBVP(BundleContext bundleContext, Dictionary properties) {
        this.bvpServiceRegistration = bundleContext.registerService(BindingsValuesProvider.class.getName(), (Object)new WCMBindingsValuesProvider(this.xssapi), properties);
    }

    private void unregisterBVP() {
        if (this.bvpServiceRegistration != null) {
            this.bvpServiceRegistration.unregister();
            this.bvpServiceRegistration = null;
        }
    }

    protected void bindXssapi(XSSAPI xSSAPI) {
        this.xssapi = xSSAPI;
    }

    protected void unbindXssapi(XSSAPI xSSAPI) {
        if (this.xssapi == xSSAPI) {
            this.xssapi = null;
        }
    }

    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        this.configurationAdmin = configurationAdmin;
    }

    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        if (this.configurationAdmin == configurationAdmin) {
            this.configurationAdmin = null;
        }
    }

}