ProductInfoServiceImpl.java 6.31 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ValueMap
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.Version
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.license.impl;

import com.adobe.granite.license.License;
import com.adobe.granite.license.ProductInfo;
import com.adobe.granite.license.ProductInfoProvider;
import com.adobe.granite.license.ProductInfoService;
import com.adobe.granite.license.impl.LicenseProvider;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Version;
import org.osgi.service.component.ComponentContext;
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
@Service
@Reference(name="productInfoProvider", referenceInterface=ProductInfoProvider.class, bind="bindProductInfoProvider", unbind="unbindProductInfoProvider", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
public class ProductInfoServiceImpl
implements ProductInfoService {
    private final Logger log = LoggerFactory.getLogger(ProductInfoServiceImpl.class);
    private static final String PROP_GRANITE_PRODUCT_VERSION = "granite.product.version";
    private static final String PROP_GRANITE_PRODUCT = "granite.product";
    private final Map<String, ProductInfoProvider> providers = new HashMap<String, ProductInfoProvider>();
    private final Map<String, ProductInfo> infos = new HashMap<String, ProductInfo>();
    @Reference
    private LicenseProvider licenseProvider;
    private ProductInfo defaultProductInfo;

    @Override
    public ProductInfo[] getInfos() {
        Collection<ProductInfo> values = this.infos.values();
        if (values.size() == 0) {
            values = Collections.singletonList(this.defaultProductInfo);
        }
        return values.toArray(new ProductInfo[values.size()]);
    }

    @Override
    public License getLicense() {
        return this.licenseProvider.getLicense();
    }

    @Activate
    protected void activate(ComponentContext context) {
        this.defaultProductInfo = new DefaultProductInfo(context.getBundleContext());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindProductInfoProvider(ProductInfoProvider provider, Map<String, Object> properties) {
        if (null != this.providers.get(provider.getClass().getName())) {
            this.log.debug("Provider [{}] already present, ignoring.", (Object)provider.getClass().getName());
            return;
        }
        Map<String, ProductInfoProvider> map = this.providers;
        synchronized (map) {
            ProductInfo info = provider.getProductInfo();
            if (info != null) {
                this.providers.put(provider.getClass().getName(), provider);
                this.infos.put(provider.getClass().getName(), info);
                this.log.info("Bound new product info provider: [{}] - providers: [{}]", (Object)provider, (Object)this.providers.size());
            } else {
                this.log.info("ProductInfoProvider returns null, ignored: [{}]", (Object)provider);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindProductInfoProvider(ProductInfoProvider provider, Map<String, Object> properties) {
        Map<String, ProductInfoProvider> map = this.providers;
        synchronized (map) {
            this.providers.remove(provider.getClass().getName());
            this.log.info("Unbound new product info provider: [{}] - providers: [{}]", (Object)provider, (Object)this.providers.size());
            Iterator<Map.Entry<String, ProductInfo>> iterator = this.infos.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, ProductInfo> entry = iterator.next();
                if (!entry.getKey().equals(provider.getClass().getName())) continue;
                iterator.remove();
            }
        }
    }

    protected void bindLicenseProvider(LicenseProvider licenseProvider) {
        this.licenseProvider = licenseProvider;
    }

    protected void unbindLicenseProvider(LicenseProvider licenseProvider) {
        if (this.licenseProvider == licenseProvider) {
            this.licenseProvider = null;
        }
    }

    private static final class DefaultProductInfo
    implements ProductInfo {
        private Version version;
        private String name;

        private DefaultProductInfo(BundleContext bundleContext) {
            this.version = new Version(bundleContext.getProperty("granite.product.version"));
            this.name = bundleContext.getProperty("granite.product");
        }

        public String getName() {
            return this.name;
        }

        public Version getVersion() {
            return this.version;
        }

        public String getShortName() {
            return this.name;
        }

        public String getShortVersion() {
            return "" + this.version.getMajor() + "." + this.version.getMinor();
        }

        public String getYear() {
            return null;
        }

        public String getVendor() {
            return null;
        }

        public String getVendorUrl() {
            return null;
        }

        public String getUrl() {
            return null;
        }

        public ValueMap getProperties() {
            return null;
        }
    }

}